Strange algorithm of determining chip flash memory size in framework mbed@5.0.0 (platform STM32)

I’m building my project with the following platformio.ini:

[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
framework = mbed
build_flags = -DHSE_VALUE=12000000U
lib_deps = https://os.mbed.com/users/hudakz/code/USBDevice_STM32F103/
lib_ignore = mbed-USBDevice

The platform/hardware is determined as:

PLATFORM: ST STM32 > STM32F103C8 (20k RAM. 64k Flash)
HARDWARE: STM32F103C8T6 72MHz 20KB RAM (64KB Flash)

The output is:

Linking .pioenvs\genericSTM32F103C8\firmware.elf
Checking size .pioenvs\genericSTM32F103C8\firmware.elf
Building .pioenvs\genericSTM32F103C8\firmware.bin
Error: The program size (67092 bytes) is greater than maximum allowed (65536 bytes)

So far so good. I’ve noticed the build process copies linker script file from targets\TARGET_STM\TARGET_STM32F1\TARGET_BLUEPILL_F103C8\device\TOOLCHAIN_GCC_ARM\STM32F103XB.ld. The flash size is specified there as 64K. So far so good.

First fix attempt

I’ve edited that file and set flash size to 128K. Then I built the program again and checked that the script file was copied to .pioenvs\genericSTM32F103C8 folder. But I got still the same output! The board is still supposed to have only 64K of flash memory!

By building the program in verbose mode I’ve verified that linker was using the correct linker script: arm-none-eabi-g++ -o .pioenvs\genericSTM32F103C8\firmware.elf -T .pioenvs\genericSTM32F103C8\STM32F103XB.ld.link_script.ld .... Why am I getting the error?

Second fix attempt

I’ve noticed that it looks like the error comes not from linker, but from firmware size checker. My second attempt was to edit boards\genericSTM32F103C8.json file inside ststm32 platform instead. I set max_size to 131072 there. And guess what? Now the build process is finished succesfully!

The platform/hardware is now determined as:

PLATFORM: ST STM32 > STM32F103C8 (20k RAM. 64k Flash)
HARDWARE: STM32F103C8T6 72MHz 20KB RAM (128KB Flash)

The build process output is:

Linking .pioenvs\genericSTM32F103C8\firmware.elf
Building .pioenvs\genericSTM32F103C8\firmware.bin
Checking size .pioenvs\genericSTM32F103C8\firmware.elf
Memory Usage -> http://bit.ly/pio-memory-usage
DATA:    [===       ]  33.5% (used 6864 bytes from 20480 bytes)
PROGRAM: [=====     ]  51.2% (used 67092 bytes from 131072 bytes)

Question

As far as I can tell, the error comes from Checking size step, not from the linker. Why the linker doesn’t complain when linking the program for 64K flash memory? It should not fit, but, apparently, there is no linker error. BIN file is created with 67092 bytes size.

So what happens if you use the STM32F103CB (20k RAM. 128k Flash) — PlatformIO latest documentation target?

It doesn’t support Mbed framework out of the box. Do you want me to repeat all steps for it as I did for genericSTM32F103C8?

You can set a custom maximum upload size. See Redirecting...

Yes, I know, thanks. I was curious about how it works without manual intervention :wink: