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 four 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 four universal serial ports are 3, 4, 8 and 9, and the corresponding device nodes under Linux are /dev/ttyS3 /dev/ttyS4 /dev/ttyS8 /dev/ttyS9. The following is an example of the usage of ttyS4, and the other serial ports are similar.
The following is a transceiver test by shorting the TX and RX of ttyS4 in hardware, wired as follows
The node "/dev/ttyS*" requires root privileges to operate, and root privileges are required to operate the serial port using command line or C programs. If you are using ssh or LX terminal, first execute the following command to get root privileges.
sudo su
View serial port information
stty -F /dev/ttyS4
Set the serial port baud rate to 115200
stty -F /dev/ttyS4 speed 115200
Set eight data bits of serial port No parity One stop bit No replay
stty -F /dev/ttyS4 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/ttyS4 &
echo -e "12345\n" > /dev/ttyS4
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
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] = 0x30 + i;
}
fd = user_uart_open(4);
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;
}
The results of the run are as follows
root@linaro-alip:/home/linaro# ./test
buf_write buf_read same