ECE 4180 Lab 1 Extra Credit 1

Dependencies:   mbed

Committer:
abraha2d
Date:
Tue Oct 09 00:12:29 2018 +0000
Revision:
0:57e5aa345871
Save point

Who changed what in which revision?

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