This post was last edited by damiaa on 2024-3-2 21:14
[Luckfox RV1106 Linux Development Board Review] 07 Serial Port Device Application
LuckFox Pico Plus/Pro/Max has five serial ports, UART2, UART3, UART4 and UART0, UART1, among which UART2 is the debug serial port.
The default enabled serial ports are UART3, UART4 and debug serial port.
/dev device directory
You can see some ttySx directories in the /dev directory. You can view them using ls /dev/ttyS*.
To configure and query communication parameters, use the stty command:
stty -F /dev/ttyS3 query communication parameters
stty -F /dev/ttyS3 ispeed 115200 ospeed 115200 Modify communication parameters
usb-uart ttl communication with windows host
Connect one end of the serial port module to the computer and the other end to pins 18 (GND), 19 (UART7_TX) and 20 (UART7_TX) of LuckFox UART3.
Download and open MObaXterm, select the serial port, and set the baud rate (115200, which has been set above).
Remember to set the serial port parameters every time you restart the power:
stty -F /dev/ttyS3 query communication parameters
stty -F /dev/ttyS3 ispeed 115200 ospeed 115200 Modify communication parameters
The experimental results are as follows:
Similarly, connect one end of the serial port module to the computer, and the other end to pins 3 (GND), 6 (UART7_TX) and 7 (UART7_TX) of LuckFox UART4. Same as the computer end.
Set the serial port parameters:
stty -F /dev/ttyS4 query communication parameters
stty -F /dev/ttyS4 ispeed 115200 ospeed 115200 Modify communication parameters
The experimental results are as follows:
Serial communication python program experiment
Use Python's serial library to send and receive data on serial port 3.
Or connect usb-ttl to serial port 3 and run it. The result is as follows:
Use python-periphery to send and receive data on serial port 3.
Or connect usb-ttl to serial port 3 and run it. The result is as follows:
C language to achieve serial communication (this program is suitable for serial port 0 1 3 4 that is, all serial ports of the max board)
The procedure is as follows
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
int main() {
int serial_port_num;
char serial_port[15];
printf("Select a serial port (0/1/3/4): ");
scanf("%d", &serial_port_num);
sprintf(serial_port,"/dev/ttyS%d",serial_port_num);
int serial_fd;
serial_fd = open(serial_port, O_RDWR | O_NOCTTY);
if (serial_fd == -1) {
perror("Failed to open serial port");
return 1;
}
struct termios tty;
memset(&tty, 0, sizeof(tty));
if (tcgetattr(serial_fd, &tty) != 0) {
perror("Error from tcgetattr");
return 1;
}
int bt_num =3;
int baud_val=B115200;
printf(" baud rate select is \n 1: 9600 \n 2: 19200\n 3: 38400\n 4: 57600\n 5: 115200\n");
scanf("%d", &bt_num);
switch(bt_num)
{
case 1:
cfsetospeed(&tty, B9600);
cfsetispeed(&tty, B9600);
baud_val =9600;
break;
case 2:
cfsetospeed(&tty, B19200);
cfsetispeed(&tty, B19200);
baud_val =19200;
break;
case 3:
cfsetospeed(&tty, B38400);
cfsetispeed(&tty, B38400);
baud_val =38400;
break;
case 4:
cfsetospeed(&tty, B57600);
cfsetispeed(&tty, B57600);
baud_val =57600;
break;
case 5:
cfsetospeed(&tty, B115200);
cfsetispeed(&tty, B115200);
baud_val =115200;
break;
default:
cfsetospeed(&tty, B115200);
cfsetispeed(&tty, B115200);
baud_val =B115200;
break;
}
printf("baud rate is %d\n",baud_val);
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
if (tcsetattr(serial_fd, TCSANOW, &tty) != 0) {
perror("Error from tcsetattr");
return 1;
}
char tx_buffer[] = "please send data\n";
int tims=10;
while(tims){
tims--;
ssize_t bytes_written = write(serial_fd, tx_buffer, sizeof(tx_buffer));
if (bytes_written < 0) {
perror("Error writing to serial port");
close(serial_fd);
return 1;
}
printf("\rtx_buffer: \n %s ", tx_buffer);
usleep(100000);
char rx_buffer[256];
int bytes_read = read(serial_fd, rx_buffer, sizeof(rx_buffer));
if (bytes_read > 0) {
rx_buffer[bytes_read] = '\0';
printf("\rrx_buffer: \n %s ", rx_buffer);
} else {
printf(" \n");
}
}
close(serial_fd);
return 0;
}
Cross-compile, transfer to the board, and modify execution properties.
run
Select the serial port and baud rate
View the sending and receiving results
Use device tree to open serial port 0 and serial port 1
We got the rg1106-pico-pro/max board, so there are serial ports 0 and 1. Note that this is not serial port 5!!!
Check it out from the following files:
Found that it was rv1106g-luckfox-pico-pro-max.dts file
Find this file
Remove the following comments for Serial Port 0 and Serial Port 1:
Compile the kernel
- Compile and select branch./build.sh
lunch
select 8
- Recompile the kernel: It seems to take only a few minutes (because it has been compiled before)
Found that two files were updated
- Burning firmware use
Copy the above files and boot.img, download.bin, .env.txt to the windows shared directory, and burn them with Rockchip's burning software:
Note: If Maskrom xx does not appear, press the boot button on the board for a few seconds and it will appear.
Then select the search path to find the file path, and then everything will come out. Check the file you want to download on the left, and then press download.
4. After restarting the board, connect the board and check, and you will find that the serial port 0 and serial port 1 are available.
Try connecting the USB-TTL to the serial port 1 of the board and the USB port of the computer.
Then connect the serial debugging software to the serial port displayed by usb-ttl, set the baud rate to 9600, and use the command to operate the serial port one.
Check the result: it is found that the serial port can communicate, so serial port 0 is not tried.
The above C program is also suitable for testing serial port 0 and serial port 1. You only need to enter the serial port number when running.
Okay, that’s all I have to report for now. Thank you everyone.