Cannot Debug STM32 Arduino

I have a different project that is not working as expected so I thought I would try a simple debug session first to confirm that debugging was working. I have a custom board based on the STM32F405RG and am connected to a Win10 PC using a ST-Link clone flashed with the latest ST firmware. I can successfully program the board using SWD. I am using VS Code w/ PIO. The code is simple:

#define myPin PB11

void setup() {
pinMode(myPin, OUTPUT);
//Serial.begin(115200);
}

void loop() {
//Serial4.println("Hello World");
digitalWrite(myPin, HIGH); // <--Breakpoint set here
delay(50);
digitalWrite(myPin, LOW);
delay(100);
}

When I go to debug the session starts and then it opens the startup_stm32f405xx.s file. It initially pauses but then loads the debug toolbar but it starts immediately running and I don’t have the option to single step, just pause and stop. If I pause I end up in the startup file on line 127 which is an infinite loop statement:

/**
 * @brief  This is the code that gets called when the processor receives an
 *         unexpected interrupt.  This simply enters an infinite loop, preserving
 *         the system state for examination by a debugger.
 * @param  None
 * @retval None
*/
    .section  .text.Default_Handler,"ax",%progbits
Default_Handler:
Infinite_Loop:
  b  Infinite_Loop
  .size  Default_Handler, .-Default_Handler

If I try any of the step commands it just continues and I have to pause again. I have set a breakpoint in the loop() routine on a digitalwrite command and I see the red dot as well as it being listed on the left side of the window. I’ve searched and haven’t found anything special I’m supposed to do so I’m a bit stumped why the debug session isn’t just starting and stopping at the first breakpoint. I would also expect that I should have to hit run first but that doesn’t happen either it just immediately starts running.

Here is what is in the debug console.

Processing stm32test (platform: ststm32; board: IOTech_STM32F405; framework: arduino)
-------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/IOTech_STM32F405.html
PLATFORM: ST STM32 5.7.0 > IOTech Villara Tester
HARDWARE: STM32F405RGT6 168MHz, 192KB RAM, 1MB Flash
DEBUG: Current (stlink) External (stlink)
PACKAGES: framework-arduinoststm32 3.10700.191028 (1.7.0), toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 10 compatible libraries
Scanning dependencies...
No dependencies
Building in debug mode
Checking size .pio\build\stm32test\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
DATA:    [          ]   0.5% (used 948 bytes from 196608 bytes)
PROGRAM: [          ]   1.5% (used 16032 bytes from 1048576 bytes)
========================= [SUCCESS] Took 2.60 seconds =========================
Reading symbols from c:\Users\george\OneDrive\Documents\PlatformIO\Projects\Just_a_Test\.pio\build\stm32test\firmware.elf...
done.
PlatformIO Unified Debugger -> http://bit.ly/pio-debug
PlatformIO: debug_tool = stlink
PlatformIO: Initializing remote target...
xPack OpenOCD, 64-bit Open On-Chip Debugger 0.10.0+dev (2019-07-17-11:28)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
hla_swd
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
none separate

Info : tcl server disabled
Info : telnet server disabled
Info : clock speed 2000 kHz
Info : STLINK V2J34S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 2.451600
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : accepting 'gdb' connection from pipe
target halted due to debug-request, current mode: Handler HardFault
xPSR: 0x61000003 pc: 0x08002fb8 msp: 0x2002ffd0
Info : device id = 0x10076413
Info : flash size = 1024 kbytes
Info : flash size = 512 bytes
WWDG_IRQHandler () at C:\Users\george\.platformio\packages\framework-arduinoststm32\system\Drivers\CMSIS\Device\ST\STM32F4xx\Source\Templates\gcc\startup_stm32f405xx.s:127
127	  b  Infinite_Loop
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08002f68 msp: 0x20030000
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 1000ms timelimit. GDB alive packet not sent! (1012). Workaround: increase "set remotetimeout" in GDB
Loading section .isr_vector, size 0x188 lma 0x8000000
Loading section .text, size 0x3820 lma 0x8000188
Loading section .rodata, size 0x5e8 lma 0x80039a8
Loading section .ARM, size 0x8 lma 0x8003f90
Loading section .init_array, size 0x14 lma 0x8003f98
Loading section .fini_array, size 0x8 lma 0x8003fac
Loading section .data, size 0x98 lma 0x8003fb4
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08002f68 msp: 0x20030000
Start address 0x8002f68, load size 16460
Transfer rate: 7 KB/sec, 2351 bytes/write.
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Unable to match requested speed 2000 kHz, using 1800 kHz
Unable to match requested speed 2000 kHz, using 1800 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08002f68 msp: 0x20030000
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08002f68 msp: 0x20030000
Temporary breakpoint 1 at 0x8000c9e: file C:\Users\george\.platformio\packages\framework-arduinoststm32\cores\arduino\main.cpp, line 56.
PlatformIO: Initialization completed
PlatformIO: Resume the execution to `debug_init_break = tbreak main`
PlatformIO: More configuration options -> http://bit.ly/pio-debug
Note: automatically using hardware breakpoints for read-only addresses.

I’ve done some additional “debugging” and found that if I change my platform.ini to use a board that has a board definition that uses a maple core instead of a stm32 core then debugging works as expected.

I’m also finding that I’m not getting expected program results if I use my board definition that is based on the stm32 core.

I’m not sure if this is a general bug or if something is wrong with my installation or configuration so I’m hoping that someone can let me know if they have successfully debugged a board using the stm32 core.

Hi,

I would guess that you have been running with clock issues.

Try setting specific clock settings for your board instead of framework specific.

Sylvio

Do you have some helpful advice for a board with the STM32F405RG and no external clock? I have this working with the Arduino IDE and STMCubeIDE but PIO has just been difficult to get working right for me.