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
Last revision Both sides next revision
products:sbc:common:applications:gpio:uart [2022/07/12 23:05]
nick
products:sbc:common:applications:gpio:uart [2023/11/07 05:09]
nick [Enable UART]
Line 3: Line 3:
 ===== Introduction ===== ===== Introduction =====
  
-This document mainly 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(#Number)                                ^  DT Overlays Node  ^  Device Node  ^ +|            UART       ^ PIN      GPIO Name  ^  DT Overlays Node  ^  Device Node  ^ 
-^  VIM1/   UART_AO_B  |  RX(15) - GPIOAO_5(#506)\\ TX(16) - GPIOAO_4(#505)  |  uart4              /dev/ttyS4   | +^  VIM1/   UART_AO_B  |  15(RX  GPIOAO_5   uart4              /dev/ttyS4   
-^  VIM3/3L  |  UART_C     |  RX(15) - GPIOH_6(#433)\\ TX(16) - GPIOH_7(#434)    |  uart3              /dev/ttyS3   | +| :::       | :::          16(TX)  |  GPIOAO_4  | :::                | :::           
-^  VIM4      UART_E     |  RX(15) GPIOY_7(#491)\\ TX(16GPIOY_6(#490   |  uart_e             /dev/ttyS4   | +^  VIM3/3L  |  UART_C      15(RX  GPIOH_6   |  uart3              /dev/ttyS3   
- +| :::       | :::          16(TX)  |  GPIOH_7   | :::                | :::           
 +^  VIM4      UART_E      15(RX |  GPIOY_7   |  uart_e            |  /dev/ttyS4   | 
 +| :::       | :::          16(TX)  |  GPIOY_6   | :::                | :::           | 
 +^  VIM1S    |  UART_C      15(RX |  GPIOD_3    uart_c             /dev/ttyS2   | 
 +| :::       | :::          16(TX)  |  GPIOD_2   | :::                | :::           |
 ===== Enable UART ===== ===== Enable UART =====
  
Line 28: Line 30:
 ``` ```
  
-After reboot, you will see the I2C device node.+After reboot, you will see the UART device node.
  
 ```shell ```shell
 $ ls /dev/ttyS3 $ ls /dev/ttyS3
 /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_E'', you need to add ''uart_e'' to node ''fdt_overlays'' if it doesn't exist.
 +
 +```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 ''/boot/dtb/amlogic/kvim3l.dtb.overlay.env'' to add uart node to ''fdt_overlays'' node if it doesn't exist.
 +
 +
 +e.g. Enable ''UART_E'', you need to add ''uart_e'' to node ''fdt_overlays'' if it doesn't exist.
 +
 +```shell
 +fdt_overlays=uart_e
 +
 +```
 +
 +After reboot, you will see the UART device node.
 +
 +```shell
 +$ ls /dev/ttyS4
 +/dev/ttyS4
 ``` ```
  
Line 47: Line 87:
 ``` ```
  
-After reboot, you will see the I2C device node.+After reboot, you will see the UART device node.
  
 ```shell ```shell
 $ ls /dev/ttyS4 $ ls /dev/ttyS4
 /dev/ttyS4 /dev/ttyS4
 +```
 +
 +<tabbox VIM1S>
 +
 +Edit ''/boot/dtb/amlogic/kvim1s.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 ''uart_c'' to node ''fdt_overlays'' if it doesn't exist.
 +
 +```shell
 +fdt_overlays=uart_c
 +
 +```
 +
 +After reboot, you will see the UART device node.
 +
 +```shell
 +$ ls /dev/ttyS2
 +/dev/ttyS2
 ``` ```
  
 </tabbox> </tabbox>
  
 +===== Demo Source Code =====
 +```c uart.c
 +#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;
 +}
 +```
 +
 +Compile test code:
 +
 +```
 +$ gcc uart.c -o uart
 +```
 +
 +===== 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
 +```
  
-===== Check UART Node =====+===== Disable UART to Use GPIO =====
  
-After reboot, you will see the uart device node.+If you want to use normal GPIO instead of UART, you can remove the UART node in [[products:sbc:common:configurations:device-tree-overlay|Device Tree Overlay]].
  
-  * VIM1/VIM2/VIM4 - /dev/ttyS4 
-  * VIM3/VIM3L - /dev/ttyS3 
Last modified: 2023/11/07 05:09 by nick