需求: 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