-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
Description
In the PR #20965 @mguetschow raised the question whether the fix could be implemented for OpenOCD as well and I discovered that using OpenOCD with RIOT on the nRF52 platform seems to have two issues:
- OpenOCD does not work with the J-Link embedded in the nRF52840 DK (I have PCA10056 Rev 3.0.1 from 2023 here, older versions might work).
PROGRAMMER=openocd BOARD=nrf52840dk make -C examples/hello-world flash
...
### Flashing Target ###
Open On-Chip Debugger 0.11.0
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
swd
Error: No J-Link device found.
To get some more debug information, you can add change line 340 of dist/tools/openocd/openocd.sh
:
- sh -c "${OPENOCD} \
+ sh -c "${OPENOCD} -d3 \
This yields the following:
...
Debug: 87 2 target.c:1639 handle_target_init_command(): Initializing targets...
Debug: 88 2 semihosting_common.c:99 semihosting_common_init():
Debug: 89 2 jlink.c:647 jlink_init(): Using libjaylink 0.2.0 (compiled with 0.2.0).
Debug: 90 12 jlink.c:526 jaylink_log_handler(): Found 0 USB device(s).
Error: 91 12 jlink.c:683 jlink_init(): No J-Link device found.
Debug: 92 12 command.c:628 run_command(): Command 'init' failed with error code -100
User : 93 12 command.c:694 command_run_line():
...
The latest libjaylink recognizes the following Product IDs (https://gitlab.zapb.de/libjaylink/libjaylink/-/blob/master/libjaylink/discovery_usb.c?ref_type=heads#L50-L71 https://gitlab.zapb.de/libjaylink/libjaylink/-/blob/master/contrib/60-libjaylink.rules?ref_type=heads#L23-L45), and Segger J-Link firmware from "2024 Oct 9 11:01" uses 1060:
chris@ThinkPias:~$ lsusb
...
Bus 003 Device 005: ID 1366:1060 SEGGER J-Link
...
I will write Mark Schink, who is the (only?) developer of libjaylink an E-Mail and ask him to add the nRF52840DK product ID to the library. HOWEVER only when OpenOCD adopts the new libjaylink and pushes a new release this issue will be fixed.
So we'll probably have to add a remark to the documentation that the nRF52840 DK is not supported by OpenOCD for the time being.
Using a different programmer than the builtin J-Link (such as an J-Link EDU, which is supported by libjaylink v0.2.0) does work, but it brings us to the next issue:
- The OpenOCD script from RIOT does not unlock the APPROTECT register of the nRF52 chip. This is an issue that was not present on older silicon revisions, because the protection mechanism was a lot less strict (similarly to how the reset used to work differently).
On newer revisions, the chip is always protected after a reset or power cycle after programming and can not be read out or written to. The only way to write to the chip is to unprotect it, which the current OpenOCD script does not do.
OpenOCD provides a command for it, called nrf52_recover
... HOWEVER it has to be called after the initialization and before any other commands trying to access it. Looking at the script in dist/tools/openocd/openocd.sh
L339ff, we would have to define a new variable:
# flash device
sh -c "${OPENOCD} \
${OPENOCD_ADAPTER_INIT} \
-f '${OPENOCD_CONFIG}' \
${OPENOCD_EXTRA_INIT} \
${OPENOCD_EXTRA_RESET_INIT} \
-c 'tcl_port 0' \
-c 'telnet_port 0' \
-c 'gdb_port 0' \
-c 'init' \
+ ${OPENOCD_POST_INIT_CMDS} \
-c 'targets' \
${OPENOCD_CMD_RESET_HALT} \
${OPENOCD_PRE_FLASH_CMDS} \
-c 'flash write_image erase \"${IMAGE_FILE}\" ${IMAGE_OFFSET} ${IMAGE_TYPE}' \
${OPENOCD_PRE_VERIFY_CMDS} \
${OPENOCD_VERIFY} \
-c 'reset run' \
-c 'shutdown'" &&
And the boards/common/nrf52/Makefile.include
would get the following entry:
# Before writing to the nRF52 flash, the APPROTECT has to be disabled
# or in Nordic terms 'recovered'. This will delete the entire flash content and
# the UICR register.
OPENOCD_POST_INIT_CMDS = -c nrf52_recover
- A somewhat unrelated issue: the variables
OPENOCD_EXTRA_INIT
andOPENOCD_EXTRA_RESET_INIT
are not exported inmakefiles/tools/openocd.inc.mk
and therefore ignored when you try to use them in a Makefile.
Apparently at the moment nobody does that, so nobody noticed it.
Versions
OpenOCD v0.1.1
libjaylink v0.2.0
Tasks
- Get Product ID 1060 added to libjaylink
- Have OpenOCD update libjaylink in the next release (hopefully?)
-
Add an information in the documentation why the nRF52840 DK is not currently supported by OpenOCD. - Add the OPENOCD_POST_INIT variable to the build system.
- Add the unexported variables to the openocd.inc.mk file.
Currently I can't really tell how high of a priority this is for me and apparently nobody else uses OpenOCD to program nRF52 chips (because if someone did, it would've surfaced this issue 😅 )
I'd like to hear some feedback about the proposed fixes.