make watch dog timeout enter mri mode
[clinton/Smoothieware.git] / src / libs / Watchdog.cpp
CommitLineData
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"
9extern 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
14Watchdog::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();
19}
20
21void Watchdog::feed()
22{
23 WDT_Feed();
24}
25
6eeb045a
MM
26void Watchdog::on_module_loaded()
27{
28 register_for_event(ON_IDLE);
29 feed();
30}
31
32void Watchdog::on_idle(void*)
33{
34 feed();
35}
36
37
0333ccb4
JM
38// when watchdog triggers, set a led pattern and enter MRI which turns everything off into a safe state
39// TODO handle when MRI is disabled
073b1698
MM
40extern "C" void WDT_IRQHandler(void)
41{
0333ccb4
JM
42 if(THEKERNEL->is_using_leds()) {
43 // set led pattern to show we are in watchdog timeout
44 leds[0]= 0;
45 leds[1]= 1;
46 leds[2]= 0;
47 leds[3]= 1;
48 }
49
50 WDT_ClrTimeOutFlag(); // bootloader uses this flag to enter DFU mode
073b1698
MM
51 WDT_Feed();
52 __debugbreak();
6eeb045a 53}