Stack and Heap Explanation

flash86 wrote on Wednesday, July 04, 2012:

Hi!
I am using FreeRTOS and Keil with my STM32 Discovery Board.
It works fine but I just wanted to know why i have to change the Stack_Size from 0x00000400 to 0x00000200 and the Heap_Size from 0x00000200 to 0x00000000 for FreeRTOS to work. Also I cannot use the heap_3.c file in my poject, it always keeps hanging on the vApplicationMallocFailedHook() function. Unfortunately I didn’t found anything about the Stack and Heap Configuration, except of http://www.freertos.org/a00111.html but this article didn’t answer my questions.
Maybe someone can tell me more about how this works?

Thanks and best regards,
Franz

edwards3 wrote on Wednesday, July 04, 2012:

Stack_Size from 0x00000400 to 0x00000200 and the Heap_Size from 0x00000200 to 0x00000000

You don’t have to do that to get FreeRTOS to run, but you might have to do that to get your application to link if it is trying to use more RAM that the STM32 actually has.

Setting the heap to 0 is a good idea if you are using heap_1.c or heap_2.c, and the linker’s allocated heap is not used. You must not set it to 0 if you are using heap_3.c though because heap_3 does use the linker’s allocated heap.

Setting the stack size down just reduces the amount of RAM the linker is trying to allocate. On the STM32 that stack is used by main(), and then re-used by interrupts. 0x200 could be too small. The interrupt stack is not checked for overflow like the tasks stacks are. If you are using heap_1.c or heap_2.c then you can reduce configTOTAL_HEAP_SIZE a little to reduce the RAM that that consumes, to free up some room to allow you to increase the stack size a bit.

flash86 wrote on Thursday, July 05, 2012:

Thanks for your answer! Now it becomes a little clearer.
As explained above I cannot use the heap_3.c file because it always hangs at the vApplicationMallocFailedHook() function. Thats because malloc() returns always NULL. I tried it with the Stack_Size of 0x00000400 and Heap_Size of 0x00000200 (default Setting of ST Microelectronics) . Is there any suggestion whats the problem here? Respectively is there any document which explains the Stack and Heap Management of FreeRTOS in detail?

Thank you, regards.
Franz