Resolving FreeRTOS Application Crashes with S/O Checking, Watchdogs, etc.
Hello @Middleware & OS
My FreeRTOS application experiences sporadic crashes or resets without an obvious cause. What steps can I take, such as enabling stack overflow checking, using watchdog timers, or analyzing core dumps, to investigate and resolve these unexpected system failures? Please provide code examples to help understand these solutions.
// Enabling stack overflow checking
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
// Handle stack overflow
printf("Stack overflow in task: %s\n", pcTaskName);
while(1); // Stay here to help debugging
}
// Using a watchdog timer
void vTaskWithWatchdog(void *pvParameters) {
for(;;) {
// Refresh watchdog
HAL_IWDG_Refresh(&hiwdg);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void main(void) {
// Initialize watchdog
IWDG_HandleTypeDef hiwdg;
hiwdg.Instance = IWDG;
hiwdg.Init.Prescaler = IWDG_PRESCALER_64;
hiwdg.Init.Reload = 0xFFF;
HAL_IWDG_Init(&hiwdg);
xTaskCreate(vTaskWithWatchdog, "Task With Watchdog", 1000, NULL, 1, NULL);
vTaskStartScheduler();
}
5 Replies
This can be very frustrating 🫠, But let's see, the current watchdog refresh rate is set to 500ms
(pdMS_TO_TICKS(500))
in vTaskWithWatchdog
. This refresh frequency might need to be adjusted based on your application's requirements tho. If your tasks take longer than 500ms to complete, the watchdog might trigger a reset too early as in prematurely.Thanks I would try this out
Hmmm 🤔, Well, you have already tried implementing a stack overflow hook, to troubleshoot . Ensure that the configCHECK_FOR_STACK_OVERFLOW setting in FreeRTOSConfig.h is set to 1 or 2
Okay
To investigate and resolve unexpected system failures in your FreeRTOS application, you can
Enable Stack Overflow Checking by using the FreeRTOS provided mechanisms to check for stack overflows. You need to define the hook function vApplicationStackOverflowHook in your code. This function will be called if a stack overflow is detected. Also, ensure stack overflow checking is enabled in FreeRTOSConfig.h,
Use Watchdog Timers, Analyze Core Dumps you can use tools like Segger J-Link to capture a snapshot of the system's state during a fault.
To facilitate core dumps, you can use the HardFault_Handler to capture relevant information
Use Trace Tools like FreeRTOS+Trace or Percepio Tracealyzer to trace task execution and find out where and why the system is failing. These tools provide detailed insights into task execution, timings, and system behavior.