Problem Flashing nrf52 chip using Openocd

9k Views Asked by At

I have a custom nrf52 chip on a pcb with swd pins exposed. I have cloned and installed the latest openocd from https://github.com/ntfreak/openocd. The latest version includes all the latest pathes for the nrf52 chip, so no need for any additional changes as suggested in many older guides online. I am able to connect to the chip using ST-LinkV2. when connected I can read and write memory locations using mdw and mdb. I can also run some basic openocd commands like dump_image e.t.c, which confirms that the setup is good. But halt and program commannds always lead to errors like:

JTAG failure -4
JTAG failure -4
JTAG failure -4
JTAG failure -4
JTAG failure -4
JTAG failure -4
target halted due to debug-request, current mode: Thread 
xPSR: 00000000 pc: 00000000 msp: 00000000
jtag status contains invalid mode value - communication failure
Polling target nrf52.cpu failed, trying to reexamine
Examination failed, GDB will be halted. Polling again in 100ms
Previous state query failed, trying to reconnect
jtag status contains invalid mode value - communication failure
Polling target nrf52.cpu failed, trying to reexamine

if I try to use flash image_write I get the error,

JTAG failure
Error setting register
error starting target flash write algorithm
Failed to enable read-only operation
Failed to write to nrf52 flash
error writing to flash at address 0x00000000 at offset 0x00000000
in procedure 'dap'
jtag status contains invalid mode value - communication failure
Polling target nrf52.cpu failed, trying to reexamine

I have read different guides online, and one of the possible solutions involves the APPPROTECT register which has to be disabled to enable any writes to flash. APP_PROTECT, But the dap commmand which is supposed to help us access this bit,

dap apreg 1 0x04 0x01

returns an error:

invalid subcommand apreg 1 0x04 0x01

Please, I will like to know if anyone has had success programming a new empty nrf52 chip with the stlink-v2 and the steps which are necessary, or if any one has encountered similar problems. Thanks.

Here is my config file:

#nRF52832 Target
source [find interface/stlink.cfg]

transport select hla_swd

source [find target/nrf52.cfg]

#reset_config srst_nogate connect_assert_srst
4

There are 4 best solutions below

1
ched On BEST ANSWER

If anyone encounters this problem, I solved the problem by getting an original Jlink-Edu. I also had to pull the reset pin of the microcontroller high to get the jlink working.

1
Turbo J On

There are lots of JTAG messages.

I think you might be missing the

transport select hla_swd

line in your (board) cfg file. The NRF5x chips only work properly with SWD, and ST-Link uses the hla_swd variant.

1
alexisdm On

Even though the dap command is listed by openOCD help, it isn't implemented for transport hla_swd that you have to use with ST-Link.

If the ST-Link is a generic type from China, it can be upgraded to CMSIS-DAP which uses the swd transport and supports the nrf52.dap apreg 1 0x04 0x01 command to disable the readback protection and erase the flash. You'll need another ST-Link to do that, or you can instead install CMSIS-DAP on a generic STM32F103C8T6 board.

After that you can either use ST-Link to program the nRF52 or continue using CMSIS-DAP, which can also be used to program STM32 MCU.

Nucleo board embedded ST-Links can also be upgraded to J-Link, which allow the use of the "recover" option in nRFgo Studio to erase the flash, it should also work with "nrfjtool --recover" or OpenOCD.

3
matthew On

I am revising this answer to cover the later versions of the nRF52 chip. From build codes Gxx and later, chip protecion is on by default after each reset.

See https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/working-with-the-nrf52-series-improved-approtect for the extra code needed to be added to the program to prevent this from happening.

However if you only want to program the chip (and not debug via OpenOCD) then you can use the following steps. (Needs openocd V0.11.0 -- Binaries are available from https://github.com/xpack-dev-tools/openocd-xpack/releases/tag/v0.11.0-1/ )

bin\openocd -f interface/cmsis-dap.cfg -f target/nrf52.cfg -c "telnet_port pipe;tcl_port disabled;gdb_port disabled;log_output 
reset init
nrf52_recover
flash write_image s132_nrf52_2.0.1_softdevice.hex
flash write_image lp.hex
reset

This pipes the current cmd window input to openOCD and removes the chip protection, using the nrf52_recover method supplied in openOCD nrf52.cfg, and writes the softdevice and then writes the compiled program (lp.hex in this case).

(Nordic has a merge utility the can combine the softdevice and the program into one .hex file)

For those of you using Arduino, my Very Low Power BLE <20uA project automates this process in the Arduino sketch upload.

That project also has instructions for a simple CMSIS-DAP programmer based on a Raspberry Pi Pico RP2040