一般来说,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”
dtoverlay=dwc2
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连接树莓派
dtoverlay=dwc2
modules-load=dwc2,g_mass_storage
dd if=/dev/zero of=/home/my_u_disk.bin bs=1024 count=1000000
vfat
格式mkfs.vfat /home/my_u_disk.bin
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 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.
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
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)
假期结束返回工作地的我们。。。
本文分享自 WriteSimpleDemo 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体分享计划 ,欢迎热爱写作的你一起参与!