I have a task running on freeRTOS and I am checking how much application stack is unused in this application. What I see is, the available stack memory decreases after some time and stays at that value for a long time. The task has a while(1) loop and should use the same amount of stack but, I do not understand why the stack gets used up after some iterations of the loop. This is what my task looks like:
void Task A(void *arg)
{
Initialize_some_variables;
while(1)
{
print(uxTaskGetStackHighWaterMark( NULL ));
sem_wait(some_sem);
xQueueReceive(some_q);
process_q_data();
send_response_over_uart();
print(uxTaskGetStackHighWaterMark( NULL ));
}
}
My stack should have the same free words as it would from the first iteration of the while loop. but, I see that after some time the free word count returned by GetStackHighWaterMark reduces and I am not able to account for that.
Ok, after reading the documentation of FreeRTOS, I now understand that uxTaskGetStackHighWaterMark always returns the minimum amount of stack that was remaining since the task started executing. So, it is not the amount of stack that is available at the given instance. That explains what I am seeing.
https://www.freertos.org/uxTaskGetStackHighWaterMark.html