[SOLVED] Debugger fails to start with custom STM32 board

I have a custom board using STM32L031F6 with 32K flash 8K ram. Using a Mac with Black Magic Probe which works for uploading. However when I try to start PIO Debug (skip Pre-Debug) the debug control panel pops up briefly and then disappears before initializing is complete.

Here is my platformio.ini

[platformio]
default_envs = 

[env]
platform_packages = 
    toolchain-gccarmnoneeabi@~1.90301.200702
debug_tool = blackmagic
upload_protocol = blackmagic
extra_scripts = blackmagic_srst.py  ; needed for M0/M0+ detection
debug_port = /dev/cu.usbmodemBFE4B7ED1
upload_port = /dev/cu.usbmodemBFE4B7ED1

[env:L031F6]
platform = ststm32
;platform = https://github.com/platformio/platform-ststm32.git
board = STM32L031F6
framework = stm32cube
build_type = release
build_flags = 
    -DSTM32L0 
    -DL0 
    -std=gnu++11 
    -Os
    -flto
    -Wl,-Map,output.map 
    -Iinclude
    -ICore/Inc

[env:debug]
platform = ststm32
;platform = https://github.com/platformio/platform-ststm32.git
board = STM32L031F6
framework = stm32cube
build_type = debug
debug_build_flags = 
    -DSTM32L0  
    -DL0 
    -std=gnu++11 
    -Os
    -ggdb3
    -flto
    -Wl,-Map,output.map
    -ICore/Inc
    -Iinclude

;; blackmagic
debug_init_cmds =
   target extended-remote $DEBUG_PORT
   monitor conn e
   monitor swdp_scan
   attach 1
   set mem inaccessible-by-default off

This board seems custom. What’s the STM32L031F6.json boar definition?

  • usually on euse uses the “PIO Debug” config without Pre-Debug. What happens when you use that?
  • what is the full log in the “Debug Console” tab of VSCode? There should be stuff in there even if debugging does not fully work.

here is the board json

{
“build”: {
“cpu”: “cortex-m0plus”,
“extra_flags”: “-DSTM32L031xx”,
“f_cpu”: “32000000L”,
“mcu”: “stm32l031f6”,
“product_line”: “STM32L031xx”,
“variant”: “STM32L0xx/L031F(4-6)T”
},
“debug”: {
“default_tools”: [
“blackmagic”
],
“jlink_device”: “STM32L031F6”,
“openocd_target”: “stm32l0”,
“svd_path”: “STM32L0x1.svd”
},
“frameworks”: [
“arduino”,
“cmsis”,
“mbed”,
“stm32cube”,
“framework-stm32cubel0@1.11.3”,
“libopencm3”,
“zephyr”
],
“name”: “STM32L031F6”,
“upload”: {
“maximum_ram_size”: 8192,
“maximum_size”: 32768,
“protocol”: “blackmagic”,
“protocols”: [
“jlink”,
“cmsis-dap”,
“stlink”,
“blackmagic”,
“mbed”
]
},
“url”: “STM32L031F6 - Ultra-low-power Arm Cortex-M0+ MCU with 32-Kbytes of Flash memory, 32 MHz CPU - STMicroelectronics”,
“vendor”: “ST”
}

I’ve had to pre-build with the -Os flag as the the normal Debug build does not fit in flash memory.

I also just learned more about platformio! Never noticed the Debug Console tab.

Linking .pio/build/L031F6/firmware.elf
/Users/buster/.platformio/packages/toolchain-gccarmnoneeabi@1.90301.200702/bin/…/lib/gcc/arm-none-eabi/9.3.1/…/…/…/…/arm-none-eabi/bin/ld: .pio/build/L031F6/firmware.elf section .rodata' will not fit in region FLASH’
/Users/buster/.platformio/packages/toolchain-gccarmnoneeabi@1.90301.200702/bin/…/lib/gcc/arm-none-eabi/9.3.1/…/…/…/…/arm-none-eabi/bin/ld: region `FLASH’ overflowed by 2236 bytes
collect2: error: ld returned 1 exit status
*** [.pio/build/L031F6/firmware.elf] Error 1
========================== [FAILED] Took 4.62 seconds ==========================

Environment Status Duration


L031F6 FAILED 00:00:04.621
==================== 1 failed, 0 succeeded in 00:00:04.621 ====================

So even with without Pre-Debug it is still building again with gcc options that cause overflow. What is the best way to override the flags in this step?

Answered my own question. Here’s how I fixed (at least for me) the problem of debugger build flags.

(1) Set default_envs to “debug”

[platformio]
default_envs = debug

(2) Set [env:debug] build flags as you prefer

[env:debug]
platform = ststm32
;platform = GitHub - platformio/platform-ststm32: ST STM32: development platform for PlatformIO
board = STM32L031F6
framework = stm32cube
build_type = debug
debug_build_flags =
-DSTM32L0
-DL0
-std=gnu++11
-Os
-g2
-ggdb2
-flto
-Wl,-Map,output.map
-ICore/Inc
-Iinclude

(3) Delete .vscode/launch.json

(4) Quit and restart the vsc app. When platformio restarts it will rebuild .vscode/launch.json with the correct entries for debugging.

1 Like