I'm using the onboard software watchdog facility of an Atmega328 like documented here.
My purpose is to limit to 2 seconds the execution of a portion of code that could lead to endless computation.
This is the pseudo-code scheme:
enable watchdog (2 seconds)
run may-be-endless code
clear watchdog
run the rest
The rest of the code should be run if the may-be-endless code manage to run in the given amount of time, otherwise the whole program must be restarted.
This is the Arduino code I loaded into the board:
#include <avr/wdt.h>
void setup() {
Serial.begin(9600);
Serial.println("Started");
wdt_enable(WDTO_2S);
Serial.println("watchdog ON");
wdt_reset();
Serial.println("watchdog OFF");
}
void loop(){
Serial.println("loop...");
delay(1000);
}
At this point, the expected output should be an endless printing of loop...
messages, after the ones in the setup (Started
, watchdog ON
/OFF
).
Surprisingly, what I get instead is this:
Started
watchdog ON
watchdog OFF
loop...
loop...
loop...
Started
watchdog ON
watchdog OFF
loop...
loop...
loop...
Started
watchdog ON
watchdog OFF
loop...
loop...
[...]
Appartently the watchdog is being set but can't be cleared. How come?