In this project, we'll use a Raspberry Pi as a cost-effective SWD (Serial Wire Debug) debugger and programmer for WIO Tracker 1110, where we can flash bootloader to the dev board. We'll utilize OpenOCD and `adafruit-nrfutil` to achieve this. This guide will walk you through the entire setup process, from wiring to programming your microcontroller using a hex file.
Prerequisites- Raspberry Pi Zero W
- WIO Tracker 1110
- Jump Wires: To connect the Raspberry Pi GPIO pins to the SWD interface of WIO Tracker 1110.
- Software: OpenOCD, `adafruit-nrfutil`, and necessary dependencies.
- Create a swap file:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
- Set the correct permissions for the swap file:
sudo chmod 600 /swapfile
- Set up the swap space:
sudo mkswap /swapfile
- Enable the swap space:
sudo swapon /swapfile
Step 1-2: Install Dependency- Update and Upgrade your Raspberry Pi
sudo apt update
sudo apt upgrade
- Install Dependencies
sudo apt install libtool libftdi-dev libusb-1.0-0-dev gcc-arm-none-eabi binutils-arm-none-eabi build-essential make libusb-dev libgpiod-dev
Note: This install steps will take sometime.
Step 1-3: Install OpenOCDOpenOCD is an open-source tool for programming and debugging ARM-based devices. Install it on your Raspberry Pi using the following steps:
- Download and Install OpenOCD
sudo apt install openocd
Step 1-4: Setup Python EnvironmentTo set up a Python environment and activate it, you can follow these steps:
- Create a virtual environment:
python3 -m venv adafruit-nrfutil
- Activate the virtual environment:
source adafruit-nrfutil/bin/activate
Step 1-5: Python Denpendency`adafruit-nrfutil` is used for handling Nordic Semiconductor’s nRF series of Bluetooth Low Energy (BLE) modules.
- Install the adafruit nrf utility
pip install adafruit-nrfutil
- To install the Intelhex package
pip install Intelhex
Stage 2: Build WIO Tracker WM1110 BootloaderStep 2-1: Clone the Adafruit nRF52 Bootloader repo- Clone the Milk-V-M/Adafruit_nRF52_Bootloader github repo
git clone https://github.com/Milk-V-M/Adafruit_nRF52_Bootloader.git
cd Adafruit_nRF52_Bootloader
git submodule update --init
Step 2-2: Make the Bootloader Firmware- Then build it with
make BOARD={board} all:
make BOARD=wio_tracker_wm1110 all
now you should get a folder with the bootloader firmware in _build folder like:
cd ~/Adafruit_nRF52_Bootloader/_build/build-wio_tracker_wm1110
Stage 3: FlashingStep 3-1: Wiring ConnectionsFirst, connect the GPIO pins of the Raspberry Pi to the SWD pins of the microcontroller. Here is the wiring diagram:
Ensure that the power supply voltage levels are compatible between the Raspberry Pi and the microcontroller.
Step 3-2: Configure OpenOCD for SWDCreate a custom configuration file for OpenOCD to use the Raspberry Pi as an SWD adapter:
- Create the Configuration File
sudo nano /etc/openocd/custom_swd.cfg
- Add the following configuration
adapter driver bcm2835gpio
bcm2835gpio peripheral_base 0x3F000000
bcm2835gpio speed_coeffs 146203 36
adapter gpio swclk 11
adapter gpio swdio 25
adapter gpio srst 24
transport select swd
adapter speed 100
set WORKAREASIZE 0x4000
source [find /usr/share/openocd/scripts/target/nrf52.cfg]
Step 3-3: Running OpenOCD- Run OpenOCD with the custom configuration file:
sudo openocd -f /etc/openocd/custom_swd.cfg
If everything is set up correctly, OpenOCD should initialize and connect to your target microcontroller using SWD.
Step 3-4: Upload Firmware via OpenOCD- Use the following command to upload the firmware to the microcontroller:
cd ~/Adafruit_nRF52_Bootloader/_build/build-wio_tracker_wm1110
sudo openocd -f /etc/openocd/custom_swd.cfg -c "program your_firmware.hex verify reset exit 0"
Replace `your_firmware.hex` with the path to your extracted firmware file.
- For my case eg:
sudo openocd -f /etc/openocd/custom_swd.cfg -c "program wio_tracker_wm1110_bootloader-366a3b9-dirty.hex verify reset exit 0"
(Optinal)Step 3-5: Using GDB for DebuggingYou can use `arm-none-eabi-gdb` or another GDB client to connect to OpenOCD and debug your application:
arm-none-eabi-gdb your_program.elf
(gdb) target remote localhost:3333
This will connect GDB to OpenOCD, allowing you to set breakpoints, step through code, and inspect memory and registers.
Troubleshooting- Connection Errors: Double-check wiring and ensure GPIO pins are correctly mapped.
- Permission Issues: Ensure you run OpenOCD with `sudo` to access GPIO pins.
- Compatibility: Verify the target configuration file matches your microcontroller.
Using a Raspberry Pi as an SWD debugger and programmer provides a powerful and flexible solution for embedded development, offering both debugging capabilities and ease of firmware management for nRF52 microcontrollers. With OpenOCD and `adafruit-nrfutil`, you have a robust setup for your development needs.
Feel free to ask questions or share your experiences in the comments below!
Reference:https://linuxjedi.co.uk/2020/12/01/programming-xilinx-jtag-from-a-raspberry-pi/
Comments