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/08/07 21:33] olivia [VIM1/2/3/3L/Edge1] |
products:sbc:common:applications:gpio:uart [2024/08/06 08:27] (current) nick |
||
---|---|---|---|
Line 3: | Line 3: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This page mainly | + | This page introduces the usage of UART on [[products: |
===== UART Information ===== | ===== UART Information ===== | ||
- | | | + | | |
- | ^ VIM1/ | + | ^ VIM1/ |
- | | ::: | ::: | + | | ::: | ::: |
- | ^ VIM3/ | + | ^ VIM3/ |
- | | ::: | ::: | + | | ::: | ::: |
- | ^ VIM4 | + | ^ VIM4 |
- | | ::: | ::: | + | | ::: | ::: |
+ | ^ VIM1S | UART_C | ||
+ | | ::: | ::: | ||
+ | ===== Enable UART ===== | ||
+ | ==== Linux ==== | ||
- | ===== Enable UART ===== | ||
In order to use the UART, you need to enable the UART function via [[products: | In order to use the UART, you need to enable the UART function via [[products: | ||
Line 31: | Line 33: | ||
``` | ``` | ||
- | After the reboot, you will see the UART device node. | + | 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 | ```shell | ||
Line 55: | Line 95: | ||
$ ls /dev/ttyS4 | $ ls /dev/ttyS4 | ||
/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 | ||
+ | ``` | ||
===== Disable UART to Use GPIO ===== | ===== Disable UART to Use GPIO ===== | ||
- | If you want to use normal GPIO instead of UART, you can remove the UART node in device tree overlays configration file. | + | If you want to use normal GPIO instead of UART, you can remove the UART node in [[products: |