STM32G4 won't boot if not build_type = debug

Hi guys,

I’m working with a STM32G4 and my platformio.ini look like this

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:genericSTM32G431CB]
framework=stm32cube
platform = ststm32
board = genericSTM32G431CB
debug_tool = stlink
upload_protocol = stlink
board_build.stm32cube.custom_dsp_library = yes
build_flags =
    -Llib/cmsisdsp
    -lcmsisdsp

;https://community.platformio.org/t/error-object-uses-vfp-register-arguments-firmware-elf-does-not/25263/2
extra_scripts = pre:add_hardfloat.py

; TODO il faut creuser ici, le build debug desactive les optimisations
build_type = debug 

I noticed that when I remove the build_type or set it to release, the upload works fine but the mcu crash.

I check the diffs between the flags according to the build_type and the differences look like that

So I tried to add -Og -g2 -ggdb2 flags to my platformio.ini build_flags but hat doesn’t change anything, the mcu crash.

The mcu works properly when build_type set to debug though

Thank you

So, in what function does it crash? What’s the stack trace?

It might be code that is not safely written that crashes when compiler optimizations are applied.

It’s look like that the calls to CMSIS functions cause the crash.

I added a more recent version than the platformio default: GitHub - Sercurio/CMSIS_6_DSP-builder: A repo for building latest CMSIS DSPs for using in embedded mcu

void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc) { process(0); }
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { process(N); }

Inside process I’ve got calls like
arm_fir_q15(&S_am_fir_i, tmp_i, tmp_i, N / 2);

When commented out, the mcu run fine.

It’s an interrupt drived by an ADC

Do you have a minimal firmware that reproduces the crash? (MRE)

You need to at least build_unflags = -Os to properly do that, otherwise it will have both -Og and -Os.

Here a minimal project that call a CMSIS function and crash just after

1 Like

I tried with your suggestion and it seems ok, I added -O3 and the mcu boot correctly

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:genericSTM32G431CB]
framework=stm32cube
platform = ststm32
board = genericSTM32G431CB
debug_tool = stlink
upload_protocol = stlink
board_build.stm32cube.custom_dsp_library = yes
build_flags =
  -Llib/cmsisdsp
  -lcmsisdsp
  -O3
build_unflags = -Os
; https://community.platformio.org/t/error-object-uses-vfp-register-arguments-firmware-elf-does-not/25263/2
extra_scripts = pre:add_hardfloat.py