Commit | Line | Data |
---|---|---|
073b1698 | 1 | #include "Watchdog.h" |
0333ccb4 | 2 | #include "Kernel.h" |
073b1698 MM |
3 | |
4 | #include <lpc17xx_wdt.h> | |
5 | ||
6 | #include <mri.h> | |
7 | ||
0333ccb4 JM |
8 | #include "gpio.h" |
9 | extern GPIO leds[]; | |
10 | ||
d337942a MM |
11 | // TODO : comment this |
12 | // Basically, when stuff stop answering, reset, or enter MRI mode, or something | |
93694d6b | 13 | |
073b1698 MM |
14 | Watchdog::Watchdog(uint32_t timeout, WDT_ACTION action) |
15 | { | |
16 | WDT_Init(WDT_CLKSRC_IRC, (action == WDT_MRI)?WDT_MODE_INT_ONLY:WDT_MODE_RESET); | |
17 | WDT_Start(timeout); | |
18 | WDT_Feed(); | |
d3c40b90 JM |
19 | if(action == WDT_MRI) { |
20 | // enable the interrupt | |
21 | NVIC_EnableIRQ(WDT_IRQn); | |
22 | NVIC_SetPriority(WDT_IRQn, 1); | |
23 | } | |
073b1698 MM |
24 | } |
25 | ||
26 | void Watchdog::feed() | |
27 | { | |
28 | WDT_Feed(); | |
29 | } | |
30 | ||
6eeb045a MM |
31 | void Watchdog::on_module_loaded() |
32 | { | |
33 | register_for_event(ON_IDLE); | |
34 | feed(); | |
35 | } | |
36 | ||
37 | void Watchdog::on_idle(void*) | |
38 | { | |
39 | feed(); | |
40 | } | |
41 | ||
42 | ||
0333ccb4 JM |
43 | // when watchdog triggers, set a led pattern and enter MRI which turns everything off into a safe state |
44 | // TODO handle when MRI is disabled | |
073b1698 MM |
45 | extern "C" void WDT_IRQHandler(void) |
46 | { | |
0333ccb4 JM |
47 | if(THEKERNEL->is_using_leds()) { |
48 | // set led pattern to show we are in watchdog timeout | |
49 | leds[0]= 0; | |
50 | leds[1]= 1; | |
51 | leds[2]= 0; | |
52 | leds[3]= 1; | |
53 | } | |
54 | ||
55 | WDT_ClrTimeOutFlag(); // bootloader uses this flag to enter DFU mode | |
073b1698 MM |
56 | WDT_Feed(); |
57 | __debugbreak(); | |
6eeb045a | 58 | } |