embeddedmicrocontrollerwatchdog

Feeding watchdog in while(1) with bare-metal firmware


I have a bare-metal firmware for a microcontroller where some interrupts generate some data and they are passed to the main context where all the processing happens.

I am considering feeding the watchdog at the end of the while(1) loop. This is because if the code gets stuck anywhere, for example any assert is triggered, the watchdog will bite since the while(1) will not loop. Note that a RTOS isn't used.

Under what conditions would this implementation be not optimal for the objectives of a good watchdog and what can be done to improve?


Solution

  • Resetting a windowed watchdog at the end of the main loop is certainly an acceptable solution.

    It should perhaps be added that you only reset the watchdog when all tasks of the main loop have been processed correctly.

    void main (void) 
    {
        init();
        while (1) {
            bool Task1_ok = do_task1();
            bool Task2_ok = do_task2();
    
            if (Task1_ok && Task2_ok) {
                ClrWdt();
            }
        }
    }