Wrong binary size when building project for STM32C031K6 using stm32cube framework

Hi all!

I would like to ask for some assistance regarding my project for an STM32C031K6 board using the stm32cube framework.

I’ve started out based on the following arduino based project from @maxgerhardt :

I’ve also took some inspiration from here which is a platformio project using STM32 HAL and an STM32C09 MCU:

Initial problems that I solved:
using framework = stm32cube fails with the following error KeyError: ‘framework-stm32cubec0’ because platformio/platform-ststm32 does not support STM32C0, specifically the following file does not include the above mentioned key: platform-ststm32/platform.json

Therefore I’m using upload_protocol = custom. This is my platformio.ini:

[platformio]
src_dir = .
include_dir = .

[env:stm32c0]
platform  = ststm32
board     = stm32c0

upload_protocol = custom
upload_command = openocd -d2 -s /home/user/stm32c0-project/packages/xpack-openocd-0.12.0-7/openocd/scripts -f interface/stlink.cfg -c "transport select swd" -f target/stm32c0x.cfg -c "program {.pio/build/stm32c0/firmware.elf} 0x8000000  verify reset; shutdown;"
platform_packages = tool-openocd@symlink://packages/xpack-openocd-0.12.0-7

build_flags =
    -Iinclude/
    -Iboards/TARGET_STM32C0/STM32C0xx_HAL_Driver/Inc
    -Iboards/TARGET_STM32C0/STM32C0xx_HAL_Driver/Inc/Legacy
    -Iboards/TARGET_STM32C0/CMSIS/Include
    -Iboards/TARGET_STM32C0/CMSIS/Device/ST/STM32C0xx/Include
    -DUSE_HAL_DRIVER
    -DSTM32C031xx

I’m also deleteing libdcc from packages/xpack-openocd-0.12.0-3-win32-x64/openocd/contrib/libdcc, otherwise it gets compiled with the rest of the code resulting in errors.

I also have the full STM32 HAL and CMSIS libraries added to the project:

The result is successful build with the following output:

Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]  12.9% (used 1580 bytes from 12288 bytes)
Flash: [====      ]  41.4% (used 13576 bytes from 32768 bytes)
Building .pio/build/stm32c0/firmware.bin

This would be fine, however my eventual binary is ~77KB, which does not fit onto the 32KB flash of my STM32C031.

Running the upload command, I’m getting the following output, which also aligns well with the issue of my .bin not fitting onto the flash:

** Programming Started **
Info : device idcode = 0x10016453 (STM32C03xx - Rev Z : 0x1001)
Info : RDP level 0 (0xAA)
Info : flash size = 32 KiB
Info : flash mode : single-bank
Info : device idcode = 0x10016453 (STM32C03xx - Rev Z : 0x1001)
Info : RDP level 0 (0xAA)
Info : OTP size is 1024 bytes, base address is 0x1fff7000
Warn : no flash bank found for address 0x08008000
Warn : no flash bank found for address 0x0801b0e8
** Programming Finished **
** Verify Started **
Error: [stm32c0x.cpu] clearing lockup after double fault
Error: [stm32c0x.cpu] error executing cortex_m crc algorithm
Error: Failed to read memory at 0x08008ff0
embedded:startup.tcl:1896: Error: ** Verify Failed **
Traceback (most recent call last):
  File "embedded:startup.tcl", line 1957, in program
    program_error {** Verify Failed **} 0
  File "embedded:startup.tcl", line 1896, in program_error
    error {** Verify Failed **}
*** [upload] Error 1

When I inspect the binary file, I see that the amount of that is indeed 13576 bytes, and the other ~65KB is zeros.

I was experimenting with adjusting the stm32c0x.cfg file of openocd, trying variations of the flash bank and program command to try to reduce the .bin size, but without success.

Would you be able to assist with this issue?

Thank you in advance!

Can you upload your exact PlatformIO project folder? It might jsut be something in the linker script, or the binary really is that large and the size is not correctly displayed because it goes into ELF sections that platform-ststm32 does not expect.

Hi @maxgerhardt,

Thanks for the quick reply!

I’m sending the project folder through wetransfer, hope that is okay.

Some additional notes:

  • I am using Ubuntu 22.04
  • For the upload command to work, first chmod +x packages/xpack-openocd-0.12.0-7/bin/openocd has to be run

Thank you for your help!

You’re not using a linker script at all to link the final .elf (from which the .bin is created).

Just take (close enough for a C031K6)

https://github.com/STMicroelectronics/STM32CubeC0/blob/main/Projects/STM32C0316-DK/Templates/STM32CubeIDE/STM32C031C6TX_FLASH.ld

copy it in the project folder (at the same level as the platformio.ini)

Text replace all occurances of (READONLY) with an empty string (the default arm-none-eaib-gcc does not understand that, higher versions toolchain-gccarmnoneeabi will) and set

board_build.ldscript = STM32C031C6TX_FLASH.ld

in the platformio.ini, then it should produce a .bin of the right size (about 9K).