Please help me get my CMake configuration correct for a static memory FreeRTOS

Please help me. I’m quite new to CMake and to FreeRTO, and so I don’t really know what I’m doing.

My project involves two RP2040s talking to each other. Each has different code, but both use FreeeRTOS.

My project folder structure looks like this:
image

The Project_Root CMakeLists.txt uses add_subdirectory() to add the CMakeLists from the two RP2040_Project folders. That all works fine. But what I would like to know is how to include FreeRTOS into my CMake.

  1. Do I mention FreeRTOS in the Project_Root CMakeLists.txt, or only in each Project’s CMakeLists.txt?

  2. Do I do it like this?

add_library(freertos_config INTERFACE)
target_include_directories(freertos_config SYSTEM INTERFACE .)
set(FREERTOS_KERNEL_PATH "../lib/FreeRTOS-Kernel")
target_include_directories(freertos_config INTERFACE "src")
  1. I have tried using the name FreeRTOS-Kernel-Static in each project’s target_link_libraries() command, but I get an error that it can’t find that.

For 1, you’ll likely have to put it at a shared location, since both projects are included in the root CMakeLists, as otherwise there would be duplicate target definitions. You would not be able to have a separate FreeRTOSConfig.h for each project. If the projects are separate, it may be better to not call them from a common root CMakeLists, and let each have its own root CMakeLists.

For 2, yes that could work, though you likely do not need the src include.

To use static FreeRTOS, you will have to enable it in your FreeRTOSConfig.h, and provide the functions for getting memory for the timer (if enabled) and idle tasks. In your code, you would use static versions of FreeRTOS APIs instead of the non-static versions.

See the following documentation:

There is also an example here:

Thank you for your help.

I have indeed set configSUPPORT_STATIC_ALLOCATION to 1 and configSUPPORT_DYNAMIC_ALLOCATION to 0. And I have created all of the necessary functions for giving static memory to the timer and tasks.

The example you linked to seems to be for a dynamically allocated FreeRTOS. Two more questions:

  1. Is there an example CMake file for a statically allocated build? I can’t seem to find one anywhere.

  2. Is FreeRTOS-Kernel-Static the correct name for the library?

Hi Rocketmagnet,

Statically allocated or dynamically allocated FreeRTOS is configured by FreeRTOSConfig.h. The same CMake project works for both configurations. One thing to be mentioned is that FREERTOS_HEAP is defined in the cmake_example. You can remove this line in order not to compile in the heap_x.c if you configure the FreeRTOS kernel with configSUPPORT_DYNAMIC_ALLOCATION is set to 0.

The library name is decided by application writer. You can use FreeRTOS-Kernel-Static for the library.

Thanks guys. I think I’m begining to understand. In the end I got it working by just including the relevant .c files from FreeRTOS in the build for each project, rather than making a library.

Thank you for sharing your solution!