Region `FLASH' overflowed on bluepill_f103c8 with mbed

I’m trying to work with an SD card on the bluepill_f103c8 with the mbed framework but the program can’t be compiled. I reduced the code so it’s easier to reproduce the issue.

Main.cpp:

#include <mbed.h>
#include "FATFileSystem.h"
#include "SDBlockDevice.h"
SDBlockDevice sd(B15 ,B14,B13,B12);
FATFileSystem fs("sd", &sd);
Serial pc(B6,B7,57600);//TX, RX,BAUD RATE
int main()
 {

    // put your setup code here, to run once:
    pc.printf("START\n");
    while(1) {
        // put your main code here, to run repeatedly:
    }
}

Platformio.ini:

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = mbed
build_flags = -DDEVICE_RTC=1 -D PIO_FRAMEWORK_MBED_RTOS_PRESENT -D PIO_FRAMEWORK_MBED_EVENTS_PRESENT -D PIO_FRAMEWORK_MBED_FILESYSTEM_PRESENT

(while not all of these flags are used in the code i need them for what i’m going to do)

Console output with verbose :

[04/20/18 12:44:56] Processing bluepill_f103c8 (platform: ststm32; build_flags: -DDEVICE_RTC=1 -D PIO_FRAMEWORK_MBED_RTOS_PRESENT; board: bluepill_f103c8; framework: mbed)

PLATFORM: ST STM32 > BluePill F103C8
SYSTEM: STM32F103C8T6 72MHz 20KB RAM (64KB Flash)
DEBUG: CURRENT(blackmagic) EXTERNAL(blackmagic, jlink, stlink)
Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF MODES: FINDER(chain) COMPATIBILITY(light)
Collected 19 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <mbed-rtos> (C:\.platformio\packages\framework-mbed\rtos)
|-- <SDBlockDevice> (C:\Users\?scar Garc?a\Documents\Semestre 9\Makerlab\Test\lib\SDBlockDevice)
|   |-- <mbed-filesystem> (C:\.platformio\packages\framework-mbed\features\filesystem)
|-- <mbed-filesystem> (C:\.platformio\packages\framework-mbed\features\filesystem)
arm-none-eabi-g++ -o .pioenvs\bluepill_f103c8\firmware.elf -mcpu=cortex-m3 -mthumb -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 -Wl,-T".pioenvs\bluepill_f103c8\STM32F103XB.ld.link_script.ld" @"C:\Users\?scar Garc?a\Documents\Semestre 9\Makerlab\Test\.pioenvs\bluepill_f103c8\longcmd-a4f60a5b6360b5bb3ab2fe4191b12cd7" -LC:\.platformio\platforms\ststm32\ldscripts -L.pioenvs\bluepill_f103c8 -Wl,--start-group .pioenvs\bluepill_f103c8\lib576\libSDBlockDevice.a -lstdc++ -lsupc++ -lm -lc -lgcc -lnosys -lc -lstdc++ -Wl,--end-group

C:\.platformio\packages\tool-scons\script\..\engine\SCons\Environment.py:1303: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
val = [x for x in val if x not in dk]
c:/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe: .pioenvs\bluepill_f103c8\firmware.elf section `.text' will not fit in region `FLASH'
c:/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe: region `FLASH' overflowed by 14800 bytes

collect2.exe: error: ld returned 1 exit status
*** [.pioenvs\bluepill_f103c8\firmware.elf] Error 1
 [ERROR] Took 4.32 seconds

Home Version: 0.9.5
Core Version: 3.5.3a9
ST STM32 Version: 4.2.0

Well, the error tells you that the compiled code is too big to fit in the flash memory of the device. The bluepill board is registered with 64K and your firmware is ~14K above that. Besides trying to cut down firmware size somehow below the maximum 64 kilobytes (e.g. using -Os / -O3 flags), there’s also a second hack: Most bluepill boards seem to actually have 128kB of flash (which would make your program fit).

You can use STM32 ST-Link utility (registered download http://www.st.com/en/development-tools/stsw-link004.html) to connect to the target to see the actual flash size.

You could then modify the linker script to allow for 128K flash size. In .platformio\platforms\ststm32\boards\bluepill_f103c8.json change maximum_size to 131072 and in .platformio\platforms\ststm32\ldscripts LENGTH = 64K to LENGTH = 128K. That will make the firmware compile. You could then activate the generation of a GCC map file with -Wl,-Map,firmware.map and use tools such as amap to analyze what functions are taking up the flash space and try to optimize it.

I have only observed that firmwares above 64K compiled with STM32Cube were still working on my bluepill board, but I don’t know if that universally works or whether you have to change more things. Might be worth a try.

If that doesn’t work you’d either need to a different frameworks (Arduino, STM32Cube) and libraries in hopes that the produced code will be small enough to fit, or switch to another board. I find the Nucleo64 boards convenient. The Nucleo F103RB would give you the same peripherals as the bluepill (F103C8) but with 128K flash. Or a Nucleo L152-RE with 512K flash + 80K RAM.