Adding FreeRTOS lib with stm32cube framework

Hi,

I’d like to integrate in my project the FreeRTOS lib but so far I haven’t been able.
If I had “lib_deps = FreeRTOS” it downloads arduino lib for FreeRTOS.
I thought that FreeRTOS would come with the stm32cube package but I’m not able to enabled it.
Can you help me figuring it out?

Below my .ini file.

[env:nucleo_f767zi]
platform = ststm32
board = nucleo_f767zi
framework = stm32cube
upload_protocol = stlink
debug_tool = stlink
build_type = debug
build_flags = 
	-D USE_FULL_ASSERT
	-D DEBUG
	-D SERIAL_DEBUG

There is no default / one-click option to add the FreeRTOS middleware to the project via just one platformio.ini option.

The easiest one is, if you have your existing STM32Cube project in the graphical configurator, to enable the FreeRTOS middleware there, let it generate all the code, then copy-paste all the code files over to the PlatformIO project, along with the compiler and linker settings. That way, the configurator will also take care of the config for other peripherals. Specifically, if you tell FreeRTOS to use the SysTick as its time base, then stuff like the default HAL_Delay() implementation won’t work anymore, because that also tries to use SysTick. Technically, it will use HAL_GetTick(), which is by default to SysTick.

The other one is to start from scratch, as in

  • copy over the contents of C:\Users\<user>\.platformio\packages\framework-stm32cubeh7\Middlewares\Third_Party\FreeRTOS\Source into lib/FreeRTOS of the project
    • remove the non-applicable files from the source tree again, that is, in the portable/ folder folder everything except GCC/ARM_CM7 and MemMang/heap_4.c, and the CMSIS_RTOS and CMSIS_RTOS_V2 folders
  • copy C:\Users\<user>\.platformio\packages\framework-stm32cubeh7\Middlewares\Third_Party\FreeRTOS\Source\include\FreeRTOSConfig_template.h into include/ of the project
  • configure linker and include settings in the platformio.ini to work with PlatformIO, see here for a Cortex-M4, Cortex-M7 will be different
  • copy C:\Users\<user>\.platformio\packages\framework-stm32cubef7\Drivers\STM32F7xx_HAL_Driver\Src\stm32f7xx_hal_timebase_tim_template.c to src/ to take care of the HAL_Delay() being based on a hardware timer instead of SysTick
  • initialize the task scheduler in your code (xTaskCreate() and finally vTaskStartScheduler();)

I can’t test it on real hardware, but here’s the project I created for that:

Hi Max, thanks for the help.
I get the following error:

 #include "stm32f7xx_hal_conf.h"
          ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
*** [.pio/build/nucleo_f767zi/FrameworkHALDriver/Src/stm32f7xx_hal_cryp_ex.o] Error 1
*** [.pio/build/nucleo_f767zi/FrameworkHALDriver/Src/stm32f7xx_hal_dac.o] Error 1
In file included from /Users/danilomessina/.platformio/packages/framework-stm32cubef7/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_dac_ex.c:38:0:
/Users/danilomessina/.platformio/packages/framework-stm32cubef7/Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal.h:29:10: fatal error: stm32f7xx_hal_conf.h: No such file or directory

But I have that file:

My.ini file

[env:nucleo_f767zi]
platform = ststm32
board = nucleo_f767zi
framework = stm32cube
upload_protocol = stlink
debug_tool = stlink
build_type = debug
monitor_port = /dev/cu.usbmodem1103
board_build.ldscript = STM32F767ZITX_FLASH.ld
board_build.stm32cube.custom_config_header = yes
lib_deps = FreeRTOS
build_flags =
  -mfpu=fpv4-sp-d16
  -mfloat-abi=softfp
  -Ilib/FreeRTOS/include
  -Ilib/FreeRTOS/CMSIS_RTOS_V2
  -Ilib/FreeRTOS/portable/GCC/ARM_CM4F
  -Iinclude
  -D USE_FULL_ASSERT
  -D DEBUG
  -D SERIAL_DEBUG
lib_archive = no

My project directory:

It seems you’ve copied a lot from the pio-stm32h7-freertos example but that’s not 100% applicable as said. E.g.,

  • you’ve set board_build.stm32cube.custom_config_header = yes but that means it’s now expecting a custom stm32f7xx_hal_conf.h file in your include folder (there is a template in the package folder). You don’t need this line.
  • You’ve set board_build.ldscript = STM32F767ZITX_FLASH.ld but the STM32F767ZITX_FLASH.ld file doesn’t exist in the root of your project. You don’t need this line.
  • You’ve set -mfpu=fpv4-sp-d16 but the Cortex-M7 has a -mfpu=fpv5-sp-d16
  • You’ve set -Ilib/FreeRTOS/portable/GCC/ARM_CM4F but you need the Cortex M7 folder (-Ilib/FreeRTOS/portable/GCC/ARM_CM7/r0p1)
  • You should delete the ARM_CM4F folder, otherwise unrelated files get compiled.

Please look more into the posted https://github.com/maxgerhardt/pio-stm32f7-freertos example because it’s directly meant for the F7, not the H7.

Yep, everything works now.
Thanks a lot.