You are here
Home > embedded >

How To Use WDT On Free RTOS Running ATSAMEx

After doing some google and spending almost 4 hours on choosing right place to start WDT, we have decided to share our learning here. Well we are using ATSAME54P19/20 controller from Microchip formally Atmel. Watchdog timer – WDT works well without Free RTOS (10.0) but when WDT is enabled along with RTOS it simply does not work well.

In big while(1) embedded applications with no RTOS you can always initialize WDT after peripheral initialization and feed/touch WDT in while/main loop. In case of RTOS there are multiple tasks/threads and to be specific in case for Free RTOS when you try to enable WDT right before vTaskStartScheduler(); call it simply does not work.

Tried with following solutions – like configure WDT for 20 sec reset time. And enable WDT just before scheduler starts. WDT is not waiting till expiry it is immediately resetting. Then tried enabling WDT after scheduler has started but no luck.

So tried with the RTOS Hook functions . Every RTOS provides hook functions for user application. There are few hook functions in Free RTOS as well. First tried with Idle Hook function by setting configUSE_IDLE_HOOK to 1 within FreeRTOSConfig.h, but it seems this vApplicationIdleHook() function gets called only when there is nothing to be done by other tasks. In our application we have 4 tasks, along with timers enabled in it. This is reason the vApplicationIdleHook() function was not getting executed.

There is other hook function ie tick hook function vApplicationTickHook(), but that is getting called at every tick. Enabling WDT at every tick is not right place. So we need some hook function which will be called once after the RTOS scheduler is initialized. And vApplicationDaemonTaskStartupHook() is the right function. Initialize this hook by setting configUSE_DAEMON_TASK_STARTUP_HOOK to 1 in FreeRTOSConfig.h , put your WDT initialization and enable code here. From any other task or timer task you can feed WDT (wdt_feed(&WDT_0);).

void vApplicationDaemonTaskStartupHook(void)
{
#ifdef WDT_IN_USE 
uint32_t clk_rate;
uint16_t timeout_period;
clk_rate       = 1000;
timeout_period = 4096;
wdt_set_timeout_period(&WDT_0, clk_rate, timeout_period);
wdt_enable(&WDT_0);
#endif
}

Happy WDT testing with RTOS on ATMEL controllers 🙂

Leave a Reply

Top