{"id":854,"date":"2020-09-08T23:29:59","date_gmt":"2020-09-08T15:29:59","guid":{"rendered":"https:\/\/www.iotch.xyz\/?p=854"},"modified":"2020-09-08T23:29:59","modified_gmt":"2020-09-08T15:29:59","slug":"%e5%9f%ba%e4%ba%8e%e6%a0%91%e8%8e%93%e6%b4%be%e7%9a%84%e5%a4%9a%e5%8a%9f%e8%83%bdusb%e5%ae%9e%e7%8e%b0-u%e7%9b%98%e6%a8%a1%e5%bc%8f%e5%92%8c%e7%bd%91%e5%8d%a1%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.iotch.xyz\/?p=854","title":{"rendered":"\u57fa\u4e8e\u6811\u8393\u6d3e\u7684\u591a\u529f\u80fdUSB\u5b9e\u73b0&#8211;U\u76d8\u6a21\u5f0f\u548c\u7f51\u5361\u6a21\u5f0f"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\" id=\"%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86\"><strong>\u5b9e\u73b0\u539f\u7406<\/strong><\/h3>\n\n\n\n<p>\u4e00\u822c\u6765\u8bf4\uff0cUSB \u8bbe\u5907\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f Host\uff0c\u6bd4\u5982\u7535\u8111\uff0c\u53ef\u4ee5\u53bb\u8bfb\u53d6\u5176\u4ed6 USB \u8bbe\u5907\u7684\u6570\u636e\uff0c\u53e6\u5916\u4e00\u79cd\u662f Device\uff0c\u6bd4\u5982\u952e\u76d8\u9f20\u6807U\u76d8\u667a\u80fd\u624b\u673a\u3002\u800cOTG\uff08\u6587\u672b\u9644\u5f55<code>what is OTG<\/code>\uff09\u5b9e\u73b0\u4e86\u8bbe\u5907\u5728host \u548c slave\uff08peripheral\uff09\u95f4\u8f6c\u6362\u3002\u4f8b\u5982\u652f\u6301OTG\u529f\u80fd\u7684\u667a\u80fd\u624b\u673a\u53ef\u4ee5\u4f5c\u4e3ahost\u8bfb\u53d6U\u76d8\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u5728\u6811\u8393\u6d3e\u7cfb\u7edf<code>\/boot\/overlays\/README<\/code>\u4e2d\uff0c\u5173\u4e8e USB controller driver\u7684\u63cf\u8ff0\u5982\u4e0b\uff08\u6587\u672b\u9644\u5f55<code>\u5173\u4e8edwc_otg\u4e0edwc2\u7684\u533a\u522b<\/code>\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Name:   dwc-otg\nInfo:   Selects the dwc_otg USB controller driver which has fiq support. This \n        is the default on all except the Pi Zero which defaults to dwc2.\nLoad:   dtoverlay=dwc-otg\nParams: &lt;None&gt;\n\n\nName:   dwc2\nInfo:   Selects the dwc2 USB controller driver\nLoad:   dtoverlay=dwc2,&lt;param&gt;=&lt;val&gt;\nParams: dr_mode                 Dual role mode: \"host\", \"peripheral\" or \"otg\"\n\n        g-rx-fifo-size          Size of rx fifo size in gadget mode\n\n        g-np-tx-fifo-size       Size of non-periodic tx fifo size in gadget\n                                mode\n<\/pre>\n\n\n\n<p>\u8fd9\u662f\u7531\u4e8e\u5728\u76ee\u524d\u6240\u6709\u6811\u8393\u6d3e\u4e2d\uff0c\u53ea\u6709\u6811\u8393\u6d3eZero\u7cfb\u5217\u652f\u6301OTG\uff0c\u6545\u53ea\u6709\u6811\u8393\u6d3eZero\u7684\u9a71\u52a8\u4e3a\u652f\u6301host\u548cslave\u7684dwc2\uff0c\u8fd9\u4f7f\u5f97\u5b9e\u73b0U\u76d8\u6a21\u5f0f\u548c\u7f51\u5361\u6a21\u5f0f\u7b49\u6210\u4e3a\u53ef\u80fd<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E5%AE%9E%E7%8E%B0\"><strong>\u5b9e\u73b0<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"%E7%BD%91%E5%8D%A1%E6%A8%A1%E5%BC%8F\"><strong>\u7f51\u5361\u6a21\u5f0f<\/strong><\/h4>\n\n\n\n<p>\u53ef\u53c2\u8003\u201c<a href=\"http:\/\/mp.weixin.qq.com\/s?__biz=MzI0NjkzNzc1NA==&amp;mid=2247484038&amp;idx=1&amp;sn=51a5c27effbe52c7e749a92a9488e998&amp;chksm=e9b6e373dec16a65e78818507297b42ff0c565f37e88ccf0fc57be8a18bff293463f273a8ba0&amp;scene=21#wechat_redirect\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u57fa\u4e8e\u6811\u8393\u6d3e\u7684\u591a\u529f\u80fdUSB\u5b9e\u73b0--\u7cfb\u7edf\u5b89\u88c5<\/a>\u201d\u4e00\u6587\u4e2d\u201c\u5f00\u673a\u51c6\u5907\u201d-&gt;\u201cPlan B\u201d<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"%E8%AE%BE%E7%BD%AE%E6%AD%A5%E9%AA%A4\"><strong>\u8bbe\u7f6e\u6b65\u9aa4<\/strong><\/h5>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5728\u6839\u76ee\u5f55\u4e0b\u9762\u7684config.txt\u6587\u4ef6\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u884c<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">dtoverlay=dwc2\n<\/pre>\n\n\n\n<ol class=\"wp-block-list\"><li>\u82e5\u8981\u7f51\u5361\u6a21\u5f0f\uff0c\u6253\u5f00 cmdline.txt \u5728 rootwait \u540e\u9762\u6dfb\u52a0\u5982\u4e0b\u5185\u5bb9\u3002<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">modules-load=dwc2,g_ether\n<\/pre>\n\n\n\n<ol class=\"wp-block-list\"><li>\u91cd\u542f\u3002<\/li><li><code>ifconfig<\/code>\u4f1a\u51fa\u73b0usb0\u7f51\u5361<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">root@raspberrypi:~# ifconfig\nlo: flags=73&lt;UP,LOOPBACK,RUNNING&gt;  mtu 65536\n        inet 127.0.0.1  netmask 255.0.0.0\n        inet6 ::1  prefixlen 128  scopeid 0x10&lt;host&gt;\n        loop  txqueuelen 1000  (Local Loopback)\n        RX packets 4  bytes 444 (444.0 B)\n        RX errors 0  dropped 0  overruns 0  frame 0\n        TX packets 4  bytes 444 (444.0 B)\n        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n\nusb0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500\n        inet 169.254.85.97  netmask 255.255.0.0  broadcast 169.254.255.255\n        inet6 fe80::ebd3:772b:6391:bfde  prefixlen 64  scopeid 0x20&lt;link&gt;\n        ether 32:f7:c3:1c:e2:00  txqueuelen 1000  (Ethernet)\n        RX packets 942  bytes 69082 (67.4 KiB)\n        RX errors 0  dropped 0  overruns 0  frame 0\n        TX packets 31  bytes 5556 (5.4 KiB)\n        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n\nwlan0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500\n        inet 192.168.18.10  netmask 255.255.255.0  broadcast 192.168.18.255\n        inet6 2408:8210:904a:ce10:ba27:ebff:fed6:bd35  prefixlen 128  scopeid 0x0&lt;global&gt;\n        inet6 fe80::d265:e222:f80b:bafe  prefixlen 64  scopeid 0x20&lt;link&gt;\n        inet6 2408:8210:904a:ce10:9858:ac15:2a92:ac26  prefixlen 64  scopeid 0x0&lt;global&gt;\n        ether b8:27:eb:d6:bd:35  txqueuelen 1000  (Ethernet)\n        RX packets 1322  bytes 88862 (86.7 KiB)\n        RX errors 0  dropped 0  overruns 0  frame 0\n        TX packets 93  bytes 18435 (18.0 KiB)\n        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"%E5%BA%94%E7%94%A8\"><strong>\u5e94\u7528<\/strong><\/h5>\n\n\n\n<p>\u8be5\u6a21\u5f0f\u76ee\u524d\u4e2a\u4eba\u4e3b\u8981\u7528\u4e8essh\u8fde\u63a5\u6811\u8393\u6d3e<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"U%E7%9B%98%E6%A8%A1%E5%BC%8F\"><strong>U\u76d8\u6a21\u5f0f<\/strong><\/h4>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"%E8%AE%BE%E7%BD%AE%E6%AD%A5%E9%AA%A4\"><strong>\u8bbe\u7f6e\u6b65\u9aa4<\/strong><\/h5>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5728\u6839\u76ee\u5f55\u4e0b\u9762\u7684config.txt\u6587\u4ef6\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u884c<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">dtoverlay=dwc2\n<\/pre>\n\n\n\n<ol class=\"wp-block-list\"><li>\u82e5\u8981\u7f51\u5361\u6a21\u5f0f\uff0c\u6253\u5f00 cmdline.txt \u5728 rootwait \u540e\u9762\u6dfb\u52a0\u5982\u4e0b\u5185\u5bb9<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">modules-load=dwc2,g_mass_storage\n<\/pre>\n\n\n\n<ol class=\"wp-block-list\"><li>\u91cd\u542f<\/li><li>\u65b0\u5efa\u4e00\u4e2a2g\u7684\u955c\u50cf\u6587\u4ef6<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">dd if=\/dev\/zero of=\/home\/my_u_disk.bin bs=1024 count=1000000\n<\/pre>\n\n\n\n<ol class=\"wp-block-list\"><li>\u7136\u540e\u683c\u5f0f\u5316\u6210<code>vfat<\/code>\u683c\u5f0f<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">mkfs.vfat \/home\/my_u_disk.bin\n<\/pre>\n\n\n\n<ol class=\"wp-block-list\"><li>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u540e\uff0c\u82e5\u6811\u8393\u6d3eZero\u63d2\u5728\u7535\u8111\u4e0a\uff0c\u5219\u5728\u7535\u8111\u4e0a\u4f1a\u5f39\u51faU\u76d8\u8bbe\u5907<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">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\"\n<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"%E5%BA%94%E7%94%A8\"><strong>\u5e94\u7528<\/strong><\/h5>\n\n\n\n<p>\u8be5\u6a21\u5f0f\u53ef\u4ee5\u7528\u4e8e\u6a21\u62df\u666e\u901aU\u76d8\uff0c\u4e5f\u53ef\u914d\u5408\u83dc\u5355\u9009\u9879\u4e2d<code>Export log<\/code>\uff0c\u5bfc\u51fa\u64cd\u4f5clog \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53U\u76d8\u955c\u50cf\u6587\u4ef6<code>modprobe<\/code>\u4e3a\u5916\u754cU\u76d8\u65f6\uff0c\u5982\u679c<code>mount<\/code>U\u76d8\u955c\u50cf\u6587\u4ef6\u5230\u6811\u8393\u6d3e\u7cfb\u7edf\u672c\u5730\u4f1a\u53d1\u73b0\uff0c\u5916\u754cU\u76d8\u7684\u6587\u4ef6\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u5e76\u4e0d\u4f1a\u540c\u6b65\u663e\u793a\uff0c\u9700<code>umount<\/code>\u548c<code>rmmod<\/code>\u4e4b\u540e\u518d<code>mount<\/code>\u6216<code>modprobe<\/code>\u4e00\u904d\u624d\u80fd\u540c\u6b65\u3002 \u6545\u5728\u4ea4\u4e92\u83dc\u5355\u680f\u4e2d\u6709<code>Flash U Disk<\/code>\u9009\u9879\uff0c\u4ee5\u4fbf\u5728<code>Export log<\/code>\u540e\u66f4\u65b0U\u76d8\u663e\u793a\u5185\u5bb9<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E9%99%84%E5%BD%95\"><strong>\u9644\u5f55<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"dwc_otg%E4%B8%8Edwc2\"><strong>dwc_otg\u4e0edwc2<\/strong><\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>dwc_otg is the driver that has been heavily patched to squeeze most performance &amp; function in host mode on the Pi: the fiq stuff etc. So heavily patched that, despite the name, <strong>it only does host mode &amp; not OTG<\/strong>. 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.<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"what-is-OTG\"><strong>what is OTG<\/strong><\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>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.<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"%E4%B8%89%E7%A7%8D%E6%A8%A1%E5%BC%8F\"><strong>\u4e09\u79cd\u6a21\u5f0f<\/strong><\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Using the modules<\/p><\/blockquote>\n\n\n\n<ul class=\"wp-block-list\"><li>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.<\/li><li>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\" &gt;&gt; \/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.<\/li><li>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.<\/li><\/ul>\n\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E5%8F%82%E8%80%83%E9%93%BE%E6%8E%A5\"><strong>\u53c2\u8003\u94fe\u63a5<\/strong><\/h3>\n\n\n\n<p>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) \u6811\u8393\u6d3e \/boot\/overlays\/README Raspberry Pi Zero OTG Mode(https:\/\/gist.github.com\/gbaman\/50b6cca61dd1c3f88f41)<\/p>\n\n\n\n<p>\u539f\u6587\u94fe\u63a5\uff1b<a href=\"https:\/\/cloud.tencent.com\/developer\/article\/1581569\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >https:\/\/cloud.tencent.com\/developer\/article\/1581569<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"854\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u73b0\u539f\u7406 \u4e00\u822c\u6765\u8bf4\uff0cUSB \u8bbe\u5907\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f Host\uff0c\u6bd4\u5982\u7535\u8111\uff0c\u53ef\u4ee5\u53bb\u8bfb\u53d6\u5176\u4ed6 USB \u8bbe\u5907\u7684\u6570\u636e\uff0c\u53e6\u5916\u4e00\u79cd\u662f Device\uff0c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-854","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=854"}],"version-history":[{"count":0,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/854\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}