-
Notifications
You must be signed in to change notification settings - Fork 1
/
watchdog.cpp
63 lines (52 loc) · 1.91 KB
/
watchdog.cpp
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
60
61
62
63
#ifdef USE_WATCHDOG
#include "Marlin.h"
#include "watchdog.h"
//===========================================================================
//=============================private variables ============================
//===========================================================================
static volatile uint8_t timeout_seconds=0;
void(* ctrlaltdelete) (void) = 0; //does not work on my atmega2560
//===========================================================================
//=============================functinos ============================
//===========================================================================
/// intialise watch dog with a 1 sec interrupt time
void wd_init()
{
WDTCSR |= (1<<WDCE )|(1<<WDE ); //allow changes
WDTCSR = (1<<WDCE )|(1<<WDE )|(1<<WDP3 )|(1<<WDP0); // Reset after 8 sec.
// WDTCSR = (1<<WDIF)|(1<<WDIE)| (1<<WDCE )|(1<<WDE )| (1<<WDP3) | (1<<WDP0);
}
/// reset watchdog. MUST be called every 1s after init or avr will reset.
void wd_reset()
{
wdt_reset();
}
//===========================================================================
//=============================ISR ============================
//===========================================================================
//Watchdog timer interrupt, called if main program blocks >1sec
ISR(WDT_vect)
{
if(timeout_seconds++ >= WATCHDOG_TIMEOUT)
{
#ifdef RESET_MANUAL
LCD_MESSAGEPGM("Please Reset!");
LCD_STATUS;
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM("Something is wrong, please turn off the printer.");
#else
LCD_MESSAGEPGM("Timeout, resetting!");
LCD_STATUS;
#endif
//disable watchdog, it will survife reboot.
WDTCSR |= (1<<WDCE) | (1<<WDE);
WDTCSR = 0;
#ifdef RESET_MANUAL
kill(__LINE__); //kill blocks
while(1); //wait for user or serial reset
#else
ctrlaltdelete();
#endif
}
}
#endif /* USE_WATCHDOG */