UART: Serial programming
Userspace
Referring to:
-
Serial Programming HOWTO (online article) - www.tpld.org
-
Serial HOWTO - www.tpld.org
-
Chapter 62 - Terminals - The Linux Programming Interface (book)
-
Serial Programming Guide for POSIX Compliant Operating Systems (online article) - Michael R. Sweet
for detail intructions when programing with serial port.
A serial port configuration is stored in a structure struct termios
, which defined in <asm/termbits.h>
. Userspace program interfaces with serial port via device file (/dev/ttyS0
for example).
When a new session with a serial port is created, current configuration will be saved in struct termios oldtio
, oldtio
is get from device by API:
int tcgetattr(int fd, struct termios *termios_p);
A new configuration struct termios newtio
is created and applied to device by API:
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
After applying compatible configuration, userspace application can communicate with serial port by calling read
and write
system call with file description fd
.
Kernelspace
Referring to:
- Linux serial drivers - bootlin.com - said about how to write an UART device driver.
To be properly intergrated in a Linux system, serial ports like UART must be visible as TTY devices from user space application. Therefore, the serial driver must be part of the kernle TTY subsystem.