Is it possible to upload to nrf52832 w/ Stlink v2?

chip: nrf52832
board: E104-BT5032A-TB (
framework: Arduino
IDE: Platformio
upload: Stlink v2

and tried simple blink to upload w/ STlink v2 in Platformio:

#include <Arduino.h>

void setup()
  // initialize LED digital pin as an output.

void loop()
  // turn the LED on (HIGH is the voltage level)
  digitalWrite(LED_BUILTIN, HIGH);
  // wait for a second
  // turn the LED off by making the voltage LOW
  digitalWrite(LED_BUILTIN, LOW);
  // wait for a second

w/ platformio.ini file:

platform = nordicnrf52
board = nrf52_dk
board_build.variant = generic
framework = arduino
upload_protocol = stlink

but end w/ error:

> Executing task in folder nrf-blink: C:\Users\max\.platformio\penv\Scripts\platformio.exe run --target upload <

Processing nrf52_dk (platform: nordicnrf52; board: nrf52_dk; framework: arduino)

Verbose mode can be enabled via `-v, --verbose` option
PLATFORM: Nordic nRF52 (4.4.1) > Nordic nRF52-DK
HARDWARE: NRF52832 64MHz, 64KB RAM, 512KB Flash
DEBUG: Current (jlink) On-board (cmsis-dap, jlink) External (blackmagic, stlink)
- framework-arduinonordicnrf5 1.600.190830 (6.0)
- tool-bossac-nordicnrf52 1.10866.191129 (1.8.66)
- tool-jlink 1.65200.0 (6.52.0)
- tool-openocd 2.1000.200630 (10.0)
- tool-sreccat 1.164.0 (1.64)
- toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> ..
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 2 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio\build\nrf52_dk\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM: [ ] 0.2% (used 140 bytes from 65536 bytes)
Flash: [ ] 0.5% (used 2536 bytes from 524288 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, jlink, mbed, nrfjprog, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\nrf52_dk\firmware.hex
xPack OpenOCD, x86_64 Open On-Chip Debugger 0.10.0+dev-00378-ge5be992df (2020-06-26-09:29)
Licensed under GNU GPL v2
For bug reports, read
debug_level: 1


nRF52 device has a CTRL-AP dedicated to recover the device from AP lock.
A high level adapter (like a ST-Link) you are currently using cannot access
the CTRL-AP so 'nrf52_recover' command will not work.
Do not enable UICR APPROTECT.

Error: timed out while waiting for target halted
embedded:startup.tcl:521: Error: ** Unable to reset target **
in procedure 'program'
in procedure 'program_error' called at file "embedded:startup.tcl", line 558
at file "embedded:startup.tcl", line 521
*** [upload] Error 1
============================================================ [FAILED] Took 2.21 seconds ============================================================
The terminal process "C:\Users\max\.platformio\penv\Scripts\platformio.exe 'run', '--target', 'upload'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

The access port protection bit has been set in the microcontroller and an STLink cannot erase the device to unset it. See explanation and solutions at noridc semiconductor forum for explanation: Flashing nRF5832 using only ST-LINK V2 and OpenOCD - Nordic Q&A - Nordic DevZone - Nordic DevZone and STLINK-V2 -> ChipID = 0 / timed out while waiting for target halted - Nordic Q&A - Nordic DevZone - Nordic DevZone.

understand, how i can run openocd under Platformio in Windows 10?

You can use the one that PlatformIO uses itself: It is installed in C:\Users\<User>\.platformio\packages\tool-openocd. Just cd in there with a terminal use the bin\openocd.exe binary and the scripts folder for the configs.

I/m sorry for my noobs questions )

I was able to run gdb server:

.platformio\packages\tool-openocd> .\bin\openocd.exe -f interface/stlink-dap.cfg -f target/nrf52.cfg

xPack OpenOCD, x86_64 Open On-Chip Debugger 0.10.0+dev-00378-ge5be992df (2020-06-26-09:29)
Licensed under GNU GPL v2
For bug reports, read
OpenOCD: Bug Reporting
Info : auto-selecting first available session transport “dapdirect_swd”. To override use ‘transport select ’.
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : STLINK V2J37S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.180196
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : stlink_dap_op_connect(connect)
Info : SWD DPIDR 0x2ba01477
Error: Could not find MEM-AP to control the core
Info : starting gdb server for nrf52.cpu on 3333
Info : Listening on port 3333 for gdb connections

According to provideed links from Nordic devzone, now I must somehow connect to this gdb server and run command:

>dap apreg 1 0x04 0x01

Yes, my next question, how I can connect to this GDB server )

Eh that already seems pretty bad.

I’m not 100% sure of the top of my head, but at the end of the openocd invocation you can just chain additional commands using -c "my command" switches. So try with -c "dap apreg 1 0x04 0x01" -c "shutdown" at the end? (might need a init; halt; command too?)

The commands available through -c might be different from the what’s available on the telnet server though. As you can see in

openocd is accepting telnet connections on (localhost) port 4444. So just open another cmd window and use telnet 4444 to connect to it. help and other commands are your friends once connected.

If you don’t have telnet available on your machine, using ncat with ncat 4444.

was able to connect w/ Putty as telnet

Open On-Chip Debugger
> dap init
SWD DPIDR 0x2ba01477
> dap apreg 1 0x04 0x01
  adapter assert |deassert [srst|trst [assert|deassert srst|trst]]
  adapter deassert |assert [srst|trst [deassert|assert srst|trst]]
  adapter driver driver_name
  adapter list
  adapter name
  adapter speed [khz]
  adapter srst
    adapter srst delay [milliseconds]
    adapter srst pulse_width [milliseconds]
  adapter transports transport ...
  adapter usb
    adapter usb location [<bus>-port[.port]...]
  dap create name '-chain-position' name
  dap info [ap_num]
  dap init
  dap names
  nrf52.dap apcsw [value [mask]]
  nrf52.dap apid [ap_num]
  nrf52.dap apreg ap_num reg [value]
  nrf52.dap apsel [ap_num]
  nrf52.dap baseaddr [ap_num]
  nrf52.dap dpreg reg [value]
  nrf52.dap info [ap_num]
  nrf52.dap memaccess [cycles]
  nrf52.dap ti_be_32_quirks [enable]
reset_config [none|trst_only|srst_only|trst_and_srst]
          [srst_gates_jtag|srst_nogate] [trst_push_pull|trst_open_drain]
  st-link serial <serial_number>
  st-link vid_pid (vid pid)+
  swd newdap
invalid subcommand "apreg 1 0x04 0x01"

> dap names
> nrf52.dap
  nrf52.dap apcsw [value [mask]]
  nrf52.dap apid [ap_num]
  nrf52.dap apreg ap_num reg [value]
  nrf52.dap apsel [ap_num]
  nrf52.dap baseaddr [ap_num]
  nrf52.dap dpreg reg [value]
  nrf52.dap info [ap_num]
  nrf52.dap memaccess [cycles]
  nrf52.dap ti_be_32_quirks [enable]
nrf52.dap: command requires more arguments
> nrf52.dap info
> nrf52.dap apreg 1 0x04 0x01

but after disconnect/restart see the some error while try to upload firmware

PS: found issue in Nordic devzone, but it doesnt work: How do I disable control access port protection on nRF52 using openOCD? - Nordic Q&A - Nordic DevZone - Nordic DevZone

“Some” error or the “Same” error?


Error: Could not find MEM-AP to control the core

I’m afraid this is a STLink or OpenOCD related problem then. You might get better help by opening a new topic on the nRF forums (or OpenOCD - Open On-Chip Debugger / Tickets).

But, given the board link you’ve given, it shows that it has a Micro-USB port and with a USB-Serial chip. Isn’t the chip supposed to be programmable through that? Is there no documentation available on how to program it with e.g. Arduino over the serial port? Maybe the UART bootloader on this chip allows erasing very simply, with something like nrfutil?

For example, using the platformio.ini of

platform = nordicnrf52
board = adafruit_feather_nrf52832
framework = arduino
upload_protocol = nrfutil

might work.

I’ve also found information on how to use a Bluepill (STM32F103C8 board) and the Black Magic Probe firmware, which are reportedly able to erase and unlock the chip:

Thank for Your help )

board has USB just for serial communication, not for upload
Arduino IDE approach also doesnt work

I will try approach w/ “blue pill” (after get it) from citated forum (

For now I’m not prepared to invest 300-800EUR for Nordic J-Link as they propose. especially after there community support.

BTW, i moved to Platformio from Arduino IDE and very satisfied

Fun side note: An STLink can be converted to a JLink by changing its firmware. element14 Community. So a cheap 5€ STLink clone might actually become a full JLink with better capabilities. Then, upload_protocol = nrfjprog might also be directly usable?

Hm weird design decision by the board makers. Do they just preload the board with some AT firmware for bluetooth connectivity and that’s supposed to be unchangeable then?

Thanks for the feedback, we hope you get around the access port lock on your specific chip so that PlatformIO can be used normally :slight_smile:

i using the NRF52 Bluefruit with ST-LINK V2 and openocd
i can burn successfully
it´s a ble application
the problem is that not merging the SOFTDEVICE with my APP.
Then i can´t execute BLE application.
To make work, i have to BURN the SOFTDEVICE out the box
Can you help me ?

Sounds like a problem with the builder script

got the same problem on University

Sorry the post was cut off due to a bad connection probably, I meant, this looks like a builder script issue in PlatformIO, please open an issue at Issues · platformio/platform-nordicnrf52 · GitHub.