jim hamblen
/
Watchdog_Demo
A basic demo of the watchdog timer. Uses mbed's 4 LEDs for status
main.cpp@0:87151a21b2e2, 2011-01-28 (annotated)
- Committer:
- 4180_1
- Date:
- Fri Jan 28 18:05:59 2011 +0000
- Revision:
- 0:87151a21b2e2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:87151a21b2e2 | 1 | #include "mbed.h" |
4180_1 | 0:87151a21b2e2 | 2 | // LEDs used to indicate code activity and reset source |
4180_1 | 0:87151a21b2e2 | 3 | DigitalOut myled1(LED1); //in main loop part 1 |
4180_1 | 0:87151a21b2e2 | 4 | DigitalOut myled2(LED2); //in main loop part 2 (where fault occurs) |
4180_1 | 0:87151a21b2e2 | 5 | DigitalOut myled3(LED3); //The pushbutton or power on caused a reset |
4180_1 | 0:87151a21b2e2 | 6 | DigitalOut myled4(LED4); //The watchdog timer caused a reset |
4180_1 | 0:87151a21b2e2 | 7 | |
4180_1 | 0:87151a21b2e2 | 8 | // Simon's Watchdog code from |
4180_1 | 0:87151a21b2e2 | 9 | // http://mbed.org/forum/mbed/topic/508/ |
4180_1 | 0:87151a21b2e2 | 10 | class Watchdog { |
4180_1 | 0:87151a21b2e2 | 11 | public: |
4180_1 | 0:87151a21b2e2 | 12 | // Load timeout value in watchdog timer and enable |
4180_1 | 0:87151a21b2e2 | 13 | void kick(float s) { |
4180_1 | 0:87151a21b2e2 | 14 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
4180_1 | 0:87151a21b2e2 | 15 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
4180_1 | 0:87151a21b2e2 | 16 | LPC_WDT->WDTC = s * (float)clk; |
4180_1 | 0:87151a21b2e2 | 17 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
4180_1 | 0:87151a21b2e2 | 18 | kick(); |
4180_1 | 0:87151a21b2e2 | 19 | } |
4180_1 | 0:87151a21b2e2 | 20 | // "kick" or "feed" the dog - reset the watchdog timer |
4180_1 | 0:87151a21b2e2 | 21 | // by writing this required bit pattern |
4180_1 | 0:87151a21b2e2 | 22 | void kick() { |
4180_1 | 0:87151a21b2e2 | 23 | LPC_WDT->WDFEED = 0xAA; |
4180_1 | 0:87151a21b2e2 | 24 | LPC_WDT->WDFEED = 0x55; |
4180_1 | 0:87151a21b2e2 | 25 | } |
4180_1 | 0:87151a21b2e2 | 26 | }; |
4180_1 | 0:87151a21b2e2 | 27 | |
4180_1 | 0:87151a21b2e2 | 28 | // Setup the watchdog timer |
4180_1 | 0:87151a21b2e2 | 29 | Watchdog wdt; |
4180_1 | 0:87151a21b2e2 | 30 | |
4180_1 | 0:87151a21b2e2 | 31 | int main() { |
4180_1 | 0:87151a21b2e2 | 32 | int count = 0; |
4180_1 | 0:87151a21b2e2 | 33 | // On reset, indicate a watchdog reset or a pushbutton reset on LED 4 or 3 |
4180_1 | 0:87151a21b2e2 | 34 | if ((LPC_WDT->WDMOD >> 2) & 1) |
4180_1 | 0:87151a21b2e2 | 35 | myled4 = 1; else myled3 = 1; |
4180_1 | 0:87151a21b2e2 | 36 | |
4180_1 | 0:87151a21b2e2 | 37 | // setup a 10 second timeout on watchdog timer hardware |
4180_1 | 0:87151a21b2e2 | 38 | // needs to be longer than worst case main loop exection time |
4180_1 | 0:87151a21b2e2 | 39 | wdt.kick(10.0); |
4180_1 | 0:87151a21b2e2 | 40 | |
4180_1 | 0:87151a21b2e2 | 41 | // Main program loop - resets watchdog once each loop iteration |
4180_1 | 0:87151a21b2e2 | 42 | // Would typically have a lot of code in loop with many calls |
4180_1 | 0:87151a21b2e2 | 43 | while (1) { |
4180_1 | 0:87151a21b2e2 | 44 | myled1 = 1; //Flash LEDs 1 & 2 to indicate normal loop activity |
4180_1 | 0:87151a21b2e2 | 45 | wait(.05); |
4180_1 | 0:87151a21b2e2 | 46 | myled1 = 0; |
4180_1 | 0:87151a21b2e2 | 47 | myled2 = 1; |
4180_1 | 0:87151a21b2e2 | 48 | wait(.05); |
4180_1 | 0:87151a21b2e2 | 49 | // Simulate a fault with an infinite loop, but only after 25 loop iterations |
4180_1 | 0:87151a21b2e2 | 50 | if (count == 25) while (1) {}; |
4180_1 | 0:87151a21b2e2 | 51 | // LED 2 will stay on during the fault |
4180_1 | 0:87151a21b2e2 | 52 | myled2 = 0; |
4180_1 | 0:87151a21b2e2 | 53 | count ++; |
4180_1 | 0:87151a21b2e2 | 54 | // End of main loop so "kick" to reset watchdog timer and avoid a reset |
4180_1 | 0:87151a21b2e2 | 55 | wdt.kick(); |
4180_1 | 0:87151a21b2e2 | 56 | } |
4180_1 | 0:87151a21b2e2 | 57 | } |