Arm Mbed OS support forum

Problem flashing STM32 with openocd


I am new to development with mbed. I started a new project and used by @ladislas as a basis, as I need to connect several devices that share a good amount of code. So far everything worked really nicely.

However, I have a problem flashing my STM32L432 using openocd. After the first time I flash, openocd can no longer connect to the device (unless I press the reset button). I have the suspicion that SWD gets disabled.

After this I can only flash using the CubeIDE or Mbed studio.
So I am clearly doing something wrong.

My openocd call looks as follows:
openocd -f openocd.cfg -c 'program $(BIN_PATH_BACK) 0x08000000' -c exit

The openocd.cfg contains this:

# This is a ST NUCLEO L432KC board with a single STM32L432KCU6 chip.

# This is for using the onboard STLINK/V2
source [find interface/stlink.cfg]

transport select hla_swd

# increase working area to 48KB
set WORKAREASIZE 0x0c000

# chip name

source [find target/stm32l4x.cfg]

# use hardware reset
reset_config srst_only srst_nogate

Any hint how to solve this?


I find that adding some delay helps:

-c 'sleep 200' -c 'reset run' -c 'exit'

I don’t use the reset_config srst_only srst_nogate

Thank you for your answer. Unfortunately this does not help.

I get this the second time I try:

openocd -f openocd.cfg -c 'program xxx.bin 0x08000000' -c 'sleep 200' -c 'reset run' -c 'exit'
Open On-Chip Debugger 0.11.0-rc1+dev-00010-gc69b4deae-dirty (2021-01-04-09:36)
Licensed under GNU GPL v2
For bug reports, read
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : clock speed 500 kHz
Info : STLINK V2J37M26 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.284961
Error: init mode failed (unable to connect to the target)
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked

Hello Bernd,

I think your issue relates to the problem of flashing the STM boards with programs built with Mbed OS 6.6.0. Read here for more info.

Happy new year and best regards Zoltan

I switched back to 6.5.0 and see still the same issue…

Did you erase the chip (get rid of the Mbed OS 6.6.0 program) before trying to program it with a program built with Mbed OS 6.5.0 ?

I can programm my Blackpill (STM32F411CEU6) board with openocd several times in a sequence with no problems. However, when I program the chip with a Mbed OS 6.6.0 program then additional attempts to program it with any version fail.
I use the following command in bash script:

openocd -s /opt/openocd/share/openocd/scripts -f ${MCU_CFG} -c """program ./BUILD/${MCU}/GCC_ARM-${PROFILE}/${PROJ_NAME}.elf verify reset exit"""

For STM32F4xx targets:


Now it seems to work. I thought I erased it, but I guess I did not. Thank you!