First line of reset handler throws exception on STM32G4


I recently transferred to Platformio from the CubeIDE for a project with the STM32G431CB. When I try to upload or debug my program on the unit it gets uploaded fine but doesn’t execute. I checked with CubeProgrammer that the upload actually is happening. The program executes properly when I try to debug through CubeIDE. After investigating I found out that the first line of the reset handler throws an exception. The reset handler is exactly the same as used in the CubeIDE project. I’m using SWD with a 4 wire config. I’m suspecting that I’m missing an option from my ini file that alters the upload / reset process.

The contents of my ini file are:

platform = ststm32
board = genericSTM32G431CB
framework = stm32cube
upload_protocol = stlink
debug_tool = stlink
debug_init_break =

Debug view after exception on first line of reset handler:
Screenshot 2024-01-11 211455

Debug output:

Reading symbols from C:\Users\Tom\Desktop\LED_G431CB\.pio\build\genericSTM32G431CB\firmware.elf...
PlatformIO Unified Debugger ->
PlatformIO: debug_tool = stlink
PlatformIO: Initializing remote target...
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
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : tcl server disabled
Info : telnet server disabled
Info : clock speed 2000 kHz
Info : STLINK V2J43M28 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.234098
Info : [stm32g4x.cpu] Cortex-M4 r0p1 processor detected
Info : [stm32g4x.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32g4x.cpu on pipe
Info : accepting 'gdb' connection from pipe
[stm32g4x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x1fff44ea msp: 0x20002cd8
Info : device idcode = 0x20036468 (STM32G43/G44xx - Rev 'unknown' : 0x2003)
Info : RDP level 0 (0xAA)
Info : flash size = 128 KiB
Info : flash mode : single-bank
Info : device idcode = 0x20036468 (STM32G43/G44xx - Rev 'unknown' : 0x2003)
Info : RDP level 0 (0xAA)
Info : OTP size is 1024 bytes, base address is 0x1fff7000
Warn : keep_alive() was not invoked in the 1000 ms timelimit. GDB alive packet not sent! (1026 ms). Workaround: increase "set remotetimeout" in GDB
0x1fff44ea in ?? ()
PlatformIO: Initialization completed

I tried to flash with OpenOCD via CubeIDE and this is the error i get:

Error: timed out while waiting for target halted
Error executing event gdb-flash-erase-start on target STM32G431CBTx.cpu:
TARGET: STM32G431CBTx.cpu - Not halted
Error: Target not halted
Error: failed erasing sectors 0 to 2
Error: flash_erase returned -304
Info : dropped 'gdb' connection
shutdown command invoked
Info : dropped 'gdb' connection

Selecting STLink GDB Server as Debug probe works fine though. Does this mean I have to switch to another debug probe in platformio, too? How would I do so?


That looks like it’s in the built-in bootloader memory, not in the flash memory (@ 0x8000000). How did you wire up BOOT0 and possibly BOOT1 of you STM32G4 chip?


It’s working after I changed the option byte nSWBOOT0 to 0. Boot0 and Boot1 are floating on my board. From the description of the option byte it makes sense to me why it wasn’t working prior. I’m curious tho whether the CubeIDE sets that byte on its own or what else does it do?