This page introduces the usage of UART on 40pin-Header.
UART | PIN | GPIO Name | DT Overlays Node | Device Node | |
---|---|---|---|---|---|
VIM1/2 | UART_AO_B | 15(RX) | GPIOAO_5 | uart4 | /dev/ttyS4 |
16(TX) | GPIOAO_4 | ||||
VIM3/3L | UART_C | 15(RX) | GPIOH_6 | uart3 | /dev/ttyS3 |
16(TX) | GPIOH_7 | ||||
VIM4 | UART_E | 15(RX) | GPIOY_7 | uart_e | /dev/ttyS4 |
16(TX) | GPIOY_6 | ||||
VIM1S | UART_C | 15(RX) | GPIOD_3 | uart_c | /dev/ttyS2 |
16(TX) | GPIOD_2 |
In order to use the UART, you need to enable the UART function via Device Tree Overlay.
Edit /boot/env.txt
to add the uart node to overlays
node if it doesn't exist.
Take VIM3 as an example to enable UART_C
, you need to add uart3
node to overlays
node if it doesn't exist.
overlays=pwm_ao_a pwm_f uart3
After reboot, you will see the UART device node.
$ ls /dev/ttyS3
/dev/ttyS3
Edit /boot/dtb/amlogic/kvim3.dtb.overlay.env
to add uart node to fdt_overlays
node if it doesn't exist.
e.g. Enable UART_C
, you need to add uart3
to node fdt_overlays
if it doesn't exist.
fdt_overlays=uart3
After reboot, you will see the UART device node.
$ ls /dev/ttyS3
/dev/ttyS3
Edit /boot/dtb/amlogic/kvim3l.dtb.overlay.env
to add uart node to fdt_overlays
node if it doesn't exist.
e.g. Enable UART_C
, you need to add uart3
to node fdt_overlays
if it doesn't exist.
fdt_overlays=uart3
After reboot, you will see the UART device node.
$ ls /dev/ttyS3
/dev/ttyS3
Edit /boot/dtb/amlogic/kvim4.dtb.overlay.env
to add uart node to fdt_overlays
node if it doesn't exist.
e.g. Enable UART_E
, you need to add uart_e
to node fdt_overlays
if it doesn't exist.
fdt_overlays=uart_e
After reboot, you will see the UART device node.
$ ls /dev/ttyS4
/dev/ttyS4
Edit /boot/dtb/amlogic/kvim1s.dtb.overlay.env
to add uart node to fdt_overlays
node if it doesn't exist.
e.g. Enable UART_C
, you need to add uart_c
to node fdt_overlays
if it doesn't exist.
fdt_overlays=uart_c
After reboot, you will see the UART device node.
$ ls /dev/ttyS2
/dev/ttyS2
UART_C
is enabled by default in kvim1s.dts
, The corresponding device node is /dev/ttyS2
.
UART_E
is enabled by default, the corresponding dev node is /dev/ttyS4
.
UART_C
is enabled by default in kvim3.dts
, The corresponding device node is /dev/ttyS3
.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> // set correct serial path #define SERIAL_PORT "/dev/ttyS2" int main() { int serial_port; struct termios tty; // open serial_port = open(SERIAL_PORT, O_RDWR | O_NOCTTY); if (serial_port < 0) { perror("Error opening serial port"); return 1; } // Configuration of serial memset(&tty, 0, sizeof(tty)); if (tcgetattr(serial_port, &tty) != 0) { perror("Error getting serial port attributes"); close(serial_port); return 1; } cfsetospeed(&tty, B921600); // set correct baud rate cfsetispeed(&tty, B921600); tty.c_cflag |= (CLOCAL | CREAD); tty.c_cflag &= ~PARENB; tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CSIZE; tty.c_cflag |= CS8; // write configration to serial if (tcsetattr(serial_port, TCSANOW, &tty) != 0) { perror("Error setting serial port attributes"); close(serial_port); return 1; } // loop test while (1) { // receive data char received_data[64]; if (read(serial_port, &received_data, sizeof(char)*64)) { // get data printf("Received: %s", received_data); } // Only send "Hello, UART!" char data_to_send[] = "Hello, UART!\n"; write(serial_port, data_to_send, strlen(data_to_send)); // sleep 100ms usleep(100000); } // close close(serial_port); return 0; }
Compile test code:
$ gcc uart.c -o uart
Run uart
$ ./uart
After connecting PIN15, PIN16 and GND to PC, open ttyUSBX and input hello
1 hello 2 Hello, UART!
Check output data:
$ ./uart Received: hello
If you want to use normal GPIO instead of UART, you can remove the UART node in Device Tree Overlay.