Uart3普通串口收發(fā)設(shè)置
作者@chhjnavy
由于UART0 被設(shè)定為系統(tǒng)dubug 輸出(簡(jiǎn)單來說就是將ttyS0 設(shè)定為console),所以使用UART3 作為普通的串口,進(jìn)行與別的設(shè)備通信。
查找TX/RX pin腳
從圖中可以看出TX = PE12 RX = PE13 。
PE12 PE13 可以通過排插用杜邦線接出,排插電路圖如下:
查看pin腳復(fù)用
記住PE12 PE13 用作UART3-TX UART3-RX 是function7 ,這個(gè)很重要,一會(huì)我們?cè)赽oard.dts 文件中需要用到。
配置設(shè)備樹
設(shè)備樹文件通過of 文件解析出屬性,進(jìn)一步被應(yīng)用(原理此處不再介紹)。
board.dts 路徑:
tina-v853-docker/device/config/chips/v851s/configs/lizard/board.dts
在設(shè)備樹中搜索UART 找到UART3 相關(guān)配置
&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 設(shè)定為 status = "okay";
將uart3 pin 腳設(shè)為 allwinner,pins = "PE12", "PE13";
將uart3 pin復(fù)用為function7 :allwinner,muxsel = <7>;
修改完成后,重新編譯img
make -j1 V=s pack
使用PhoenixSuit燒錄后,通過adb shell 進(jìn)入終端,就可以查看到設(shè)備節(jié)點(diǎn):/dev/ttyS3 ,通過open 設(shè)備終端,就可以進(jìn)行read write 操作。
進(jìn)行收發(fā)操作
1)寫應(yīng)用程序:
#include/*標(biāo)準(zhǔn)輸入輸出定義*/ #include /*標(biāo)準(zhǔn)函數(shù)庫(kù)定義*/ #include /*Unix標(biāo)準(zhǔn)函數(shù)定義*/ #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 設(shè)置串口通信速率 * @param fd 類型 int打開串口的文件句柄長(zhǎng) * @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 ?設(shè)置串口數(shù)據(jù)位,停止位和效驗(yàn)位 *@param fd ?類型 ? ?int 打開的串口文件句柄 *@param ?databits 類 型 ?int 數(shù) 據(jù)位 ?取值 ?為 ?7 或者8 *@param ?stopbits 類 型 ?int 停 止位 ?取值為 ?1 或者2 *@param parity 類型 int 效驗(yàn)類型 取值為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) /*設(shè)置數(shù)據(jù)位數(shù)*/ ? ?{ ? ? ? ?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); /* 設(shè)置為奇效驗(yàn)*/ ? ? ? ? ? ?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; ? ?} ? ?/* 設(shè)置停止位*/ ? ?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 后,產(chǎn)生可執(zhí)行文件,adb push 到開發(fā)板中進(jìn)行執(zhí)行,執(zhí)行命令:
./UARTTest /dev/ttyS3 4
4)用杜邦線將PE12 PE13 GND 三根線接到電腦串口(需要串口轉(zhuǎn)換小板子),波特率目前設(shè)定19200 ,使用串口工具,向板子隨便發(fā)送數(shù)據(jù),板子接受到數(shù)據(jù),就會(huì)發(fā)送數(shù)據(jù)到電腦。
審核編輯:湯梓紅
-
通信
+關(guān)注
關(guān)注
18文章
6036瀏覽量
136079 -
串口
+關(guān)注
關(guān)注
14文章
1555瀏覽量
76587 -
uart
+關(guān)注
關(guān)注
22文章
1237瀏覽量
101447 -
燒錄
+關(guān)注
關(guān)注
8文章
259瀏覽量
35612 -
設(shè)備樹
+關(guān)注
關(guān)注
0文章
38瀏覽量
3138
原文標(biāo)題:【快速上手】在V85x上將uart3設(shè)置成普通串口收發(fā)模式
文章出處:【微信號(hào):gh_79acfa3aa3e3,微信公眾號(hào):全志在線】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論