使用設備的usb接口進行串口的輸出

需求: html

1. 現有開發板只支持串口輸出,可是現有串口是調試用的, 使用起來及其不方便,特別是對非專業人員來講,更不容易。linux

2. 如今已知該開發板有usb接口,想利用usb接口做爲串口輸出用。工具

 

所需測試環境:測試

1. 一根兩頭都有usb-to-serial芯片的串口線設備ui

2. linux下的串口終端工具,kermit 和 minicomspa

 

解決方法:調試

1. 添加usb-to-serial驅動,通過確認該usb線材中的usb-to-serial芯片是CP2102, make menuconfig 後選擇相應的驅動,而後查看.configcode

+# add usb-to-serial
+CONFIG_PACKAGE_kmod-usb-serial=y
+CONFIG_PACKAGE_kmod-usb-serial-cp210x=y

開發板啓動後,查看該驅動是否加載 lsmodorm

root@AP-54:F0:~# lsmod | grep serial
usbserial              17531  3 cp210x
root@AP-54:F0:~# 

插入usb線纜,啓動系統,查看是否識別usb設備 dmesg | grep usbhtm

root@AP-54:F0:~# dmesg | grep usb
[    5.480000] usbcore: registered new interface driver usbfs
[    5.480000] usbcore: registered new interface driver hub
[    5.480000] usbcore: registered new device driver usb
[    5.550000] usbcore: registered new interface driver usb-storage
[   21.110000] usbcore: registered new interface driver usbserial
[   21.110000] usbcore: registered new interface driver usbserial_generic
[   21.110000] usbserial: USB Serial support registered for generic
[   21.220000] usbcore: registered new interface driver cp210x
[   21.220000] usbserial: USB Serial support registered for cp210x
[   31.510000] usb 1-1: new full-speed USB device number 2 using ehci-platform
[   31.910000] usb 1-1: reset full-speed USB device number 2 using ehci-platform
[   32.100000] usb 1-1: cp210x converter now attached to ttyUSB0

查看是否生成設備文件, ttyUSB0 是usb接口的設備文件,至此,usb驅動加載成功

root@AP-54:F0:~# ls /dev/tty
tty      ttyS0    ttyUSB0

 

2. 查看開發板默認的串口輸出是 ttyS0, 能夠從bootargs看出

Starting kernel ...

[    0.000000] Linux version 3.14.77 (sprint@dell) (gcc version 5.2.0 (HOS GCC 5.2.0 unknown) ) #4 Mon Aug 13 17:54:40 CST 2018
[    0.000000] arg 1: bootargs=console=ttyS0,115200
[    0.000000] arg 2: root=31:02
[    0.000000] arg 3: rootfstype=jffs2
[    0.000000] arg 4: init=/sbin/init
[    0.000000] arg 5: mtdparts=ath-nor0:256k(u-boot),64k(u-boot-env),14528k(rootfs),1408k(uImage),64k(mib)
[    0.000000] arg 6: ramdisk_size=128M
[    0.000000] arg 7: mem=126M
[    0.000000] bootconsole [early0] enabled

嘗試去修改bootloader的傳遞參數 boot args,修改方法爲: 

setenv bootargs "bootargs=console=ttyUSB0,115200 root=31:02 rootfstype=jffs2 init=/sbin/init mtdparts=ath-nor0:256k(u-boot),64k(u-boot-env),14528k(rootfs),1408k(uImage),64k(mib)"

結果: kernel的啓動log顯示已經修改成 ttyUSB0了,但是實際上仍是使用的串口ttyS0進行的輸出

Starting kernel ...

[    0.000000] Linux version 3.14.77 (sprint@dell) (gcc version 5.2.0 (HOS GCC 5.2.0 unknown) ) #4 Mon Aug 13 17:54:40 CST 2018
[    0.000000] arg 1: console=ttyUSB0,115200
[    0.000000] arg 2: root=31:02
[    0.000000] arg 3: rootfstype=jffs2
[    0.000000] arg 4: init=/sbin/init
[    0.000000] arg 5: mtdparts=ath-nor0:256k(u-boot),64k(u-boot-env),14528k(rootfs),1408k(uImage),64k(mib)
[    0.000000] arg 6: ramdisk_size=128M
[    0.000000] arg 7: mem=126M
[    0.000000] bootconsole [early0] enabled

3. 修改/etc/inittab文件,在裏邊將 ttyS0修改成 ttyUSB0:

root@AP-54:F0:~# cat /etc/inittab 
::sysinit:/etc/init.d/rcS S boot
::sysinitc:/etc/init.d/rcS S boot
::sysinito:/etc/init.d/rcS S boot
::shutdown:/etc/init.d/rcS K shutdown
ttyS0::respawn:/sbin/getty -L ttyUSB0 115200 vt100

修改後,執行reboot,前半段的log是由ttyS0輸出,後半段的log是由ttyUSB0輸出的,估計是 inittab腳本執行後就將串口重定向到了ttyUSB0了。

至此,問題基本解決

4. 在3的基礎上發現,若是保留ttyS0,則原有串口ttyS0以及新增的串口ttyUSB0都可做爲串口輸出,可是啓動的log仍是會輸出到ttyS0上,不過經過兩個串口均可登陸進入開發板

root@AP-54:F0:~# cat /etc/inittab 
::sysinit:/etc/init.d/rcS S boot
::sysinitc:/etc/init.d/rcS S boot
::sysinito:/etc/init.d/rcS S boot
::shutdown:/etc/init.d/rcS K shutdown
ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100
ttyUSB0::respawn:/sbin/getty -L ttyUSB0 115200 vt100

 關於 init 的中 getty的介紹,能夠參考以下:

http://docs.huihoo.com/gnu_linux/sag_cn/chapter7.html

相關文章
相關標籤/搜索