~

Dependencies:   MCP23S17 mbed

Files at this revision

API Documentation at this revision

Comitter:
fkhan39
Date:
Mon Sep 12 19:43:57 2016 +0000
Parent:
1:cd5247f3a499
Commit message:
added watchdog timer to lab 1

Changed in this revision

asm_extra.s Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Sep 11 22:24:03 2016 +0000
+++ b/main.cpp	Mon Sep 12 19:43:57 2016 +0000
@@ -19,6 +19,28 @@
 PwmOut rgb_b(p22);
 PwmOut rgb_r(p23);
 PwmOut rgb_g(p24);
+// WATCHDOG
+DigitalOut myled3(LED3); 
+DigitalOut myled4(LED4);
+// http://mbed.org/forum/mbed/topic/508/ (Simon)
+class Watchdog {
+public:
+// Load timeout value in watchdog timer and enable
+    void kick(float s) {
+        LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
+        uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
+        LPC_WDT->WDTC = s * (float)clk;
+        LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
+        kick();
+    }
+// "kick" or "feed" the dog - reset the watchdog timer
+// by writing this required bit pattern
+    void kick() {
+        LPC_WDT->WDFEED = 0xAA;
+        LPC_WDT->WDFEED = 0x55;
+    }
+};
+Watchdog wdt;
 
 int main()
 {
@@ -34,6 +56,16 @@
     chip.direction(PORT_B, 0xFF); // set all B to input
     switch_state = 0;
     
+    // WATCHDOG
+    int count = 0;
+    // On reset, indicate a watchdog reset or a pushbutton reset on LED 4 or 3
+    if ((LPC_WDT->WDMOD >> 2) & 1)
+        myled4 = 1; else myled3 = 1;
+        
+    // setup a 10 second timeout on watchdog timer hardware
+    // needs to be longer than worst case main loop exection time
+    wdt.kick(5.0);  
+    
     while(1) {
         // 1
         red_led = dip_switch3;
@@ -60,5 +92,10 @@
         rgb_r = control_pot;
         rgb_g = 0.5f - control_pot;
         rgb_b = 1.0f - control_pot;
+        
+        // WATCHDOG
+        if (count == 15) while (1) {};
+        count++;
+        wdt.kick();
     }
 }