FreeRTOS heap and stack

nth0x wrote on Wednesday, April 28, 2010:

I do not understand how FreeRTOS manages memory. When new thread is created, where is his stack allocated, and how stacks for thread are organized. Is stack for thread localized in the heap, and what FreeRTOS do there when switch context.  How FreeRTOS set thread stack pointer and handler stack pointer. I use Cortex-M3 and heap_3. I need some information for property handling _sbrk function, as that heap do not hook stack.

richard_damon wrote on Wednesday, April 28, 2010:

While I am not familiar with anything special about the Cortex port, but in general FreeRTOS allocates memory from its heap, which if you are using heap_3 is the main program heap. Task stacks will be allocated from that heap. For task switching, inside the TCB buffer for each task is a saved copy of that tasks Stack pointer, which is restored when that task becomes active.

If you do not want task stacks to be placed on the heap, then you need to either change to heap_1 or heap_2 to allocate FreeRTOS items from a separate heap that FreeRTOS will manage. (This heap will include not just the stacks, but all FreeRTOS data structures). If you want just the stacks to go into a separate area, you will need to modify task.c to use a different function to allocate the stack space from the memory you want it to come out of.

nth0x wrote on Thursday, April 29, 2010:

Thank you for reply richard_damon.

Cortex M3 has two modes Handle Mode (default mode after launch, a single stack pointer), and Handler Mode (non-privileged mode, additional stack pointer). I do not know whether FreeRTOS use it, which the index is saved when switching contexts. Initially when you start the program stack pointer points at the end of SRAM, later FreeRTOS already manages? What determines the value of configMINIMAL_STACK_SIZE (RTOSconfig), or is there still need some additional stack outside the reserved memory on the heap?