Files
rover-pi/gadget.md
T
2026-06-29 00:09:46 +00:00

68 lines
3.4 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Gadget Mode to setup a serial connection over USB
Prepare Raspberry Pi OS connection over a microUSB serial cable
Important:  choose either PiUART or OTG.  If you do both, it is possible that neither will work.  Also, you will want to disable the serial console if you are trying to use that serial port for communicating with other devices.  Important reading:  Raspberry PI UART configuration
USB-OTG method (Works on Zero, Zero W, and Compute Module)
Based upon Adafruit guide: https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/serial-gadget:
# Raspberry Pi Connect
1. Make sure you have your USB cable plugged into the left port **USB** and not the right port marked **PWR**.
1. Open a connection to the device
1. Become root (the superuser)
`sudo su`
1. Enable the DWC USB peripheral device tree overlay by appending it to the end after the `[all]` category
`echo "dtoverlay=dwc2" >> /boot/firmware/config.txt`
1. We add the required module `dwc2` for loading at startup
`echo "dwc2" >> /etc/modules`
1. Check that we can load the libcomposite module
`modprobe libcomposite`
1. Put a copy of this repository in root's home directory using git
```
apt install -y git
git clone https://gitea.cs.ru.is/RU-V207/rover-pi.git
```
1. Go to Scripts and test if the `gadget.sh` runs without errors and hopefully a Serial port appers in Device Manager or equivalent
```
cd rover-pi/Scripts/
./gadget.sh
```
1. Install the systemd script
`cp serial-ether-gadget.service /lib/systemd/system/.`
1. Enable the getty (which connects a login terminal to the serial port)
```
systemctl enable serial-getty@ttyGS0.service
```
1. Reboot and see if the serial port appears
## Additional hints
Look for the SD card reader on the VM under "Devices > USB" and click on it  Iff you get errors, you may have to shut down the VM and set the USB version to 2.0 or 3.0
The Boot and Root partition should mount in Linux under /media/$USER/boot and /media/$USER/rootfs
Navigate to /media/$USER/boot and edit:
config.txt: add "dtoverlay=dwc2" at the end
cmdline.txt:  remove "init=/usr/lib/raspi-config/init_resize.sh" and add "modules-load=dwc2,g_serial" to the end.  Make sure everything is on one line!
Eject the card
Boot the pi with a keyboard and monitor attached
Login
sudo systemctl enable serial-getty@ttyGS0.service
sudo systemctl start serial-getty@ttyGS0.service
# connecting to the serial terminal
If using a Linux VM, you will need to pass the Pi USB connection there.  This is similar to the procedure for accessing an Arduino.
Start the Linux VM if you haven't already
Menu:  Devices > USB
Look for "Silicon Labs CP210x USB to UART Bridge" and click on it.  
Run "ls -l /dev/ttyU*" to check if port /dev/ttyUSB0 is found.  The Pi will most often be accessible over this port.
The user must be in the dialout group to access it.  Use the Linux command: id  -to check if you are in the group.  If not in the dialout group, use:
sudo usermod -a -G dialout <username>
You will use the screen app in Linux to access the Pi.   You can install it with:
sudo apt install screen
You should now gain access to the Pi with:
sudo screen /dev/ttyUSB0 115200
Press enter and you will be prompted to log in to the Pi with default credentials. User: pi - PW: raspberry