Rockchip UART (Universal Asynchronous Receiver/Transmitter) is based on the 16550A serial port standard. The kernel uses the 8250 serial universal driver, so it can support standard serial programming under Linux.
There are three general-purpose serial ports on the board, and one debug serial port, so it is recommended not to use the debug serial port for other purposes.
The serial numbers of these three general-purpose serial ports are 5, 7 and 9 respectively, and the corresponding device nodes under Linux are /dev/ttyS5 /dev/ttyS7 /dev/ttyS9. Below is an example of ttyS9 to introduce its usage, and the other serial ports are also similar.
The following is an example of the use of ttyS9, and other serial ports are also similar. The following is a test of sending and receiving by shorting the TX and RX of ttyS9 on the hardware, with the following wiring
The nodes in "/dev/ttyS*" need root privileges to operate, and the programs compiled from command line or C language need root privileges to operate the serial port. If you are using ssh or LX terminal, execute the following command to get root privileges.
sudo su
Viewing Serial Port Information
stty -F /dev/ttyS9
Set the serial port baud rate to 115200
stty -F /dev/ttyS9 speed 115200
Set eight data bits of serial port No parity One stop bit No replay
stty -F /dev/ttyS9 cs8 -parenb -cstopb -echo
Short the TXRX of the serial port, and then conduct a send/receive test
Receive data in the background and send it 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
By linking to the libperipheral_api.a static library, the following interfaces can be called in C to operate the UART
/**
* @name: user_uart_open
* @description: open UART device
* @param uart_num: Serial port number, the board leads to the serial port including 3 4 8 9
* @return Greater than or equal to 0 - success, return value is file descriptor; less than 0 - failure
*/
int user_uart_open(int uart_num);
/**
* @name: user_uart_set_property
* @description: Set baud rate Bit number Stop bit Checksum bit
* @param fd: File descriptor, user_uart_open return value
* @param speed: Baud rate, supports 921600, 460800, 230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300
* @param databits: Length, 7 8-bit data bits supported
* @param stopbits: Stop bit, support 1 2-bit stop bit
* @param parity: checksum, 'n' no checksum, 'o' odd checksum, 'e' even checksum
*
* @return 0 - success ; less than 0 - fail
*/
int user_uart_set_property(int fd, int speed, int databits, int stopbits, int parity);
/**
* @name: user_uart_read
* @description: Read data from UART port buffer
* @param fd: File descriptor, user_uart_open return value
* @param buf: Pointer to data
* @param buf_len: The length of the area pointed to by buf
*
* @return 0 - success ; less than 0 - fail; Equal to 0 - no data read
*/
int user_uart_read(int fd, char *buf, unsigned int buf_len);
/**
* @name: user_uart_write
* @description: Write data to the UART buffer
* @param fd: File descriptor, user_uart_open return value
* @param buf: Pointer to data
* @param buf_len: The length of the area pointed to by buf
*
* @return 0 - success ; less than 0 - fail; Equal to 0 - no data written
*/
int user_uart_write(int fd, const char *buf, unsigned int buf_len);
/**
* @name: user_uart_close
* @description: close UART
* @param fd: File descriptor, user_uart_open return value
* @return 0 - success ; less than 0 - fail
*/
int user_uart_close(int fd);
The test demo is as follows, taking the operation of ttyS4 as an example, shorting 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 the test demo source code test.c into the same path, compile the command as follows
aarch64-none-linux-gnu-gcc test.c peripheral_api.a -I. -o uarttest
The results of the run are as follows