Is there a static software timer option?


Today I have a simple and short question. The official documentation (FreeRTOS - Configuring a real time embedded application to enable the use of RTOS software timers) doesn’t say anything about creating a software timer in static fashion, I mean, without using malloc(), which makes me think that the only way to create software timers is using dynamic memory.

Am I right? Did I overlook something? Thank you!

Did you look at FreeRTOS - Open Source software for microcontrollers - FreeRTOS xTimerCreateStatic() API function description


Yes, I did. Did you look at FreeRTOS - Configuring a real time embedded application to enable the use of RTOS software timers? It points out, as part of enabling the software timers, that:

Sets the size of the stack (in words, not bytes) allocated to the timer service task.

Timer callback functions execute in the context of the timer service task. The stack requirement of the timer service task therefore depends on the stack requirements of the timer callback functions.

So, do I need to set up a stack depth if I’m going to use static timers? What should I write in configTIMER_TASK_STACK_DEPTH if I’m not going to use the dynamic version?


Now you are getting into a different question, how to make the Timer/Service task to be created with a static allocation.

If you set the flag configSUPPORT_STATIC_ALLOCATION, then the idle task, and if needed the timer task are created with callback to the application to get the static buffers for their TCB/Stack with calls to vApplicationGetIdleTaskMemory and vApplicationGetTimerTaskMemory.

These subroutine return the address of the static buffer for the TCB, that stack, and the size of the stack.

When these are used, the stack size parameters are not used (unless your own code for the call backs use them)

Got it!

Actually that was what I want to ask in first place, “How to create statically the timer daemon?”

Now I have two issues:

  1. There’s a typo in

Point 1 says “Add timers.c” and should say “Add timers.h”

  1. I tried to compile a project without any dynamic support in order to test completely static timers and I got this error:

FreeRTOS/src/timers.c:866: undefined reference to vPortFree’`

I’ve left out any heap_x.c file from compiling, and in the FreeRTOSConfig.h file I’ve played with these options:

then the project compiles.

then I got such error.

I guess I must not to ignore configSUPPORT_DYNAMIC_ALLOCATION at all. Any hint?

That page says to add timers.c to your PROJECT so you compile the code in it. The files that will use the timers also need to include timers.h for the definitions.

If you don’t have the #define configSUPPORT_DYNAMIC_ALLOCATION 0 statement in your code, the system assumes that the flag is set to one, so since when you created the objects they could have possibly been created either statically or dynamically, it needs to include the code to free them when you destroy them.