需求:
1. 現有開發板只支持串口輸出,但是現有串口是調試用的, 使用起來及其不方便,特別是對非專業人員來說,更不容易。
2. 現在已知該開發板有usb接口,想利用usb接口作為串口輸出用。
所需測試環境:
1. 一根兩頭都有usb-to-serial芯片的串口線設備
2. linux下的串口終端工具,kermit 和 minicom
解決方法:
1. 添加usb-to-serial驅動,經過確認該usb線材中的usb-to-serial芯片是CP2102, make menuconfig 后選擇相應的驅動,然后查看.config
+# add usb-to-serial +CONFIG_PACKAGE_kmod-usb-serial=y +CONFIG_PACKAGE_kmod-usb-serial-cp210x=y
開發板啟動后,查看該驅動是否加載 lsmod
root@AP-54:F0:~# lsmod | grep serial usbserial 17531 3 cp210x root@AP-54:F0:~#
插入usb線纜,啟動系統,查看是否識別usb設備 dmesg | grep usb
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