This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
products:sbc:common:applications:gpio:uart [2022/07/12 22:49] nick |
products:sbc:common:applications:gpio:uart [2024/08/06 08:27] (current) nick |
||
---|---|---|---|
Line 3: | Line 3: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This document mainly | + | This page introduces the usage of UART on [[products: |
+ | ===== UART Information ===== | ||
+ | |||
+ | | | ||
+ | ^ VIM1/ | ||
+ | | ::: | ::: | ||
+ | ^ VIM3/ | ||
+ | | ::: | ::: | ||
+ | ^ VIM4 | ||
+ | | ::: | ::: | ||
+ | ^ VIM1S | UART_C | ||
+ | | ::: | ::: | ||
===== Enable UART ===== | ===== Enable UART ===== | ||
- | Uart PIN in 40-Pin Header is PIN15 and PIN16. Refer to [[products: | + | ==== Linux ==== |
- | Enable uart node via device tree overlays. Edit ''/ | + | |
+ | In order to use the UART, you need to enable the UART function | ||
+ | |||
+ | <tabbox VIM1/ | ||
+ | |||
+ | Edit ''/ | ||
+ | |||
+ | Take VIM3 as an example | ||
```shell | ```shell | ||
- | overlays=uart4 pwm_ao_a pwm_f i2c0 | + | overlays=pwm_ao_a pwm_f uart3 |
``` | ``` | ||
- | Reboot to effect. | + | After reboot, you will see the UART device node. |
- | <WRAP info > | + | ```shell |
- | * VIM1/VIM2/VIM4 - uar4 | + | $ ls /dev/ttyS3 |
- | * VIM3/VIM3L - uart3 | + | /dev/ttyS3 |
- | </WRAP> | + | ``` |
+ | <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 /dev/ttyS3 | ||
+ | /dev/ttyS3 | ||
+ | ``` | ||
+ | |||
+ | <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 /dev/ttyS3 | ||
+ | /dev/ttyS3 | ||
+ | ``` | ||
+ | |||
+ | <tabbox VIM4> | ||
+ | |||
+ | Edit ''/ | ||
+ | |||
+ | |||
+ | e.g. Enable '' | ||
+ | |||
+ | ```shell | ||
+ | fdt_overlays=uart_e | ||
+ | |||
+ | ``` | ||
+ | |||
+ | After reboot, you will see the UART device node. | ||
+ | |||
+ | ```shell | ||
+ | $ ls /dev/ttyS4 | ||
+ | /dev/ttyS4 | ||
+ | ``` | ||
+ | |||
+ | <tabbox VIM1S> | ||
+ | |||
+ | Edit ''/ | ||
+ | |||
+ | |||
+ | e.g. Enable '' | ||
+ | |||
+ | ```shell | ||
+ | fdt_overlays=uart_c | ||
+ | |||
+ | ``` | ||
+ | |||
+ | After reboot, you will see the UART device node. | ||
+ | |||
+ | ```shell | ||
+ | $ ls /dev/ttyS2 | ||
+ | /dev/ttyS2 | ||
+ | ``` | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ==== 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 | ||
+ | ``` | ||
- | ===== Check UART Node ===== | + | ===== Disable |
- | After reboot, you will see the uart device | + | If you want to use normal GPIO instead of UART, you can remove |
- | * VIM1/ | ||
- | * VIM3/VIM3L - /dev/ttyS3 |