STM32 mbed: very large compile size

I’m using an STM32 Nucleo L031K6 with the mbed framework. The blink example is producing much larger binaries than are necessary for such a small program:

DATA:    [=         ]  12.3% (used 1008 bytes from 8192 bytes)
PROGRAM: [======    ]  58.9% (used 19292 bytes from 32768 bytes)
.pio/build/nucleo_l031k6/firmware.elf  :
section             size        addr
.text              18892   134217728
.ARM.exidx             8   134236620
.data                392   536871104
.bss                 616   536871496
.heap               2896   536872112
.ARM.attributes       40           0
.comment             126           0
.debug_info        16639           0
.debug_abbrev       5969           0
.debug_aranges      4920           0
.debug_ranges       5992           0
.debug_line        25166           0
.debug_str         19602           0
.debug_frame       15972           0
Total             117230

This is with everything set to default values except buildflags = -Os in platformio.ini.

I’ve tried using newlib-nano specs as instructed here but it caused an error while linking. Even with --verbose there isn’t much of use on the output:

arm-none-eabi-cpp -E -P -T .pio/build/nucleo_l031k6/STM32L031K6.ld.link_script.ld -Wl,--gc-sections -Wl,--wrap,main -Wl,--wrap,_malloc_r -Wl,--wrap,_free_r -Wl,--wrap,_realloc_r -Wl,--wrap,_memalign_r -Wl,--wrap,_calloc_r -Wl,--wrap,exit -Wl,--wrap,atexit -Wl,-n --specs=nano.specs -mcpu=cortex-m0plus -mthumb -DMBED_ROM_START=0x8000000 -DMBED_ROM_SIZE=0x8000 -DMBED_RAM_START=0x20000000 -DMBED_RAM_SIZE=0x2000 -DMBED_BOOT_STACK_SIZE=4096 --specs=nano.specs /Users/richard/.platformio/packages/framework-mbed/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L031K6/device/TOOLCHAIN_GCC_ARM/STM32L031K6.ld -o .pio/build/nucleo_l031k6/STM32L031K6.ld.link_script.ld
========================================================================================================================== [ERROR] Took 47.74 seconds ==========================================================================================================================

For repeatability, the project was created in VS Code with the “Nucleo L031K6” board, then main.cpp from the example was copy-pasted into src/main.cpp. buildflags = -Os was added to platformio.ini.

Is there a way get below 19kB for a simple blink program? (I am writing a more complex program but the problems remain).

Looks like part of the problem might be that the PlatformIO Mbed implementation requires you to include the RTOS: Mbed double framework · Issue #819 · platformio/platformio-core · GitHub. Still, it looks like it might also be a general problem with Mbed: Bare-metal profile by theotherjimmy · Pull Request #9800 · ARMmbed/mbed-os · GitHub shows that the bare metal profile still results in 30kB binaries.

Considering that the docs say

By default, project is built without the OS feature.

I’d double-check that. Can you add

#ifdef MBED_CONF_RTOS_PRESENT
#error "RTOS present"
#else
#error "RTOS not present"
#endif

to the main.cpp and check the compiler output?

You’re right, it errors on #error "RTOS not present". Looks like the large size is just the standard Mbed libraries.

I’ve tried adding lib_ignore = mbed-rtos, mbed-events, mbed-fs, mbed-net, mbed-rpc, mbed-dsp, mbed-USBHost, mbed-USBDevice to platformio.ini, but it didn’t change the binary size.

Can you provide a linker .map file for the correct smal blinky example for this microcontroller when compiled with mbed? Then we can compare it with the PIO map file (build_flags = -Wl,-Map,output.map) and amap.

Hi, I am facing a similar issue with a code that is expected to take only 800 bytes, shows upto 14K program size. Could you solve this issue already? See the memory map attached.

We would need to see your full code and platformio.ini as well as a reference binary and the information with which mbed-os version that was compiled.

Hi
Thanks for the reply.

Here is the original code which my current code is based on:

I configured this code for “stm32l0xx” microcontroller (updated the CMSIS folder and modified accordingly) and built the project using VSCode+PlatformIO.
Please see the platformio.ini and reference binary file attached.

Also, please note that I have disabled the fault handler using the build_flag
-D MBED_FAULT_HANDLER_DISABLED

Current Memory Usage →
DATA: [== ] 17.4% (used 3560 bytes from 20480 bytes)
PROGRAM: [= ] 8.9% (used 17424 bytes from 196608 bytes)

How do I see the current mbed OS version from the project? Any shortcuts?
I can see the version as 5.11.5 looking at the file mbed_version.h, after an explorer search.

I also tried the blinky code for stm32l073rz: The results were as below,

Memory Usage -> http://bit.ly/pio-memory-usage
DATA:    [==        ]  19.8% (used 4056 bytes from 20480 bytes)
PROGRAM: [===       ]  27.7% (used 54368 bytes from 196608 bytes)

platformio.ini contents:
	[env:Blinky]
	platform = ststm32
	framework = mbed
	board = nucleo_l073rz
	build_flags = ${env.build_flags}
	   -D STM32_Blinky

with newlib-nano, the size reduced as below,
Memory Usage → Redirecting...
DATA: [= ] 9.6% (used 1968 bytes from 20480 bytes)
PROGRAM: [== ] 19.2% (used 37676 bytes from 196608 bytes)

platformio.ini contents:
[env:Blinky]
platform = ststm32
framework = mbed
board = nucleo_l073rz
build_flags = ${env.build_flags}
   -D STM32_Blinky
extra_scripts = linker_flags_newlib-nano.py

This 37K for blinky code is too much, isnt it?

How will I share my files here? I can only see an option to attach images.