-
Notifications
You must be signed in to change notification settings - Fork 9
/
WatchDog.ino
59 lines (53 loc) · 1.42 KB
/
WatchDog.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
byte busyState;
int busyCounter;
#define TIMEOUT 12000 // * 5ms = 1 minute
#define EE_CTR 78
#define EE_STATE 79
void SetupWatchdog()
{
#ifdef USE_WD
// set watchdog timeout to 4 seconds
wdt_enable(WDTO_4S);
#endif
// initialize counters
busyCounter = 0;
busyState = 0;
}
void busy(byte function)
{
if(function==0)
{
busyCounter=0;
}
if(busyState!=function)
{
busyState = function;
}
}
// this is called every 5ms to keep the watchdog from resetting the board
void CheckWatchdog()
{
// increment the counter as long as we are executing a function
busyCounter++;
// keep resetting the watchdog until busycounter gets really big
// 12000 * 5ms + 4s = 64 seconds of inactivity before a reset occurs
// A .connect() function can take up to 15 seconds because of DNS
// so a normal watchdog would not be long enough.
if(busyCounter<TIMEOUT)
{
// reset watchdog counter
wdt_reset();
}
else
{
// while waiting for the reset-by watchdog, save the current busystate
eeprom_write_byte ((uint8_t*)EE_STATE, busyState);
// increment the watchdog reset counter
byte ctr = eeprom_read_byte ((uint8_t*)EE_CTR) + 1;
eeprom_write_byte ((uint8_t*)EE_CTR, ctr);
// save all counters
SaveValues();
// wait for the reset to come
while(1);
}
}