Uart3普通串口收发设置
作者@chhjnavy
由于UART0 被设定为系统dubug 输出(简单来说就是将ttyS0 设定为console),所以使用UART3 作为普通的串口,进行与别的设备通信。
查找TX/RX pin脚
从图中可以看出TX = PE12 RX = PE13 。
PE12 PE13 可以通过排插用杜邦线接出,排插电路图如下:
查看pin脚复用
记住PE12 PE13 用作UART3-TX UART3-RX 是function7 ,这个很重要,一会我们在board.dts 文件中需要用到。
配置设备树
设备树文件通过of 文件解析出属性,进一步被应用(原理此处不再介绍)。
board.dts 路径:
tina-v853-docker/device/config/chips/v851s/configs/lizard/board.dts
在设备树中搜索UART 找到UART3 相关配置
&uart0 { pinctrl-names = "default", "sleep"; pinctrl-0 = <&uart0_pins_active>; pinctrl-1 = <&uart0_pins_sleep>; status = "okay"; }; &uart1 { pinctrl-names = "default", "sleep"; pinctrl-0 = <&uart1_pins_active>; pinctrl-1 = <&uart1_pins_sleep>; status = "disabled"; }; &uart2 { pinctrl-names = "default", "sleep"; pinctrl-0 = <&uart2_pins_active>; pinctrl-1 = <&uart2_pins_sleep>; status = "disabled"; }; &uart3 { pinctrl-names = "default", "sleep"; pinctrl-0 = <&uart3_pins_active>; pinctrl-1 = <&uart3_pins_sleep>; status = "okay"; }; &pio { uart0_pins_active: uart0@0 { allwinner,pins = "PH9", "PH10"; allwinner,function = "uart0"; allwinner,muxsel = <5>; allwinner,drive = <1>; allwinner,pull = <1>; }; uart0_pins_sleep: uart0@1 { allwinner,pins = "PH9", "PH10"; allwinner,function = "gpio_in"; allwinner,muxsel = <0>; }; uart1_pins_active: uart1@0 { allwinner,pins = "PG6", "PG7"; allwinner,function = "uart1"; allwinner,muxsel = <4>; allwinner,drive = <1>; allwinner,pull = <1>; }; uart1_pins_sleep: uart1@1 { allwinner,pins = "PG6", "PG7"; allwinner,function = "gpio_in"; allwinner,muxsel = <0>; }; uart2_pins_active: uart2@0 { allwinner,pins = "PA8", "PA9"; allwinner,function = "uart2"; allwinner,muxsel = <6>; allwinner,drive = <1>; allwinner,pull = <1>; }; uart2_pins_sleep: uart2@1 { allwinner,pins = "PA8", "PA9"; allwinner,function = "gpio_in"; allwinner,muxsel = <0>; }; uart3_pins_active: uart3@0 { allwinner,pins = "PE12", "PE13"; allwinner,function = "uart3"; allwinner,muxsel = <7>; allwinner,drive = <1>; allwinner,pull = <1>; }; uart3_pins_sleep: uart3@1 { allwinner,pins = "PE12", "PE13"; allwinner,function = "gpio_in"; allwinner,muxsel = <0>; };
将uart3 设定为 status = "okay";
将uart3 pin 脚设为 allwinner,pins = "PE12", "PE13";
将uart3 pin复用为function7 :allwinner,muxsel = <7>;
修改完成后,重新编译img
make -j1 V=s pack
使用PhoenixSuit烧录后,通过adb shell 进入终端,就可以查看到设备节点:/dev/ttyS3 ,通过open 设备终端,就可以进行read write 操作。
进行收发操作
1)写应用程序:
#include/*标准输入输出定义*/ #include /*标准函数库定义*/ #include /*Unix标准函数定义*/ #include #include /*六文件控制定义*/ #include /*PX终端控制定义*/ #include #include #include enum parameter_type { PT_PROGRAM_NAME = 0, PT_DEV_NAME, PT_CYCLE, PT_NUM }; #define DBG(string, args...) do { printf("%s, %s()%u---",__FILE__,__FUNCTION__,__LINE__); printf(string, ##args); printf(" "); } while (0) void usage(void) { printf("You should input as: "); printf(" select_test [/dev/name] [Cyclee Cnt] "); } int OpenDev(char *name) { int fd = open(name, O_RDWR ); if (-1 == fd) DBG("Can not OPen(%s)!", name); return fd; } /* * @brief 设置串口通信速率 * @param fd 类型 int打开串口的文件句柄长 * @param speed 类型 int 串口速度 * @return void */ void set_speed(int fd, int speed) { int i; int status; struct termios Opt = {0}; int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, }; int name_arr[] = {230400,115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, }; tcgetattr(fd, &Opt); for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) { if (speed == name_arr[i]) break; } tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arr[i]); cfsetospeed(&Opt, speed_arr[i]); Opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/ Opt.c_oflag &= ~OPOST; /*Output*/ status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) { DBG("tcsetattr fd"); return; } tcflush(fd, TCIOFLUSH); } /** *@brief 设置串口数据位,停止位和效验位 *@param fd 类型 int 打开的串口文件句柄 *@param databits 类 型 int 数 据位 取值 为 7 或者8 *@param stopbits 类 型 int 停 止位 取值为 1 或者2 *@param parity 类型 int 效验类型 取值为N,E,O,,S */ int set_Parity(int fd,int databits,int stopbits,int parity) { struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return -1; } options.c_cflag &= ~CSIZE; switch (databits) /*设置数据位数*/ { case 7: options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS8; break; default: fprintf(stderr,"Unsupported data size "); return -1; } switch (parity) { case 'n': case 'N': options.c_cflag &= ~PARENB; /* Clear parity enable */ options.c_iflag &= ~INPCK; /* Enable parity checking */ break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'e': case 'E': options.c_cflag |= PARENB; /* Enable parity */ options.c_cflag &= ~PARODD; /* */ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'S': case 's': /*as no parity*/ options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB;break; default: fprintf(stderr,"Unsupported parity "); return -1; } /* 设置停止位*/ switch (stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: fprintf(stderr,"Unsupported stop bits "); return -1; } /* Set input parity option */ if (parity != 'n') options.c_iflag |= INPCK; tcflush(fd,TCIFLUSH); options.c_cc[VTIME] = 150; /* 15 seconds*/ options.c_cc[VMIN] = 0; /* Update the options and do it NOW */ if (tcsetattr(fd,TCSANOW,&options) != 0) { perror("SetupSerial 3"); return -1; } return 0; } void str_print(char *buf, int len) { int i; for (i=0; i
2)编写makefile:
需注意,将makefile 中的编译工具路径修改为自己的工具路径。
#CROSS_COMPILE = arm-linux- #CROSS_COMPILE = arm-linux-gnueabihf- CROSS_COMPILE=/home/xxx/v851s/Yuzukilizard/toolchain/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi- # CPU = cortex-a7 # FPU = fpv4-sp-d16 AS = $(CROSS_COMPILE)as LD = $(CROSS_COMPILE)ld CC = $(CROSS_COMPILE)gcc CXX = $(CROSS_COMPILE)g++ CPP = $(CC) -E AR = $(CROSS_COMPILE)ar NM = $(CROSS_COMPILE)nm STRIP = $(CROSS_COMPILE)strip OBJCOPY = $(CROSS_COMPILE)objcopy OBJDUMP = $(CROSS_COMPILE)objdump RANLIB = $(CROSS_COMPILE)ranlib SIZE = $(CROSS_COMPILE)size TOP_DIR = $(shell pwd) CFLAGS_inc_path += -I$(TOP_DIR) EXE = UARTTest # CFLAGS_inc_path += -I$(TOP_DIR)external/jpeg-9a # for H.264 hardware codec #CFLAGS_inc_path += -I$(TOP_DIR)/../../prebuilt/include/cedar # CFLAGS += -std=gnu99 -mthumb -mabi=aapcs-linux -mlittle-endian # CFLAGS += -fdata-sections -ffunction-sections # CFLAGS += -mcpu=$(CPU) -mtune=$(CPU) -mfpu=$(FPU) -mfloat-abi=hard SRC_HAL += ./ LIB_SRC := $(foreach spath, $(SRC_HAL), $(wildcard $(spath)*.c)) OBJECT += $(patsubst %.c,%.o,$(LIB_SRC)) LOCAL_INCLUDE += -I./ #CFLAGS += -O2 -ggdb3 -DNDEBUG CFLAGS += $(LOCAL_INCLUDE) -static # LDFLAGS = -L../../library/arm-linux-gnueabihf # LIBS += -lawh264 -lvdecoder -lcdc_base -lMemAdapter -lVE -lvencoder -lvideoengine all:$(EXE) $(EXE):$(OBJECT) @$(CC) $(OBJECT) -o $(EXE) $(CFLAGS) # @echo -- gcc $(SRCS) -- @echo "Compile target done." @echo "use src files" $(OBJECT): .PHONY:clean clean: @rm -rf *.o $(OBJECT) $(EXE) @echo "Clean done."
3)make 后,产生可执行文件,adb push 到开发板中进行执行,执行命令:
./UARTTest /dev/ttyS3 4
4)用杜邦线将PE12 PE13 GND 三根线接到电脑串口(需要串口转换小板子),波特率目前设定19200 ,使用串口工具,向板子随便发送数据,板子接受到数据,就会发送数据到电脑。
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !