Raspberry Pi Zero USB OTG

Embedded
Published

May 4, 2021

Modified

May 4, 2021

Using an SD card with Raspberry Pi OS:

seq 1 2 | parallel "pmount /dev/mmcblk0p{}"              # mount
sync ; seq 1 2 | parallel "pumount /media/mmcblk0p{}"    # unmount

The g_ether modules able virtual Ethernet point to point networking.

In the /boot partition:

# add the OTG driver
echo "dtoverlay=dwc2" >> config.txt
# enable SSH in a headless setup
touch ssh
# Look for rootwait, and add modules-load=dwc2,g_ether immediately after
vi cmdline.txt 

In the / root partition, write a static IP address configuration:

cat > etc/dhcpcd.conf <<EOF
interface usb0
static ip_address=192.168.7.2
static routers=192.168.7.1
static domain_name_servers=192.168.7.1
EOF

On the host computer:

sudo dmesg | grep cdc_ether    # renames the interface to enx*
# interface name and mac-address
ip -o link | grep enx | awk '{print $2, $17}'

Assign an IP address to the interface enx*:

iface=$(ip -o link | grep enx | awk '{print $2}' | tr -d ':')
sudo ip a add 192.168.7.1/24 dev $iface
sudo ip link set dev $iface up
echo 'MulticastDNS=yes' > /etc/systemd/resolved.conf.d/mdns.conf
systemctl restart systemd-resolved
systemd-resolve --set-mdns=yes --interface=usb0 \
        && systemd-resolve --status usb0

The g_serial module enables serial console on a connected computer:

sudo modprobe g_serial
sudo systemctl start getty@ttyGS0.service

References…