UART
1. UART Introduction1.1 Serial Port Resource OverviewThe serial port resources of the EASY EAI Nano-TB development board are divided into two categories:
- Unavailable Serial Ports: Occupied by specific functions and cannot be used for general purposes.
- Available Serial Ports: Can be freely used by users.
Unavailable Serial Ports Distribution
The hardware distribution is shown in the figure below.
Available Serial Ports Distribution
The hardware distribution is shown in the figure below.
- Normal Wiring: For communication between devices.
Wiring for This Test Case: Short the Rxd and Txd pins with a jumper cap to implement loopback (self-transmit and self-receive) testing
If you are reading this document for the first time, refer to 《Getting Started Guide / Development Environment Preparation / Easy-Eai Compilation Environment Preparation and Update 》,and follow the instructions to deploy the compilation environment.
Execute the run script in the Ubuntu system on the PC to enter the EASY-EAI compilation environment:
cd ~/develop_environment
./run.shIn the background terminal of the virtual machine, create a directory for managing peripheral singleton source code:
cd /opt
mkdir -p EASY-EAI-Nano-TB/demoDownload the relevant singleton program from Baidu Netdisk:
- Link: https://pan.baidu.com/s/1Br608Hiff2Xs65PzWO_qWQ?pwd=1234
- Extraction Code: 1234
Save the downloaded singleton program to a local directory (e.g., This PC\D:\BaiduNetdisk; no fixed path, user-selectable), as shown in the figure.
Copy the downloaded singleton program to the virtual machine file system (as shown in the figure).
📷📷
Navigate to the corresponding example directory and execute the compilation command:
cd EASY-EAI-Nano-TB/demo/06_UART
./build.shNote:
- Since dependent libraries are deployed on the board, the
/mntmount must be maintained during cross-compilation.
After successful compilation, 2 executable programs will be generated in the Release directory and automatically deployed to the /userdata/ directory of the development board:
- Transmitter demo:
test-Send - Receiver demo:
test-Recv
Access the board background via serial port debuggingor SSH, and navigate to the example deployment directory:
cd /userdataRun the receiver demo in the background first:
sudo ./test-Recv /dev/ttyS2 &The receiver will wait for data from the transmitter (see the figure below for execution results).
Then run the transmitter demo:
sudo ./test-Send /dev/ttyS2The C language code for the serial port receiver is located at 06_UART/test-uart/Recv.c (for reference). The following code demonstrates the operation process of the serial port receiver:
int main(int argc, char **argv)
{
if(2 != argc){
printf("Usage:\n");
printf(" sudo %s %s\n", argv[0], "/dev/ttyS<2/6/7>");
return -1;
}
int fd = UART_Open(argv[1]);
if(fd < 0){
printf("\033[33m【Open ERROR!】%s\n", DEBUG_COLOR_TAIL);
return -1;
}
if(false == UART_Set(fd, 115200, 0, 8, 1, 'N')){
printf("\033[33m【Init ERROR!】%s\n", DEBUG_COLOR_TAIL);
return -1;
}
const char *strReceiver = "I am uart Receiver";
printf("\033[36m【Init OK \"%s\"】%s\n", strReceiver, DEBUG_COLOR_TAIL);
char recvBuf[128]={0};
while(1){
if(UART_Recv(fd, recvBuf, sizeof(recvBuf)) <= 0){
continue;
}else{
printf("\033[36m【Recv Msg from Sender】:%s", DEBUG_COLOR_TAIL);
printf(" %s\n", recvBuf);
break;
}
}
UART_Close(fd);
printf("\033[42m【Recv date OK. BYE BYE!】%s\n", DEBUG_COLOR_TAIL);
return 0;
}The C language code for the serial port transmitter is located at 06_UART/test-uart/Send.c (for reference). The following code demonstrates the operation process of the serial port transmitter:
int main(int argc, char **argv)
{
if(2 != argc){
printf("Usage:\n");
printf(" sudo %s %s\n", argv[0], "/dev/ttyS<2/6/7>");
return -1;
}
int fd = UART_Open(argv[1]);
if(fd < 0){
printf("\033[33m【Open ERROR!】%s\n", DEBUG_COLOR_TAIL);
return -1;
}
if(false == UART_Set(fd, 115200, 0, 8, 1, 'N')){
printf("\033[33m【Init ERROR!】%s\n", DEBUG_COLOR_TAIL);
return -1;
}
char *strSender = "I am uart Sender";
printf("\033[36m【Init OK \"%s\"】%s\n", strSender, DEBUG_COLOR_TAIL);
int len = UART_Send(fd, strSender, strlen(strSender));
if(len <= 0){
printf("\033[41m【Send data ERROR!】%s\n", DEBUG_COLOR_TAIL);
return -1;
}
UART_Close(fd);
printf("\033[42m【Send date OK. BYE BYE!】%s\n", DEBUG_COLOR_TAIL);
return 0;
}UART_Open(), UART_Set(), UART_Send(), and UART_Recv() are user-friendly encapsulations of system calls, implemented in 06_UART/commonApi/uart.c.




Comments