实现原理
一般来说,USB 设备有两种,一种是 Host,比如电脑,可以去读取其他 USB 设备的数据,另外一种是 Device,比如键盘鼠标U盘智能手机。而OTG(文末附录what is OTG
)实现了设备在host 和 slave(peripheral)间转换。例如支持OTG功能的智能手机可以作为host读取U盘内容。
在树莓派系统/boot/overlays/README
中,关于 USB controller driver的描述如下(文末附录关于dwc_otg与dwc2的区别
):
Name: dwc-otg Info: Selects the dwc_otg USB controller driver which has fiq support. This is the default on all except the Pi Zero which defaults to dwc2. Load: dtoverlay=dwc-otg Params: <None> Name: dwc2 Info: Selects the dwc2 USB controller driver Load: dtoverlay=dwc2,<param>=<val> Params: dr_mode Dual role mode: "host", "peripheral" or "otg" g-rx-fifo-size Size of rx fifo size in gadget mode g-np-tx-fifo-size Size of non-periodic tx fifo size in gadget mode
这是由于在目前所有树莓派中,只有树莓派Zero系列支持OTG,故只有树莓派Zero的驱动为支持host和slave的dwc2,这使得实现U盘模式和网卡模式等成为可能
实现
网卡模式
可参考“基于树莓派的多功能USB实现--系统安装”一文中“开机准备”->“Plan B”
设置步骤
- 在根目录下面的config.txt文件的末尾添加一行
dtoverlay=dwc2
- 若要网卡模式,打开 cmdline.txt 在 rootwait 后面添加如下内容。
modules-load=dwc2,g_ether
- 重启。
ifconfig
会出现usb0网卡
root@raspberrypi:~# ifconfig lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 4 bytes 444 (444.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4 bytes 444 (444.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 169.254.85.97 netmask 255.255.0.0 broadcast 169.254.255.255 inet6 fe80::ebd3:772b:6391:bfde prefixlen 64 scopeid 0x20<link> ether 32:f7:c3:1c:e2:00 txqueuelen 1000 (Ethernet) RX packets 942 bytes 69082 (67.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 31 bytes 5556 (5.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.18.10 netmask 255.255.255.0 broadcast 192.168.18.255 inet6 2408:8210:904a:ce10:ba27:ebff:fed6:bd35 prefixlen 128 scopeid 0x0<global> inet6 fe80::d265:e222:f80b:bafe prefixlen 64 scopeid 0x20<link> inet6 2408:8210:904a:ce10:9858:ac15:2a92:ac26 prefixlen 64 scopeid 0x0<global> ether b8:27:eb:d6:bd:35 txqueuelen 1000 (Ethernet) RX packets 1322 bytes 88862 (86.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 93 bytes 18435 (18.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
应用
该模式目前个人主要用于ssh连接树莓派
U盘模式
设置步骤
- 在根目录下面的config.txt文件的末尾添加一行
dtoverlay=dwc2
- 若要网卡模式,打开 cmdline.txt 在 rootwait 后面添加如下内容
modules-load=dwc2,g_mass_storage
- 重启
- 新建一个2g的镜像文件
dd if=/dev/zero of=/home/my_u_disk.bin bs=1024 count=1000000
- 然后格式化成
vfat
格式
mkfs.vfat /home/my_u_disk.bin
- 执行以下命令后,若树莓派Zero插在电脑上,则在电脑上会弹出U盘设备
modprobe g_mass_storage file=/home/my_u_disk.bin removable=1 dVendor=0x0781 idProduct=0x5572 bcdDevice=0x011a iManufacturer="SanDisk" iProduct="Cruzer Switch" iSerialNumber="1234567890"
应用
该模式可以用于模拟普通U盘,也可配合菜单选项中Export log
,导出操作log 需要注意的是,当U盘镜像文件modprobe
为外界U盘时,如果mount
U盘镜像文件到树莓派系统本地会发现,外界U盘的文件增删查改操作并不会同步显示,需umount
和rmmod
之后再mount
或modprobe
一遍才能同步。 故在交互菜单栏中有Flash U Disk
选项,以便在Export log
后更新U盘显示内容
附录
dwc_otg与dwc2
dwc_otg is the driver that has been heavily patched to squeeze most performance & function in host mode on the Pi: the fiq stuff etc. So heavily patched that, despite the name, it only does host mode & not OTG. dwc2 is an upstream driver which can do the OTG host/gadget flip dictated by OTG_SENSE. In host mode performance will pale of dwc_otg, hence it's only recommended for gadget mode.
what is OTG
USB On-The-Go (USB OTG or just OTG) is a specification first used in late 2001 that allows USB devices, such as tablets or smartphones, to act as a host, allowing other USB devices, such as USB flash drives, digital cameras, mice or keyboards, to be attached to them. Use of USB OTG allows those devices to switch back and forth between the roles of host and device. A mobile phone may read from removable media as the host device, but present itself as a USB Mass Storage Device when connected to a host computer.
三种模式
Using the modules
- g_serial - To use the standard serial module, you need to tell the Pi to forward the serial console to it with sudo systemctl enable getty@ttyGS0.service, then you can connect to the device via Putty or Screen.
- g_ether - Using virtual ethernet, you should simply be able to ssh into the address of your Raspberry Pi. To do this, there is a little extra configuration required though. There is a few ways we could set up the point to point networking. The proper way would be to set up a DHCP server on one of the ends. A far simplier was though is just to give the Raspberry Pi a fixed IP address. To do this, you will need to run sudo echo -e "interface usb0 \nstatic ip_address=169.254.64.64" >> /etc/dhcpcd.conf. You can then access the Raspberry Pi Zero by connecting to 169.254.64.64, or by using raspberrypi.local if your computer has Bonjour installed (Mac and most Linux OSs including Raspbian). Note this method does not support adding a fixed address to the cmdline.txt file. For that, you have to use the Ethernet only kernel below.
- g_mass_storage - To have your Pi Zero appear as a mass storage device (flash drive), first create a mini filesystem in a file on your Pi with sudo dd if=/dev/zero of=/piusb.bin bs=512 count=2880 and set it up as a fat32 filesystem with sudo mkdosfs /piusb.bin. Then, when enabling it, add file=/piusb.bin stall=0 onto the end, for example sudo modprobe g_mass_storage file=/piusb.bin stall=0.
In theory, most USB devices should work alongside these kernels, to switch to USB OTG mode, simply don't use an OTG adapter cable and use a standard USB cable to plug your Pi Zero into another computer, it should auto switch.
参考链接
Difference between DWCOTG and DWC2(https://www.raspberrypi.org/forums/viewtopic.php?t=179259) USB On-The-Go(https://en.wikipedia.org/wiki/USB_On-The-Go) 树莓派 /boot/overlays/README Raspberry Pi Zero OTG Mode(https://gist.github.com/gbaman/50b6cca61dd1c3f88f41)
叨叨几句... NOTHING