lysrgic
Posts: 1
Joined: Thu Dec 13, 2018 8:09 am

Using Ras Pi as USB host and slave

Thu Dec 13, 2018 8:19 am

Hi guys.
Im trying to build an iot device that will connect to a pc as a slave device, while being a host to a mass storage device.

From my understanding, this can't be done with the B,B+ since they may have 4 USB ports, but only one chip.
I know that pi zero can be connected to pc as a slave device. but the question is - can it be a host for other usb device in the same time? or maybe i can use some sort of shield to enhance capabilities? (https://www.waveshare.com/usb-hub-hat.htm)

Thanks for the help!

User avatar
thagrol
Posts: 1397
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Using Ras Pi as USB host and slave

Thu Dec 13, 2018 4:40 pm

Short answer: No

Long answer: While the zero, zeroW A, A+ and 3A+ don't have the USB hub chip the B* models do that prevents them acting as USB slave devices, they can only work in one mode at a time though there are ways to hot swap between them.

There are USB host add ons for arduino that you might be able to connect but I've no idea if there is suitable driver software for them. I can't advise further on this as I've not used one. You'll likely need to add level convertion between 5v and 3.3v to avoid damage to the Pi.

Perhaps you should consider other ways of connecting your IOT device and your PC. Depending on the amount of data you need to move and the performance required this could be via TTL serial, wifi (on zeroW), bluetooth (again on zeroW), by adding an ethernet port via SPI, etc.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

hippy
Posts: 4886
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Using Ras Pi as USB host and slave

Thu Dec 13, 2018 5:55 pm

A Pi can only be a host or slave USB device, not both together. You can use two Pi and link them via serial or other means, or add extra hardware to allow host and device ports, but you would likely still have problems trying to have two sides accessing the same Mass Storage Device file system.

I tried to use a Zero W as a USB MSD which could have files dropped on it via Wi-Fi but never got that to work. This might however help -

https://www.raspberrypi.org/magpi/pi-ze ... ash-drive/

User avatar
thagrol
Posts: 1397
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Using Ras Pi as USB host and slave

Thu Dec 13, 2018 6:41 pm

hippy wrote:
Thu Dec 13, 2018 5:55 pm
A Pi can only be a host or slave USB device, not both together. You can use two Pi and link them via serial or other means, or add extra hardware to allow host and device ports, but you would likely still have problems trying to have two sides accessing the same Mass Storage Device file system.
Zero/zeroW can swap modes without a reboot just by unplugging the otg cable and plugging in a standard micro USB cable from the host. Provided that the zero has its own PSU.

A/A+ can swap modes by unloading and reloading the dwc2 module with an apropriate dr_mode parameter to force the mode. 3A+ is probably the same but as I don't have one I can't confirm that.

The trick is having physical access to the Pi and knowing when it's safe to do either of the above.

For remote access on the hardware side you could do what I believe the CM dev board does and use a USB multiplexer/switch to handle the change in device connection but that doesn't overcome the safety issue.
I tried to use a Zero W as a USB MSD which could have files dropped on it via Wi-Fi but never got that to work. This might however help -

https://www.raspberrypi.org/magpi/pi-ze ... ash-drive/
That tutorial is flawed as it changes the contents of the MSD gagdet's storage while it's online with the host and forces a disconnect/reconnect on the host regardless of what the host is doing at the time.

It's quite clear in the g_mass_storage docs that "The only safe way to share the backing storage between the host and the gadget's operating system at the same time is to make it read-only on both sides." (see http://www.linux-usb.org/gadget/file_storage.html). The safe way to write to it is to unmount the otherside first.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

hippy
Posts: 4886
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Using Ras Pi as USB host and slave

Thu Dec 13, 2018 8:32 pm

thagrol wrote:
Thu Dec 13, 2018 6:41 pm
That tutorial is flawed as it changes the contents of the MSD gagdet's storage while it's online with the host and forces a disconnect/reconnect on the host regardless of what the host is doing at the time.
You're right. As far as I remember one has to have two directories, a Pi local directory which acts as a mirror for what's presented via MSD, which files can be copied into and deleted, and the otherpresented as the MSD, which has to be unmounted, synchronised with the first, then remounted. And vice-versa.

User avatar
thagrol
Posts: 1397
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Using Ras Pi as USB host and slave

Fri Dec 14, 2018 12:58 pm

hippy wrote:
Thu Dec 13, 2018 8:32 pm
thagrol wrote:
Thu Dec 13, 2018 6:41 pm
That tutorial is flawed as it changes the contents of the MSD gagdet's storage while it's online with the host and forces a disconnect/reconnect on the host regardless of what the host is doing at the time.
You're right. As far as I remember one has to have two directories, a Pi local directory which acts as a mirror for what's presented via MSD, which files can be copied into and deleted, and the otherpresented as the MSD, which has to be unmounted, synchronised with the first, then remounted. And vice-versa.
Or just unmount the MSD and remount it when you're done changing its data. Or the other way around. Of course, you need access to both sides to know when it's safe to unmount.

Frankly, in most cases, it's going to be quicker and less hassle to just use a USB drive and take it to your PC than following that tutorial. Not only do you have the issues I outlined above, it'll probably be faster as it won't be limited by wifi speeds and slow writes to the SD card.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

Return to “Other projects”