What is the proper way to build code with custom framework

Hi! I am trying to build ChibiOS based app with platformio. I created new project based on cube32 framework. I need to remove cube part from the build, but dont know how to properly remove it from the build scripts. What is the proper way to build it?

platformio.ini content:

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = stm32cube
board_build.ldscript = $PROJECT_DIR/include/ChibiOS/os/common/startup/ARMCMx/compilers/GCC/ld/STM32F103x8.ld
build_flags =
    -DUSE_LINK_GC=yes
    -DUSE_LTO=yes
    -DUSE_SMART_BUILD=yes
    -Wl,--defsym=__process_stack_size__=0x400
    -Wl,--defsym=__main_stack_size__=0x400
    -L$PROJECT_DIR/include/ChibiOS/os/common/startup/ARMCMx/compilers/GCC/ld
    -I$PROJECT_DIR/include/cfg
    -I$PROJECT_DIR/include/ChibiOS/os/common/ports/ARMCMx
    -I$PROJECT_DIR/include/ChibiOS/os/common/ports/ARMCMx/compilers/GCC
    -I$PROJECT_DIR/include/ChibiOS/os/common/startup/ARMCMx/devices/STM32F1xx
    -I$PROJECT_DIR/include/ChibiOS/os/rt/include
    -I$PROJECT_DIR/include/ChibiOS/os/oslib/include
    -I$PROJECT_DIR/include/ChibiOS/os/hal/include
    -I$PROJECT_DIR/include/ChibiOS/os/hal/osal/rt-nil
    -I$PROJECT_DIR/include/ChibiOS/os/hal/ports/STM32/STM32F1xx
    -I$PROJECT_DIR/include/ChibiOS/os/hal/ports/STM32/LLD
    -I$PROJECT_DIR/include/ChibiOS/os/hal/ports/STM32/LLD/DMAv1
    -I$PROJECT_DIR/include/ChibiOS/os/hal/ports/STM32/LLD/TIMv1
    -I$PROJECT_DIR/include/ChibiOS/os/hal/ports/STM32/LLD/GPIOv1
    -I$PROJECT_DIR/include/ChibiOS/os/hal/boards/STM32F103C8_MINIMAL
    -I$PROJECT_DIR/include/ChibiOS/os/hal/ports/Common/ARMCMx
    -I$PROJECT_DIR/include/ChibiOS/os/license

main.c content:

#include "ch.h"
#include "hal.h"

static THD_WORKING_AREA(waThread1, 0);
static THD_FUNCTION(Thread1, arg) {
  (void)arg;
  chRegSetThreadName("blinker");

GPIOv1
  while (true) {
    chThdSleepMilliseconds(500);
  }
}

int main(void) {
  halInit();
  chSysInit();
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL);

  while (true) {
    chThdSleepSeconds(1);
  }

  return 0;
}

build log:

 Executing task: C:\Users\ionbuggy\.platformio\penv\Scripts\platformio.exe run <

Processing bluepill_f103c8 (platform: ststm32; board: bluepill_f103c8; framework: stm32cube)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/bluepill_f103c8.html
PLATFORM: ST STM32 (8.1.0) > BluePill F103C8
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (stlink) External (blackmagic, jlink, stlink)
PACKAGES:
 - framework-stm32cube 2.0.200813
 - tool-ldscripts-ststm32 0.1.0
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 11 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <BSP-lis302dl>
|   |-- <BSP-Common>
Building in release mode
Compiling .pio\build\bluepill_f103c8\src\main.o
Archiving .pio\build\bluepill_f103c8\lib308\libCommon.a
Compiling .pio\build\bluepill_f103c8\lib422\lis302dl\lis302dl.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_adc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_adc_ex.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_can.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_cec.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_cortex.o
In file included from include\ChibiOS\os\hal\include/hal.h:29:0,
                 from src\main.c:2:
include\ChibiOS\os\hal\boards\STM32F103C8_MINIMAL/board.h:45:0: warning: "STM32F103xB" redefined
 #define STM32F103xB
 
<command-line>:0:0: note: this is the location of the previous definition
Archiving .pio\build\bluepill_f103c8\lib422\liblis302dl.a
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_crc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_dac.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_dac_ex.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_dma.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_eth.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_exti.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_flash.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_flash_ex.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_gpio.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_gpio_ex.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_hcd.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_i2c.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_i2s.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_irda.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_iwdg.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_mmc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_nand.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_nor.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_pccard.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_pcd.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_pcd_ex.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_pwr.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_rcc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_rcc_ex.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_rtc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_rtc_ex.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_sd.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_smartcard.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_spi.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_sram.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_tim.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_tim_ex.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_uart.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_usart.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_hal_wwdg.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_adc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_crc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_dac.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_dma.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_exti.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_fsmc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_gpio.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_i2c.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_pwr.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_rcc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_rtc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_sdmmc.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_spi.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_tim.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_usart.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_usb.o
Compiling .pio\build\bluepill_f103c8\FrameworkHALDriver\Src\stm32f1xx_ll_utils.o
Compiling .pio\build\bluepill_f103c8\FrameworkCMSISDevice\gcc\startup_stm32f103xb.o
Compiling .pio\build\bluepill_f103c8\FrameworkCMSISDevice\system_stm32f1xx.o
Archiving .pio\build\bluepill_f103c8\libFrameworkCMSISDevice.a
Archiving .pio\build\bluepill_f103c8\libFrameworkHALDriver.a
Linking .pio\build\bluepill_f103c8\firmware.elf
.pio\build\bluepill_f103c8\src\main.o: In function `Thread1':
main.c:(.text.Thread1+0xe): undefined reference to `chThdSleep'
main.c:(.text.Thread1+0x14): undefined reference to `ch'
.pio\build\bluepill_f103c8\src\main.o: In function `main':
main.c:(.text.startup.main+0x2): undefined reference to `halInit'
main.c:(.text.startup.main+0x6): undefined reference to `chSysInit'
main.c:(.text.startup.main+0x16): undefined reference to `chThdCreateStatic'
main.c:(.text.startup.main+0x1e): undefined reference to `chThdSleep'
.pio\build\bluepill_f103c8\libFrameworkCMSISDevice.a(startup_stm32f103xb.o): In function `LoopFillZerobss':
(.text.Reset_Handler+0x34): undefined reference to `_sidata'
(.text.Reset_Handler+0x38): undefined reference to `_sdata'
(.text.Reset_Handler+0x40): undefined reference to `_sbss'
(.text.Reset_Handler+0x44): undefined reference to `_ebss'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\bluepill_f103c8\firmware.elf] Error 1

So I removed framework from platofmio.ini and added few more includes.

platformio.ini looks like this:

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
board_build.ldscript = $PROJECT_DIR/lib/ChibiOS/os/common/startup/ARMCMx/compilers/GCC/ld/STM32F103x8.ld
lib_extra_dirs = $PROJECT_DIR/lib/ChibiOS
build_flags =
    -DUSE_LINK_GC=yes
    -DUSE_LTO=yes
    -DUSE_SMART_BUILD=yes
    -Wl,--defsym=__process_stack_size__=0x400
    -Wl,--defsym=__main_stack_size__=0x400
    -L$PROJECT_DIR/lib/ChibiOS/os/common/startup/ARMCMx/compilers/GCC/ld
    -I$PROJECT_DIR/src/cfg
    -I$PROJECT_DIR/lib/ChibiOS/os/common/ports/ARMCMx
    -I$PROJECT_DIR/lib/ChibiOS/os/common/ports/ARMCMx/compilers/GCC
    -I$PROJECT_DIR/lib/ChibiOS/os/common/startup/ARMCMx/devices/STM32F1xx
    -I$PROJECT_DIR/lib/ChibiOS/os/common/startup/ARMCMx/compilers/GCC/ld
    -I$PROJECT_DIR/lib/ChibiOS/os/common/ext/ST/STM32F1xx
    -I$PROJECT_DIR/lib/ChibiOS/os/common/ext/ARM/CMSIS/Core/Include
    -I$PROJECT_DIR/lib/ChibiOS/os/rt/include
    -I$PROJECT_DIR/lib/ChibiOS/os/oslib/include
    -I$PROJECT_DIR/lib/ChibiOS/os/hal/include
    -I$PROJECT_DIR/lib/ChibiOS/os/hal/osal/rt-nil
    -I$PROJECT_DIR/lib/ChibiOS/os/hal/ports/STM32/STM32F1xx
    -I$PROJECT_DIR/lib/ChibiOS/os/hal/ports/STM32/LLD
    -I$PROJECT_DIR/lib/ChibiOS/os/hal/ports/STM32/LLD/DMAv1
    -I$PROJECT_DIR/lib/ChibiOS/os/hal/ports/STM32/LLD/TIMv1
    -I$PROJECT_DIR/lib/ChibiOS/os/hal/ports/STM32/LLD/GPIOv1
    -I$PROJECT_DIR/lib/ChibiOS/os/hal/boards/STM32F103C8_MINIMAL
    -I$PROJECT_DIR/lib/ChibiOS/os/hal/ports/Common/ARMCMx
    -I$PROJECT_DIR/lib/ChibiOS/os/license

But now I have a problem with nothing included in the linker.

here is the log:

Executing task: C:\Users\ionbuggy\.platformio\penv\Scripts\platformio.exe run <

Processing bluepill_f103c8 (platform: ststm32; board: bluepill_f103c8)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/bluepill_f103c8.html
PLATFORM: ST STM32 (8.1.0) > BluePill F103C8
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (stlink) External (blackmagic, jlink, stlink)
PACKAGES:
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 10 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\bluepill_f103c8\src\main.o
Linking .pio\build\bluepill_f103c8\firmware.elf
c:/users/ionbuggy/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe: warning: cannot find entry symbol Reset_Handler; defaulting to 0800000c
Checking size .pio\build\bluepill_f103c8\firmware.elf
Building .pio\build\bluepill_f103c8\firmware.bin
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   5.5% (used 1132 bytes from 20480 bytes)
Flash: [          ]   2.2% (used 1420 bytes from 65536 bytes)

Where to look for this problem?

So you already have ChibiOS as a library. Then just create a [library.json] in lib/ChibiOS that has all the include settings, so you don’t need that armada of extra -I build flags in your platformio.ini.

Are you compiling in the .S startup file which defines the Reset_Handler?

Thanks for the response.

Any good examples of library.json?

Everything I changes is in the post. How to check that .S is compiled? Or how to add it?

Ah I see that you have the startup file… in the library. What file(s) is in there? Can you try adding lib_archive = no (docs) to the platformio.ini? Since the library is built as an archive file and then linked, the linker might not see these specific startup symbols…

There is only one file in $PROJECT_DIR/lib/ChibiOS/os/common/startup/ARMCMx/devices/STM32F1xx cmparams.h

.S files are located in $PROJECT_DIR/lib/ChibiOS/os/common/startup/ARMCMx/compilers/GCC

  • crt0_v6m.S
  • crt1.c
  • vectors.S

I tried to find how to include these .S files in the build, but found nothing. Any ideas how to add it?

They are already contained in the lib/ folder, so they will just be compiled normally. No need to add any further include flags.

You can also check this from the normal compilation output of clean → Build. The .S files should be in the liste there.

Have you tried this? The symbol it doesn’t find is definitely in the startup file, but might not be linked correctly.

Yes, literally nothing changes.

Here is the log of clean and build

Executing task: C:\Users\ionbuggy\.platformio\penv\Scripts\platformio.exe run --target clean <

Processing bluepill_f103c8 (platform: ststm32; board: bluepill_f103c8)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Removed .pio\build\bluepill_f103c8\.sconsign37.dblite
Removed .pio\build\bluepill_f103c8\firmware.bin
Removed .pio\build\bluepill_f103c8\firmware.elf
Removed .pio\build\bluepill_f103c8\src\main.o
Done cleaning
=============================================================================================== [SUCCESS] Took 0.59 seconds ===============================================================================================
Executing task: C:\Users\ionbuggy\.platformio\penv\Scripts\platformio.exe run <

Processing bluepill_f103c8 (platform: ststm32; board: bluepill_f103c8)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/bluepill_f103c8.html
PLATFORM: ST STM32 (8.1.0) > BluePill F103C8
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (stlink) External (blackmagic, jlink, stlink)
PACKAGES:
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 10 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\bluepill_f103c8\src\main.o
Linking .pio\build\bluepill_f103c8\firmware.elf
c:/users/ionbuggy/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/bin/ld.exe: warning: cannot find entry symbol Reset_Handler; defaulting to 0800000c
Checking size .pio\build\bluepill_f103c8\firmware.elf
Building .pio\build\bluepill_f103c8\firmware.bin
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   5.5% (used 1132 bytes from 20480 bytes)
Flash: [          ]   2.2% (used 1420 bytes from 65536 bytes)
=============================================================================================== [SUCCESS] Took 5.11 seconds ===============================================================================================

Looks like it does not build anything.

It doesn’t pick up the ChibiOS dependency at all. Have you set lib_deps = ChibiOS in the platformio.ini?

After adding this it starts to build everything including demos and fails. So I think something else is needed. What is the proper way of ignoring demos and other MCU’s files?

For redistribution I think you can just remove the Demo folders. Usually a library of this complexity contains a library.json which has the srcFilter configuration option. There are also examples available there.

I just cloned ChibiOS distribution into lib folder, so it’s not library. Any way to pass only certain folders for compiler with just platformio.ini?

Any folder you put into lib/ is by definition a library.

Yes, src_filter is also available in the platformio.ini (docs). Example

; include everything, then exclude certain folder names
src_filter = +<*> -<.git/> -<.svn/> -<example/> -<examples/> -<test/> -<tests/>

I’m not sure however whether this also applies to the libraries that are built – just put that string then in the library.json according to the documentation I posted earlier using srcFilter.

1 Like