Welcome! Log In Create A New Profile

Advanced

How can I debug the STM32F103 code and remove compiler optimization

Posted by myjtag 
How can I debug the STM32F103 code and remove compiler optimization
January 08, 2024 04:12AM
Hi
I'm new to marlin and I want to study it,
I have downloaded the repo and changed the platformio.ini like this

default_envs = STM32F103CB_malyan

Also I have copied the Malyan/M200 Configuration.h and Configuration_adv.h files into the marlin project and could compile the code, I have these questions
How can I disable compiler optimization?
The next step is to remove compiler optimization so the code can be debugged with ease, so I have edited line number 798 in the main makefile
from OPT = s to OPT = 0
And I expected the output flash size to change, But noting would change, so Where should I change to remove the compiler optimization,

Also when I hit F5 or start debugging in VScode, I would get this error

Error: Build environment 'simulator_linux_debug' is incompatible with BOARD_MALYAN_M200. Use one of these: STM32F103CB_malyan, STM32F103CB_malyan_maple
where should I change to be able to debug the code, I prefer to use jlink as debugger too.

Regards
Re: How can I debug the STM32F103 code and remove compiler optimization
January 08, 2024 05:24AM
Your all over the place.

1) vscode does not look at or use the makefile, that is for make via command line. (which is not well maintained, as very very few use it)

2) this board is using -Os
you can override it by editing ini/stm32f1.ini

In section [env : STM32F103CB_malyan]
eg
[env : STM32F103CB_malyan]
extends          = common_stm32
board            = malyanm200_f103cb
build_flags      = ${common_stm32.build_flags} -O0
                   -DHAL_PCD_MODULE_ENABLED -DDISABLE_GENERIC_SERIALUSB
                   -DHAL_UART_MODULE_ENABLED
build_src_filter = ${common.default_src_filter} + -
build_unflags    = -Os
But Note: this will not build as the code exceeds the flash space of this particularly small controller (128K flash)
forcing no optimization results in the error "region `FLASH' overflowed by 31684 bytes"

3) the default debug system in marlin is setup for the marlin simulator. See native.ini for how to set that up
This doesn't require any additional hardware
real debugging require a controller chip and a stlink or jlink type device
To set debuging to jlink
add the following to [env : STM32F103CB_malyan]
debug_tool      = jlink
build_type      = debug

Edited 7 time(s). Last edit at 01/08/2024 09:19AM by Dust.
Re: How can I debug the STM32F103 code and remove compiler optimization
January 09, 2024 07:56AM
Dear Dust
Thanks for your feedback, it really helped a lot, Now at least I know how to disable optimization,
Also Regarding debugging the code with a real chip and jlink, I have hooked a stm32f103RBT device and a jlink to my PC using win11,

Also I have added the

debug_tool      = jlink
build_type      = debug

To the stm32f1.ini file,

now this section is like this

[env: STM32F103CB_malyan]
extends          = common_stm32
board            = malyanm200_f103cb
build_flags      = ${common_stm32.build_flags}
                   -DHAL_PCD_MODULE_ENABLED -DDISABLE_GENERIC_SERIALUSB
                   -DHAL_UART_MODULE_ENABLED
build_src_filter = ${common.default_src_filter} +
debug_tool      = jlink
build_type      = debug
# build_unflags    = -Os

But when I hit F5, the code just get compiled and it would not go to debugging, the console output is like this

Quote
console
Executing task: C:\Users\Ali\.platformio\penv\Scripts\platformio.exe debug

Processing STM32F103CB_malyan (board: malyanm200_f103cb; platform: ststm32@~12.1; framework: arduino)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: [docs.platformio.org]
PLATFORM: ST STM32 (12.1.1) > Malyan M200 V1
HARDWARE: STM32F103CBT6 72MHz, 20KB RAM, 120KB Flash
DEBUG: Current (jlink) External (blackmagic, cmsis-dap, jlink, stlink)
PACKAGES:
- framework-arduinoststm32 @ 4.10900.200819 (1.9.0)
- framework-cmsis @ 2.50501.200527 (5.5.1)
- toolchain-gccarmnoneeabi @ 1.90201.191206 (9.2.1)
Converting Marlin.ino
LDF: Library Dependency Finder -> [bit.ly]
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 10 compatible libraries
Scanning dependencies...
Dependency Graph
|-- IWatchdog @ 1.0.0
|-- SPI @ 1.0
|-- EEPROM @ 2.0.1
|-- SoftwareSerial @ 1.0
|-- Servo @ 1.1.2
|-- Wire @ 1.0
Building in debug mode
Compiling .pio\build\STM32F103CB_malyan\src\src\inc\Warnings.cpp.o
Checking size .pio\build\STM32F103CB_malyan\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM: [==== ] 43.9% (used 8996 bytes from 20480 bytes)
Flash: [==========] 96.9% (used 119132 bytes from 122880 bytes)
================================================================================================== [SUCCESS] Took 3.40 seconds ==================================================================================================

Environment Status Duration
------------------ -------- ------------
STM32F103CB_malyan SUCCESS 00:00:03.400
================================================================================================== 1 succeeded in 00:00:03.400 ==================================================================================================
* Terminal will be reused by tasks, press any key to close it.

what Should I do next?
Re: How can I debug the STM32F103 code and remove compiler optimization
January 13, 2024 08:27AM
Hi Dust
Would you please guide me on how to debug the marlin code using jlink?
Re: How can I debug the STM32F103 code and remove compiler optimization
January 13, 2024 09:26AM
Sorry, only registered users may post in this forum.

Click here to login