Steps to convert a Dynamic allocation build into a Static allocation build

Hi,

This question is a tangent from my previous question: Static allocation on RP2040 Pico

I have followed a YouTube tutorial to implement a simple FreeRTOS project on the Raspberry Pi Pico. This works perfectly.

My question is, what steps should I take to convert this from dynamic allocation to static allocation?

My first thought was simply to edit the constants in my FreeRTOSConfig.h file.

#define configSUPPORT_STATIC_ALLOCATION         0
#define configSUPPORT_DYNAMIC_ALLOCATION        1

… and to use xTaskCreateStatic() etc in my code.

However, this doesn’t work, and gives the error:
heap_4.c:51:6: error: #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0

But I don’t know how to remove this file from the build. My CMakeLists.txt looks like this:

add_executable(${ProjectName}
    main.c
)

target_include_directories(${ProjectName} PRIVATE
    ${CMAKE_CURRENT_LIST_DIR}
)

target_link_libraries(${ProjectName} 
    pico_stdlib 
    FreeRTOS-Kernel-Heap4 
    )

pico_add_extra_outputs(${ProjectName})

It’s using the library FreeRTOS-Kernel-Heap4, but I don’t know what to replace this with to use Static allocation. Searching through the example code, I can’t find the string FreeRTOS-Kernel-Heap4 anywhere! Where is this defined, and what should I use instead?

If you use static allocation then you don’t need any of the heap implementations, so can you just remove FreeRTOS-Kernel_Heap4?

Hi rtel,

Hmm. I’m not sure how that could work. If I remove that library, then how am I linking in anything to do with FreeRTOS?

OK, let’s try it. Here’s my CMakeLists.txt now. I have just commented out the FreeRTOS-Kernel-Heap4.

add_executable(${ProjectName}
    main.c
)

target_include_directories(${ProjectName} PRIVATE
    ${CMAKE_CURRENT_LIST_DIR}
)

target_link_libraries(${ProjectName} 
    pico_stdlib 
    #FreeRTOS-Kernel-Heap4 
    )

pico_add_extra_outputs(${ProjectName})

Now, when I build, I get the error: undefined reference to `xTaskCreateStatic’ as expeted.

Surely I need to tell it to either link in a different library, or compile some .c files explicitly.

If you want to use a Static allocation, why you :
#define configSUPPORT_STATIC_ALLOCATION 0


You should use 1.

Wong,

Apologies. I mis-typed in my question. I meant to say that I set static to 1 and dynamic to 0, like this:

#define configSUPPORT_STATIC_ALLOCATION         0
#define configSUPPORT_DYNAMIC_ALLOCATION        1

The correct name for the library is FreeRTOS-Kernel when you want to use static allocation.

The code you typed here is still wrong. It should be the following:

#define configSUPPORT_STATIC_ALLOCATION         1
#define configSUPPORT_DYNAMIC_ALLOCATION        0

Thank you for sharing your solution!