0
0
FreeRTOSprogramming~30 mins

Stack overflow detection (method 1 and 2) in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Stack overflow detection (method 1 and 2)
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Change the config macro to 2 and write beyond the buffer size inside the task to trigger overflow detection.