Article directory
-
-
- Note when playing with H616:
-
- Linux kernel version 5.16 and above, you need to manually configure the i2c-3 uart5 driver
-
- Configuration example
- When compiling by files, each file must be compiled together (separated by spaces)
-
- Example: ggc a.c b.c b.h -lpthread -lxxx..;
- Common commands
-
- View driver files
- View kernel detection information/hardware
- Use the wiringPi library to complete the serial communication program:
-
- serialTest.c
- Use the standard library to complete the self-made serial port library (implemented in separate files):
-
- uartTool.h
- uartTool.c
- my_uart_ttyS5_test.c
-
H616 Notes when playing:
Linux kernel version 5.16 and above, you need to manually configure the i2c-3 uart5 driver
uart5 is the ttyS5 serial communication port
Configuration example
orangepi@orangepizero2:~/y$ uname -r //Check the kernel version 5.16.17-sun50iw9 //Open and modify the configuration file under boot orangepi@orangepizero2:~/y$ sudo vi /boot/orangepiEnv.txt 1 verbosity=7 2 bootlogo=serial 3 console=both 4 disp_mode=1920x1080p60 5 overlay_prefix=sun50i-h616 6 rootdev=UUID=5e468073-a25c-4048-be42-7f5 cfc36ce25 7 rootfstype=ext4 8 overlays=i2c3 uart5 //Add adaptation to i2c-3 and uart5 here 9 usbstoragequirks=0x2537:0x1066:u,0x2537: 0x1068:u
When compiling by files, each file must be compiled together (separated by spaces)
Example: ggc a.c b.c b.h -lpthread -lxxx…;
Commonly used scripts can be encapsulated into a build.sh script for easy compilation; example: orangepi@orangepizero2:~/y$ cat build.sh gcc $1 uartTool.h uartTool.c -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt After giving execution permission to the sh script, the compilation can be completed by using: orangepi@orangepizero2:~/y$ ./build.sh my_uartxxx.c
Common commands
View driver files
Example_serial port: ls /dev/ttyS*
View kernel detection information/hardware
Example_usb device: ls /dev/bus/usb
Use the wiringPi library to complete the serial communication program:
serialTest.c
/* serialTest.c */ #include <stdio.h> #include <string.h> #include <errno.h> #include <wiringPi.h> #include <wiringSerial.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> int fd; void* func1() { char buf[32] = {0}; char *data = buf; while(1){ memset(data,'\0',32); printf("input send data to serial: \\ "); scanf("%s",data); //Send data_serial port int i = 0; while(*data){ serialPutchar (fd, *data + + ); } } } void* func2() { while(1){ while(serialDataAvail(fd)){ //Receive data_serial port printf("%c",serialGetchar (fd)); //Clear/refresh the write buffer (standard output stream stdout); //Ensure printout is displayed immediately on the console rather than waiting in a buffer. fflush (stdout); } } } int main() { unsigned int nextTime; pthread_t t1,t2; if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0) { fprintf (stderr, "Unable to open serial device: %s\\ ", strerror (errno)) ; return 1; } pthread_create( & amp;t1,NULL,func1,NULL); pthread_create( & amp;t2,NULL,func2,NULL); if (wiringPiSetup () == -1) { fprintf (stdout, "Unable to start wiringPi: %s\\ ", strerror (errno)) ; return 1; } while(1){sleep(10);}; return 0; }
Use the standard library to complete the self-made serial port library (implemented in separate files):
uartTool.h
/* uartTool.h */ int mySerialOpen (const char *device, const int baud); void mySerialsend (const int fd,const char* s); int mySerialread (const int fd,char* buf);
uartTool.c
/* uartTool.c*/ #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <stdarg.h> #include <string.h> #include <termios.h> #include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include "wiringSerial.h" int mySerialOpen (const char *device, const int baud) { struct termios options; speed_t myBaud; int status, fd; switch (baud) { case 9600: myBaud = B9600; break; case 115200: myBaud = B115200; break; default: return -2; } if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1) return -1 ; fcntl (fd, F_SETFL, O_RDWR); // Get and modify current options: tcgetattr (fd, & amp;options); cfmakeraw ( & amp;options) ; cfsetispeed ( & amp;options, myBaud) ; cfsetospeed ( & amp;options, myBaud) ; options.c_cflag |= (CLOCAL | CREAD); options.c_cflag & amp;= ~PARENB ; options.c_cflag & amp;= ~CSTOPB ; options.c_cflag & amp;= ~CSIZE ; options.c_cflag |= CS8; options.c_lflag & amp;= ~(ICANON | ECHO | ECHOE | ISIG); options.c_oflag & amp;= ~OPOST ; options.c_cc [VMIN] = 0; options.c_cc [VTIME] = 100; // Ten seconds (100 deciseconds) tcsetattr (fd, TCSANOW, & amp;options); ioctl (fd, TIOCMGET, & amp;status); status |= TIOCM_DTR ; status |= TIOCM_RTS ; ioctl (fd, TIOCMSET, & amp;status); usleep (10000); // 10mS return fd; } void mySerialsend (const int fd, const char* s) { int ret; ret = write (fd, s, strlen (s)); if (ret < 0){ printf("Serial Puts Error\\ "); } } int mySerialread (const int fd,char* buf) { int n_read = read (fd, buf, 1); if(n_read < 0){ perror("read error"); } return n_read; }
my_uart_ttyS5_test.c
/* my_uart_ttyS5_test.c */ #include <stdio.h> #include <string.h> #include <errno.h> // #include <wiringPi.h> // #include <wiringSerial.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include "uartTool.h" int fd; void* func1() { char data[32] = {0}; while(1){ memset(data,'\0',32); scanf("%s",data); //Send data_serial port mySerialsend (fd, data); } } void* func2() { char buf[32] = {0}; while(1){ while(mySerialread(fd,buf)){ //Receive data_serial port printf("%s",buf); } } } int main() { pthread_t t1,t2; if ((fd = mySerialOpen ("/dev/ttyS5", 115200)) < 0) { fprintf (stderr, "Unable to open serial device: %s\\ ", strerror (errno)) ; return 1; } pthread_create( & amp;t1,NULL,func1,NULL); pthread_create( & amp;t2,NULL,func2,NULL); while(1){sleep(10);}; return 0; }