I use CubeMX from ST with FreeRTOS version 10.0.1. I use pvPortMalloc and free in my code and sometimes it seems like the allocted memory will get corrupted. I chose heap_4. Now I am confused about memory allocation:
In Cube I can chose mem allocation as Dynamic, Static or mixed - do not exactly understand it.
Allocation for tasks I chose Static because I want to know the size of my code in RAM at link time, rather then run-time. But then I would like to know If I call pvPortMalloc() in Task_A - where it takes the mem? Does it take space from Task_A Stack?
Like the standard C library malloc() function, pvPortMalloc() always
allocated memory from the heap - provided the heap has enough free
memory for the allocation to complete. How the heap is allocated
depends on whether you are building heap_1, heap_2, heap_3, heap_4 or
heap_5: https://www.freertos.org/a00111.html
Now I have 3 static allocated tasks - altogether they have 9216 B. Also had FreeRTOS timer with 400 B and 3 queues - 888 B. Total sum is 10504 Bytes. It means the value of TOTAL_HEAP_SIZE should be set above this value is not it? For example if I set HEAP SIZE to 12000 B, then there is about 12000-10504 = 1496 Bytes for pvPortMalloc allocation?
But:
I do not understand why is possible in CubeMX to set Total Heap Size only on 8000 Bytes and everything seems to work properly even if I use a lot of pvPortmMalloc and vPortFree (which is value under the space is needed for Stacks, TCB, Queues etc.) I must point out that Tasks and queues are allocated statically with Heap_4.
Statically allocated memory, by definition, does not come out of the heap. The amount of memory available for use as the heap is the total memory available, minus statically allocated memory (minus the .data section, etc.). This is how compilers and linker behave, regardless if what your build includes FreeRTOS or not.
I am sorry but I still don’t understand.
I set TOTAL_HEAP_SIZE to 1000 Bytes, but why I can not set stack size for 3 statically allocated tasks for example to 256 words (every stack size 256 words)? At least CubeMX will not allow it.
Because as you said, if it is allocated statically -the stacks for tasks will not come form the heap memory.
Why can’t you set the 3 statically allocated tasks stack sizes to 256
words? What is preventing you? Is it just CubeMX that won’t let you do
it - and if so - how does it prevent you? (for example, will it just
not let you enter the number or does it let you enter the number but
then not let you link the application?
What is configSUPPORT_STATIC_ALLOCATION set to?
What if you just edit the FreeRTOSConfig.h file without using CubeMX?
The dialog box in the screenshot seems to indicate that the stack sizes
were updated. What makes you think CubeMX is not allowing the stack
sizes to be updated?