FreeRTOS HEAP and STACK Memory issue

Hai,
I am using One FreeRTOS task for reading ADC value. Inside the task i am using one buffer for storing the ADC value. Here my doubt is, which memory is allocated for that buffer. FreeRTOS stack or system stack. System heap is allocated for FreeRTOS. In this FreeRTOS heap, it’s having individual stack for task. My FreeRTOS task buffer is using FreeRTOS stack or system stack?

Thanks
Manikandan D

Do you dynamically allocate memory for the buffer? If so then where do you allocate it from (what do you call to allocate it)? If you don’t dynamically allocate the buffer then the compiler and linker determine where the buffer is located as they would for any C code. Note if this is in a task and you declare the buffer as a stack variable then each task has its own stack.

1 Like

xTaskCreate(vTaskAnalog, “vTaskAnalog”, 2048, 0, (tskIDLE_PRIORITY + 1), &vTaskAnalogHandle)

what is the use of this 2048.
I am using this function to create a task. I declare that buffer in global(int buffer[256]). But I am using that buffer inside the task.
RAM size= 320k
FreeRTOS heap size = 32k
What is the use of “task STACK”. my buffer is taking FreeRTOS task STACK or system STACK.

Thanks
Manikandan D

As the name suggests the task stack is the stack reserved for the task being created.
When using xTaskCreate this stack memory is allocated internally from FreeRTOS heap as documented in the API docs.
Local/stack variables used in the task code are put on this stack. You can of course also access/use global variables in a task.
For instance Cortex-M MCUs support a main and a so called process stack. The latter is used by FreeRTOS to manage the task stacks (on context switch) and the main stack is used by ISRs.

1 Like

Thanks for your answer @hs2

If your buffer was declared as automatic variable to the a function, then the buffer is allocated in your task stack, which in turns was allocated either in the heap (if you used xTaskCreate()) or in the system memory (if you used xTaskCreateStatic()).

However, if the buffer was declared with dynamic memory (through a call to malloc()), then it is allocated in the system’s heap (as long as you’re using your malloc’s compiler implementation).

thanks for the awesome information.

1 Like