📖 Scenario: You are working on an embedded system using FreeRTOS. You want to protect your tasks from crashing due to stack overflow errors. FreeRTOS offers two methods to detect stack overflow. You will write code to enable and test both methods.
🎯 Goal: Enable stack overflow detection methods 1 and 2 in FreeRTOS and write a simple task that triggers the detection. You will configure the FreeRTOS settings, implement the overflow hook functions, and observe the detection in action.
📋 What You'll Learn
Create a FreeRTOS task with a small stack size
Enable stack overflow detection method 1
Enable stack overflow detection method 2
Implement the stack overflow hook functions
Trigger a stack overflow to test detection
Print a message when overflow is detected
💡 Why This Matters
🌍 Real World
Embedded systems often run multiple tasks with limited memory. Detecting stack overflow prevents crashes and hard-to-debug errors.
💼 Career
Embedded software engineers must ensure system reliability by using RTOS features like stack overflow detection to catch bugs early.
Progress0 / 4 steps
1
Create a FreeRTOS task with a small stack size
Write code to create a FreeRTOS task called vOverflowTask with a stack size of configMINIMAL_STACK_SIZE and priority 1. Use the function xTaskCreate() and include the task function prototype void vOverflowTask(void *pvParameters);.
FreeRTOS
Hint
Use xTaskCreate() with the task function name, a name string, stack size, parameters as NULL, priority 1, and no task handle.
2
Enable stack overflow detection method 1
Add the FreeRTOS configuration line #define configCHECK_FOR_STACK_OVERFLOW 1 before including FreeRTOS headers. This enables stack overflow detection method 1.
FreeRTOS
Hint
Place the #define before including FreeRTOS headers to enable method 1.
3
Implement the stack overflow hook function for method 1
Write the function void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) that prints "Stack overflow detected in task: " followed by the task name pcTaskName. Use printf().
FreeRTOS
Hint
Implement the hook function exactly with the given signature and use printf to show the task name.
4
Enable stack overflow detection method 2 and trigger overflow
Add #define configCHECK_FOR_STACK_OVERFLOW 2 to enable method 2 instead of method 1. Modify vOverflowTask to declare a local array char buffer[10] and write beyond its bounds in a loop to cause stack overflow. Print "Stack overflow detected in task: " with the task name in vApplicationStackOverflowHook. Run the program to see the detection message.
FreeRTOS
Hint
Change the config macro to 2 and write beyond the buffer size inside the task to trigger overflow detection.
Practice
(1/5)
1. What is the main purpose of stack overflow detection in FreeRTOS?
easy
A. To reduce RAM usage by tasks
B. To prevent tasks from using more stack memory than allocated
C. To increase CPU clock speed
D. To speed up task switching
Solution
Step 1: Understand stack overflow concept
Stack overflow happens when a task uses more stack memory than allocated, causing memory corruption.
Step 2: Identify purpose of detection
Stack overflow detection helps catch this problem early to prevent crashes or unpredictable behavior.
Final Answer:
To prevent tasks from using more stack memory than allocated -> Option B
Quick Check:
Stack overflow detection = prevent excess stack use [OK]
Hint: Stack overflow detection stops tasks using too much stack [OK]
Common Mistakes:
Confusing stack overflow with CPU speed
Thinking it reduces RAM usage
Assuming it speeds up task switching
2. Which FreeRTOS configuration enables Method 1 stack overflow detection?
easy
A. configMAX_PRIORITIES set to 5
B. configUSE_PREEMPTION set to 0
C. configUSE_IDLE_HOOK set to 1
D. configCHECK_FOR_STACK_OVERFLOW set to 1 or 2
Solution
Step 1: Identify config for stack overflow detection
FreeRTOS uses configCHECK_FOR_STACK_OVERFLOW to enable Method 1 detection.
Step 2: Understand valid values
Setting configCHECK_FOR_STACK_OVERFLOW to 1 or 2 activates automatic detection on context switches.
Final Answer:
configCHECK_FOR_STACK_OVERFLOW set to 1 or 2 -> Option D
Quick Check:
configCHECK_FOR_STACK_OVERFLOW = 1 or 2 [OK]
Hint: Method 1 uses configCHECK_FOR_STACK_OVERFLOW = 1 or 2 [OK]
Common Mistakes:
Confusing unrelated config options
Using configUSE_PREEMPTION for stack checks
Setting configMAX_PRIORITIES thinking it affects stack
3. Given this FreeRTOS hook function for Method 1:
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
printf("Stack overflow detected in task: %s\n", pcTaskName);
while(1); // Halt
}
What will happen if a stack overflow is detected?
medium
A. Prints task name and halts the system
B. Resets the task automatically
C. Ignores the overflow and continues
D. Logs overflow but continues running
Solution
Step 1: Analyze the hook function code
The function prints the task name where overflow occurred and then enters an infinite loop.
Step 2: Understand effect of infinite loop
The infinite loop halts the system to prevent further damage after overflow detection.
Final Answer:
Prints task name and halts the system -> Option A
Quick Check:
Stack overflow hook prints and halts [OK]
Hint: Stack overflow hook prints and stops system [OK]
Common Mistakes:
Thinking system resets automatically
Assuming overflow is ignored
Believing task restarts without halt
4. In Method 2 stack overflow detection, which mistake will cause incorrect detection?
medium
A. Using vApplicationStackOverflowHook function
B. Enabling configCHECK_FOR_STACK_OVERFLOW to 2
C. Not initializing the stack pointer check value properly
D. Setting configUSE_PREEMPTION to 1
Solution
Step 1: Understand Method 2 detection
Method 2 manually checks stack pointer against a known value to detect overflow.
Step 2: Identify common error
If the initial stack pointer check value is not set correctly, the comparison will be invalid, causing false negatives or positives.
Final Answer:
Not initializing the stack pointer check value properly -> Option C