Flashing NRF5 MCUs without JLink
How to flash a cheap NRF5 MCU board without an expensive Segger/JLink programmer
Frugal NRF5 flashing
I got inspired by PineTime open source smartwatch (which I do not yet have) to play with Nordic NRF5 Bluetooth Low Energy MCUs again.
Since I am frugal, I only have various NRF51 and NRF52 boards and modules that do not come with Segger J-Link programmers included. And stand-alone JLink programmer tend to cost more than my 10€ limit I tend to like to keep my purchases under. So what other programmers are usable for flashing and debugging NRF5 based devices? Turns out quite many.
Cheap (≈$2) Chinese ST-Link v2 clones are probably the default choice of a hobbyist for flashing and debugging STM8, STM32 and NRF5 microcontrollers.
OpenOCD (Open On-Chip Debugger) is the the software you need. Unfortunately the last officially released version of OpenOCD is quite ancient, so you have to use latest Git version to flash and debug NRF52 devices.
Command to flash an image look something like:
openocd -f interface/stlink-v2.cfg -c 'transport select hla_swd' -f target/nrf52.cfg -c 'program _build/nrf52832_xxaa.hex verify reset; shutdown;'
To erase the flash (e.g. before flashing Softdevice):
openocd -f interface/stlink-v2.cfg -c 'transport select hla_swd;' -f target/nrf52.cfg -c 'init_reset halt; init; halt; nrf51 mass_erase; reset; exit'
One annoying limitation of these programmers is that if your NRF52 device is flash locked (“control access port protection” enabled), it is not possible to unlock with ST-Linkv2. Unfortunately some NRF52 based devices (PineTime?) and even development boards (Ebyte E73) are delived locked. See “Flash locked” section below.
Black Magic Probe
Black Magic Probe made of “Blackpill”
Black Magic Probe is an interesting open source programmer/debugger. With BMP there is no need to use OpenOCD server, as it runs a GDB server on chip directly.
You can buy one (not cheap), but since it is open source, it is possible to build one self, using either those cheap ST-Link devices or really cheap STM32F103 (<$2) development boards, usually called Blue Pill (or variation of it, “BlackPill”, like in image) as a hardware platform.
I had some issues flashing my extra ST-Links, so I opted to use BluePill/BlackPill (Blackpill should have more reliable USB).
Use these instructions: https://buger.dread.cz/black-magic-probe-bmp-on-bluepill-stm32f103c8-minimum-system-development-board.html
To flash NRF52 with BMP using embedded ARM GDB:
arm-none-eabi-gdb -nx --batch -ex "target extended-remote /dev/ttyACM0" -ex "monitor swdp_scan" -ex "attach 1" -ex "load" -ex "compare-sections" -ex "kill" _build/nrf52832_xxaa.hex
arm-none-eabi-gdb -nx --batch -ex "target extended-remote /dev/ttyACM0" -ex "monitor swdp_scan" -ex "attach 1" -ex "monitor erase_mass" -ex "kill"
These commands got stuck sometimes, and I had to retry.
CJMU-232H FT232H board
FT232H is a general purpose USB to serial+GPIO+SPI+I2C converter. And it is possible to use it as SWD programmer. Wiring is bit annoying, since FT232H needs to use separate in/out pins for SWDIO. Instructions suggest putting a small resistor (I used 470Ω) to one of them.
AD0 <-> SWDCLK
AD1 <-> SWDIO
AD2 <-> 470ohm resistor <-> SWDIO
Flash NRF5 with openocd:
openocd -f board/nordic_nrf52_ftx232.cfg -c 'program _build/nrf52832_xxaa.hex verify reset; shutdown;
openocd -f board/nordic_nrf52_ftx232.cfg -c 'init; halt; nrf51 mass_erase; reset; exit'
Over 10€ limit, but “everybody” has at least one of the models of Raspberry Pi single board computer. And it’s GPIO ports can be used as a SWD programmer with openocd.
I used Raspberry Pi Zero W and it worked fine to resolve the locked flash issue.
One article https://email@example.com/coding-nrf52-with-rust-and-apache-mynewt-on-visual-studio-code-9521bcba6004 (section “Remove nRF52 Flash Protection With Raspberry Pi”)
This is an option I have not yet tried, but later will. There are version of this programmer available under $10, but it should be possible to flash CMSIS-DAP firmware to a ST-Link or a Bluepill similarly as Black Magic Probe above.
- on a cheap ST-LINK V2 mini adapter: https://mvdlande.wordpress.com/2015/10/05/cmsis-dap-on-a-cheap-st-link-v2-mini-adapter/
- STM32 (bluepill): https://github.com/mick909/stm32-dap
Flash locked? (“control access port protection”)
As mentioned above, your flash may be locked. So, if flashing your new device fails, you may try
using OpenOCD to check if access protection is enabled by executing the command:
dap apreg 1 0x0c
This command reads the register at address 0x0c in the access port at index 1 (the control access port’s index). If it returns 0x0 then access port protection is enabled.
Unfortunately ST-Linkv2 is not able to do that, so from our programmer selection, you have to use either Raspberry Pi or FT232H programmers (or maybe CMSIS-DAP?). For Raspberry Pi solution, see the article linked above.
Unlocking command would be something like:
openocd -f interface/raspberrypi-native.cfg -c "transport select swd" -f target/nrf52.cfg -c "init; nrf52.dap apreg 1 0x0c; nrf52.dap apreg 1 0x04 0x01; reset; nrf5 mass_erase; reset; shutdown;"