Instructions for setting up a Raspberry Pi Zero for running OpenOCD specifically for the purpose of disabling Access Port Protection on nrf52 boards.
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 ..
wget https://gist.github.com/tstellanova/7d193fb8144d703e474ed37a371adfd0/raw/3f7b2d3061b3aece697f47c69763a8b89955db5d/swd-pi0.ocd
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.