Problems with OpenOCD resulting in errors when uploading to STM32

Hi there, I’m struggling with uploading my code to an STM32 successfully on PlatformIO. My issue seems to be quite similar to Error while trying to upload code to BluePIll [SOLVED], except for the fact that my issue likely lies with OpenOCD.

Here is my platformio.ini file:

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = arduino
upload_protocol = stlink
debug_tool = stlink
upload_flags = -c set CPUTAPID 0x2ba01477

build_flags =
    -Os
    -I
    -O1
    -Wl,--undefined,_printf_float
    -Wl,--undefined,_scanf_float
    -DPIN_SERIAL_TX=PA9
    -DPIN_SERIAL_RX=PA10

lib_deps =
  adafruit/Adafruit BNO055 @ ^1.6.3
  adafruit/Adafruit BusIO @ ^1.14.5
  Wire
  SPI
  adafruit/Adafruit Unified Sensor @ ^1.1.14

And here is the error I’m getting:

Building in release mode
Checking size .pio\build\bluepill_f103c8\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   8.0% (used 1636 bytes from 20480 bytes)
Flash: [=====     ]  52.7% (used 34532 bytes from 65536 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, dfu, jlink, mbed, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\bluepill_f103c8\firmware.elf
xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:04)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

0x2ba01477
hla_swd
none separate

Warn : UNEXPECTED idcode: 0x1ba01477
Error: expected 1 of 1: 0x2ba01477
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked

*** [upload] Error 1

I’ve re-installed OpenOCD quite a few times now through mingw and added it to my system PATH variables. The OpenOCD command does work on powershell, but I also get this error:

Open On-Chip Debugger 0.12.0
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
embedded:startup.tcl:28: Error: Can't find openocd.cfg
in procedure 'script'
at file "embedded:startup.tcl", line 28
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Error: Debug Adapter has to be specified, see "adapter driver" command
embedded:startup.tcl:28: Error:
in procedure 'script'
at file "embedded:startup.tcl", line 28

Which I can’t seem to fix no matter what I’ve tried from what I could find online. Like in the other thread I linked up top, I tried connecting to my STM32’s chip through the STM32CubeProg and was able to successfully connect, so I highly suspect this is an issue with OpenOCD.

Any help on how to properly install OpenOCD and get it working with PlatformIO would be greatly appreciated!

Here you explicitly make OpenOCD expect the CPUTAPID of 0x2ba01477 when OpenOCD reads the CPUTAPID of 0x1ba01477 from the chip. That means you don’t have a fake Bluepill (real STM32 instead of CS32 chip) and you need to delete the upload_flags line from your platformio.ini.

Thanks for the reply! I wasn’t sure if it was a real STM32 or not, so I tried both with and without the upload flags. However, even without the upload flags, I’m still getting some errors. Here is my platformio.ini without the upload flags:

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = arduino
upload_protocol = stlink
debug_tool = stlink
;upload_flags = -c set CPUTAPID 0x2ba01477

build_flags =
    -Os
    -I
    -O1
    -Wl,--undefined,_printf_float
    -Wl,--undefined,_scanf_float
    -DPIN_SERIAL_TX=PA9
    -DPIN_SERIAL_RX=PA10

lib_deps =
  adafruit/Adafruit BNO055 @ ^1.6.3
  adafruit/Adafruit BusIO @ ^1.14.5
  Wire
  SPI
  adafruit/Adafruit Unified Sensor @ ^1.1.14

And the new errors I am getting:

xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:04)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

hla_swd
none separate

Error: init mode failed (unable to connect to the target)
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked

*** [upload] Error 1
===================================== [FAILED] Took 4.40 seconds =====================================

Can you please try this upload method in case you screwed up the SWDIO or SWCLK pins:

  1. Hold down the RESET button on the bluepull
  2. Press “Upload” in PlatformIO
  3. Keep it pressed until it looks like OpenOCD is being invoked, then release the RESET button

Timing can be a bit tricky.

You can only have one optimization level. Os and O1 makes no sense btw.

Right. I tried that earlier from a different thread and just tried a few times again, but am still getting the same error. Occasionally, the upload time goes up to 20+ seconds before aborting, so I’m not sure if I’m getting the timing exactly right. Also switched the SWDIO and SWCLK pins a few times and I’m fairly confident that they are correctly connected…

Also removed the redundant optimization levels in my platformio.ini. Thanks for catching that!

Easy check: With your current wiring, can you still connect to the chip with the STM32CubeProgrammer?

Do you connect the NRST signal too between the bluepill and ST-Link?

Does it still work if you set “Reset: Software Reset” in the STM32CubeProgrammer? Or does it need the Hardware Reset?

Yes I can connect to the chip still

Sorry, not exactly sure what that is, but if you are talking about something like a USB to TTL converter, then yes, I have that connected to my computer as well.

I believe so, if you are talking about this:

Then it should be working as far as I know.

I just found what I think should be the NSRT pin (pin R on the STM32?) and connected it to the ST-Link. I do think I’m slowly getting somewhere since the error is a bit different now:

Building in release mode
Checking size .pio\build\bluepill_f103c8\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   8.0% (used 1636 bytes from 20480 bytes)
Flash: [=====     ]  52.7% (used 34532 bytes from 65536 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, dfu, jlink, mbed, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\bluepill_f103c8\firmware.elf
xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:04)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

hla_swd
none separate

Error: libusb_open() failed with LIBUSB_ERROR_ACCESS
Error: open failed
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked

*** [upload] Error 1
============================================================================================ [FAILED] Took 3.81 seconds ============================================================================================

Any ideas on how to fix this?

Sorry, last reply! For the reference of anybody else who comes across this thread and is going through the same issues:

  • Reinstalled the platformio library in terminal
  • Disconnected and reconnected everything (after now connecting the NSRT pin)
  • Disconnected the ST-Link from the STM32CubeProg

No clue what combination of those above items got it working but my STM32 is happily flashing now :slight_smile:

1 Like