In the previous post, I was able to port my MSP430 variant for the FreeRTOS. Because that MCU has only 1 KB RAM memory, I was thinking if I should try out using FreeRTOS static memory allocation. I won’t be using the heap anyway because I don’t want to dynamically allocate memory in the small embedded systems. It is easier to see how much memory is needed and I don’t have to guess what the heap size should be during the build time nor check the memory allocation failures. I could also link RTOS objects to some specific memory location in linker file if that would be necessary.
FreeRTOS provides two definitions to control how memory will be reserved, configSUPPORT_STATIC_ALLOCATION and configSUPPORT_DYNAMIC_ALLOCATION. After setting configSUPPORT_STATIC_ALLOCATION in FreeRTOSConfig.h header, RTOS objects will be created using the memory provided by the application writer instead of creating those using the heap. Also, vApplicationGetIdleTaskMemory callback function must be implemented to provide the stack and a Task Control Block (TCB) for the idle task created when the scheduler is started.
After setting configSUPPORT_DYNAMIC_ALLOCATION to 0, I had to remove heap_1.c file from the build and replace all xTaskCreate with xTaskCreateStatic function calls. This function takes a pointer to the stack and to the TCB for the task to be created. I took the example implementation from the document without modifications for the vApplicationGetIdleTaskMemory function. Then I modified flash.c to create two tasks to flash the same LED. So, with three tasks, there will be still over hundred bytes free RAM to be used for something else.
As we can see, with this amount of memory, we can use only couple of tasks and maybe some other RTOS objects, for task synchronization for example. But anyway, by using static memory allocation it is possible to have more tasks running compared to dynamic allocation and it is easier for a developer to keep track of used memory.