2

Recently I was working with Rtos and created some tasks to perform my required actions. Although it seems like every time when I create new task with xTaskCreate or TI GUI configuration, I simply try to keep my stack size as much so that the stack must not get overflowed. Is there any way to calculate the maximum stack size used by my task with respect to these events? 1. Stack used by global and local variable 2. Stack used by the maximum number of recursion of the function 3. Including the interrupt context switching

Ashish
  • 23
  • 1
  • 6

2 Answers2

5

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

Richard
  • 3,081
  • 11
  • 9
0

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);
}