Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


products:sbc:common:applications:gpio:uart

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
products:sbc:common:applications:gpio:uart [2022/11/30 04:05]
ivan
products:sbc:common:applications:gpio:uart [2023/11/07 05:09] (current)
nick [Enable UART]
Line 3: Line 3:
 ===== Introduction ===== ===== Introduction =====
  
-This page introduces the usage of UART on [[products:sbc:common:applications:gpio:40pin-header|40-Pin Header]]. +This page introduces the usage of UART on [[products:sbc:common:applications:gpio:40pin-header|40pin-Header]].
  
 ===== UART Information ===== ===== UART Information =====
  
-|            UART       ^ PIN      ^ GPIO Name  ^ GPIO Number   DT Overlays Node  ^  Device Node  ^ +|            UART       ^ PIN      ^ GPIO Name  ^  DT Overlays Node  ^  Device Node  ^ 
-^  VIM1/   UART_AO_B  |  15(RX)  |  GPIOAO_5  |  506         |  uart4              /dev/ttyS4   | +^  VIM1/   UART_AO_B  |  15(RX)  |  GPIOAO_5  |  uart4              /dev/ttyS4   | 
-| :::       | :::          16(TX)  |  GPIOAO_4  |  505         | :::                | :::           | +| :::       | :::          16(TX)  |  GPIOAO_4  | :::                | :::           | 
-^  VIM3/3L  |  UART_C      15(RX)  |  GPIOH_6   |  433         |  uart3              /dev/ttyS3   | +^  VIM3/3L  |  UART_C      15(RX)  |  GPIOH_6    uart3              /dev/ttyS3   | 
-| :::       | :::          16(TX)  |  GPIOH_7   |  434         | :::                | :::           | +| :::       | :::          16(TX)  |  GPIOH_7   | :::                | :::           | 
-^  VIM4      UART_E      15(RX)  |  GPIOY_7   |  491         |  uart_e            |  /dev/ttyS4   | +^  VIM4      UART_E      15(RX)  |  GPIOY_7    uart_e            |  /dev/ttyS4   | 
-| :::       | :::          16(TX)  |  GPIOY_6   |  490         | :::                | :::           | +| :::       | :::          16(TX)  |  GPIOY_6   | :::                | :::           | 
-^  VIM1S    |  UART_C      15(RX)  |  GPIOD_3   |  457         |  uart_c            |  /dev/ttyS2   | +^  VIM1S    |  UART_C      15(RX)  |  GPIOD_3    uart_c            |  /dev/ttyS2   | 
-| :::       | :::          16(TX)  |  GPIOD_2   |  456         | :::                | :::           | +| :::       | :::          16(TX)  |  GPIOD_2   | :::                | :::           |
-^  Edge2    |  UART1      |   2(TX)  |  GPIO0_B5  |  12          |  uart1                              +
-| :::       | :::           3(RX)  |  GPIO0_B6  |  13          | :::                | :::           |+
 ===== Enable UART ===== ===== Enable UART =====
  
Line 31: Line 28:
 ```shell ```shell
 overlays=pwm_ao_a pwm_f uart3 overlays=pwm_ao_a pwm_f uart3
 +```
 +
 +After reboot, you will see the UART device node.
 +
 +```shell
 +$ ls /dev/ttyS3
 +/dev/ttyS3
 +```
 +
 +<tabbox VIM3 with 5.15 kernel>
 +
 +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.
 +
 +```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 ''/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.
 +
 +```shell
 +fdt_overlays=uart3
 +
 ``` ```
  
Line 78: Line 113:
 ``` ```
  
-<tabbox Edge2>+</tabbox>
  
-Edit ''/boot/dtb/rockchip/rk3588s-khadas-edge2.dtb.overlay.env'' to add uart node to ''fdt_overlays'' node if it doesn't exist. +===== Demo Source Code ===== 
-```shell +```c uart.c 
-fdt_overlays=uart1+#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; 
 +}
 ``` ```
-After reboot, you will see the UART device node.  
  
-```shell +Compile test code: 
-ls /dev/xx + 
-/dev/xx+``` 
 +gcc uart.c -o uart
 ``` ```
  
-</tabbox>+===== Test demonstration =====
  
 +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
 +```
  
 ===== Disable UART to Use GPIO ===== ===== Disable UART to Use GPIO =====
Last modified: 2022/11/30 04:05 by ivan