kayceeFour
Posts: 3
Joined: Sat Mar 14, 2015 2:46 pm

Pi emulate USB stick?

Fri Sep 21, 2018 2:55 pm

On my CNC machine instead of intelligently using a computer for control, communication and to transfer files, the only way to load GCode is through the USB port on the janky, 3rd party controller. This means that about 50 times a day I have to do the following:

take out the USB stick,
plug it into my lapto
transfer the new GCode files,
eject the USB stick,
go back to the machine
insert the stick,
wait for it to be read,
scroll through the files and
find the one I want and then . . . . . you get it.[/list]

What I'd like to do is set up my Raspberry Pi3 + and have it EMULATE a USB stick so that I can transfer files via wifi and have the controller read it as a USB stick.

Is this nuts? Or is it possible? Just no sure where to start looking. "Googling" it for several hours over multiple days has not come up with anything useful - all are directives on how to READ a USB devise with a Pi but nothing useful to make it ACT as a USB. Any help would make my life (and the lives of countless others whose various lasers, CNC, 3D printing and other digital machines using janky, 3rd party controllers) a LOT happier!

Thanks

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 14780
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Pi emulate USB stick?

Fri Sep 21, 2018 4:13 pm

Not possible with a PI3(+) or PI2, possible with a PI Zero.
A PI3 cannot act as an USB device, only as an USB master.

A zero can be an USB OTG device.

Main reason for other PI's why this isn't possible is because of the LAN9512 (or equivalent) chip in between USB ports and SoC.

Just plug an USB stick into your PI, and transfer the files to it.

kayceeFour
Posts: 3
Joined: Sat Mar 14, 2015 2:46 pm

Re: Pi emulate USB stick?

Fri Sep 21, 2018 4:25 pm

Thanks for the info on the Zero - will check it out.

However your recommendation about plugging the stick into the Pi - how is that different than plugging my stick into my laptop? That is not the question.

The question is how to enable the Pi to BE the stick, to EMULATE a stick, to ACT AS a stick from the POV of the stupid controller. I.E. connected to the controller (which is expecting a stick) Then I can upload files to the Pi via wifi and then the controller will be able to acess it's USB port as usual and read the file. Except that there is now not a stick there but a Pi. :D Capeesh? Of course this will require a male/male USB cable to connect the Pi to the controller.

Not sure that's possible. Thanks for your feedback..

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 14780
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Pi emulate USB stick?

Fri Sep 21, 2018 4:28 pm

kayceeFour wrote:
Fri Sep 21, 2018 4:25 pm

However your recommendation about plugging the stick into the Pi - how is that different than plugging my stick into my laptop? That is not the question.
I know, but because that isn't possible, I mentioned the alternative, the other alternative is to use a (wired or wireless) network.

fruitoftheloom
Posts: 27225
Joined: Tue Mar 25, 2014 12:40 pm

Re: Pi emulate USB stick?

Fri Sep 21, 2018 5:01 pm

kayceeFour wrote:
Fri Sep 21, 2018 4:25 pm
Thanks for the info on the Zero - will check it out.

However your recommendation about plugging the stick into the Pi - how is that different than plugging my stick into my laptop? That is not the question.

The question is how to enable the Pi to BE the stick, to EMULATE a stick, to ACT AS a stick from the POV of the stupid controller. I.E. connected to the controller (which is expecting a stick) Then I can upload files to the Pi via wifi and then the controller will be able to acess it's USB port as usual and read the file. Except that there is now not a stick there but a Pi. :D Capeesh? Of course this will require a male/male USB cable to connect the Pi to the controller.

Not sure that's possible. Thanks for your feedback..

Though a Zero W can act as a Mass Storage Device:

https://blog.gbaman.info/?p=699

I have never tried to access over WiFi when in Slave Mode.

YMMV
Take what I advise as advice not the utopian holy grail, and it is gratis !!

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

Re: Pi emulate USB stick?

Fri Sep 21, 2018 5:27 pm

It's possible with a zeroW but there are limitations primarily due to the way the usb mass storage gadget (g_mass_storage) module works.

In more detail:
  • Updates don't propogate between the USB host and the Pi in either direction. Changes made on either side will likely cause file system corruption at best or break things on the USB host side as it can lead to garbage data being passed to the device.
  • g_mass_storage needs a block device or a flat file (which it treats as a block device) so you can't just use a mounted network share
There are ways around these limitations but they might be more hassle than pulling the USB stick and using your lapto to make the changes.

if you have a NAS or server that supports ATAoE, this is one way to do it: https://www.instructables.com/id/NAS-Ac ... d-Devices/ Full disclosure: I'm the author.

If you want to run the server part on the zero, I'd do something like this:
  1. Make sure your USB host works corrently with devices with removable storage, e.g. USB CD/DVD drives. Particularly with regard to disc changes and insertions. If it doesn't there's little point in following this as you'll have to go to the USB host to remove and replug the Pi.
  2. Add a third partition to the Pi's SD card. Format it with a filesystem that the USB host understands.
  3. Install and configure a samba server (or nfs if your remote connections are from a linux box)
  4. Start g_mass_storage with the removable flag set to 1 and the readonly flag also set to 1:

    Code: Select all

    sudo modprobe g_mass_storage removable=1 ro=1
To use as a USB storage device for you CNC machine:
  • Shutdown the samba server
  • Make sure the target partition is not mounted under linux
  • Pass said partition to the g_mass_storage module:

    Code: Select all

    sudo echo /dev/mmcblk0p2 > /sys/devices/platform/soc/20980000.usb/gadget/lun0/file
To make changes to the USB gadget's storage partition:
  • Remove partition from g_mass_storage:

    Code: Select all

    sudo echo "" > /sys/devices/platform/soc/20980000.usb/gadget/lun0/file
  • Mount the partition read/write
  • Start the samba server
  • Make the desired changes.
Obviously, you'll need to tweak some of the above to match your exact situation and you'll need a working WiFi network with ssh enabled.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

kayceeFour
Posts: 3
Joined: Sat Mar 14, 2015 2:46 pm

Re: Pi emulate USB stick?

Fri Sep 21, 2018 6:00 pm

Now THAT looks like a possibility!

Just ordered a Pi Zero W (with Wifi and Bluetooth!) and then run through this to see what the CNC controller thinks it is - LOL. Of course I will try a very simple GCode file before sending anything to the machine that might cause - GRRRrrrreeeeeeechzerKKKKK_KA_Boink! Broken bit or worse. That's the only reason no one else has tried this but these are only text files after all and as long as the machine can read these with no interstitiary, random GCode-esque commands being inserted it should be rather robust, as opposed to BUST!

Thanks for the guidance! I will keep you posted.

Return to “Advanced users”