windows-10openocdstm32f1stm32cubeide

STM32CubeIDE and OpenOCD: Error: timed out while waiting for target halted


Hardware/IDE Context:

In attempting to build/flash a simple PC_13 LED blinky program to my BluePill board, I experience errors from OpenOCD like so:

Open On-Chip Debugger 0.10.0+dev-01288-g7491fb4 (2020-10-27-17:36)
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 V2J37S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.256346
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 0x1ba01477
Info : STM32F103C8Tx.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : STM32F103C8Tx.cpu: external reset detected
Info : starting gdb server for STM32F103C8Tx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : accepting 'gdb' connection on tcp/3333
Error: timed out while waiting for target halted
Error executing event gdb-attach on target STM32F103C8Tx.cpu:
TARGET: STM32F103C8Tx.cpu - Not halted
Info : device id = 0x20036410
Info : flash size = 64kbytes
Info : accepting 'gdb' connection on tcp/3333
Error: timed out while waiting for target halted
Error executing event gdb-attach on target STM32F103C8Tx.cpu:
TARGET: STM32F103C8Tx.cpu - Not halted
Error: timed out while waiting for target halted
Error executing event gdb-flash-erase-start on target STM32F103C8Tx.cpu:
TARGET: STM32F103C8Tx.cpu - Not halted
Error: Target not halted
Error: failed erasing sectors 0 to 5
Error: flash_erase returned -304
shutdown command invoked
Info : dropped 'gdb' connection
shutdown command invoked

I'm interested in using OpenOCD-based flashing for my project to make use of some STM32F103C8 clone boards I have lying around, but the upload process works again when I switch the flashing mode/"Debug Probe" in STM32CubeIDE back to ST-Link (ST-Link GDB Server) from ST-Link (OpenOCD).

This is a peculiar error to me, especially since I specifically remember this exact configuration (STM32CubeIDE + OpenOCD + ST-Link + STM32F103C8) working a couple of months ago. Does anyone have any ideas as to what this could be caused by? I have the OpenOCD debugger to use the standard auto-generated config file.

Also please let me know if there is any more information/details you'd need to help diagnose this issue. I'd be happy to provide anything necessary.

EDIT 2/22/2021:

Here is a copy of the auto-generated (by STM32CubeIDE) OpenOCD .cfg file:

# This is an genericBoard board with a single STM32F103C8Tx chip
#
# Generated by STM32CubeIDE
# Take care that such file, as generated, may be overridden without any early notice. Please have a look to debug launch configuration setup(s)

source [find interface/stlink-dap.cfg]


set WORKAREASIZE 0x5000

transport select "dapdirect_swd"

set CHIPNAME STM32F103C8Tx
set BOARDNAME genericBoard

# Enable debug when in low power modes
set ENABLE_LOW_POWER 1

# Stop Watchdog counters when halt
set STOP_WATCHDOG 1

# STlink Debug clock frequency
set CLOCK_FREQ 8000

# Reset configuration
# use hardware reset, connect under reset
# connect_assert_srst needed if low power mode application running (WFI...)
reset_config srst_only srst_nogate connect_assert_srst
set CONNECT_UNDER_RESET 1
set CORE_RESET 0

# ACCESS PORT NUMBER
set AP_NUM 0
# GDB PORT
set GDB_PORT 3333



# BCTM CPU variables

source [find target/stm32f1x.cfg]

#SWV trace
tpiu config disable 

Solution

  • Ultimately, after some further research and trial & error, I settled on a fix that seems to work for me. I noticed that when the error with halting the CPU came up, the correct program seemed to have been loaded and the RESET button just needed to be toggled manually. These are the OpenOCD settings I ended up settling on:

    enter image description here

    Changes from the default configuration:

    This works for me with the following output:

    Open On-Chip Debugger 0.10.0+dev-01288-g7491fb4 (2020-10-27-17:36)
    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 V2J37S7 (API v2) VID:PID 0483:3748
    Info : Target voltage: 3.254751
    Info : clock speed 4000 kHz
    Info : stlink_dap_op_connect(connect)
    Info : SWD DPIDR 0x1ba01477
    Info : STM32F103C8Tx.cpu: hardware has 6 breakpoints, 4 watchpoints
    Info : STM32F103C8Tx.cpu: external reset detected
    Info : starting gdb server for STM32F103C8Tx.cpu on 3333
    Info : Listening on port 3333 for gdb connections
    Info : accepting 'gdb' connection on tcp/3333
    Info : device id = 0x20036410
    Info : flash size = 64kbytes
    undefined debug reason 8 - target needs reset
    Info : accepting 'gdb' connection on tcp/3333
    undefined debug reason 8 - target needs reset
    target halted due to debug-request, current mode: Thread 
    xPSR: 0x01000000 pc: 0x08000474 msp: 0x20005000
    target halted due to debug-request, current mode: Thread 
    xPSR: 0x01000000 pc: 0x08000474 msp: 0x20005000
    shutdown command invoked
    Info : dropped 'gdb' connection
    shutdown command invoked