This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
products:sbc:common:applications:gpio:uart [2022/06/29 21:51] 127.0.0.1 external edit |
products:sbc:common:applications:gpio:uart [2024/08/06 08:27] (current) nick |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | # UART | + | ===== UART ===== |
| + | |||
| + | ===== Introduction ===== | ||
| + | |||
| + | This page introduces the usage of UART on [[products: | ||
| + | |||
| + | ===== UART Information ===== | ||
| + | |||
| + | | | ||
| + | ^ VIM1/ | ||
| + | | ::: | ::: | ||
| + | ^ VIM3/ | ||
| + | | ::: | ::: | ||
| + | ^ VIM4 | ||
| + | | ::: | ::: | ||
| + | ^ VIM1S | UART_C | ||
| + | | ::: | ::: | ||
| + | ===== Enable UART ===== | ||
| + | |||
| + | ==== Linux ==== | ||
| + | |||
| + | |||
| + | In order to use the UART, you need to enable the UART function via [[products: | ||
| + | |||
| + | <tabbox VIM1/ | ||
| + | |||
| + | Edit ''/ | ||
| + | |||
| + | Take VIM3 as an example to enable '' | ||
| + | |||
| + | ```shell | ||
| + | overlays=pwm_ao_a pwm_f uart3 | ||
| + | ``` | ||
| + | |||
| + | After reboot, you will see the UART device node. | ||
| + | |||
| + | ```shell | ||
| + | $ ls / | ||
| + | / | ||
| + | ``` | ||
| + | |||
| + | <tabbox VIM3 with 5.15 kernel> | ||
| + | |||
| + | Edit ''/ | ||
| + | |||
| + | |||
| + | e.g. Enable '' | ||
| + | |||
| + | ```shell | ||
| + | fdt_overlays=uart3 | ||
| + | |||
| + | ``` | ||
| + | |||
| + | After reboot, you will see the UART device node. | ||
| + | |||
| + | ```shell | ||
| + | $ ls / | ||
| + | / | ||
| + | ``` | ||
| + | |||
| + | <tabbox VIM3L with 5.15 kernel> | ||
| + | |||
| + | Edit ''/ | ||
| + | |||
| + | |||
| + | e.g. Enable '' | ||
| + | |||
| + | ```shell | ||
| + | fdt_overlays=uart3 | ||
| + | |||
| + | ``` | ||
| + | |||
| + | After reboot, you will see the UART device node. | ||
| + | |||
| + | ```shell | ||
| + | $ ls / | ||
| + | / | ||
| + | ``` | ||
| + | |||
| + | <tabbox VIM4> | ||
| + | |||
| + | Edit ''/ | ||
| + | |||
| + | |||
| + | e.g. Enable '' | ||
| + | |||
| + | ```shell | ||
| + | fdt_overlays=uart_e | ||
| + | |||
| + | ``` | ||
| + | |||
| + | After reboot, you will see the UART device node. | ||
| + | |||
| + | ```shell | ||
| + | $ ls / | ||
| + | / | ||
| + | ``` | ||
| + | |||
| + | <tabbox VIM1S> | ||
| + | |||
| + | Edit ''/ | ||
| + | |||
| + | |||
| + | e.g. Enable '' | ||
| + | |||
| + | ```shell | ||
| + | fdt_overlays=uart_c | ||
| + | |||
| + | ``` | ||
| + | |||
| + | After reboot, you will see the UART device node. | ||
| + | |||
| + | ```shell | ||
| + | $ ls / | ||
| + | / | ||
| + | ``` | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Android ==== | ||
| + | |||
| + | <tabbox VIM1S> | ||
| + | |||
| + | '' | ||
| + | |||
| + | |||
| + | <tabbox VIM4> | ||
| + | |||
| + | '' | ||
| + | |||
| + | <tabbox VIM3> | ||
| + | |||
| + | '' | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Demo Source Code ===== | ||
| + | ```c uart.c | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // set correct serial path | ||
| + | #define SERIAL_PORT "/ | ||
| + | |||
| + | int main() { | ||
| + | int serial_port; | ||
| + | struct termios tty; | ||
| + | |||
| + | // open | ||
| + | serial_port = open(SERIAL_PORT, | ||
| + | if (serial_port < 0) { | ||
| + | perror(" | ||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | // Configuration of serial | ||
| + | memset(& | ||
| + | if (tcgetattr(serial_port, | ||
| + | perror(" | ||
| + | close(serial_port); | ||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | cfsetospeed(& | ||
| + | cfsetispeed(& | ||
| + | |||
| + | 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, | ||
| + | perror(" | ||
| + | close(serial_port); | ||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | // loop test | ||
| + | while (1) { | ||
| + | // receive data | ||
| + | char received_data[64]; | ||
| + | if (read(serial_port, | ||
| + | // get data | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | // Only send " | ||
| + | char data_to_send[] = " | ||
| + | write(serial_port, | ||
| + | |||
| + | // sleep 100ms | ||
| + | usleep(100000); | ||
| + | } | ||
| + | |||
| + | // close | ||
| + | close(serial_port); | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | Compile test code: | ||
| + | |||
| + | ``` | ||
| + | $ gcc uart.c -o uart | ||
| + | ``` | ||
| + | |||
| + | ===== Test demonstration ===== | ||
| + | |||
| + | Run '' | ||
| + | |||
| + | ``` | ||
| + | $ ./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 | ||
| + | ``` | ||
| + | |||
| + | ===== Disable UART to Use GPIO ===== | ||
| + | |||
| + | If you want to use normal GPIO instead of UART, you can remove the UART node in [[products: | ||