Rockchip UART (Universal Asynchronous Receiver/Transmitter) is based on the 16550A serial port standard. The kernel uses the 8250 serial port universal driver, so it can support standard serial port programming under Linux.
The board leads to three sets of universal serial ports and one debug serial port. It is recommended not to use the debug serial port for other purposes.
The serial numbers of these three sets of universal serial ports are 5, 7, and 9, and the corresponding device nodes under Linux are /dev/ttyS5 /dev/ttyS7 /dev/ttyS9. The following takes ttyS9 as an example to introduce its usage, and the other serial ports are similar.
Next, we will short-circuit the TX and RX of ttyS9 on the hardware to perform a transceiver test. The wiring is as follows
All nodes of "/dev/ttyS*" require root privileges to operate. Using the command line or a program compiled in C language to operate the serial port requires root privileges. If you are using ssh or LX terminal, execute the following command to obtain root privileges first.
sudo su
View serial port information
stty -F /dev/ttyS9
Set the serial port baud rate to 115200
stty -F /dev/ttyS9 speed 115200
Set the serial port eight data bits, no parity, one stop bit, no echo
stty -F /dev/ttyS9 cs8 -parenb -cstopb -echo
Short-circuit the TXRX of the serial port, and then perform a send and receive test
Receive data in the background, and send in the foreground
cat /dev/ttyS9 &
echo -e "12345\n" > /dev/ttyS9
The execution result is as follows. You can see that the received data is the sent data
By connecting to the libperipheral_api.a static library, you can use C language to call the following interface to operate the serial port
/**
* @name: user_uart_open
* @description: Open the serial port
* @param uart_num: serial port number, the serial ports led out on the board include 3 4 8 9
* @return greater than or equal to 0 - success, the return value is the file descriptor less than 0 - failure
*/
int user_uart_open(int uart_num);
/**
* @name: user_uart_set_property
* @description: Set the baud rate, number of bits, stop bits, and check bits
* @param fd: File descriptor, user_uart_open return value
* @param speed: baud rate, currently supports 921600, 460800, 230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300
* @param databits: length, currently supports 7 8 data bits
* @param stopbits: stop bits, currently supports 1 2 stop bits
* @param parity: check, n no check, o odd check, e even check
*
* @return equal to 0 - success less than 0 - failure
*/
int user_uart_set_property(int fd, int speed, int databits, int stopbits, int parity);
/**
* @name: user_uart_read
* @description: Read data from the serial port buffer
* @param fd: file descriptor, user_uart_open return value
* @param buf: pointer to read data
* @param buf_len: length of the area pointed to by buf
*
* @return greater than 0 - success less than 0 - failure equal to 0 - no data was read
*/
int user_uart_read(int fd, char *buf, unsigned int buf_len);
/**
* @name: user_uart_write
* @description: Write data to the serial port buffer
* @param fd: file descriptor, user_uart_open return value
* @param buf: pointer to read data
* @param buf_len: length of the area pointed to by buf
*
* @return greater than 0 - success less than 0 - failure equal to 0 - no data was written
*/
int user_uart_write(int fd, const char *buf, unsigned int buf_len);
/**
* @name: user_uart_close
* @description: Close the serial port
* @param fd: file descriptor, user_uart_open return value
* @return greater than or equal to 0 - success, return value is the file descriptor less than 0 - failure
*/
int user_uart_close(int fd);
The test demo is as follows, taking the operation of serial port 9 as an example, short-circuiting TX RX
#include "peripheral_api.h"
void uart_api_test(void)
{
int ret = 0;
int fd = -1;
unsigned char buf_write[5] = {0};
unsigned char buf_read[5] = {0};
for (unsigned char i = 0; i < sizeof(buf_write); i++) {
buf_write[i] = 0x32 + i;
}
fd = user_uart_open(9);
if (fd < 0) {
printf("user_uart_open fail \n");
return;
}
ret = user_uart_set_property(fd, 115200, 8, 1, 'n');
if (ret < 0) {
printf("user_uart_set_property fail \n");
return;
}
ret = user_uart_write(fd, buf_write, sizeof(buf_write));
if (ret <= 0) {
printf("user_uart_write fail \n");
user_uart_close(fd);
return;
}
fsync(fd);
usleep(200 * 1000);
ret = user_uart_read(fd, buf_read, sizeof(buf_read));
if (ret <= 0) {
printf("user_uart_read fail \n");
user_uart_close(fd);
return;
}
user_uart_close(fd);
if (memcmp(buf_write, buf_read, sizeof(buf_write)) == 0) {
printf("buf_write buf_read same\n");
} else {
printf("buf_write buf_read diff %d %d %d %d %d \n", buf_read[0], buf_read[1], buf_read[2], buf_read[3], buf_read[4]);
}
return;
}
int main()
{
uart_api_test();
return 0;
}
Put the peripheral_api.a static library, peripheral_api.h and test demo source code test.c in the same path, and the compilation command is as follows
aarch64-none-linux-gnu-gcc test.c peripheral_api.a -I. -o uarttest
The running results are as follows