How to write build_flags in platformio.ini

Hi all,
I’m using VScode and PlatformIO plugin to develop STM32. Actually, I’m trying to replace STM32CubeIDE with the above solution.
In most IDEs(such as MDK, CubeIDE), there is a settings page to include head files and source files.


STM32CubdeIDE

Now I generate a Cube project with STM32CubeMX and wanna to build & compile with PlatformIO. I’m trying to add build flags in file ‘platformio.ini’ to include HAL libraries & other .h files. But failed.

[platformio]
; include_dir = ./Core/Inc
src_dir = ./

[env:disco_f429zi]
platform = ststm32
board = disco_f429zi
framework = stm32cube

build_flags =
    -O0
    -D STM32F429xx
    -D USE_HAL_DRIVER
    -I Core/Inc
    -I Drivers/STM32F4xx_HAL_Driver/Inc
    -I Drivers/STM32F4xx_HAL_Driver/Inc/Legacy
    -I Drivers/CMSIS/Device/ST/STM32F4xx/Include
    -I Drivers/CMSIS/Include

; build_src_filter =
;     +<startup_stm32f429bitx.s>
;     +<Drivers/>
;     +<Core/Src>

board_build.ldscript = ./STM32F429BITX_FLASH.ld

upload_protocol = stlink
debug_tool = stlink

Error message like this:

Compiling .pio\build\disco_f429zi\FrameworkHALDriver\Src\stm32f4xx_hal_tim_ex.o
C:\Users\***\.platformio\packages\framework-stm32cubef4\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rtc_ex.c:940:10: error: conflicting types for 'HAL_RTCEx_DeactivateWakeUpTimer'
 uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from Drivers\STM32F4xx_HAL_Driver\Inc/stm32f4xx_hal_rtc.h:683:0,
                 from Core\Inc/stm32f4xx_hal_conf.h:391,
                 from Drivers\STM32F4xx_HAL_Driver\Inc/stm32f4xx_hal.h:29,
                 from C:\Users\***\.platformio\packages\framework-stm32cubef4\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rtc_ex.c:82:
Drivers\STM32F4xx_HAL_Driver\Inc/stm32f4xx_hal_rtc_ex.h:873:19: note: previous declaration of 'HAL_RTCEx_DeactivateWakeUpTimer' was here
 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** [.pio\build\disco_f429zi\FrameworkHALDriver\Src\stm32f4xx_hal_rtc_ex.o] Error 1
C:\Users\***\.platformio\packages\framework-stm32cubef4\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_tim.c:525:19: error: conflicting types for 'HAL_TIM_Base_Start_DMA'
 HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
                   ^~~~~~~~~~~~~~~~~~~~~~
In file included from Core\Inc/stm32f4xx_hal_conf.h:407:0,
                 from Drivers\STM32F4xx_HAL_Driver\Inc/stm32f4xx_hal.h:29,
                 from C:\Users\***\.platformio\packages\framework-stm32cubef4\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_tim.c:186:
Drivers\STM32F4xx_HAL_Driver\Inc/stm32f4xx_hal_tim.h:1901:19: note: previous declaration of 'HAL_TIM_Base_Start_DMA' was here
 HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, const uint32_t *pData, uint16_t Length);
                   ^~~~~~~~~~~~~~~~~~~~~~

When I add ‘include_dir’ below [platformio]

[platformio]
include_dir = ./Core/Inc ;new added
src_dir = ./

[env:disco_f429zi]
platform = ststm32
board = disco_f429zi
framework = stm32cube

build_flags =
    -O0
    -D STM32F429xx
    -D USE_HAL_DRIVER
    -I Core/Inc
    -I Drivers/STM32F4xx_HAL_Driver/Inc
    -I Drivers/STM32F4xx_HAL_Driver/Inc/Legacy
    -I Drivers/CMSIS/Device/ST/STM32F4xx/Include
    -I Drivers/CMSIS/Include

; build_src_filter =
;     +<startup_stm32f429bitx.s>
;     +<Drivers/>
;     +<Core/Src>

board_build.ldscript = ./STM32F429BITX_FLASH.ld

upload_protocol = stlink
debug_tool = stlink

A simmilar error is still there:

Compiling .pio\build\disco_f429zi\FrameworkHALDriver\Src\stm32f4xx_hal_smartcard.o
C:\Users\***\.platformio\packages\framework-stm32cubef4\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rtc_ex.c:940:10: error: conflicting types for 'HAL_RTCEx_DeactivateWakeUpTimer'
 uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from Drivers\STM32F4xx_HAL_Driver\Inc/stm32f4xx_hal_rtc.h:683:0,
                 from Core\Inc/stm32f4xx_hal_conf.h:391,
                 from Drivers\STM32F4xx_HAL_Driver\Inc/stm32f4xx_hal.h:29,
                 from C:\Users\***\.platformio\packages\framework-stm32cubef4\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rtc_ex.c:82:
Drivers\STM32F4xx_HAL_Driver\Inc/stm32f4xx_hal_rtc_ex.h:873:19: note: previous declaration of 'HAL_RTCEx_DeactivateWakeUpTimer' was here
 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiling .pio\build\disco_f429zi\FrameworkHALDriver\Src\stm32f4xx_hal_smbus.o
*** [.pio\build\disco_f429zi\FrameworkHALDriver\Src\stm32f4xx_hal_rtc_ex.o] Error 1
===================================================================== [FAILED] Took 9.42 seconds =====================================================================

I’m wandering what the correct writing should look like. I’ve read the platformio docunment and searched with google, bing and github, but no solution yet. Any ideas?

This heavily conflicts. The framework = stm32cube is telling PlatformIO to pull in its sources for the STM32F4xx HAL etc., but then you also put the STM32F4xx HAL in the project itself, in a version that’s even different to what PlatformIO has in its registry. So, the src/ files only need to be the actual firmware, none of the CMSIS or HAL driver stuff. See https://github.com/platformio/platform-ststm32/tree/develop/examples/stm32cube-hal-blink for reference.

You must decide whether you want to use PlatformIO’s STM32F4xx HAL package (which might be slightly outdated), or your files. If you want to use the generated STM32CubeMX project files as-is, it’s probably safer to use STM32CubeMX’s sources.

For that, you need to delete the framework = ... line to create a bare-metal project. With that, PlatformIO will only compile the sources that are in src/ or lib/<subfolder>. However, this also puts the burden on you to provide all files, that is including the right startup.S file and system_...c file (should be somewhere in src/), and the right linker.ld file (which should be references as board_build.ldscript = <relative path to .ld file> in the platformio.ini).

The tool https://github.com/ussserrr/stm32pio automates this conversion process.

1 Like

Hi,
Thanks for your relpy!
I remove ‘framework = stm32cube’ under ‘[env:xxx]’ and it works. PlatformIO uses its HAL lib not mine XD.I will try the tool ‘stm32pio’, thanks.

And, by the way, is it necessary to write ‘build_src_filter’ and ‘include_dir’ after I remove ‘include_dir’ ?