SlideShare a Scribd company logo
9
Most read
11
Most read
12
Most read
LINUX SERIAL DRIVER
1
2
Byte Frame
8N1 signifies 8 Data bits, No Parity and 1 Stop Bit
TTL/CMOS Serial Logic Waveform
RS-232 Logic Waveform
Frame
Baud rate
1) 50 – 921.6kbps
Data bits
1) 5, 6, 7, 8
Parity
1) None, Odd, Even, Mark, Space
Stop bits
1) 1, 1.5/2 (1.5 for data bits=5)
Flow control
1) RTS/CTS, Xon/Xoff, DTR/DSR
3
Serial Parameter/Setting
N81 is common used in RS-232.
Sometimes E71/E72/O71/O72 will also be selected.
Modem line (in/out define)
Neck Name In/Out Comment
RTS Out Control the remote send or pause, to do
flow control
CTS In the remote note me to send or not, to do
flow control
DTR Out DTE ready, the old method to do flow
control
DSR In DCE ready, the old method to do flow
control
DCD In for DTE
Out for DCE
to indicate the phone is hank off
RI In for DTE
Out for DCE
Ring, the telephone is ringed
5
How to connect to remote – RS232
Tx
Rx
RTS
CTS
DTR
DSR
DCD
RI
Tx
Rx
RTS
CTS
DTR
DSR
DCD
RI
Hardware flow control to use RTS/CTS
1) RTS low to let the remoter to stop transmitting
2) RTS high to let the remoter to restart transmitting
3) CTS low to stop my transmitting
4) CTS high to restart my transmitting
Software flow control to use some character, so the data must
be escape it
1) Send Xoff to let the remoter to stop transmitting
2) Send Xon to let the remoter to restart transmitting
3) Receive Xoff to stop my transmitting
4) Receive Xon to restart my transmitting
6
Flow control
7
Flow Control with RTS/CTS (H/W)
Tx
Tx
Rx
Rx
Rx Buffer
low
high
RTS
CTS
high
low
8
Flow Control with Xon/Xoff (S/W)
Tx
Tx
Rx
Rx
Rx Buffer
low
high
Xoff
Xon
Xoff
Xon
9
Serial Device Driver Architecture
Hardware Serial Device
Serial Device Driver
tty Device Driver
Line description Device Driver
Application by termio/termios API Kernel
To do the basic
terminal
read/write/ioc
tl control
To control the
hardware
receive/transfer
To do about
the terminal
control
8250是一種不成文的硬件規格
原始碼所在目錄:drivers/serial
SoC的serial port通常都會使用
10
標準8250 serial device driver
serial_core.c
8250.c
tty
console
宣告一個resource sturcture如下:
static struct uart_port my_uart_init[] __initdata = {
[0] = {
.membase = VA_UART_BASE, // virtual address
.mapbase = PA_UART_BASE, // physical address
.irq = IRQ_UART,
.flags = UPF_SKIP_TEST,
.iotype = UPIO_MEM32,
.regshift = 2,
.uartclk = 921600 * 16,
.line = 0, // which port number on system
.type = PORT_16550A,
.fifosize = 16,
},
};
呼叫kernel API加入
early_serial_setup(&my_uart_init[0]); // embedded UART port
11
如何增加一個8250的port
Examlpe
MACHINE_START(MOXACPU, "Moxa CPU development platform")
.phys_io = 0x80000000,
.io_pg_offst = ((IO_ADDRESS(0x80000000)>>18)&0xfffc),
.map_io = map_io,
.init_irq = irq_init_irq,
.timer = &moxacpu_timer,
.fixup = fixup,
.boot_params = 0x100,
.init_machine = moxacpu_init,
MACHINE_END
void __init map_io(void)
{
iotable_init(mycpu_io_desc, sizeof(mycpu_io_desc)/sizeof(struct
map_desc));
early_serial_setupS
除了標準的8250暫存器之外,還有其它額
外的暫存器
FIFO大小通常是大於16個
會有多個port在系統上
有的會有CPU的智能卡
13
非標準的8250 serial driver
struct tty_driver myuart_sdriver =
alloc_tty_driver(TOTAL_PORTS);
Initialize the tty_driver structure
myuart_sdriver->owner = THIS_MODULE;
myuart_sdriver->magic = TTY_DRIVER_MAGIC;
myuart_sdriver->name = "ttyMI";
myuart_sdriver->major = ttymajor;
myuart_sdriver->minor_start = 0;
myuart_sdriver->num = MXSER_PORTS + 1;
myuart_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
myuart_sdriver->subtype = SERIAL_TYPE_NORMAL;
myuart_sdriver->init_termios = tty_std_termios;
myuart_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
myuart_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
tty_set_operations(myuart_sdriver, &myuart_ops);
retval = tty_register_driver(myuart_sdriver);
tty_register_device(myuart_sdriver, ports, NULL);
14
Driver的初始化
static const struct tty_operations mxser_ops = {
.open = myuart_open,
.close = myuart_close,
.write = myuart_write,
.put_char = myuart_put_char,
.flush_chars = myuart_flush_chars,
.write_room = myuart_write_room,
.chars_in_buffer = myuart_chars_in_buffer,
.flush_buffer = myuart_flush_buffer,
.ioctl = myuart_ioctl,
.throttle = myuart_throttle,
.unthrottle = myuart_unthrottle,
.set_termios = myuart_set_termios,
.stop = myuart_stop,
.start = myuart_start,
.hangup = myuart_hangup,
.break_ctl = myuart_rs_break,
.wait_until_sent = myuart_wait_until_sent,
.tiocmget = myuart_tiocmget,
.tiocmset = myuart_tiocmset,
};
15
Set tty operations
Driver Initialize Example
static int __init mxser_module_init(void)
{
mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
mxvar_sdriver->name = "ttyMI";
mxvar_sdriver->major = ttymajor;
mxvar_sdriver->minor_start = 0;
mxvar_sdriver->num = MXSER_PORTS + 1;
mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
mxvar_sdriver->init_termios = tty_std_termios;
mxvar_sdriver->init_termios.c_cflag =B9600|CS8|CREAD|HUPCL|CLOCAL;
mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
tty_set_operations(mxvar_sdriver, &mxser_ops);
retval = tty_register_driver(mxvar_sdriver);
retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
brd);
…………………………..
for (i = 0; i < brd->info->nports; i++)
tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
……………………….
return 0;
}
Driver Open/Close Example
static int mxser_open(struct tty_struct *tty, struct file *filp)
{
port = tty->index; // minor or port number
tty->driver_data = info; // set private data
spin_lock_irqsave(&info->slock, flags);
info->port.count++;
spin_unlock_irqrestore(&info->slock, flags);
retval = mxser_startup(info);
return 0;
}
static void mxser_close(struct tty_struct *tty, struct file *filp)
{
struct mxser_port *info = tty->driver_data;
if (--info->port.count) {
return;
}
…………………………
if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
tty_wait_until_sent(tty, info->port.closing_wait);
mxser_shutdown(info);
mxser_flush_buffer(tty);
tty_ldisc_flush(tty);
}
Driver Write Example
static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
{
………………………..
while (1) {
…………………..
memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
…………………..
}
if (info->xmit_cnt && !tty->stopped) {
if (!tty->hw_stopped ||
(info->type == PORT_16550A) ||
(info->board->chip_flag)) {
spin_lock_irqsave(&info->slock, flags);
outb(info->IER & ~UART_IER_THRI, info->ioaddr +
UART_IER);
info->IER |= UART_IER_THRI;
outb(info->IER, info->ioaddr + UART_IER);
spin_unlock_irqrestore(&info->slock, flags);
}
}
return total;
}
Receive Interrupt Example
recv_room = tty->receive_room;
if ((recv_room == 0) && (!port->ldisc_stop_rx))
mxser_stoprx(tty);
…………………………………
while (gdl--) {
ch = inb(port->ioaddr + UART_RX);
tty_insert_flip_char(tty, ch, 0);
cnt++;
}
…………………………………..
spin_unlock(&port->slock);
tty_flip_buffer_push(tty);
spin_lock(&port->slock);
……………………………
#define NCCS 32
struct termios
{
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[NCCS]; /* control characters */
speed_t c_ispeed; /* input speed */
speed_t c_ospeed; /* output speed */
#define _HAVE_STRUCT_TERMIOS_C_ISPEED 1
#define _HAVE_STRUCT_TERMIOS_C_OSPEED 1
};
20
termios structure
/* c_cc characters */
#define VINTR 0
#define VQUIT 1
#define VERASE 2
#define VKILL 3
#define VEOF 4
#define VTIME 5
#define VMIN 6
#define VSWTC 7
#define VSTART 8
#define VSTOP 9
#define VSUSP 10
#define VEOL 11
#define VREPRINT 12
#define VDISCARD 13
#define VWERASE 14
#define VLNEXT 15
#define VEOL2 16
#include <termios.h>
#include <unistd.h>
int tcgetattr(int fd, struct termios *termios_p);
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
int tcsendbreak(int fd, int duration);
int tcdrain(int fd);
int tcflush(int fd, int queue_selector);
int tcflow(int fd, int action);
void cfmakeraw(struct termios *termios_p);
speed_t cfgetispeed(const struct termios *termios_p);
speed_t cfgetospeed(const struct termios *termios_p);
int cfsetispeed(struct termios *termios_p, speed_t speed);
int cfsetospeed(struct termios *termios_p, speed_t speed);
int cfsetspeed(struct termios *termios_p, speed_t speed);
21
API
AP example
#include <termios.h>
int main(int argc, char *argv[])
{
int fd;
struct termios trem;
fd = open(“/dev/ttyS0”, O_RDWR);
tcgetattr(fd, &term);
term.lflag = 0;
term.iflag = 0;
term.cflag = B115200 | CS8|CREAD|CLOCAL;
term.oflag = 0;
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 1;
tcsetattr(fd, TCDRAIN, &term);
tcflush(fd,TCIOFLUSH);
……………
write(fd, buf, len);
……………………
read(fd, buf, len);
………………..
close(fd);
}

More Related Content

What's hot (20)

PDF
Jagan Teki - U-boot from scratch
linuxlab_conf
 
PDF
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
PDF
Introduction To Linux Kernel Modules
dibyajyotig
 
PDF
Process Address Space: The way to create virtual address (page table) of user...
Adrian Huang
 
PPTX
U-Boot presentation 2013
Wave Digitech
 
PPTX
The TCP/IP Stack in the Linux Kernel
Divye Kapoor
 
PPT
Basic Linux Internals
mukul bhardwaj
 
PPT
linux device driver
Rahul Batra
 
PDF
Embedded linux network device driver development
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
PPTX
Linux Initialization Process (1)
shimosawa
 
PPTX
Slab Allocator in Linux Kernel
Adrian Huang
 
PDF
Arm device tree and linux device drivers
Houcheng Lin
 
PDF
How A Compiler Works: GNU Toolchain
National Cheng Kung University
 
PDF
The linux networking architecture
hugo lu
 
PPT
Troubleshooting Linux Kernel Modules And Device Drivers
Satpal Parmar
 
PDF
Anatomy of the loadable kernel module (lkm)
Adrian Huang
 
PDF
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Adrian Huang
 
PDF
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
Adrian Huang
 
DOCX
linux file sysytem& input and output
MythiliA5
 
PDF
A practical guide to buildroot
Emertxe Information Technologies Pvt Ltd
 
Jagan Teki - U-boot from scratch
linuxlab_conf
 
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
Introduction To Linux Kernel Modules
dibyajyotig
 
Process Address Space: The way to create virtual address (page table) of user...
Adrian Huang
 
U-Boot presentation 2013
Wave Digitech
 
The TCP/IP Stack in the Linux Kernel
Divye Kapoor
 
Basic Linux Internals
mukul bhardwaj
 
linux device driver
Rahul Batra
 
Embedded linux network device driver development
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
Linux Initialization Process (1)
shimosawa
 
Slab Allocator in Linux Kernel
Adrian Huang
 
Arm device tree and linux device drivers
Houcheng Lin
 
How A Compiler Works: GNU Toolchain
National Cheng Kung University
 
The linux networking architecture
hugo lu
 
Troubleshooting Linux Kernel Modules And Device Drivers
Satpal Parmar
 
Anatomy of the loadable kernel module (lkm)
Adrian Huang
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Adrian Huang
 
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
Adrian Huang
 
linux file sysytem& input and output
MythiliA5
 
A practical guide to buildroot
Emertxe Information Technologies Pvt Ltd
 

Viewers also liked (8)

PPTX
Linux Jffs2 & Linux MTD Device
艾鍗科技
 
PPTX
GPS + Google fusion table 雲端應用
艾鍗科技
 
PPTX
成果展簡報-Zigbee無線自動燈光及溫度調控系統
艾鍗科技
 
PPTX
銀髮幼童健康定位手環
艾鍗科技
 
PPT
用Raspberry Pi 完成一個智慧型六足機器人
艾鍗科技
 
PPTX
智能風扇
艾鍗科技
 
PPTX
ARM Processor
Aniket Thakur
 
PDF
Programming The Arm Microprocessor For Embedded Systems
joshparrish13
 
Linux Jffs2 & Linux MTD Device
艾鍗科技
 
GPS + Google fusion table 雲端應用
艾鍗科技
 
成果展簡報-Zigbee無線自動燈光及溫度調控系統
艾鍗科技
 
銀髮幼童健康定位手環
艾鍗科技
 
用Raspberry Pi 完成一個智慧型六足機器人
艾鍗科技
 
智能風扇
艾鍗科技
 
ARM Processor
Aniket Thakur
 
Programming The Arm Microprocessor For Embedded Systems
joshparrish13
 
Ad

Similar to Linux Serial Driver (20)

ODP
Intel Quark HSUART
Shubham Kumar
 
DOCX
LINUX RS232程式設計
艾鍗科技
 
PPTX
MicroProcessors and MicroControllersUnit3
deepakdmaat
 
PPTX
8251 -USART.pptx
VikasMahor3
 
DOCX
Lab 2_5
Joseph Chandler
 
PPTX
0.pptx
DebanjanMaity13
 
PPTX
Micro c lab8(serial communication)
Mashood
 
PPT
8051 serial communication-UART
Pantech ProLabs India Pvt Ltd
 
PPTX
28. 8251 programmable communication interface
sandip das
 
PPTX
8086 microprocessor -Input/Output INTERFACING
C.Helen Sulochana
 
PPTX
Serial Communication in 8051
Sudhanshu Janwadkar
 
PPTX
Serial Io
Aisu
 
PPTX
IO INTERFACING in unit 2 8086 Microprocessor
tamil arasan
 
PPT
AN INTRODUCTION TO SERIAL PORT INTERFACING
Total Project Solutions
 
PPTX
Microprocessors and Microcontrollers ppt
pradeepA32
 
PPT
Unit_V_CPU_PC_interfacing_With_External_Devices_RS232_IEEE_488.ppt
aasitworld
 
PPT
12 mt06ped019
vijaydeepakg
 
PDF
8255 & IO Interfacing.pdf
Ilavarasan Tamizh
 
PDF
SEM88_Presentation
Luka Penger
 
Intel Quark HSUART
Shubham Kumar
 
LINUX RS232程式設計
艾鍗科技
 
MicroProcessors and MicroControllersUnit3
deepakdmaat
 
8251 -USART.pptx
VikasMahor3
 
Micro c lab8(serial communication)
Mashood
 
8051 serial communication-UART
Pantech ProLabs India Pvt Ltd
 
28. 8251 programmable communication interface
sandip das
 
8086 microprocessor -Input/Output INTERFACING
C.Helen Sulochana
 
Serial Communication in 8051
Sudhanshu Janwadkar
 
Serial Io
Aisu
 
IO INTERFACING in unit 2 8086 Microprocessor
tamil arasan
 
AN INTRODUCTION TO SERIAL PORT INTERFACING
Total Project Solutions
 
Microprocessors and Microcontrollers ppt
pradeepA32
 
Unit_V_CPU_PC_interfacing_With_External_Devices_RS232_IEEE_488.ppt
aasitworld
 
12 mt06ped019
vijaydeepakg
 
8255 & IO Interfacing.pdf
Ilavarasan Tamizh
 
SEM88_Presentation
Luka Penger
 
Ad

More from 艾鍗科技 (20)

PPTX
AI 技術浪潮, 什麼是機器學習? 什麼是深度學習, 什麼是生成式AI, AI 能力認證
艾鍗科技
 
PDF
TinyML - 4 speech recognition
艾鍗科技
 
PPTX
Appendix 1 Goolge colab
艾鍗科技
 
PPTX
Project-IOT於餐館系統的應用
艾鍗科技
 
PPTX
02 IoT implementation
艾鍗科技
 
PPTX
Tiny ML for spark Fun Edge
艾鍗科技
 
PDF
Openvino ncs2
艾鍗科技
 
PDF
Step motor
艾鍗科技
 
PDF
2. 機器學習簡介
艾鍗科技
 
PDF
5.MLP(Multi-Layer Perceptron)
艾鍗科技
 
PDF
3. data features
艾鍗科技
 
PPTX
心率血氧檢測與運動促進
艾鍗科技
 
PPTX
利用音樂&情境燈幫助放鬆
艾鍗科技
 
PPTX
IoT感測器驅動程式 在樹莓派上實作
艾鍗科技
 
PPTX
無線聲控遙控車
艾鍗科技
 
PPT
最佳光源的研究和實作
艾鍗科技
 
PPTX
無線監控網路攝影機與控制自走車
艾鍗科技
 
PPTX
Reinforcement Learning
艾鍗科技
 
PPTX
人臉辨識考勤系統
艾鍗科技
 
PPTX
智慧家庭Smart Home
艾鍗科技
 
AI 技術浪潮, 什麼是機器學習? 什麼是深度學習, 什麼是生成式AI, AI 能力認證
艾鍗科技
 
TinyML - 4 speech recognition
艾鍗科技
 
Appendix 1 Goolge colab
艾鍗科技
 
Project-IOT於餐館系統的應用
艾鍗科技
 
02 IoT implementation
艾鍗科技
 
Tiny ML for spark Fun Edge
艾鍗科技
 
Openvino ncs2
艾鍗科技
 
Step motor
艾鍗科技
 
2. 機器學習簡介
艾鍗科技
 
5.MLP(Multi-Layer Perceptron)
艾鍗科技
 
3. data features
艾鍗科技
 
心率血氧檢測與運動促進
艾鍗科技
 
利用音樂&情境燈幫助放鬆
艾鍗科技
 
IoT感測器驅動程式 在樹莓派上實作
艾鍗科技
 
無線聲控遙控車
艾鍗科技
 
最佳光源的研究和實作
艾鍗科技
 
無線監控網路攝影機與控制自走車
艾鍗科技
 
Reinforcement Learning
艾鍗科技
 
人臉辨識考勤系統
艾鍗科技
 
智慧家庭Smart Home
艾鍗科技
 

Recently uploaded (20)

PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 

Linux Serial Driver

  • 2. 2 Byte Frame 8N1 signifies 8 Data bits, No Parity and 1 Stop Bit TTL/CMOS Serial Logic Waveform RS-232 Logic Waveform Frame
  • 3. Baud rate 1) 50 – 921.6kbps Data bits 1) 5, 6, 7, 8 Parity 1) None, Odd, Even, Mark, Space Stop bits 1) 1, 1.5/2 (1.5 for data bits=5) Flow control 1) RTS/CTS, Xon/Xoff, DTR/DSR 3 Serial Parameter/Setting N81 is common used in RS-232. Sometimes E71/E72/O71/O72 will also be selected.
  • 4. Modem line (in/out define) Neck Name In/Out Comment RTS Out Control the remote send or pause, to do flow control CTS In the remote note me to send or not, to do flow control DTR Out DTE ready, the old method to do flow control DSR In DCE ready, the old method to do flow control DCD In for DTE Out for DCE to indicate the phone is hank off RI In for DTE Out for DCE Ring, the telephone is ringed
  • 5. 5 How to connect to remote – RS232 Tx Rx RTS CTS DTR DSR DCD RI Tx Rx RTS CTS DTR DSR DCD RI
  • 6. Hardware flow control to use RTS/CTS 1) RTS low to let the remoter to stop transmitting 2) RTS high to let the remoter to restart transmitting 3) CTS low to stop my transmitting 4) CTS high to restart my transmitting Software flow control to use some character, so the data must be escape it 1) Send Xoff to let the remoter to stop transmitting 2) Send Xon to let the remoter to restart transmitting 3) Receive Xoff to stop my transmitting 4) Receive Xon to restart my transmitting 6 Flow control
  • 7. 7 Flow Control with RTS/CTS (H/W) Tx Tx Rx Rx Rx Buffer low high RTS CTS high low
  • 8. 8 Flow Control with Xon/Xoff (S/W) Tx Tx Rx Rx Rx Buffer low high Xoff Xon Xoff Xon
  • 9. 9 Serial Device Driver Architecture Hardware Serial Device Serial Device Driver tty Device Driver Line description Device Driver Application by termio/termios API Kernel To do the basic terminal read/write/ioc tl control To control the hardware receive/transfer To do about the terminal control
  • 11. 宣告一個resource sturcture如下: static struct uart_port my_uart_init[] __initdata = { [0] = { .membase = VA_UART_BASE, // virtual address .mapbase = PA_UART_BASE, // physical address .irq = IRQ_UART, .flags = UPF_SKIP_TEST, .iotype = UPIO_MEM32, .regshift = 2, .uartclk = 921600 * 16, .line = 0, // which port number on system .type = PORT_16550A, .fifosize = 16, }, }; 呼叫kernel API加入 early_serial_setup(&my_uart_init[0]); // embedded UART port 11 如何增加一個8250的port
  • 12. Examlpe MACHINE_START(MOXACPU, "Moxa CPU development platform") .phys_io = 0x80000000, .io_pg_offst = ((IO_ADDRESS(0x80000000)>>18)&0xfffc), .map_io = map_io, .init_irq = irq_init_irq, .timer = &moxacpu_timer, .fixup = fixup, .boot_params = 0x100, .init_machine = moxacpu_init, MACHINE_END void __init map_io(void) { iotable_init(mycpu_io_desc, sizeof(mycpu_io_desc)/sizeof(struct map_desc)); early_serial_setupS
  • 14. struct tty_driver myuart_sdriver = alloc_tty_driver(TOTAL_PORTS); Initialize the tty_driver structure myuart_sdriver->owner = THIS_MODULE; myuart_sdriver->magic = TTY_DRIVER_MAGIC; myuart_sdriver->name = "ttyMI"; myuart_sdriver->major = ttymajor; myuart_sdriver->minor_start = 0; myuart_sdriver->num = MXSER_PORTS + 1; myuart_sdriver->type = TTY_DRIVER_TYPE_SERIAL; myuart_sdriver->subtype = SERIAL_TYPE_NORMAL; myuart_sdriver->init_termios = tty_std_termios; myuart_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL; myuart_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV; tty_set_operations(myuart_sdriver, &myuart_ops); retval = tty_register_driver(myuart_sdriver); tty_register_device(myuart_sdriver, ports, NULL); 14 Driver的初始化
  • 15. static const struct tty_operations mxser_ops = { .open = myuart_open, .close = myuart_close, .write = myuart_write, .put_char = myuart_put_char, .flush_chars = myuart_flush_chars, .write_room = myuart_write_room, .chars_in_buffer = myuart_chars_in_buffer, .flush_buffer = myuart_flush_buffer, .ioctl = myuart_ioctl, .throttle = myuart_throttle, .unthrottle = myuart_unthrottle, .set_termios = myuart_set_termios, .stop = myuart_stop, .start = myuart_start, .hangup = myuart_hangup, .break_ctl = myuart_rs_break, .wait_until_sent = myuart_wait_until_sent, .tiocmget = myuart_tiocmget, .tiocmset = myuart_tiocmset, }; 15 Set tty operations
  • 16. Driver Initialize Example static int __init mxser_module_init(void) { mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1); mxvar_sdriver->name = "ttyMI"; mxvar_sdriver->major = ttymajor; mxvar_sdriver->minor_start = 0; mxvar_sdriver->num = MXSER_PORTS + 1; mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL; mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL; mxvar_sdriver->init_termios = tty_std_termios; mxvar_sdriver->init_termios.c_cflag =B9600|CS8|CREAD|HUPCL|CLOCAL; mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV; tty_set_operations(mxvar_sdriver, &mxser_ops); retval = tty_register_driver(mxvar_sdriver); retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser", brd); ………………………….. for (i = 0; i < brd->info->nports; i++) tty_register_device(mxvar_sdriver, brd->idx + i, NULL); ………………………. return 0; }
  • 17. Driver Open/Close Example static int mxser_open(struct tty_struct *tty, struct file *filp) { port = tty->index; // minor or port number tty->driver_data = info; // set private data spin_lock_irqsave(&info->slock, flags); info->port.count++; spin_unlock_irqrestore(&info->slock, flags); retval = mxser_startup(info); return 0; } static void mxser_close(struct tty_struct *tty, struct file *filp) { struct mxser_port *info = tty->driver_data; if (--info->port.count) { return; } ………………………… if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE) tty_wait_until_sent(tty, info->port.closing_wait); mxser_shutdown(info); mxser_flush_buffer(tty); tty_ldisc_flush(tty); }
  • 18. Driver Write Example static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count) { ……………………….. while (1) { ………………….. memcpy(info->port.xmit_buf + info->xmit_head, buf, c); ………………….. } if (info->xmit_cnt && !tty->stopped) { if (!tty->hw_stopped || (info->type == PORT_16550A) || (info->board->chip_flag)) { spin_lock_irqsave(&info->slock, flags); outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER); info->IER |= UART_IER_THRI; outb(info->IER, info->ioaddr + UART_IER); spin_unlock_irqrestore(&info->slock, flags); } } return total; }
  • 19. Receive Interrupt Example recv_room = tty->receive_room; if ((recv_room == 0) && (!port->ldisc_stop_rx)) mxser_stoprx(tty); ………………………………… while (gdl--) { ch = inb(port->ioaddr + UART_RX); tty_insert_flip_char(tty, ch, 0); cnt++; } ………………………………….. spin_unlock(&port->slock); tty_flip_buffer_push(tty); spin_lock(&port->slock); ……………………………
  • 20. #define NCCS 32 struct termios { tcflag_t c_iflag; /* input mode flags */ tcflag_t c_oflag; /* output mode flags */ tcflag_t c_cflag; /* control mode flags */ tcflag_t c_lflag; /* local mode flags */ cc_t c_line; /* line discipline */ cc_t c_cc[NCCS]; /* control characters */ speed_t c_ispeed; /* input speed */ speed_t c_ospeed; /* output speed */ #define _HAVE_STRUCT_TERMIOS_C_ISPEED 1 #define _HAVE_STRUCT_TERMIOS_C_OSPEED 1 }; 20 termios structure /* c_cc characters */ #define VINTR 0 #define VQUIT 1 #define VERASE 2 #define VKILL 3 #define VEOF 4 #define VTIME 5 #define VMIN 6 #define VSWTC 7 #define VSTART 8 #define VSTOP 9 #define VSUSP 10 #define VEOL 11 #define VREPRINT 12 #define VDISCARD 13 #define VWERASE 14 #define VLNEXT 15 #define VEOL2 16
  • 21. #include <termios.h> #include <unistd.h> int tcgetattr(int fd, struct termios *termios_p); int tcsetattr(int fd, int optional_actions, const struct termios *termios_p); int tcsendbreak(int fd, int duration); int tcdrain(int fd); int tcflush(int fd, int queue_selector); int tcflow(int fd, int action); void cfmakeraw(struct termios *termios_p); speed_t cfgetispeed(const struct termios *termios_p); speed_t cfgetospeed(const struct termios *termios_p); int cfsetispeed(struct termios *termios_p, speed_t speed); int cfsetospeed(struct termios *termios_p, speed_t speed); int cfsetspeed(struct termios *termios_p, speed_t speed); 21 API
  • 22. AP example #include <termios.h> int main(int argc, char *argv[]) { int fd; struct termios trem; fd = open(“/dev/ttyS0”, O_RDWR); tcgetattr(fd, &term); term.lflag = 0; term.iflag = 0; term.cflag = B115200 | CS8|CREAD|CLOCAL; term.oflag = 0; term.c_cc[VMIN] = 1; term.c_cc[VTIME] = 1; tcsetattr(fd, TCDRAIN, &term); tcflush(fd,TCIOFLUSH); …………… write(fd, buf, len); …………………… read(fd, buf, len); ……………….. close(fd); }