I am using OpenOCD with an ST-Link V2 USB dongle for a while now to
flash nRF51 modules. The latest released version of OpenOCD is
0.10.0 (as of March 2020). Even if this version is already some years
old, it was tagged on Jan 22 2017, it runs very well and does a
perfect job for a wide range of microcontrollers with support for many
programmers.
As an example, here is a typical session to flash a nRF51822
module. The hex file is from the eddystone beacon example of Nordics SDK
12.3.0,
build on Linux/Debian Buster (10.3).
For newer nRF52832 modules an OpenOCD target target/nrf52.cfg is
also defined. But if you try to flash with it, you will get an error
message which does not directly describe what went wrong. Neither
program {hexfile} nor flash write_image {hexfiles} works:
So then what can be done? As it turns out, OpenOCD has developed
further and further in recent years. There is an active community with
many contributors and, of course, also the nRF52 support improved a
lot! It’s just that no official release has been made for a long time.
We have to build a more current version by ourselves…
Building OpenOCD
The first thing to do after cloning the git repository is to build an
autotools configure file. This will be done with the bootstrap
script located in the root folder of the repo. The usual configure;
make; make install runs smooth with dev-packages from debian. For
“cmsis-dap” support i had to install some usb libraries:
OpenJTAG requires:
After installing all dependent dev-packages, building should look
similar to the following output. To get a full-featured OpenOCD the
output of the “OpenOCD configuration summary” should tell “yes” in
every line.
The last step “make install” creates /opt/openocd/bin/openocd.
Running OpenOCD with nRF52 target
OpenOCD typically runs as a program in one terminal,
and you interact with it via telnet on port 4444 from a second
terminal. One neat trick to avoid the additional telnet session is to
add -c "telnet_port pipe" to your command line. This will pipe stdin into
openocd instead of opening the telnet port. With this you can easily run your
session in just one terminal.
Some things have changed with the Dec 2019 version. The ST-Link
interface configuration file for example has been renamed.
WARNING: interface/stlink-v2.cfg is deprecated, please switch to interface/stlink.cfg
To erase all flash contents of the chip call nrf5
mass_erase. This is not a typo. “nrf5”, not “nrf52”. But don’t
get confused: For nRF51 you still have to use nrf51 mass_erase.
Finally, flashing a nrf52832_xxaa.hex firmware using a ST-Link V2 USB
dongle is very similar to the nRF51822 procedure:
OpenOCD, the “Open On-Chip Debugger”, is more than a tool to flash
firmware. I also like to use it for debugging via GDB and for
collecting diagnostic output via RTT. But that are topics for another day.