Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


products:sbc:edge2:applications:gpio:uart

Differences

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

Link to this comparison view

Next revision
Previous revision
products:sbc:edge2:applications:gpio:uart [2022/11/20 22:02]
ivan created
products:sbc:edge2:applications:gpio:uart [2023/08/13 22:40] (current)
jacobe
Line 1: Line 1:
 +~~tag>Edge2 UART~~
 +
 ====== Edge2 UART ====== ====== Edge2 UART ======
  
  
  
-{{page>products:sbc:common:applications:gpio:uart&noheader}}+===== Introduction ===== 
 + 
 +This page introduces the usage of hardware UART on [[products:sbc:edge2:applications:gpio:40pin-header|16-pin Header]]. 
 + 
 +<WRAP important > 
 +For Edge2, in order to use the UART , you need to attach the IO extension board. 
 +</WRAP> 
 +===== UART Information ===== 
 + 
 +|            UART       ^ PIN      ^ GPIO Name  ^ GPIO Number  ^  DT Overlays Node  ^  Device Node  ^ 
 +^  Edge2    |  UART7      |  12(TX)  |  GPIO3_C0  |  112          edge2-io-uart     |  /dev/ttyS7      
 +| :::       | :::          14(RX)  |  GPIO3_C1  |  113         | :::                | :::           | 
 +===== Enable UART ===== 
 + 
 +In order to use the UART, you need to enable the UART function via [[products:sbc:common:configurations:device-tree-overlay|Device Tree Overlay]]. 
 + 
 + 
 +Edit ''/boot/dtb/rockchip/rk3588s-khadas-edge2.dtb.overlay.env'' to add uart node to ''fdt_overlays'' node if it doesn't exist. 
 + 
 +e.g. Enable ''UART7'', you need to add ''edge2-io-uart'' to node ''fdt_overlays'' if it doesn't exist. 
 + 
 +```shell 
 +fdt_overlays=edge2-io-uart 
 +``` 
 +After reboot, you will see the UART device node.  
 + 
 +```shell 
 +$ ls /dev/ttyS7 
 +/dev/ttyS7 
 +``` 
 + 
 +===== 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 serial path 
 +#define SERIAL_PORT "/dev/ttyS7" 
 + 
 +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, B1500000); // set baud rate 
 +    cfsetispeed(&tty, B1500000); 
 +     
 +    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 
 +``` 
 + 
 +Run ''uart'' 
 + 
 +``` 
 +$ ./uart 
 +``` 
 + 
 +After connecting PIN12(STX), PIN14(CSO) 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 ===== 
 + 
 +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]].
Last modified: 2022/11/20 22:02 by ivan