Many guides explain how to use FreeRTOS in STM32 projects without using the CMSIS wrapper library, but I still want to write one more.

At first

  • Download FreeRTOS source code from https://www.freertos.org I downloaded FreeRTOS 202112.00
  • Create a new STM32Cube project
  • Don’t configure FreeRTOS with STM32Cube. This guide aims to add the FreeRTOS lib to the project manually. This way CMSIS-RTOS wrapper lib will not be used.

Adding FreeRTOS lib to the project

  • Add a new source folder “ThirdParty” to the project in STM32Cube.
  • With file explorer, create a folder “FreeRTOS” inside “ThirdParty”.
  • Copy the files of the “source” folder in FreeRTOS source code to “FreeRTOS” folder in the project.
  • Some of the files are unnecessary and can be removed from the project.

    • delete “ThirdParty/FreeRTOS/.gitmodules”
    • delete all subfolders of “ThirdParty/FreeRTOS/portable” except “GCC” and “MemMang”
    • delete all subfolders of “ThirdParty/FreeRTOS/portable/GCC” except the target processor.
    • for example “STM32F407G-DISC1” board has “STM32F407VG” mcu. This mcu has “Cortex M4” processor. So the folder “ARM_CM4F” can be used.
  • Memory manager should be selected.

    • delete all files in “ThirdParty/FreeRTOS/portable/MemMang” except the one that will be used.
    • this link explains how memory managers work. https://freertos.org/a00111.html
    • STM32Cube provides a memory manager. And because a new manager will be used from FreeRTOS, the former should be removed from the project.
    • delete or “exclude from project” the file “Src/sysmem.c”

Project explorer will look like in STM32Cube:

  • The path of the added codes needs to be known by STM32Cube.

    • Right Click Project > Properties > C/C++ Build > Settings > Tool Settings > MCU GCC Compiler > Include paths
    • Add relative path “ThirdParty/FreeRTOS”
    • Add relative path “ThirdParty/FreeRTOS/include”
    • Add relative path “ThirdParty/FreeRTOS/portable/GCC/<target_processor_folder>”
  • FreeRTOSConfig.h is needed to configure the FreeRTOS kernel. It might be copied from other projects and modified. Detailed info can be found in the link.

Configuring STM32Cube Settings

  • In Device Configuration Tool (.ioc) > System Core > NVIC > Code Generation

    uncheck “Generate IRQ Handler” for “Time base: System tick timer”

    uncheck “Generate IRQ Handler” for “Pendable Request for system service”

    uncheck “Generate IRQ Handler” for “System Service call via SWI instruction”

  • In Device Configuration Tool (.ioc) > System Core > NVIC

    select Priority Group “4 bits for preemption, 0 bit for subpriority”

  • In Device Configuration Tool (.ioc) > System Core > SYS

    select Timebase source other than “Systick”