If you have a dog in your home. You need to feed that dog at a regular interval. if you can’t feed one day, it will bite you! And likewise, this is the working logic behind the watchdog timer.
In our example, we created 2 tasks that contain some loop that runs repeatedly, one is called “Small_Task” and the other is called “Big_Task”. We are enabling the watchdog timer is loaded with an initial value (5 seconds) greater than the total delay in the “Small_Task”, but shorter than the “Big_Task”.
For the successful case, the watchdog is being refreshed/feed within 5 seconds, however, for the failed case, the loop is under processing and the watchdog is not being fresh after 5 seconds, which triggers the watchdog (dog barks), an interrupt is generated to reset the processor. Likewise, the watchdog timer protects the micro-controller from the hanging case.
Then we move to the coding part for this example, for this example, you will only need the RTL8722CSM/RTL8722DM/RTL8722DM MINI Board itself.
Firstly, make sure the correct Ameba development board is selected in Arduino IDE: “Tools” -> “Board” -> “RTL8722CSM/RTL8722DM” (or “RTL8722DM MINI”). Then open the “Watchdog Timer” example in “File” -> “Examples” -> “AmebaWatchdog” -> “Watchdog Timer”:
Upon successfully upload the sample code, open the serial monitor, and press the reset button. You will find that the “Small_Task” can refresh the watchdog within the 5 seconds (initialized in the watchdog timer). However, the “Big_Task” will not be able to refresh the watchdog within 5 seconds, which the watchdog “barks” then the microcontroller reset.
You can also set “#define RUN_CALLBACK_IF_WATCHDOG_BARKS (0)” value be “1”, for customized “my_watchdog_irq_handler”. Write the code inside the handler for execution after “barks”.