Reputation: 43
Recently I was working with FreeRTOS and created some tasks to perform my required actions. Although it seems like every time when I create a new task with xTaskCreate()
or TI GUI configuration, I simply try to keep my stack size as large as required so as to not have a stack overflow.
Is there any way to calculate the maximum stack size used by my tasks with respect to the following events?
Upvotes: 4
Views: 8046
Reputation: 1
I'm used this code:
TaskHandle_t cipTask;
UBaseType_t uxHighWaterMark;
/* Print actual size of stack has used */
for (;;) {
uxHighWaterMark = uxTaskGetStackHighWaterMark(cipTask);
Serial.println(uxHighWaterMark);
}
Upvotes: 0
Reputation: 3246
The compiler, compiler optimisation level, CPU architecture, local variable allocations and function call nesting depth all have a large impact on the stack size. The RTOS has minimal impact. For example, FreeRTOS will add approximately 60 bytes to the stack on a Cortex-M - which is used to store the task's context when the task is not running. Whichever method you use to calculate stack usage in your non-RTOS project can be used in your RTOS project too - then add approximately 60 bytes.
You can calculate these things, and that can be important in safety critical applications, but in other cases a more pragmatic approach is to try it and see - use the features of the RTOS to measure how much stack is actually being used and use the stack overflow detection - then adjust until you find something optimal. http://www.freertos.org/Stacks-and-stack-overflow-checking.html http://www.freertos.org/uxTaskGetStackHighWaterMark.html
Upvotes: 6