HOWTO: Enable USB device mode on Pi Zero


69 posts   Page 2 of 3   1, 2, 3
by thagrol » Mon Dec 28, 2015 10:44 pm
I think the problem is at a lower level the the gadget driver as I get the same behaviour with the g_serial and g_ether modules.

Unfortunately thats about the limit of my kernel and hardware debugging knowledge.
User avatar
Posts: 220
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
by DaveB » Mon Dec 28, 2015 11:35 pm
From your description, it sounds like the host isn't detecting anything at all. If you use a Linux host do you get anything USB related in dmesg?

Also, I don't think that D+ and D- need to be crossed in your hacked up cable.
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm
by thagrol » Tue Dec 29, 2015 12:13 pm
Nope nothing in dmesg on the host, nothing showing in lsusb and nothing new in /dev either.

I didn't think a cross wired cable should be necessary but thought it was worth a try on the off chance.

My guess is that the issue is lower down in the USB stack, in the dt blob, or in the hardware. But thats only a guess.
User avatar
Posts: 220
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
by mikerr » Tue Dec 29, 2015 9:40 pm
I setup an image based on Jessie Lite - so it fits on 2GB SD card

You can grab the complete image here:
https://sourceforge.net/projects/pizero-usbhost/ (misnamed project!)
Android app - Raspi Card Imager - download and image SD cards - No PC required !
User avatar
Posts: 2416
Joined: Thu Jan 12, 2012 12:46 pm
Location: Up north , UK
by mob-i-l » Tue Dec 29, 2015 11:08 pm
gregeric wrote:@mob-i-l does that uSD have max_usb_current=1 in config.txt, might it be a power issue? It should re-enumerate serial=1, then the msd.elf gets squirted across & executed. Unlike the linux module method, this gives access to the whole SD. I haven't disassembled them but I guess usbbootcode.bin & msd.elf are both threadx/vc4, not running on the arm.


Yes, it worked by setting max_usb_current=1 and I also got more power by disconnecting a WiFi-dongle and using ethernet cable instead. I use a Raspberry Pi B+ as host now.
Code: Select all
[   71.046769] usb 1-1.4: new high-speed USB device number 7 using dwc_otg
[   71.147838] usb 1-1.4: New USB device found, idVendor=0a5c, idProduct=2763
[   71.147863] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=1
[   71.147875] usb 1-1.4: Product: Raspberry Pi
[   71.147887] usb 1-1.4: Manufacturer: Broadcom
[   71.147897] usb 1-1.4: SerialNumber: Broadcom
[   71.319632] usb 1-1.4: USB disconnect, device number 7
[   72.326759] usb 1-1.4: new high-speed USB device number 8 using dwc_otg
[   72.427885] usb 1-1.4: New USB device found, idVendor=0a5c, idProduct=0001
[   72.427909] usb 1-1.4: New USB device strings: Mfr=2, Product=1, SerialNumber=3
[   72.427921] usb 1-1.4: Product: Compute Module
[   72.427933] usb 1-1.4: Manufacturer: Raspberry Pi
[   72.427942] usb 1-1.4: SerialNumber: 0001
[   72.435036] usb-storage 1-1.4:1.0: USB Mass Storage device detected
[   72.449070] scsi host1: usb-storage 1-1.4:1.0
[   73.447632] scsi 1:0:0:0: Direct-Access     Raspberr y PiMass storage      PQ: 0 ANSI: 2
[   73.454005] sd 1:0:0:0: [sdb] 15278080 512-byte logical blocks: (7.82 GB/7.28 GiB)
[   73.454342] sd 1:0:0:0: Attached scsi generic sg1 type 0
[   73.454442] sd 1:0:0:0: [sdb] Write Protect is off
[   73.454463] sd 1:0:0:0: [sdb] Mode Sense: 0f 00 00 00
[   73.454854] sd 1:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[   73.466968]  sdb: sdb1
[   73.469791] sd 1:0:0:0: [sdb] Attached SCSI removable disk
[   74.361659] FAT-fs (sdb1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
[  246.328795] sd 1:0:0:0: [sdb] UNKNOWN(0x2003) Result: hostbyte=0x07 driverbyte=0x08
[  246.328828] sd 1:0:0:0: [sdb] Sense Key : 0x4 [current]
[  246.328843] sd 1:0:0:0: [sdb] ASC=0x0 ASCQ=0x0
[  246.328861] sd 1:0:0:0: [sdb] CDB: opcode=0x2a 2a 00 00 00 7c 72 00 00 01 00

I could not get the Raspberry Pi A to work as a USB-memory using sudo rpiboot in Ubuntu 14.04 on a PC, but this might be due to too little power.

Now I'm confident the cable/adapter works, and I can try the gadget mode using e.g. images with gadget mode, i.e. if they are supposed to work on Pi Model A.
Raspberry Pi 0&1 A&1 B2&1 B+&2 B w/ Raspbian. Started w/ BASIC on ABC80 & ZX81 then Forth, Z80… http://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://www.youtube.com/MOBiL4u/ https://twitter.com/mobluse/
Posts: 194
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden
by mob-i-l » Tue Dec 29, 2015 11:14 pm
mikerr wrote:I setup an image based on Jessie Lite - so it fits on 2GB SD card

You can grab the complete image here:
https://sourceforge.net/projects/pizero-usbhost/ (misnamed project!)


Do you think this image also works with Pi Model A?
Raspberry Pi 0&1 A&1 B2&1 B+&2 B w/ Raspbian. Started w/ BASIC on ABC80 & ZX81 then Forth, Z80… http://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://www.youtube.com/MOBiL4u/ https://twitter.com/mobluse/
Posts: 194
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden
by mikerr » Tue Dec 29, 2015 11:22 pm
mob-i-l wrote:Do you think this image also works with Pi Model A?

It should do, but I don't have any A/A+ to test it with here.
Android app - Raspi Card Imager - download and image SD cards - No PC required !
User avatar
Posts: 2416
Joined: Thu Jan 12, 2012 12:46 pm
Location: Up north , UK
by gregeric » Wed Dec 30, 2015 8:28 am
thagrol wrote:Nope nothing in dmesg on the host, nothing showing in lsusb and nothing new in /dev either.

I didn't think a cross wired cable should be necessary but thought it was worth a try on the off chance.

My guess is that the issue is lower down in the USB stack, in the dt blob, or in the hardware. But thats only a guess.

Same here on A+ with a cable proven to work with the Compute Module msd.elf trick - fine on the Zero, not working on A+. This was with Gbaman's all modules kernel, g_serial.

Edit: of course I should've been using the hard-coded gadget only kernel, not the multi/dual. Will try again later.
Posts: 1493
Joined: Mon Nov 28, 2011 10:08 am
by thagrol » Wed Dec 30, 2015 2:30 pm
This thread is getting confusing, there seem to be two seperate issues being discussed.

Anyway, back to the issue with usb gadgets not showing up on the host.

It's my understanding that the pin used for selecting otg or host mode is permanently grounded on the A & A+. So to duplicate this arrangement on the Zero I used the pi zero's usb host cable and an A male to A male usb cable to connect to the host.

Using the same SD card as before, I now get the same result (nothing detected on the host) as on the A and A+. Switching to a standard micro usb cable allows the host to recognise the pi as a serial device.

To recap, kernel was configured to gadget only mode and g_serial was built as a module and loaded from rc.local.
User avatar
Posts: 220
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
by gregeric » Wed Dec 30, 2015 4:13 pm
Ah OK, that's good detective work thagrol.

So, for the A/A+ there's something else needed in software, or we need hack the hardware so that the USB_OTGID pin is not grounded, so enabling gadget mode.

A software-only solution must be possible, as the gadget mode works under threadx/vc4 (msd.elf mass storage) on A/A+.
Posts: 1493
Joined: Mon Nov 28, 2011 10:08 am
by mattmiller » Wed Dec 30, 2015 10:39 pm
I setup an image based on Jessie Lite - so it fits on 2GB SD card

Wow!
What's major things did you remove?

Python?
Scratch?
Posts: 1234
Joined: Thu Feb 05, 2015 11:25 pm
by DougieLawson » Wed Dec 30, 2015 11:25 pm
mattmiller wrote:
I setup an image based on Jessie Lite - so it fits on 2GB SD card

Wow!
What's major things did you remove?

Python?
Scratch?


Wolfram & XWindows and all the desktop junk (scratch, idle, sonic-pi, epiphany) that goes with it. It's not removed, it's just never installed before the point that Jessie Lite is spun off from the regular system build process.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 28140
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by mattmiller » Thu Dec 31, 2015 12:06 am
Didn't realise the Lite existed :)

NBG for me - I'm a desktop person :)
Posts: 1234
Joined: Thu Feb 05, 2015 11:25 pm
by mob-i-l » Thu Dec 31, 2015 12:50 am
mikerr wrote:
mob-i-l wrote:Do you think this image also works with Pi Model A?

It should do, but I don't have any A/A+ to test it with here.

I tested pi-usbnetwork.img in Pi Model A, but I didn't get the network to respond using ping 169.254.64.64 or ssh pi@169.254.64.64. I connected it with SD-card inserted to Pi Model B+ and an x86-laptop with Ubuntu 14.04. Nothing showed up in dmesg. I had a PAL-screen connected to the composite output of Pi A and I could see it boot until it finished, but still nothing in dmesg or lsusb. When I connect the Pi A without SD-card it is recognized and after I connect the SD-card and run sudo rpiboot it works as an USB-drive in both Pi B+ Raspbian Jessie and x86 Ubuntu 14.04. There is probably nothing wrong with the cable/adapter or power.
Last edited by mob-i-l on Fri Jan 01, 2016 2:07 pm, edited 2 times in total.
Raspberry Pi 0&1 A&1 B2&1 B+&2 B w/ Raspbian. Started w/ BASIC on ABC80 & ZX81 then Forth, Z80… http://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://www.youtube.com/MOBiL4u/ https://twitter.com/mobluse/
Posts: 194
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden
by Tomlinsky » Fri Jan 01, 2016 1:32 am
gregeric wrote:A software-only solution must be possible, as the gadget mode works under threadx/vc4 (msd.elf mass storage) on A/A+.


0x7e980000 ( USBOTG_GUSBCFG)

Bit 30: ForceDevMode Force Device Mode Writing a 1 to this bit forces the core to device mode irrespective of utmiotg_iddig input pin (1'b0: Normal Mode 1'b1: Force Device Mode). After setting the force bit, the application must wait at least 25 ms before the change to take effect. When the simulation is in scale down mode, waiting for 500 us is sufficient. This bit is valid only when OTG_MODE = 0, 1 or 2. In all other cases, this bit reads 0.
Posts: 4
Joined: Fri Jan 01, 2016 1:17 am
by mob-i-l » Fri Jan 01, 2016 2:17 pm
mikerr wrote:I setup an image based on Jessie Lite - so it fits on 2GB SD card

You can grab the complete image here:
https://sourceforge.net/projects/pizero-usbhost/ (misnamed project!)

I could not get it to fit on a 2GB microSD, because the microSD actually 1.9GB according to fdisk. I can easily fit the official Raspbian Jessie Lite on this microSD since I think it's 1.8GB. Your image seems to be a lot bigger than the official. I did install your image on an 8GB microSD. I used the 8GB card with an adapter in Pi Model A. It booted, but it was not recognized as a device or anything, as I wrote earlier.
Raspberry Pi 0&1 A&1 B2&1 B+&2 B w/ Raspbian. Started w/ BASIC on ABC80 & ZX81 then Forth, Z80… http://scratch.mit.edu/users/mobluse/ https://github.com/mobluse/ https://www.youtube.com/MOBiL4u/ https://twitter.com/mobluse/
Posts: 194
Joined: Sat Dec 29, 2012 2:45 am
Location: Lund, Skåne/Scania, Sweden
by gregeric » Sat Jan 02, 2016 9:38 am
Tomlinsky wrote:
gregeric wrote:A software-only solution must be possible, as the gadget mode works under threadx/vc4 (msd.elf mass storage) on A/A+.


0x7e980000 ( USBOTG_GUSBCFG)

Bit 30: ForceDevMode Force Device Mode Writing a 1 to this bit forces the core to device mode irrespective of utmiotg_iddig input pin (1'b0: Normal Mode 1'b1: Force Device Mode). After setting the force bit, the application must wait at least 25 ms before the change to take effect. When the simulation is in scale down mode, waiting for 500 us is sufficient. This bit is valid only when OTG_MODE = 0, 1 or 2. In all other cases, this bit reads 0.

Thanks for that. I had a quick look at the dwc2 driver & without digging deeply (I'm out of my depth already), it appears to be doing that: https://github.com/raspberrypi/linux/bl ... et.c#L3582
Posts: 1493
Joined: Mon Nov 28, 2011 10:08 am
by thagrol » Mon Jan 04, 2016 10:41 pm
gregeric wrote:
Tomlinsky wrote:
gregeric wrote:A software-only solution must be possible, as the gadget mode works under threadx/vc4 (msd.elf mass storage) on A/A+.


0x7e980000 ( USBOTG_GUSBCFG)

Bit 30: ForceDevMode Force Device Mode Writing a 1 to this bit forces the core to device mode irrespective of utmiotg_iddig input pin (1'b0: Normal Mode 1'b1: Force Device Mode). After setting the force bit, the application must wait at least 25 ms before the change to take effect. When the simulation is in scale down mode, waiting for 500 us is sufficient. This bit is valid only when OTG_MODE = 0, 1 or 2. In all other cases, this bit reads 0.

Thanks for that. I had a quick look at the dwc2 driver & without digging deeply (I'm out of my depth already), it appears to be doing that: https://github.com/raspberrypi/linux/bl ... et.c#L3582


I'm not sure that this is being done as "USBOTG_GUSBCFG" does not appear in the file you reference. GUSBCFG_FORCEDEVMODE does but it's been so long since I did any C coding I was unable to work out where and what this is being defined/set to.

And likewise, I'm out of my depth now.
User avatar
Posts: 220
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
by ali1234 » Tue Jan 05, 2016 1:54 am
For the people trying to use usbboot, here is a quick and dirty hack to get it working. You need to give it a FAT filesystem image with kernel.img, start.elf etc. The same stuff you would find on /boot.

You can put a Linux kernel image and initrd in it. That is what the buildroot does. However, the maximum size of the filesystem is very small, so you cannot boot a full OS this way, only a small busybox initrd.

You can also substitute in a bare metal kernel such as the blinker demo from https://github.com/dwelch67/raspberrypi ... /blinker01, which is what this Makefile does. It's a very quick and easy way to test, but make sure you get the right blinker for the Pi you are using, because there is obviously no hardware abstraction when doing bare metal.

Code: Select all
all: fatimage

fatimage: blinker01.bin start.elf
   dd if=/dev/zero of=fatimage bs=1M count=4
   mkfs.vfat fatimage
   mkdir -p .fatmnt
   sudo mount -o loop fatimage .fatmnt/
   sudo cp start.elf .fatmnt/
   sudo cp blinker01.bin .fatmnt/kernel.img
   sudo umount .fatmnt
   rmdir .fatmnt

clean:
   rm -f fatimage

run: all
   sudo rpiboot -b fatimage
Posts: 12
Joined: Tue Jan 05, 2016 1:31 am
by gregeric » Tue Jan 05, 2016 11:08 am
That's great, thanks. Much quicker than writing/swapping SD cards, less wear & tear, should accelerate arm bare-metal development.

On a side note, I've tried a couple of times to build the linux buildroot image as per https://github.com/raspberrypi/tools/tr ... er/usbboot but failed. Have you had any joy with that?
Posts: 1493
Joined: Mon Nov 28, 2011 10:08 am
by ali1234 » Tue Jan 05, 2016 1:32 pm
gregeric wrote:On a side note, I've tried a couple of times to build the linux buildroot image as per https://github.com/raspberrypi/tools/tr ... er/usbboot but failed. Have you had any joy with that?


I built it before ages ago on Ubuntu and then booted it on an A+. I didn't have any problems doing so, but as soon as I tried to modify anything in the buildroot it stopped working. I haven't tried recently but I could give it a shot.
Posts: 12
Joined: Tue Jan 05, 2016 1:31 am
by gregeric » Wed Jan 06, 2016 12:15 am
I was trying to compile the buildroot image native on Pi. Just curious to see it work. If you can, that'd be great.

If any mods are reading this, the last four posts would be better moved to a separate thread in Bare Metal, suggested title "USB Boot Bare Metal ARM".
Posts: 1493
Joined: Mon Nov 28, 2011 10:08 am
by Tomlinsky » Wed Jan 06, 2016 10:47 am
gregeric wrote:Thanks for that. I had a quick look at the dwc2 driver & without digging deeply (I'm out of my depth already), it appears to be doing that: https://github.com/raspberrypi/linux/bl ... et.c#L3582


Apologies, cross-referencing documentation I made an error...

0x7e98000C (GUSBCFG)

"Bit 30: ForceDevMode Force Device Mode Writing a 1 to this bit forces the core to device mode irrespective of utmiotg_iddig input pin (1'b0: Normal Mode 1'b1: Force Device Mode). After setting the force bit, the application must wait at least 25 ms before the change to take effect. When the simulation is in scale down mode, waiting for 500 us is sufficient. This bit is valid only when OTG_MODE = 0, 1 or 2. In all other cases, this bit reads 0."

When settting up device mode vcore firmware sets this register to #0x40407200.
Posts: 4
Joined: Fri Jan 01, 2016 1:17 am
by ali1234 » Wed Jan 13, 2016 8:23 pm
Buildroot was successful for me with the following:

Code: Select all
git clone git://github.com/raspberrypi/tools
git clone git://git.buildroot.net/buildroot
cd buildroot
git checkout 7d37754113e9c689b2a617ceb227dbd3a0135726
patch -p1 < ../tools/usbboot/buildroot.patch
chmod a+x board/raspberrypi/usb_test/post_build.sh
make raspberrypi_defconfig
make
Posts: 12
Joined: Tue Jan 05, 2016 1:31 am
by mikerr » Mon Jan 18, 2016 12:31 pm
If you use the modular kernels, you can flip between device and host mode easily:

http://pi.gbaman.info/?p=699

just playing with that now.
Android app - Raspi Card Imager - download and image SD cards - No PC required !
User avatar
Posts: 2416
Joined: Thu Jan 12, 2012 12:46 pm
Location: Up north , UK