For using Linux USB "On-The-Go" (OTG) with the Raspberry Pi Zero W, a different USB driver is necessary. The standard heavily patched and performance oriented driver does not support OTG mode. Instead the dwc2 driver needs to be used.
For OTG mode there are different gadget drivers. A Raspberry Pi Zero W can behave like an USB stick with the g_mass_storage driver. A more complex alternative is using libcomposite with configfs.
In
/boot/firmware/config.txt
comment outotg_mode
and adddtoverlay=dwc2
sudo grep -qxF '#otg_mode=1' /boot/firmware/config.txt || sudo sed -i '/^otg_mode=1/s/^/#/' /boot/firmware/config.txt
sudo sh -c "grep -qxF 'dtoverlay=dwc2' /boot/firmware/config.txt || echo 'dtoverlay=dwc2' >> /boot/firmware/config.txt"
In
/etc/modules
adddwc2
:
sudo sh -c "grep -qxF 'dwc2' /etc/modules || echo 'dwc2' >> /etc/modules"
Create usb image file, create partition and format.
sudo dd bs=1M if=/dev/zero of=/piusb.bin count=3072 # create file
echo 'type=07' | sudo sfdisk /piusb.bin # create exfat primary partition
loopd=$(sudo losetup --partscan --show --find /piusb.bin) # create loop device
sudo mkfs.exfat -L Raspi "${loopd}p1" # format with exfat
sudo mkdir -p /mnt/usb_share
Create system service to activate USB mass storage at the very end of the boot process. Therefore setting the type to idle is important.
Copy usb-storage.service
to /etc/systemd/system/
then
sudo mv /home/pi/usb-storage.service /etc/systemd/system/
sudo chmod 644 /etc/systemd/system/usb-storage.service
sudo systemctl enable usb-storage.service
- Create the destination directory on the server e.g. /home/myuser/transfer
- Install expect
- Change password in scp-copy.exp
sudo apt-get update
sudo apt-get -y install expect
sudo apt-get update
sudo apt-get -y install python3-dropbox
Copy usb-copy.service
and usb-upload.service
to /etc/systemd/system/
then
sudo chmod 644 /etc/systemd/system/usb-copy.service
sudo systemctl enable usb-copy.service
sudo chmod 644 /etc/systemd/system/usb-upload.service
sudo systemctl enable usb-upload.service