Compare commits

..

20 Commits

Author SHA1 Message Date
foley 39c0f158fe switch to logger (more POSIX) 2026-07-02 11:04:19 +00:00
foley 9f51718235 test if running as root 2026-07-02 10:39:13 +00:00
foley 01b3a189ed enable/disable gettys as needed 2026-07-02 10:34:26 +00:00
foley 3ef2ca02a6 test if MyGadget already exists 2026-07-02 10:29:54 +00:00
foley cc0b37af8b Merge branch 'master' of https://gitea.cs.ru.is/RU-V207/rover-pi 2026-07-02 10:08:56 +00:00
foley 17c0ffab67 ECM was not breaking network 2026-07-02 10:08:47 +00:00
foley 29fb570ae5 Adding module detect functionality 2026-07-02 10:05:09 +00:00
foley b667926315 API link 2026-07-02 09:49:02 +00:00
foley 1522244561 Started file on making initial connection 2026-07-02 09:47:30 +00:00
foley ca46da2501 imager 2026-07-02 09:45:23 +00:00
foley aebf3aaab8 Formatted code indentation to match sequence 2026-07-02 09:37:25 +00:00
foley f8dac3c07f Testing code formatting 2026-07-02 09:33:56 +00:00
foley c6f1642209 Ignore generated html files 2026-07-02 09:33:44 +00:00
foley 37f5100dac TODO: test Imager manifest modified for otg 2026-06-30 17:57:17 +00:00
foley 60284fdbd7 uninstallation help 2026-06-29 16:59:58 +00:00
foley 1eb4e23862 Official Raspi OTG whitepaper 2026-06-29 00:38:48 +00:00
foley 39aeb47d0e disabling ethernet 2026-06-29 00:35:34 +00:00
foley 6a70ef94d1 more cleaning up 2026-06-29 00:22:16 +00:00
foley b7a327904e format and info on serial terminal 2026-06-29 00:19:35 +00:00
foley 2595422ec2 dwc2 needs to match 2026-06-29 00:09:46 +00:00
6 changed files with 158 additions and 40 deletions
+1
View File
@@ -225,3 +225,4 @@ cython_debug/
# PyPI configuration file
.pypirc
*.html
+1
View File
@@ -21,6 +21,7 @@ Scripts and documentation for setting up a Raspberry Pi (Zero W 1 or 2) for use
- [Turning your Raspberry Pi Zero into a USB Gadget](https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/serial-gadget) Official Adafruit guide to gadget mode to allow the USB port to be used as a serial console to connect to it. We had trouble getting it to work in 2026 due to some changes in Raspberry Pi OS.
- [Python gpiozero library](https://gpiozero.readthedocs.io/en/stable/) a simple interface for working with GPIO devices
- [CircuitPython Libraries on Linux and Raspberry Pi](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi) This is one option for GPIO, I2C, SPI, and PWM access in python.
- [Comparing Rasbperry Pi python interfaces](https://raspberry.tips/en/raspberrypi-tutorials/raspberry-pi-gpio-python)
## For the Future
- [Setting up Zeroconf to use local names (.local) to access your Pi](https://learn.adafruit.com/bonjour-zeroconf-networking-for-windows-and-linux): this does not work on iot-research because multicast and MDNS are broken.
+33 -3
View File
@@ -2,10 +2,34 @@
# Adapted from
# "Using OTG mode on Raspberry Pi SBCs"
# https://pip-assets.raspberrypi.com/categories/685-app-notes-guides-whitepapers/documents/RP-009276-WP/Using-OTG-mode-on-Raspberry-Pi-SBCs
sudo modprobe libcomposite
# This script only works if you are running it as root
if [ $(id -u) -ne 0 ]; then
echo "Please run as root or use sudo"
exit 1
fi
# Is the dwc2 overlay installed? Nothing will work if it isn't.
if lsmod | grep -q "dwc2"; then
echo "DWC2 module loaded, setting up OTG gadget serial and ethernet" | logger -t $0 -p info
echo "DWC2 module loaded"
else
echo "DWC2 module missing. Aborting OTG setup." | logger -t $0 -p info
echo "DWC2 module missing"
systemctl disable serial-getty@ttyGS0.service
exit 1
fi
modprobe libcomposite
cd /sys/kernel/config/usb_gadget
if [ -d "MyGadget" ]; then
echo "MyGadget already exists. Aborting OTG setup." | logger -t $0 -p info
exit 1
fi
# Create our new gadget
sudo mkdir MyGadget
mkdir MyGadget
cd MyGadget
# Add some boilerplate data that the gadget needs
echo 0x1d6b > idVendor # Linux Foundations USB VID. Replace with your own VID if required
@@ -30,6 +54,7 @@ echo "Config 1" > configs/c.1/strings/0x409/configuration
### Remember to create the storage block device first
## cd /root
### Example: Make a 256 MB file to act as "USB stick"
## Only create it once or it will delete the previous content
## sudo dd if=/dev/zero of=drive.bin bs=1M count=256
### Create a VFAT file system on the backing store
## sudo mkfs.vfat drive.bin
@@ -45,9 +70,14 @@ mkdir functions/acm.usb0
ln -s functions/acm.usb0 configs/c.1/
## Ethernet (RNDIS and ECM)
## Of note, this may cause the IP to show up as 127.0.0.1
## and wireless to suddenly fail to connect
mkdir functions/ecm.usb0
ln -s functions/ecm.usb0 configs/c.1/
## USB Device Controller update
UDC=`ls /sys/class/udc`
echo $UDC > UDC
echo $UDC > UDC
# Enable the gettys
systemctl enable serial-getty@ttyGS0.service
+10
View File
@@ -0,0 +1,10 @@
# Initial connection to the Raspberry Pi
Now that you have an image with ssh and Raspberry Pi connect on your SD, you can connect to the pi
## Connecting using Raspberry Pi Connect
1. Login to https://connect.raspberrypi.com if you haven't already
1. Look at the list of devices, find the one you want, and click on "Connect"
![connect-devices.png](Graphics/connect-devices.png)
1. A remote shell window will pop up
![connect-shell.png](Graphics/connect-shell.png)
+111 -28
View File
@@ -1,36 +1,119 @@
# 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:
The author discovered the guide was out of date, consulted the [2025 Raspberry Pi whitepaper on OTG](https://pip-assets.raspberrypi.com/categories/685-app-notes-guides-whitepapers/documents/RP-009276-WP/Using-OTG-mode-on-Raspberry-Pi-SBCs), found additional errors, and was finally able to correct them thanks to this blog https://www.isticktoit.net/?p=1383
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
## Important :
- **Using the Gadget Mode will make the usb port unable to connect to a mouse or keyboard!!!** You will need to disable it make them work again.
- You will want to disable the serial console on RX/TX in `raspi-config` if you are trying to use that serial port for communicating with other devices.
## Procedure
# 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
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 via ssh or Raspberry Pi Connect
1. Become root (the superuser)
```
sudo su
```
1. Enable the DWC version 2 USB peripheral device tree overlay by appending it to the end after the `[all]` category (which is at the end)
```
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
```
cp serial-ether-gadget.service /lib/systemd/system/.
```
1. Enable the service
```
systemctl enable serial-ether-gadget.service
```
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
# Connecting to the serial terminal
You will need a serial terminal program (which used to be very very common) in order to communicate over your serial port
## Windows
- Putty: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
- MobaXterm: https://mobaxterm.mobatek.net/
## Linux
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.
1. Start the Linux VM if you haven't already
1. Menu: Devices > USB
1. Look for "Silicon Labs CP210x USB to UART Bridge" and click on it.
1. check if port /dev/ttyUSB0 is found. The Pi will most often be accessible over this port.
```
ls -l /dev/ttyU*
```
1. The user must be in the dialout group to access it. Use the Linux command: `uid` -to check if you are in the group. If not in the dialout group, use:
```
sudo usermod -a -G dialout <username>
```
1. You will use the GNU `screen` application in Linux to access the Pi. You can install it with:
```
sudo apt install screen
```
1. You should now gain access to the Pi with:
```
` sudo screen /dev/ttyUSB0 115200
```
1. Press enter and you will be prompted to log in to the Pi with default credentials. User: pi - PW: raspberry
# Disabling the OTG mode to use a keyboard and mouse again
**WARNING: Untested**
1. Comment out the dwg2 line in `/boot/firmware/config.txt` using a `#` in front of the line
```
[all]
#dtoverlay=dwg2
```
1. Comment out the dwg2 line in `/etc/modules` using a `#` in front of the line
```
#dwg2
```
1. Disable the gadget service
```
systemctl disable serial-ether-gadget.service
```
1. Disable the getty
```
systemctl disable serial-getty@ttyGS0.service
```
# Connecting over the USB ethernet to ssh into your Pi
I have not tested this procedure because the serial console and normal network connection is sufficient for me --foley
https://www.raspberrypi.com/news/usb-gadget-mode-in-raspberry-pi-os-ssh-over-usb/
# Untested
Here are items that bear investigating but haven't been tested yet
- [Modify Imager manifest to install otg](https://github.com/raspberrypi/rpi-imager/tree/main/doc/local_json)
+2 -9
View File
@@ -23,7 +23,7 @@ We don't have many monitors and keyboards in V207 for students to use, so we wil
![imager-ssh_nokey.png](Graphics/imager-ssh_nokey.png)
1. Once you have uploaded your `id_edcsa.pub` file it should look like this.
![imager-ssh_key.png](Graphics/imager-ssh_key.png)
1. Since we don't have enough monitors, we will need another way to setup the initial connection. The officially supported option is Raspberry Pi Connect. You will need to:
1. Since we don't have a monitor, keyboard, and mouse available, we will need another way to setup the initial connection. The officially supported option is Raspberry Pi Connect. You will need to enable it then:
1. Register for an account first at https://connect.raspberrypi.com
2. Confirm your account with email
2. Login with that account
@@ -42,12 +42,5 @@ We don't have many monitors and keyboards in V207 for students to use, so we wil
1. Now you get to watch the writing then verifying.
![imager-writeimage-progress.png](Graphics/imager-writeimage-progress.png)
![imager-writeimage-verifying.png](Graphics/imager-writeimage-verifying.png)
1. The process is complete!
1. The process is complete! Now you can [connect to your pi](connect.md)
![imager-complete.png](Graphics/imager-complete.png)
## Connecting using Raspberry Pi Connect
1. Login to https://connect.raspberrypi.com if you haven't already
1. Look at the list of devices, find the one you want, and click on "Connect"
![connect-devices.png](Graphics/connect-devices.png)
1. A remote shell window will pop up
![connect-shell.png](Graphics/connect-shell.png)