Heap and stack sizes in Kinetis project

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Heap and stack sizes in Kinetis project

Jump to solution
2,874 Views
yuliap
Contributor II

Hi,

I have a Kinetis project wth FreeRTOS and lwip. I am a little confused with the heap and stack definitions. I can set the heap and stack size in the CPU component of the Processor Expert. In addition, the FreeRTOS has its own heap size config, and lwip has its own mem size definition and the sizes are passed as options to the linker. Do any of them override the others? I tried to add the ping demo functionality to my project and copied all the linker settings from the demo project. I verified that the FreeRTOS and lwip settings were the same as the demo project. Still I get a memory allocation errors when my program starts running. Is there any more heap/stack size definitions besides the ones in CPU component, linker misc options, FreeRTOSConfig and lwipopts?

Regards,

Julia

Labels (1)
0 Kudos
1 Solution
901 Views
BlackNight
NXP Employee
NXP Employee

Hello,

The FreeRTOS heap is used by FreeRTOS for the *task* stack allocation and for the allocation of any RTOS objects (semaphores, queues, etc).

In FreeRTOS you can use different heap allocation schemes (configFRTOS_MEMORY_SCHEME). Unless you use scheme 3 which is using normal malloc()/free() from the C/C++ library, FreeRTOS uses its own heap mangement.

So for FreeRTOS, all its objects including the task stacks are in that heap. And FreeRTOS makes that heap management thread safe.

Now there is the 'normal' heap: the one used by the C/C++ libary malloc() and free() calls. So this a separate thing, and might not be thread safe. So if your application does calls to malloc(), that library heap is used. This is the heap lwip is using (from what I remember).

The heap size of FreeRTOS is defined like this:

#define configTOTAL_HEAP_SIZE                 ((size_t)(20000)) /* size of heap in bytes */

Now about the heap and stack size you can configure in the Processor Expert component:

The stack size is what is allocated as stack 'out of reset': so if your application starts (reset or power-on-reset), that stack is used. E.g. up to calling main(). But at the time you start FreeRTOS, the RTOS will switch to the stack(s) used by the tasks, which are in the FreeRTOS heap.

The heap size you configure in Processor Expert is the heap size for malloc().

Both the heap size and stack size you define here will be configured in the linker file, e.g. as:

__heap_size = 0x1000;                /* required amount of heap  */
__stack_size = 0x0200;             /* required amount of stack */

I hope this helps,

Erich

View solution in original post

0 Kudos
3 Replies
902 Views
BlackNight
NXP Employee
NXP Employee

Hello,

The FreeRTOS heap is used by FreeRTOS for the *task* stack allocation and for the allocation of any RTOS objects (semaphores, queues, etc).

In FreeRTOS you can use different heap allocation schemes (configFRTOS_MEMORY_SCHEME). Unless you use scheme 3 which is using normal malloc()/free() from the C/C++ library, FreeRTOS uses its own heap mangement.

So for FreeRTOS, all its objects including the task stacks are in that heap. And FreeRTOS makes that heap management thread safe.

Now there is the 'normal' heap: the one used by the C/C++ libary malloc() and free() calls. So this a separate thing, and might not be thread safe. So if your application does calls to malloc(), that library heap is used. This is the heap lwip is using (from what I remember).

The heap size of FreeRTOS is defined like this:

#define configTOTAL_HEAP_SIZE                 ((size_t)(20000)) /* size of heap in bytes */

Now about the heap and stack size you can configure in the Processor Expert component:

The stack size is what is allocated as stack 'out of reset': so if your application starts (reset or power-on-reset), that stack is used. E.g. up to calling main(). But at the time you start FreeRTOS, the RTOS will switch to the stack(s) used by the tasks, which are in the FreeRTOS heap.

The heap size you configure in Processor Expert is the heap size for malloc().

Both the heap size and stack size you define here will be configured in the linker file, e.g. as:

__heap_size = 0x1000;                /* required amount of heap  */
__stack_size = 0x0200;             /* required amount of stack */

I hope this helps,

Erich

0 Kudos
901 Views
yuliap
Contributor II

Hi Erich,

Thanks for the explanation.

Just to be 100% sure.. If I use scheme 3 in FreeRTOS, does that mean that I need to define a large heap in the PE CPU component, and the heap defined in FreeRTOSConfig.h can be set to zero?

Regards,

Julia

0 Kudos
901 Views
BlackNight
NXP Employee
NXP Employee

Hi Julia,

yes, that's correct: if you use scheme 3, then you can have the FreeRTOS configTOTAL_HEAP_SIZE set to zero (because configTOTAL_HEAP_SIZE won't be used).

Erich

0 Kudos