Linux serial driver and use

summary

  • Serial driver is different from IIC/SPI driver. It has no difference between host and device. There is only one serial driver.
  • The official website will generally write the serial port driver. What we really need to do is to add the serial port node information to be used in the device tree.
  • After the system is started, the serial port driver and the device are matched successfully, and the corresponding serial port will be driven to generate a / dev/ttyx node.
  • Porting minicom serial port tool can easily read and write to the serial port.
  • Although the serial driver does not need us to write, we still need to understand the serial driver framework.

Experimental purpose

  • Configure the device tree to enable the serial port USART1 of Atmel SAMA5D34 CPU;
  • Compile the device tree, confirm that the serial driver is compiled into the kernel, download and run;
  • View the serial port configuration and set the serial port configuration;
  • Test the sending and receiving of serial port through command;
  • Migrate the minicom tool.

Configure device tree

Add the serial port device tree in the device tree with the status of "okey"

usart1: serial@f0020000 {
	compatible = "atmel,at91sam9260-usart";
	reg = <0xf0020000 0x100>;
	interrupts = <13 IRQ_TYPE_LEVEL_HIGH 5>;
	dmas = <&dma0 2 AT91_DMA_CFG_PER_ID(5)>,
		   <&dma0 2 (AT91_DMA_CFG_PER_ID(6) | AT91_DMA_CFG_FIFOCFG_ASAP)>;
	dma-names = "tx", "rx";
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_usart1>;
	clocks = <&usart1_clk>;
	clock-names = "usart";
	status = "okay";
};

Compatible = "ATMEL, AT91SAM9260 USART" is used to match the name of the driver. The corresponding driver can be searched in the drivers directory of the kernel. Execute search: grep -nR "ATMEL, AT91SAM9260 USART *", and the serial port drive path is: / drivers/ tty/serial/atmel_serial.c

The pin used by serial port USART1 can be in pinctrl_ View in USART1 child node. Check USART1_TX and USART1_ Whether these two RX pins are used for other functions. If so, shield them. Ensure that these two IO S are only used for USART1. Remember!!!

pinctrl_usart1: usart1-0 {
	atmel,pins =
		<AT91_PIOB 28 AT91_PERIPH_A AT91_PINCTRL_NONE	/* PB28 periph A */
		 AT91_PIOB 29 AT91_PERIPH_A AT91_PINCTRL_PULL_UP>;	/* PB29 periph A with pullup */
};

Compile device tree

Execute make dtbs in the root directory of the kernel to generate DTBS dtb object file.
Check whether the serial driver is compiled into the kernel: you can see obj - $(config_serial_atmel) + = ATMEL in / drivers/ tty/serial/Makefile_ serial. o. Just under the root directory Config file_ SERIAL_ ATMEL = y "serial driver will be compiled into the kernel.
After downloading the device tree, enter the device tree directory CD / proc / device tree /, and execute find -name "serial *" to find the configuration of the corresponding device tree.

Serial port transceiver test

The node generated by the serial port is / dev/ttyx. The specific tty can be viewed through the driver. usart1 in this routine generates the node name / dev/ttyS2.
Enter "stty -F /dev/ttyS2 -a" to view the serial port information, such as baud rate.

# stty -F /dev/ttyS2 -a
speed 115200 baud;stty: /dev/ttyS2
 line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke

Input "stty -F /dev/ttyS2 ispeed 115200 ospeed 115200 cs8" to set the serial port baud rate, stop the check bit, etc.
Execute echo "test" > / dev / ttys2 to send the test character through the serial port.
Execute cat /dev/ttyS2 to receive serial port data and print it.

Migrating minicom tools

To be continued

Keywords: Linux

Added by toivo on Tue, 04 Jan 2022 07:47:56 +0200