This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
products:sbc:common:applications:gpio:uart [2022/12/02 01:44] ivan [Introduction] |
products:sbc:common:applications:gpio:uart [2023/11/07 05:09] nick [Enable UART] |
||
---|---|---|---|
Line 7: | Line 7: | ||
===== UART Information ===== | ===== UART Information ===== | ||
- | | | + | | |
- | ^ VIM1/ | + | ^ VIM1/ |
- | | ::: | ::: | + | | ::: | ::: |
- | ^ VIM3/ | + | ^ VIM3/ |
- | | ::: | ::: | + | | ::: | ::: |
- | ^ VIM4 | + | ^ VIM4 |
- | | ::: | ::: | + | | ::: | ::: |
- | ^ VIM1S | UART_C | + | ^ VIM1S | UART_C |
- | | ::: | ::: | + | | ::: | ::: |
- | ^ Edge2 | UART7 | 12(TX) | + | |
- | | ::: | ::: | + | |
===== Enable UART ===== | ===== Enable UART ===== | ||
Line 37: | Line 35: | ||
$ ls /dev/ttyS3 | $ ls /dev/ttyS3 | ||
/dev/ttyS3 | /dev/ttyS3 | ||
+ | ``` | ||
+ | |||
+ | <tabbox VIM3 with 5.15 kernel> | ||
+ | |||
+ | 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 VIM3L with 5.15 kernel> | ||
+ | |||
+ | Edit ''/ | ||
+ | |||
+ | |||
+ | e.g. Enable '' | ||
+ | |||
+ | ```shell | ||
+ | fdt_overlays=uart_e | ||
+ | |||
+ | ``` | ||
+ | |||
+ | After reboot, you will see the UART device node. | ||
+ | |||
+ | ```shell | ||
+ | $ ls /dev/ttyS4 | ||
+ | /dev/ttyS4 | ||
``` | ``` | ||
Line 77: | Line 113: | ||
``` | ``` | ||
- | < | + | </tabbox> |
- | Edit ''/ | + | ===== Demo Source Code ===== |
+ | ```c uart.c | ||
+ | #include <stdio.h> | ||
+ | #include <stdlib.h> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include <termios.h> | ||
- | e.g. Enable '' | + | // set correct serial path |
+ | #define SERIAL_PORT "/ | ||
- | ```shell | + | int main() { |
- | fdt_overlays=uart7 | + | int serial_port; |
+ | struct termios tty; | ||
+ | |||
+ | // 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; | ||
+ | } | ||
``` | ``` | ||
- | After reboot, you will see the UART device node. | ||
- | ```shell | + | Compile test code: |
- | $ ls / | + | |
- | /dev/ttyS7 | + | ``` |
+ | $ 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 ===== |