Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


FreeRTOS heap and stack

Posted by nth0x on 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.

RE: FreeRTOS heap and stack

Posted by Richard Damon on 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.

RE: FreeRTOS heap and stack

Posted by nth0x on 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?

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Version 10.1.0 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner