完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
一、运行效果:
[root@ROCKEMD:/]# dwuart Usage: dwuart /dev/ttyS* data +-------data--------+ | c - clear | | t - rockemd | | q - qr | +-------------------+ [root@ROCKEMD:/]# dwuart /dev/ttyS3 c [root@ROCKEMD:/]# dwuart /dev/ttyS3 t [root@ROCKEMD:/]# dwuart /dev/ttyS3 q 二、上代码显示 From 14476e8c31f4957ab6e0cdae506d85badd1fc031 Mon Sep 17 00:00:00 2001 From: rockemd Date: Wed, 4 Nov 2020 14:09:55 +0800 Subject: [PATCH] add dwuart --- .../configs/rockchip_rockemd_rv1109_emmc_defconfig | 1 + buildroot/package/rockchip/Config.in | 1 + buildroot/package/rockchip/app-dwuart/Config.in | 4 + .../package/rockchip/app-dwuart/app-dwuart.mk | 26 +++ external/dwuart/Makefile | 11 ++ external/dwuart/README.md | 1 + external/dwuart/dwuart.c | 134 +++++++++++++++ external/dwuart/uartconfig.c | 186 +++++++++++++++++++++ external/dwuart/uartconfig.h | 23 +++ 11 files changed, 387 insertions(+) create mode 100755 buildroot/package/rockchip/app-dwuart/Config.in create mode 100755 buildroot/package/rockchip/app-dwuart/app-dwuart.mk create mode 100755 external/dwuart/Makefile create mode 100755 external/dwuart/README.md create mode 100755 external/dwuart/dwuart.c create mode 100755 external/dwuart/uartconfig.c create mode 100755 external/dwuart/uartconfig.h diff --git a/buildroot/configs/rockchip_rockemd_rv1109_emmc_defconfig b/buildroot/configs/rockchip_rockemd_rv1109_emmc_defconfig index 864f5dd..97b50c5 100755 --- a/buildroot/configs/rockchip_rockemd_rv1109_emmc_defconfig +++ b/buildroot/configs/rockchip_rockemd_rv1109_emmc_defconfig @@ -88,4 +88,5 @@ BR2_PACKAGE_NGINX_DEBUG=y BR2_PACKAGE_NGINX_RTMP=y # BR2_PACKAGE_NTP_NTPD is not set +BR2_PACKAGE_APP_DWUART=y diff --git a/buildroot/package/rockchip/Config.in b/buildroot/package/rockchip/Config.in index 983eb52..b109824 100755 --- a/buildroot/package/rockchip/Config.in +++ b/buildroot/package/rockchip/Config.in @@ -249,5 +249,6 @@ source "package/rockchip/rksl/Config.in" source "package/rockchip/pcba_adb_test/Config.in" source "package/rockchip/camera_factory_test_server/Config.in" +source "package/rockchip/app-dwuart/Config.in" source "package/rockchip/thunderboot/Config.in" endif diff --git a/buildroot/package/rockchip/app-dwuart/Config.in b/buildroot/package/rockchip/app-dwuart/Config.in new file mode 100755 index 0000000..d1d5485 --- /dev/null +++ b/buildroot/package/rockchip/app-dwuart/Config.in @@ -0,0 +1,4 @@ +config BR2_PACKAGE_APP_DWUART + bool "dwuart" + help + rockemd dwuart demo diff --git a/buildroot/package/rockchip/app-dwuart/app-dwuart.mk b/buildroot/package/rockchip/app-dwuart/app-dwuart.mk new file mode 100755 index 0000000..39322ad --- /dev/null +++ b/buildroot/package/rockchip/app-dwuart/app-dwuart.mk @@ -0,0 +1,26 @@ +############################################################# +# +# DWUART +# +############################################################# +ifeq ($(BR2_PACKAGE_APP_DWUART), y) +APP_DWUART_VERSION = 1.0.0 +APP_DWUART_SITE_METHOD:=local +APP_DWUART_SITE=$(TOPDIR)/../external/dwuart +APP_DWUART_INSTALL_TARGET:=YES + +define APP_DWUART_BUILD_CMDS + $(MAKE) CC="$(TARGET_CC)" LD="$(TARGET_LD)" -C $(@D) all +endef + +define APP_DWUART_INSTALL_TARGET_CMDS + $(INSTALL) -D -m 0755 $(@D)/dwuart $(TARGET_DIR)/bin +endef + +define APP_DWUART_PERMISSIONS + /bin/dwuart f 4755 0 0 - - - - - +endef + +$(eval $(generic-package)) + +endif diff --git a/external/dwuart/Makefile b/external/dwuart/Makefile new file mode 100755 index 0000000..3f4006f --- /dev/null +++ b/external/dwuart/Makefile @@ -0,0 +1,11 @@ +all: dwuart + +dwuart: dwuart.o uartconfig.o + $(CC) -o dwuart dwuart.o uartconfig.o + +clean: + rm -rf *.o + rm -rf dwuart + +install: + $(INSTALL) -D -m 0755 dwuart $(TARGET_DIR)/bin diff --git a/external/dwuart/README.md b/external/dwuart/README.md new file mode 100755 index 0000000..656f52d --- /dev/null +++ b/external/dwuart/README.md @@ -0,0 +1 @@ +git clone https://github.com/simonqin09/uarttest diff --git a/external/dwuart/dwuart.c b/external/dwuart/dwuart.c new file mode 100755 index 0000000..f851630 --- /dev/null +++ b/external/dwuart/dwuart.c @@ -0,0 +1,134 @@ +#include "uartconfig.h" + +int main(int argc, char **argv) +{ + /* + clear + AA 01 + 00 00 + CC 33 C3 3C + + text + AA 11 + 41 + FF FF + 00 00 + 00 20 00 80 + 72 6F 63 6B 65 6D 64 //rockemd + CC 33 C3 3C + + + QR + AA 21 + 00 08 00 08 + 04 + 68 74 74 70 3A 2F 2F 77 77 77 2E 72 6F 63 6B 65 6D 64 2E 63 6F 6D 2F //http://www.rockemd.com/ + CC 33 C3 3C + */ + + int fd,len,ch; + char data_send[1024]; + char data_read[1024]; + unsigned char clear[MAX_DATA_SIZE] = "AA010000CC33C33C"; + unsigned char rockemd[MAX_DATA_SIZE] = "AA1141FFFF000000200080726F636B656D64CC33C33C"; + unsigned char qr[MAX_DATA_SIZE] = "AA210008000804687474703A2F2F7777772E726F636B656D642E636F6D2FCC33C33C"; + int in_data_len = MAX_DATA_SIZE; + unsigned char out_data0[MAX_DATA_SIZE] = {0}; + int out_data_len = MAX_DATA_SIZE; + int i; + + /* check required parameters(corresponding uart port device) for main application*/ + if(argc != 3){ + printf("Usage: %s /dev/ttyS* data n",argv[0]); + printf("+-------data--------+n"); + printf("| c - clear |n"); + printf("| t - rockemd |n"); + printf("| q - qr |n"); + printf("+-------------------+n"); + return -1; + } + /* open and config uart port, only bandrate can be set here --- + --- the orther need to be modified in uart_config in uartconfig.c file */ + fd = uart_open(argv[1]); + + /* predefined baud rate list: + B921600,B460800,B230400,B115200,B57600,B38400,B19200,B9600,B4800,B2400,B1200*/ + ch = uart_config(fd, B115200); //B115200 + if(ch == -1) + { + printf("uart setup error!"); + return -1; + } + /*Sending data part*/ + //len = sprintf(data_send,"this is a test programrn"); + //================================================ + + switch(argv[2][0]) + { + case 'c': + { + in_data_len = strlen(clear); + string2hex(clear, in_data_len, out_data0, &out_data_len); + //for (i = 0; i < out_data_len; i++) { + // printf("0x%02X ", out_data0); + //} + //printf("n"); + if(out_data_len != -1) + { + ch = uart_send(fd,out_data0,out_data_len); + if(ch == -1) + { + printf("send errorn"); + uart_close(fd); + return -1; + } + } + break; + } + case 't': + { + in_data_len = strlen(rockemd); + string2hex(rockemd, in_data_len, out_data0, &out_data_len); + //for (i = 0; i < out_data_len; i++) { + // printf("0x%02X ", out_data0); + //} + //printf("n"); + if(out_data_len != -1) + { + ch = uart_send(fd,out_data0,out_data_len); + if(ch == -1) + { + printf("send errorn"); + uart_close(fd); + return -1; + } + } + break; + } + case 'q': + { + in_data_len = strlen(qr); + string2hex(qr, in_data_len, out_data0, &out_data_len); + //for (i = 0; i < out_data_len; i++) { + // printf("0x%02X ", out_data0); + //} + //printf("n"); + if(out_data_len != -1) + { + ch = uart_send(fd,out_data0,out_data_len); + if(ch == -1) + { + printf("send errorn"); + uart_close(fd); + return -1; + } + } + break; + } + default : + printf("Invalid graden" ); + } + uart_close(fd); + return 0; +} diff --git a/external/dwuart/uartconfig.c b/external/dwuart/uartconfig.c new file mode 100755 index 0000000..0fe630c --- /dev/null +++ b/external/dwuart/uartconfig.c @@ -0,0 +1,186 @@ +#include "uartconfig.h" + +int uart_open(char* port) +{ + int fd_temp; + int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; + fd_temp = open(port, flags); + if(fd_temp < 0) + { + perror("Can't open Serial Port"); + return(-1); + } + return fd_temp; +} + +void uart_close(int fd) +{ + close(fd); +} + +int uart_config(int fd, int bandrate) +{ + struct termios tty; + + if(tcgetattr(fd, &tty) != 0) + { + perror("failed setup Serial"); + return(-1); + } + + /*config bandrate*/ + cfsetispeed(&tty,bandrate); + cfsetospeed(&tty,bandrate); + + /*config data bits = 8*/ + tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; + + /*config parity = none*/ + tty.c_cflag &= ~PARENB; + + /*config stop bits = 1*/ + tty.c_cflag &= ~CSTOPB; + + /* config flow control = none*/ + tty.c_cflag &= ~CRTSCTS; + + /*config control mode*/ + tty.c_cflag |= CLOCAL; + tty.c_cflag |= CREAD; + + /* config RAW data mode input and output*/ + tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/ + tty.c_oflag &= ~OPOST; /*Output*/ + + /*config wait time and minimal receive characters*/ + tty.c_cc[VTIME] = 100; /* wait time 100*(1/10)s */ + tty.c_cc[VMIN] = 1; /* minimal receive characters = 1 */ + + /*data overrun processing*/ + tcflush(fd,TCIFLUSH); + + /* active above configuration*/ + if (tcsetattr(fd, TCSANOW, &tty) != 0) + { + fprintf (stderr, "error %d from tcsetattr", errno); + return -1; + } + return(1); +} + +int uart_send(int fd, char *buf_send, int buf_len) +{ + int len; + len = write(fd,buf_send,buf_len); + if(len == buf_len) + { + //printf("send successfllyn"); + return len; + } + else + { + tcflush(fd,TCOFLUSH); + printf("send errorn"); + return -1; + } +} + +int uart_read(int fd, char *buf_read, int buf_len) +{ + int len,fs_sel; + fd_set fs_read; + struct timeval time; + + FD_ZERO(&fs_read); + FD_SET(fd,&fs_read); + + time.tv_sec = 10; + time.tv_usec = 0; + + fs_sel = select(fd+1,&fs_read,NULL,NULL,&time); + if(fs_sel) + { + len = read(fd,buf_read,buf_len); + return len; + } + else + { + return -1; + } +} + +int uart_485(int fd) +{ + struct serial_rs485 rs485conf; + + /* Enable RS485 mode: */ + rs485conf.flags |= SER_RS485_ENABLED; + + /* Set logical level for RTS pin equal to 0 when sending: */ + rs485conf.flags |= SER_RS485_RTS_ON_SEND; + /* or, set logical level for RTS pin equal to 1 when sending: */ + //rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND); + + /* Set logical level for RTS pin equal to 0 after sending: */ + //rs485conf.flags |= SER_RS485_RTS_AFTER_SEND; + /* or, set logical level for RTS pin equal to 1 after sending: */ + rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND); + + /* Set rts delay before send, if needed:(in microseconds) */ + rs485conf.delay_rts_before_send = 100; + + /* Set rts delay after send, if needed: (in microseconds) */ + rs485conf.delay_rts_after_send = 100; + + /* Set this flag if you want to receive data even whilst sending data */ + rs485conf.flags |= SER_RS485_RX_DURING_TX; + + if (ioctl(fd, TIOCSRS485, &rs485conf) < 0) { + printf("Error: TIOCSRS485 ioctl not supported.n"); + return -1; + } + else + return 1; +} + +int string2hex(unsigned char *in_data, int in_data_len, unsigned char *out_data, int *out_data_len) +{ + int i; + int loop_count; + int convert_point = 0; + int mem_point = 0; + unsigned char convert_result; + unsigned char temp[3] = {0}; /* Why is 3 */ + + /* Check the validity of the parameters */ + if (in_data == NULL || in_data_len <= 0 || out_data == NULL || out_data_len == NULL || (in_data_len % 2) != 0) { + printf("invalid parametersn"); + return -1; + } + + /* Determine whether it exceeds the hexadecimal range 0 ~ F */ + for (i = 0; i < in_data_len; i++) { + if ((in_data < '0') || (in_data > 'f') || ((in_data > '9') &&(in_data < 'A'))) { + printf("out of rangen"); + return -1; + } + } + + loop_count = in_data_len / 2; + memset(out_data, 0x00, *out_data_len); + *out_data_len = 0; + + for (i = 0; i < loop_count; i++) { + memset(temp, 0x00, sizeof(temp)); + memcpy(temp, in_data + convert_point, 2); + convert_point += 2; + + convert_result = strtoul(temp, NULL, 16); + + memcpy(out_data + mem_point, &convert_result, sizeof(unsigned char)); + mem_point += sizeof(unsigned char); + *out_data_len += sizeof(unsigned char); + } + + return 0; +} diff --git a/external/dwuart/uartconfig.h b/external/dwuart/uartconfig.h new file mode 100755 index 0000000..5e5a66e --- /dev/null +++ b/external/dwuart/uartconfig.h @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TIOCGRS485 0x542E +#define TIOCSRS485 0x542F + +#define MAX_DATA_SIZE 255 + +int uart_open(char* port); +void uart_close(int fd); +int uart_config(int fd, int bandrate); +int uart_send(int fd, char *buf_send, int buf_len); +int uart_read(int fd, char *buf_read, int buf_len); +int uart_485(int fd); +int string2hex(unsigned char *in_data, int in_data_len, unsigned char *out_data, int *out_data_len); + -- 2.7.4 三、编译 cd buildroot/ source build/envsetup.sh make app-dwuart-rebuild |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
基于米尔瑞芯微RK3576核心板/开发板的人脸疲劳检测应用方案
533 浏览 0 评论
803 浏览 1 评论
700 浏览 1 评论
1926 浏览 1 评论
3171 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 14:02 , Processed in 0.578539 second(s), Total 70, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号