|
@@ -0,0 +1,48 @@
|
|
1
|
+#ifdef USE_WATCHDOG
|
|
2
|
+
|
|
3
|
+#include <avr/wdt.h>
|
|
4
|
+#include <avr/interrupt.h>
|
|
5
|
+
|
|
6
|
+volatile uint8_t timeout_seconds=0;
|
|
7
|
+
|
|
8
|
+void(* ctrlaltdelete) (void) = 0; //does not work on my atmega2560
|
|
9
|
+
|
|
10
|
+//Watchdog timer interrupt, called if main program blocks >1sec
|
|
11
|
+ISR(WDT_vect)
|
|
12
|
+{
|
|
13
|
+ if(timeout_seconds++ >= WATCHDOG_TIMEOUT)
|
|
14
|
+ {
|
|
15
|
+
|
|
16
|
+ #ifdef RESET_MANUAL
|
|
17
|
+ LCD_MESSAGE("Please Reset!");
|
|
18
|
+ ECHOLN("echo_: Something is wrong, please turn off the printer.");
|
|
19
|
+ #else
|
|
20
|
+ LCD_MESSAGE("Timeout, resetting!");
|
|
21
|
+ #endif
|
|
22
|
+ //disable watchdog, it will survife reboot.
|
|
23
|
+ WDTCSR |= (1<<WDCE) | (1<<WDE);
|
|
24
|
+ WDTCSR = 0;
|
|
25
|
+ #ifdef RESET_MANUAL
|
|
26
|
+ kill(); //kill blocks
|
|
27
|
+ while(1); //wait for user or serial reset
|
|
28
|
+ #else
|
|
29
|
+ ctrlaltdelete();
|
|
30
|
+ #endif
|
|
31
|
+ }
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+/// intialise watch dog with a 1 sec interrupt time
|
|
35
|
+void wd_init()
|
|
36
|
+{
|
|
37
|
+ WDTCSR = (1<<WDCE )|(1<<WDE ); //allow changes
|
|
38
|
+ WDTCSR = (1<<WDIF)|(1<<WDIE)| (1<<WDCE )|(1<<WDE )| (1<<WDP2 )|(1<<WDP1)|(0<<WDP0);
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+/// reset watchdog. MUST be called every 1s after init or avr will reset.
|
|
42
|
+void wd_reset()
|
|
43
|
+{
|
|
44
|
+ wdt_reset();
|
|
45
|
+ timeout_seconds=0; //reset counter for resets
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+#endif /* USE_WATCHDOG */
|