前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于树莓派的多功能USB实现--U盘模式和网卡模式

基于树莓派的多功能USB实现--U盘模式和网卡模式

作者头像
PedroQin
发布2020-02-12 11:02:13
8.4K0
发布2020-02-12 11:02:13
举报
文章被收录于专栏:WriteSimpleDemoWriteSimpleDemo

实现原理

一般来说,USB 设备有两种,一种是 Host,比如电脑,可以去读取其他 USB 设备的数据,另外一种是 Device,比如键盘鼠标U盘智能手机。而OTG(文末附录what is OTG)实现了设备在host 和 slave(peripheral)间转换。例如支持OTG功能的智能手机可以作为host读取U盘内容。

在树莓派系统/boot/overlays/README中,关于 USB controller driver的描述如下(文末附录关于dwc_otg与dwc2的区别):

代码语言:javascript
复制
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”

设置步骤
  1. 在根目录下面的config.txt文件的末尾添加一行
代码语言:javascript
复制
dtoverlay=dwc2
  1. 若要网卡模式,打开 cmdline.txt 在 rootwait 后面添加如下内容。
代码语言:javascript
复制
modules-load=dwc2,g_ether
  1. 重启。
  2. ifconfig会出现usb0网卡
代码语言:javascript
复制
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盘模式
设置步骤
  1. 在根目录下面的config.txt文件的末尾添加一行
代码语言:javascript
复制
dtoverlay=dwc2
  1. 若要网卡模式,打开 cmdline.txt 在 rootwait 后面添加如下内容
代码语言:javascript
复制
modules-load=dwc2,g_mass_storage
  1. 重启
  2. 新建一个2g的镜像文件
代码语言:javascript
复制
dd if=/dev/zero of=/home/my_u_disk.bin bs=1024 count=1000000
  1. 然后格式化成vfat格式
代码语言:javascript
复制
mkfs.vfat /home/my_u_disk.bin
  1. 执行以下命令后,若树莓派Zero插在电脑上,则在电脑上会弹出U盘设备
代码语言:javascript
复制
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盘时,如果mountU盘镜像文件到树莓派系统本地会发现,外界U盘的文件增删查改操作并不会同步显示,需umountrmmod之后再mountmodprobe一遍才能同步。 故在交互菜单栏中有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)

往期回顾

基于树莓派的多功能USB实现--系统安装

基于树莓派的多功能USB实现--显示屏和按键交互菜单

假期结束返回工作地的我们。。。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WriteSimpleDemo 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现原理
  • 实现
    • 网卡模式
      • 设置步骤
      • 应用
    • U盘模式
      • 设置步骤
      • 应用
  • 附录
    • dwc_otg与dwc2
      • what is OTG
        • 三种模式
        • 参考链接
        • 往期回顾
        相关产品与服务
        对象存储
        对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档