前言
專欄總目錄本文主要講解如何關(guān)于RK3588開發(fā)板UART的使用和調(diào)試方法,包括UART作為普通串口和控制臺兩種不同使用場景一. 功能特點(diǎn)
RockchipUART (Universal Asynchronous Receiver/Transmitter) 基于16550A串口標(biāo)準(zhǔn),完整模塊支持以下功能:
支持5、6、7、8 bits數(shù)據(jù)位。支持1、1.5、2 bits停止位。支持奇校驗(yàn)和偶校驗(yàn),不支持mark校驗(yàn)和space校驗(yàn)。支持接收FIFO和發(fā)送FIFO,一般為32字節(jié)或者64字節(jié)。支持最高4M波特率,實(shí)際支持波特率需要芯片時(shí)鐘分頻策略配合。支持中斷傳輸模式和DMA傳輸模式。 支持硬件自動流控,RTS+CTS。二、代碼位置
在Linuxkernel 中,使用8250串口通用驅(qū)動,以下為主要驅(qū)動文件:
drivers/tty/serial/8250/8250_core.c # 8250串口驅(qū)動核心
【資料圖】
drivers/tty/serial/8250/8250_dw.c # Synopsis DesignWare 8250串口驅(qū)動
drivers/tty/serial/8250/8250_dma.c # 8250串口DMA驅(qū)動
drivers/tty/serial/8250/8250_port.c # 8250串口端口操作
drivers/tty/serial/8250/8250_early.c # 8250串口early console驅(qū)動
SDK中提供的UART默認(rèn)配置已經(jīng)使用了8250驅(qū)動我們就不需要修改
三、硬件原理圖
串口功能的硬件上比較簡單,這是只附上調(diào)試串口的原理圖
四、設(shè)備樹配置
rk平臺的設(shè)備樹修改路徑都是在kernel\\arch\\arm64\\boot\\dts\\rockchip下面,具體哪個(gè)文件根據(jù)對應(yīng)開發(fā)板來決定,通常描述設(shè)備硬件配置在rkxxxx.dtsi中,比如在rk3588s.dtsi中:
uart2: serial@feb50000 {compatible = "rockchip,rk3588-uart", "snps,dw-apb-uart";reg = < 0x0 0xfeb50000 0x0 0x100 >;interrupts = < GIC_SPI 333 IRQ_TYPE_LEVEL_HIGH >;clocks = < &cru SCLK_UART2 >, < &cru PCLK_UART2 >;clock-names = "baudclk", "apb_pclk";reg-shift = < 2 >;reg-io-width = < 4 >;dmas = < &dmac0 10 >, < &dmac0 11 >;pinctrl-names = "default";pinctrl-0 = < &uart2m1_xfer >;status = "disabled";};
4.1作為普通串口
假入我們想使用w3開發(fā)板上40PIN上的uart7
我們在dts可以使用如下配置打開
&uart7 {status = "okay";pinctrl-names = "default";pinctrl-0 = < &uart7m1_xfer >;};
4.2作為調(diào)試串口
Rockchip UART作為控制臺,使用fiq_debugger流程。
在dts中fiq_debugger節(jié)點(diǎn)配置如下。由于fiq_debugger和普通串口互斥,在使能fiq_debugger節(jié)點(diǎn)后必須禁用對應(yīng)的普通串口uart節(jié)點(diǎn)。
chosen: chosen {bootargs = "earlycon=uart8250,mmio32,0xfe660000 console=ttyFIQ0";};fiq-debugger {compatible = "rockchip,fiq-debugger";rockchip,serial-id = < 2 >;rockchip,wake-irq = < 0 >;/* If enable uart uses irq insteadof fiq */rockchip,irq-mode-enable = < 1 >;rockchip,baudrate = < 1500000 >; /* Only 115200 and 1500000 */interrupts = < GIC_SPI 252 IRQ_TYPE_LEVEL_LOW >;pinctrl-names = "default";pinctrl-0 = < &uart2m0_xfer >;status = "okay";};&uart2 {status = "disabled";};
rockchip,serial-id:使用的UART編號。修改serial-id到不同UART,fiq_debugger設(shè)備也會注冊成ttyFIQ0設(shè)備。 rockchip,irq-mode-enable:配置為1使用irq中斷,配置為0使用fiq中斷。interrupts:配置的輔助中斷,保持默認(rèn)即可。pinctrl-0:使用的串口引腳rockchip,baudrate:波特率配置五、串口相關(guān)問題
5.1設(shè)備注冊
普通串口設(shè)備將會根據(jù)dts中的aliase來對串口進(jìn)行編號,對應(yīng)注冊成ttySx設(shè)備。注冊的節(jié)點(diǎn)為/dev/ttyS4,命名規(guī)則是通過dts中的aliases來的。
aliases {serial0 = &uart0;serial1 = &uart1;serial2 = &uart2;serial3 = &uart3;}
對應(yīng)uart0注冊為ttyS0,uart0注冊為ttyS1,如果需要把uart3注冊成ttyS1,可以進(jìn)行以下修改
serial1 = &uart3; serial3 = &uart1;
5.2控制臺打印相關(guān)
Rockchip UART打印通常包括DDR階段、Miniloader階段、TF-A (Trusted Firmware-A)階段、OP-TEE階段、Uboot階段和Kernel階段,我們平時(shí)主要關(guān)注的是uboot階段和kernel階段的打印,在這兩個(gè)階段我們可以嘗試關(guān)閉所有打印或切換所有打印到其他UART,RK平臺默認(rèn)的調(diào)試串口是uart2_m0這一組引腳,假如現(xiàn)在我將打印換成其他串口,可以嘗試以下做法。
5.2.1DDR Loader修改方法
DDR Loader中關(guān)閉或切換打印,需要修改DDR Loader中的UART打印配置,修改文件rkbin/tools/ddrbin_param.txt中的以下參數(shù):
uart id= # UART控制器id,配置為0xf為關(guān)閉打印
uart iomux= # 復(fù)用的IOMUX引腳 uart
baudrate= # 115200 or 1500000
修改完成后,使用以下命令重新生成ddr.bin固件。
./ddrbin_tool ddrbin_param.txt rk3588_ddr_lp4_2112MHz_lp5_2736MHz_v1.09.bin
5.2.2Uboot修改方法
Uboot中關(guān)閉打印,需要在menuconfig中,打開配CONFIG_DISABLE_CONSOLE,保存到.config文件
Uboot中切換打印,由傳參機(jī)制決定,不需要進(jìn)行額外修改。uboot解析傳參機(jī)制相關(guān)代碼在arch/arm/mach-rockchip/board.c的board_init_f_init_serial()函數(shù)中。
5.2.3kernel修改方法
去掉打印需要在menuconfig中,關(guān)閉配置CONFIG_SERIAL_8250_CONSOLE。
Device Drivers --->
Character devices --->
Serial drivers --->
[ ]Console on 8250/16550 and compatible serial port
在dts配置中找到類似以下內(nèi)容,并去掉UART基地址和console相關(guān)配置參數(shù)
chosen: chosen {bootargs = "earlycon=uart8250,mmio32,0xfeb50000 console=ttyFIQ0 irqchip.gicv3_pseudo_nmi=0 root=PARTUUID=614e0000-0000 rw rootwait";};
將0xfeb50000 console=ttyFIQ0 去掉,然后找到fiq-debugger節(jié)點(diǎn),修改serial-id為0xffffffff,去掉UART引腳復(fù)用相關(guān)配置。注意,需要保持fiqdebugger節(jié)點(diǎn)使能,保持fiq-debugger流程系統(tǒng)才能正常啟動
fiq_debugger: fiq-debugger {compatible = "rockchip,fiq-debugger";rockchip,serial-id = < 0xffffffff >;rockchip,wake-irq = < 0 >;/* If enable uart uses irq instead of fiq */rockchip,irq-mode-enable = < 1 >;rockchip,baudrate = < 1500000 >; /* Only 115200 and 1500000 */interrupts = < GIC_SPI 423 IRQ_TYPE_LEVEL_LOW >;status = "okay";};
切換打印串口例如將Kernel打印從UART2切換到UART3,在dts配置中找到類似以下內(nèi)容,將UART基地址由UART2改為UART3.
bootargs = "earlycon=uart8250,mmio32,0xfe670000 console=ttyFIQ0";
0xfe670000是UART3基地址,然后找到fiq-debugger節(jié)點(diǎn),修改serial-id為3,修改UART3引腳復(fù)用配置pinctrl-0 = <&uart3m0_xfer>。注意,同時(shí)需要將切換為打印串口的UART3作為普通串口的節(jié)點(diǎn)禁用。
六、串口測試
在開發(fā)板上跑一套應(yīng)用程序,可以發(fā)送數(shù)據(jù),可以接收數(shù)據(jù),測試方法可以短接TX_RX
#include < stdio.h >#include < stdlib.h >#include < errno.h >#include < unistd.h >#include < fcntl.h >#include < string.h >#include < termio.h >#include < time.h >#include < pthread.h >int read_data(int fd, void *buf, int len);int write_data(int fd, void *buf, int len);int setup_port(int fd, int baud, int databits, int parity, int stopbits);void print_usage(char *program_name);pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;int data_available = 0;void *read_thread(void *arg) { int fd = *(int *)arg; char buffer[1024]; // 存儲讀取的數(shù)據(jù) while (1) { int bytes_read = read_data(fd, buffer, sizeof(buffer)); if (bytes_read > 0) { printf("Read Thread: Read %d bytes: %s\\n", bytes_read, buffer); } else { // 處理讀取錯(cuò)誤或設(shè)備關(guān)閉的情況 break; } } pthread_exit(NULL);}void *write_thread(void *arg) { int fd = *(int *)arg;char input[1024]; // 存儲用戶輸入的數(shù)據(jù) while (1) { printf("Enter data to write (or "q" to quit): "); fgets(input, sizeof(input), stdin); if (strcmp(input, "q\\n") == 0 || strcmp(input, "Q\\n") == 0) { // 用戶輸入 "q" 或 "Q",退出循環(huán) break; } int len = strlen(input); int bytes_written = write_data(fd, input, len); if (bytes_written > 0) { printf("Write Thread: Wrote %d bytes: %s\\n", bytes_written, input); } } pthread_exit(NULL);}int main(int argc, char *argv[]) //./a.out /dev/ttyS4 115200 8 0 1{ int fd; int baud; int len; int count; int i; int databits; int stopbits; int parity; if (argc != 6) { print_usage(argv[0]); return 1; } baud = atoi(argv[2]); if ((baud < 0) || (baud > 921600)) { fprintf(stderr, "Invalid baudrate!\\n"); return 1; } databits = atoi(argv[3]); if ((databits < 5) || (databits > 8)) { fprintf(stderr, "Invalid databits!\\n"); return 1; } parity = atoi(argv[4]); if ((parity < 0) || (parity > 2)) { fprintf(stderr, "Invalid parity!\\n"); return 1; } stopbits = atoi(argv[5]); if ((stopbits < 1) || (stopbits > 2)) { fprintf(stderr, "Invalid stopbits!\\n"); return 1; } fd = open(argv[1], O_RDWR, 0); if (fd < 0) { fprintf(stderr, "open < %s > error %s\\n", argv[1], strerror(errno)); return 1; } if (setup_port(fd, baud, databits, parity, stopbits)) { fprintf(stderr, "setup_port error %s\\n", strerror(errno)); close(fd); return 1; }pthread_t read_tid, write_tid; int ret; // 創(chuàng)建讀取線程 ret = pthread_create(&read_tid, NULL, read_thread, &fd); if (ret != 0) { fprintf(stderr, "Failed to create read thread\\n"); return 1; } // 創(chuàng)建寫入線程 ret = pthread_create(&write_tid, NULL, write_thread, &fd); if (ret != 0) { fprintf(stderr, "Failed to create write thread\\n"); return 1; } // 等待讀取線程和寫入線程結(jié)束 pthread_join(read_tid, NULL); pthread_join(write_tid, NULL); close(fd); return 0;}static int baudflag_arr[] = { B921600, B460800, B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1800, B1200, B600, B300, B150, B110, B75, B50};static int speed_arr[] = { 921600, 460800, 230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300, 150, 110, 75, 50};int speed_to_flag(int speed){ int i; for (i = 0; i < sizeof(speed_arr)/sizeof(int); i++) { if (speed == speed_arr[i]) { return baudflag_arr[i]; } } fprintf(stderr, "Unsupported baudrate, use 9600 instead!\\n"); return B9600;}static struct termio oterm_attr;int setup_port(int fd, int baud, int databits, int parity, int stopbits){ struct termio term_attr; if (ioctl(fd, TCGETA, &term_attr) < 0) { return -1; } memcpy(&oterm_attr, &term_attr, sizeof(struct termio)); term_attr.c_iflag &= ~(INLCR | IGNCR | ICRNL | ISTRIP); term_attr.c_oflag &= ~(OPOST | ONLCR | OCRNL); term_attr.c_lflag &= ~(ISIG | ECHO | ICANON | NOFLSH); term_attr.c_cflag &= ~CBAUD; term_attr.c_cflag |= CREAD | speed_to_flag(baud); term_attr.c_cflag &= ~(CSIZE); switch (databits) { case 5: term_attr.c_cflag |= CS5; break; case 6: term_attr.c_cflag |= CS6; break; case 7: term_attr.c_cflag |= CS7; break; case 8: default: term_attr.c_cflag |= CS8; break; } switch (parity) { case 1: term_attr.c_cflag |= (PARENB | PARODD); break; case 2: term_attr.c_cflag |= PARENB; term_attr.c_cflag &= ~(PARODD); break; case 0: default: term_attr.c_cflag &= ~(PARENB); break; } switch (stopbits) { case 2: term_attr.c_cflag |= CSTOPB; break; case 1: default: term_attr.c_cflag &= ~CSTOPB; break; } term_attr.c_cc[VMIN] = 1; term_attr.c_cc[VTIME] = 0; if (ioctl(fd, TCSETAW, &term_attr) < 0) { return -1; } if (ioctl(fd, TCFLSH, 2) < 0) { return -1; } return 0;} int read_data(int fd, void *buf, int len){ int count; int ret; ret = 0; count = 0; //while (len > 0) { ret = read(fd, (char*)buf + count, len); if (ret < 1) { fprintf(stderr, "Read error %s\\n", strerror(errno)); //break; } count += ret; len = len - ret; //} *((char*)buf + count) = 0; return count;} int write_data(int fd, void *buf, int len){ int count; int ret; ret = 0; count = 0; while (len > 0) { ret = write(fd, (char*)buf + count, len); if (ret < 1) { fprintf(stderr, "Write error %s\\n", strerror(errno)); break; } count += ret; len = len - ret; } return count;}void print_usage(char *program_name){ fprintf(stderr, "*************************************\\n" " A Simple Serial Port Test Utility\\n" "*************************************\\n\\n" "Usage:\\n %s < device > < baud > < databits > < parity > < stopbits > \\n" " databits: 5, 6, 7, 8\\n" " parity: 0(None), 1(Odd), 2(Even)\\n" " stopbits: 1, 2\\n" "Example:\\n %s /dev/ttyS4 115200 8 0 1\\n\\n", program_name, program_name );}
運(yùn)行效果如下:
審核編輯:湯梓紅
標(biāo)簽: