0
0
FreeRTOSprogramming~10 mins

Stack size allocation in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stack size allocation
Define Task
Specify Stack Size
Create Task with Stack Size
Task Runs
Stack Used by Task
Stack Overflow Check
Adjust Stack Size if Needed
Back to Create Task
This flow shows how a FreeRTOS task is created with a specified stack size, how the task uses the stack, and how stack overflow is checked and handled.
Execution Sample
FreeRTOS
xTaskCreate(TaskFunction, "Task1", 128, NULL, 1, NULL);
Creates a FreeRTOS task named 'Task1' with a stack size of 128 words.
Execution Table
StepActionStack Size (words)Stack Used (words)Stack Overflow CheckResult
1Define TaskFunction---Task function code ready
2Call xTaskCreate with stack size 1281280NoTask created, stack allocated
3Task starts running12820NoTask uses 20 words of stack
4Task runs more code12850NoStack usage increased to 50 words
5Task runs deep function calls128130YesStack overflow detected
6Adjust stack size to 256256130NoStack overflow prevented
7Task runs safely256130NoTask continues normally
💡 Execution stops when task runs safely without stack overflow.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
Stack Size (words)-128128128128256256
Stack Used (words)002050130130130
Stack OverflowNoNoNoNoYesNoNo
Key Moments - 3 Insights
Why does the stack overflow even though we allocated 128 words?
Because the task used 130 words which is more than the allocated 128, as shown in execution_table row 5.
What happens if we don't adjust the stack size after overflow?
The task may crash or behave unpredictably due to stack overflow, so increasing stack size as in row 6 prevents this.
Is the stack size in bytes or words?
In FreeRTOS, stack size is specified in words, not bytes, so 128 means 128 words (usually 4 bytes each).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the stack usage at step 4?
A50 words
B130 words
C20 words
D128 words
💡 Hint
Check the 'Stack Used (words)' column at step 4 in the execution_table.
At which step does the stack overflow occur?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for 'Stack Overflow Check' column showing 'Yes' in the execution_table.
If we change the initial stack size to 256, what would happen at step 5?
AStack overflow occurs
BTask creation fails
CNo stack overflow occurs
DStack usage becomes 256
💡 Hint
Refer to execution_table row 6 and 7 where stack size is increased to 256 and no overflow occurs.
Concept Snapshot
FreeRTOS stack size allocation:
- Stack size is set in words when creating a task.
- Task uses stack during execution; usage varies.
- If usage exceeds allocation, stack overflow occurs.
- Adjust stack size to prevent overflow.
- Stack size must be enough for worst-case usage.
Full Transcript
In FreeRTOS, when you create a task, you specify how much stack memory it needs in words. The system allocates this stack for the task. As the task runs, it uses some of this stack for function calls and variables. If the task uses more stack than allocated, a stack overflow happens, which can cause errors. To avoid this, you monitor stack usage and increase the stack size if needed. This process ensures your task runs safely without crashing due to stack overflow.