"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Saturday, 3 March 2018

Sharing files on the Raspberry Pi Zero W


I eventually managed to find some time to continue working to my Pi-zero camera project. During initial tests I took advantage of Pi-Zero wireless interface and used “sftp" command in order to transfer files between Raspberry and desktop computer. A more suitable way of transferring files using USB, like real digital cameras, would be of course advisable.

Raspberry Pi Zero as mass storage

The Pi Zero USB port is directly connected to processor, unlike others Raspberrys using a on-board USB Hub. This, cheaper, solution means the Pi Zero can be configured to act as USB host, like a computer, or as a “USB gadget” like all cell-phones and digital cameras do. In order to activate such “USB gadget” mode some specific “dcw2” kernel modules must be enabled, in my specfic case the needed module is called “g_mass_storage”. I followed this simple guide from a Github user.
First I enabled DCW2 USB driver
echo "dtoverlay=dwc2" | sudo tee -a /boot/config.txtecho "dwc2" | sudo tee -a /etc/modules

then I created a virtual disk image to be mounted as mass storage and formatted if as a FAT32 disk.
sudo dd if=/dev/zero of=/usb-drive.img bs=1M count=1000sudo mkdosfs -F 32 /usb-drive.img
Once the Raspberry PI is conncted to a computer the mass storage can be enabledl ike this
sudo modprobe g_mass_storage file=/usb-drive.img stall=0 removable=1
and disabled with following modprobe command
sudo modprobe -r g_mass_storage
the same virtual disk can be availabe to Raspberri PI by mounting it
sudo mkdir /media/usb-drivesudo mount -o loop,rw /usb-drive.img /media/usb-drive/
of course the same virtual disk cannot be both available as mass storage drive and mounted on Raspberry at the same time, since it could lead to disk corruption. The camera software will have so to manage the switch between two modes.