The bin file firmware.bin generated by the platformio doesn't work, how to fix it?

For the same project, there are two bin files which generated by Platformio and stm32cubeide respectively, and I upload these two bin files at platformioIDE, the bin file generated by stm32cubeide can work properly, but the bin file generated by Platformio do not work. It seems that the compiler settings in platformio has some problem, but i cant solve it.

With 0 info on the actual code and configuration files, we effectively can’t help.

my pfatformio.ini file:

[env:disco_f100rb]
platform = ststm32
board = disco_f100rb
;framework = cmsis
;extra_scripts = extra_script.py
debug_tool = jlink
upload_protocol = jlink

build_flags =
-I./src/user
-I./src/ST_Lib/Lib_Standard
-I./src/ST_Lib/Lib_Standard/inc
-I./src/ST_Lib/core
-I./src/Bsp
-I./src/Mode_Desktp/Inc
-D STM32F10X_MD_VL
-D USE_STDPERIPH_DRIVER

board_build.ldscript = ./src/ST_Lib/core/STM32F100CBTX_FLASH.ld

and these two bin files upload sucess

Thank you!

Ok great so yuo’re flashing via a JLink. Can you use Debugging->Start Debugging in VScode to see if the firmware is stuck on a particular point? What’s the exact expected behavior vs the current one?

Oh, when I debug, it can work ok, no stuck. but when upload , the board doesn’t work

Add

build_type = debug

int the platformio.ini and upload again normally.

It’s ok ! Thank you very much!

No it’s not. That means some code in your firmware is depending on being compiled with debug optimization (-Og) instead of release optimization (-Os) to work correctly. That’s really, really bad. Setting build type to debug is not a solution at all.

You can set

debug_build_flags = -Os -g3 -ggdb3

(docs) to do a “Minimum Release Size with Debug Info” build and do Debug->Start debugging again see where the firmware gets stuck when compiled with release optimization.

If you have delay loops like

for(int i=0; i < 1000000; i++) { 
}

then those get optimized away in release mode completely and you need to insert asm volatile("nop"); at least.

Also release mode is less forgiving if you have memory access errors / array out of bounds errors. You can use PlatformIO’s built-in static code analysis to maybe find this.

It means that I can use PlatformIO’s built-in [static code analysis ] to find the problem in my code?
I set the " debug_build_flags = -Os -g3 -ggdb3 ", there is still no stuck or maybe in here as the picture

When you compile with those debug build flags, is the behavior still faulty?

like this?
屏幕截图 2023-04-26 165711

It’s sucessful.

That’s weird, I thought it didn’t work with -Os?

Now I found that when I delete the “debug_build_flags = -Os -g3 -ggdb3” , it still can work ,really weird, I changed nothing

Do you do regular uploading or debugging? debug_build_flags only apply when using Debugging->Start Debugging. Do you still have build_type = debug set?

regular uploading without build_type = debug

Well. If it works nice. Just make sure it’s not by random chance. Better check the static code analysis results whether they show anything high or critcial.

OK. Thanks for your help!