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/07/04 22:27]
nick ↷ Links adapted because of a move operation
products:sbc:common:applications:gpio:uart [2023/11/07 05:09] (current)
nick [Enable UART]
Line 3: Line 3:
 ===== Introduction ===== ===== Introduction =====
  
-This document mainly introduces UART. You will learn how to enable UART on 40-Pin Header on Khadas SBC.+This page introduces the usage of UART on [[products:sbc:common:applications:gpio:40pin-header|40pin-Header]].
  
 +===== UART Information =====
 +
 +|            UART       ^ PIN      ^ GPIO Name  ^  DT Overlays Node  ^  Device Node  ^
 +^  VIM1/   UART_AO_B  |  15(RX)  |  GPIOAO_5  |  uart4              /dev/ttyS4   |
 +| :::       | :::          16(TX)  |  GPIOAO_4  | :::                | :::           |
 +^  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 =====
  
-Uart PIN in 40-Pin Header is PIN15 and PIN16. Refer to [[products:sbc:vim4:configurations:device-tree-overlay|Device Tree Overlay]].+In order to use the UART, you need to enable the UART function via [[products:sbc:common:configurations:device-tree-overlay|Device Tree Overlay]].
  
-Enable uart node via device tree overlays. Edit ''/boot/env.txt'' to add ''uartX'' to overlays. E.g.+<tabbox VIM1/2/3/3L/Edge1>
  
-```sh +Edit ''/boot/env.txt'' to add the uart node to ''overlays'' node if it doesn't exist. 
-overlays=uart4 pwm_ao_a pwm_f i2c0+ 
 +Take VIM3 as an example to enable ''UART_C'', you need to add ''uart3'' node to ''overlays'' node if it doesn't exist. 
 + 
 +```shell 
 +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 ''/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
 +
 +```
 +
 +After reboot, you will see the UART device node.
 +
 +```shell
 +$ ls /dev/ttyS3
 +/dev/ttyS3
 +```
 +
 +<tabbox VIM4>
 +
 +Edit ''/boot/dtb/amlogic/kvim4.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 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>
 +
 +===== 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: 2022/07/04 22:27 by nick