The OpenMower mainboard consists of two independent computers: The Raspberry Pi 4 as main computer and the Raspberry Pi Pico microcontroller as low level controller. The Pico basically handles all small real-time tasks (sensor communication, …), whereas the Raspberry Pi 4 does the heavy lifting (navigation, …).
The Raspberry Pi Pico can be programmed in multiple ways, but it would be nice if we could program it directly from the Raspberry Pi 4 or from an external development PC on the network.
This guide shows you how to flash code to the Pico from the Raspberry Pi 4 and debug it via network.
Please make sure, that you have an Ubuntu 20.04 system running on your Raspberry Pi. You can find the guide here: https://x-tech.online/2022/01/installing-ros-noetic-on-a-headless-raspberry-pi-4-with-ubuntu-20-04/
You don’t need to install ROS for this guide, but I recommend it, since we will be needing it anyways.
Installing the correct version of OpenOCD
OpenOCD is the interface which allows us to debug the Pico. Basically your IDE connects to OpenOCD, which then generates the physical signals to debug the target microcontroller. Currently, the latest OpenOCD version available in the package manager does not support the Raspberry Pi Pico and therefore we need to compile it from source. But no worries, this is quite easy. Just follow these steps:
# dependencies: git build-essential automake autoconf build-essential libtool libftdi-dev libusb-1.0-0-dev texinfo pkg-config rpi.gpio-common gdb-multiarch # First, install the dependencies sudo apt install git build-essential automake autoconf build-essential libtool libftdi-dev libusb-1.0-0-dev texinfo pkg-config rpi.gpio-common gdb-multiarch # Then, we get the OpenOCD sources, compile and install them on our system git clone https://github.com/raspberrypi/openocd.git --recursive --branch rp2040 --depth=1 cd openocd ./bootstrap ./configure --enable-sysfsgpio --enable-bcm2835gpio sudo make install
That’s all you need, you can now flash the Raspberry Pi Pico which is attached to the Raspberry Pi 4’s GPIO pins.
Flashing a Binary
If you’re using the OpenMower Mainboard, the Raspberry Pi’s power is controlled by the Pico. This means that as soon as the Pico gets reset, the Raspberry Pi 4 loses its power. Therefore, we need to run the following commands in order to force the power to stay on even during flashing:
# Only needed, if you use the OpenMower mainboard: sudo -i echo "10" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio10/direction echo "1" > /sys/class/gpio/gpio10/value exit
Then, you can flash a compiled firmware to the Pico using the following command:
openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg -c “program your_firmware.elf verify reset exit”
Starting OpenOCD for debugging
As before, if you are using the OpenMower Mainboard, run the following commands to force the power to stay on:
# Only needed, if you use the OpenMower mainboard: sudo -i echo "10" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio10/direction echo "1" > /sys/class/gpio/gpio10/value
This command starts OpenOCD in debugging mode:
openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg -c "bindto 0.0.0.0"
You can then connect to the interface using any IDE with GDB debugging capabilities. I’m currently using PlatformIO in VS Code for this and it works like a charm.