STM32L Nucleo OpenOCD error on second pass

Hi,
I have one unusual problem with STM32 Nucleo-L053R8 board (original board).
When I first time build and program it, everything works just fine. But, when I try to program it for second time I get error:
Error: init mode failed (unable to connect to the target)
Then I need to go to the STM32CUBE and program board with literary anything, and, when I return to the PlatformIO everything works fine. And then all over again.
Here is some data:

OS: Windows 10
Editor: VSC or CLion (the same behavior)

platformio.ini
[env:nucleo_l053r8]
platform = ststm32
board = nucleo_l053r8
framework = arduino
upload_protocol = stlink

Programming command :
openocd -d2 -s C:\Users\goran.platformio\packages\tool-openocd/scripts -f interface/stlink.cfg -c “transport select hla_swd” -f target/stm32l0.cfg -c “program {.pio\build\nucleo_l053r8\firmware.elf} verify reset; shutdown;”
xPack OpenOCD, x86_64 Open On-Chip Debugger 0.11.0-00155-ge392e485e (2021-03-15-16:44)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
debug_level: 2

hla_swd
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : clock speed 300 kHz
Info : STLINK V2J37M27 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.281213
Error: init mode failed (unable to connect to the target)
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked

*** [upload] Error 1

STM32CubeIDE log:
Open On-Chip Debugger 0.11.0-rc2+dev-00037-g4c4dbd9 (2021-02-09-13:39)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : STLINK V2J37M27 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.276467
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : clock speed 4000 kHz
Info : stlink_dap_op_connect(connect)
Info : SWD DPIDR 0x0bc11477
Info : STM32L053R8Tx.cpu: hardware has 4 breakpoints, 2 watchpoints
Info : STM32L053R8Tx.cpu: external reset detected
Info : starting gdb server for STM32L053R8Tx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : accepting ‘gdb’ connection on tcp/3333
target halted due to debug-request, current mode: Thread
xPSR: 0xf1000000 pc: 0x08004d48 msp: 0x20002000
STM32L0: Enabling HSI16
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Device: STM32L0xx (Cat. 3)
Info : STM32L flash size is 64kb, base address is 0x8000000
Info : accepting ‘gdb’ connection on tcp/3333
target halted due to debug-request, current mode: Thread
xPSR: 0xf1000000 pc: 0x08004d48 msp: 0x20002000
STM32L0: Enabling HSI16
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0xf1000000 pc: 0x08004d48 msp: 0x20002000
STM32L0: Enabling HSI16
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Info : Unable to match requested speed 8000 kHz, using 4000 kHz
Warn : keep_alive() was not invoked in the 1000 ms timelimit. GDB alive packet not sent! (1003 ms). Workaround: increase “set remotetimeout” in GDB
target halted due to debug-request, current mode: Thread
xPSR: 0xf1000000 pc: 0x08000900 msp: 0x20002000
shutdown command invoked
Info : dropped ‘gdb’ connection
shutdown command invoked

Any idea?

THX
Goran

Usually this indicates a problem with the used reset method (software / hardware).

Could you please try the following: Build the project normally once, then open a CLI and execute the command

C:\Users\goran\.platformio\packages\tool-openocd\bin\openocd -d2 -s C:\Users\goran\.platformio\packages\tool-openocd\scripts -f interface/stlink.cfg -c "transport select hla_swd" -f target/stm32l0.cfg -c "reset_config srst_only" -c "program {.pio\build\nucleo_l053r8\firmware.elf} verify reset; shutdown;"

aka the same command as above but with -c "reset_config srst_only" to use a different reset method.

You may also try different values from here.

Once one works it can be put in the platformio.ini with e.g.

upload_flags =
    -c
    reset_config srst_only

Edit: corrected -c "set reset_config srst_only" to -c "reset_config srst_only" to which should be right

Hi Max,

thank you for answering. I try several options, without success (I did not try all, but I will).
But, I somehow think that this is not a problem. Why? Well, when I take empty, or really simple application, then I do not have problem. Problem start when I enable certain functions on MCU. This project now use TFT LCD with SPI interface, and some joystick analog inputs on analog pins. So, I’m using PB3 for SCLK for SPI, and PA0 and PA1 for analog inputs.
I think that problem can be in PB3 pin, since on Nucleo it is a SWO.
I do not know OpenOCD, so, question is - is there a chance that in CubeIDE they only use two pins (TMS/TCK) and on PIO he want to use more pins / like SWO?
If this is a case, how to turn it off, since it’s work with CubeIDE version of OpenOCD.

THX
Goran

SWO is serial-wire-out, that’s an optional debug output. Not used for programming. OpenOCD, as well as the CubeProgrammer, should only use the SWD lines (SWDIO + SWDCLK) and NRST if configured to do so.

Well the OpenOCD version is definitely newer but there could also be a difference in used configuration files or flags. Are you able to see those?

Hi Max,

thank you. I found what is going on (it’s on me :frowning: ), but I can’t find solution for this.
So, I use almost all pins on MCU, and I among other use pins PA13/PA14. Those are pins TMS/TCK, and when I set their mode, in my case INPUT_PULLDOWN, immediately after programing I receive error that board can not be reset, and after that I was unable to program it again.
CubeIDE do it right, so, there is probably some parameter to solve this.
I think you right, that problem lies in way PIO OOCD reset board. I will play with reset options and try to find out how to avoid this.

Thank you
Goran

1 Like

Thank you for the information.
I could set connect under reset for stlink by the following configration.
ref: connect under reset with st-link v2 - SparkFun Electronics

upload_flags =
  -c reset_config srst_only srst_nogate connect_assert_srst

Forthemore, copied (Chinese?) stlink is unable to reset so I need stlink which is resettable.

ref: 試行錯誤な日々: stm32マイコンでLowPowerライブラリ利用時にstlink経由で書き込む場合はリセット可能なstlinkと接続時のリセット設定が必要