Skip to content

Instantly share code, notes, and snippets.

@fishi0x01
Forked from tstellanova/setup_rpi_openocd.md
Created February 13, 2021 09:36
Show Gist options
  • Save fishi0x01/ba96c3f8eda6882ebd2f5ae7dfee0ade to your computer and use it in GitHub Desktop.
Save fishi0x01/ba96c3f8eda6882ebd2f5ae7dfee0ade to your computer and use it in GitHub Desktop.
Setup rpi0 with openocd for flashing nrf52

Instructions for setting up a Raspberry Pi Zero for running OpenOCD specifically for the purpose of disabling Access Port Protection on nrf52 boards.

Build OpenOCD with CMSIS-DAP and GPIO support

The below script builds natively on the RPi0, and will take a very long time.

sudo apt install wget git autoconf libtool make pkg-config libusb-1.0-0 libusb-1.0-0-dev libhidapi-dev libftdi-dev libhidapi-dev telnet
git clone https://github.com/ntfreak/openocd
cd openocd
./bootstrap
./configure --enable-sysfsgpio --enable-bcm2835gpio --enable-cmsis-dap
make
cd ..

Download the OpenOCD script

wget https://gist.github.com/tstellanova/7d193fb8144d703e474ed37a371adfd0/raw/3f7b2d3061b3aece697f47c69763a8b89955db5d/swd-pi0.ocd

Disabling Access Port Protection on nrf52

In a first terminal, start openocd:

sudo ./openocd/src/openocd \
   -s ./openocd/tcl \
   -d4 \
   -f swd-pi0.ocd

In a second terminal, enter:

telnet localhost 4444
nrf52.dap apreg 1 0x0c

This queries the Access Port Protection status (in APPROTECTSTATUS register). It should show 0, meaning protection is enabled.

Now erase the flash on the nrf52 by setting the ERASEALL register to 1:

nrf52.dap apreg 1 0x04 0x01
nrf52.dap apreg 1 0x04

This should show 1, which means that the nRF52 is ready to be erased. Remove power from the nrf52 board.
This can be done by eg removing the +3.3 VDC power jumper between the rpi and the nrf52. Kill the openocd process on RPi. Reconnect power to the nrf52 board.

Restart openocd in a first terminal:

sudo ./openocd/src/openocd \
   -s ./openocd/tcl \
   -d4 \
   -f swd-pi0.ocd

In a second terminal:

telnet localhost 4444
targets
halt
nrf52.dap apreg 1 0x0c

This queries the status of Access Port Protection (in APPROTECTSTATUS register). It should be 1, meaning protection is disabled. You may now disconnect the nRF52 board from the RPi and use ST-Link to flash and debug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment