Upload_flags to unlock STM32F4

I’m developing for mikroE Clicker 2 for STM32 boards, these boards come with MCUs that are set to RDP level 1 (reversible readout protection). RDP must be disabled for uploading, or it will fail

** Programming Started **
Error: stm32x device protected
Error: failed erasing sectors 0 to 4
embedded:startup.tcl:521: Error: ** Programming Failed **
in procedure 'program' 
in procedure 'program_error' called at file "embedded:startup.tcl", line 586
at file "embedded:startup.tcl", line 521
*** [upload] Error 1

the unlock procedure is described in MikroE Clicker 2 for STM32 — Zephyr Project Documentation , but how can I make platformio do this automatically for me? I tried adding upload_flags to platformio.ini like

upload_flags = 
  -c "reset halt" 
  -c "stm32f4x unlock 0" 
  -c "reset run"
  -c shutdown

but this also fails:

Uploading .pio\build\Clicker2_for_STM32\firmware.elf
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
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

Unexpected command line argument: halt
*** [upload] Error 1

There must be an init; before any of the reset etc. commands. A stand-alone command that worked for me on an STM32F1xx was

openocd -f interface/cmsis-dap.cfg -f target/stm32f1x.cfg -c “init; reset halt; stm32f1x unlock 0; reset halt; exit”

Check the project task “Advanced → Verbose Upload” to see the produced invocation of OpenOCD and adjust accordingly to make it a valid command. Otherwise you’re flying blind.

Depending on the position where your commands are executed (before or after the flash command), you might have to use a custom upload command though.

1 Like

I’m using this command

C:\Users\username\.platformio\packages\tool-openocd>bin\openocd.exe -f scripts\interface\stlink.cfg -f scripts\target\stm32f4x.cfg -c init -c "reset halt" -c "stm32f4x unlock 0" -c "reset run" -c shutdown

to unlock my STM32F4s.

verbose upload outputs

openocd -d2 -s C:\Users\username\.platformio\packages\tool-openocd/scripts -f interface/stlink.cfg -c "transport select hla_swd" -f target/stm32f4x.cfg -c "program {.pio\build\Clicker2_for_STM32\firmware.elf}  verify reset; shutdown;"

upload_flags are appended at the start of the argument list. I’ve tried to add the commands but now I’m stuck at argument order issues. I think a dedicated unlock task is the easiest way to go.

Well if I do

[env:stm32f4]
board = black_f407ve
platform = ststm32
framework = arduino
upload_flags =
  -f
  interface/stlink.cfg
  -c
  transport select hla_swd
  -f
  target/stm32f4x.cfg
  -c
  init; reset halt; stm32f4x unlock 0; reset run

it does

openocd -f interface/stlink.cfg -c “transport select hla_swd” -f target/stm32f4x.cfg -c “init; reset halt; stm32f4x unlock 0; reset run” -d2 -s C:\Users\Max.platformio\packages\tool-openocd/scripts -f interface/stlink.cfg -c “transport select hla_swd” -f target/stm32f4x.cfg -c “program {.pio\build\stm32f4\firmware.elf} verify reset; shutdown;”

which might work, even with those double config files. If not, see this section of advanced scripting for modifying the upload command. Or again specify the whole upload_command.

Indeed Custom Targets can also be used.

If I add to the .ini file

extra_scripts = add_unlock_task.py

with add_unlock_task.py

Import("env")
env.AddCustomTarget(
    "unlock",
    None,
    '"$PROJECT_PACKAGES_DIR/tool-openocd/bin/openocd" -f interface/stlink.cfg -f target/stm32f4x.cfg -c "init; reset halt; stm32f4x unlock 0; reset run; exit"'
)

doing a

>pio run -t unlock -v
"C:\Users\Max\.platformio\packages/tool-openocd/bin/openocd" -f interface/stlink.cfg -f target/stm32f4x.cfg -c "init; reset halt; stm32f4x unlock 0; reset run; exit"
xPack OpenOCD, x86_64 Open On-Chip Debugger 0.11.0-00155-ge392e485e (2021-03-15-16:44)
Licensed under GNU GPL v2
...

shows that it tries to do the right thing. The task also shows up in the VSCode project task list.

There’s a lot of ways to achieve this.

1 Like