CodeShare

Dependencies:   mbed

Committer:
jeremycai3721
Date:
Sun Sep 25 20:14:40 2016 +0000
Revision:
0:09da8ea2d7a2
CodeShare

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jeremycai3721 0:09da8ea2d7a2 1 #include "mbed.h"
jeremycai3721 0:09da8ea2d7a2 2
jeremycai3721 0:09da8ea2d7a2 3 //DigitalIn Button1(p26); // Button = 1 when the button is not pushed
jeremycai3721 0:09da8ea2d7a2 4 //DigitalIn Button2(p25); // Button2 : the PWN pushbutton
jeremycai3721 0:09da8ea2d7a2 5 DigitalOut LED(p22); // normal LED
jeremycai3721 0:09da8ea2d7a2 6 //PwmOut LED(p22); // PWN object for LED
jeremycai3721 0:09da8ea2d7a2 7
jeremycai3721 0:09da8ea2d7a2 8
jeremycai3721 0:09da8ea2d7a2 9 // Part 1 Extra Credit Setup
jeremycai3721 0:09da8ea2d7a2 10 // LEDs used to indicate code activity and reset source
jeremycai3721 0:09da8ea2d7a2 11 DigitalOut myled1(LED1); //in main loop part 1
jeremycai3721 0:09da8ea2d7a2 12 DigitalOut myled2(LED2); //in main loop part 2 (where fault occurs)
jeremycai3721 0:09da8ea2d7a2 13 DigitalOut myled3(LED3); //The pushbutton or power on caused a reset
jeremycai3721 0:09da8ea2d7a2 14 DigitalOut myled4(LED4); //The watchdog timer caused a reset
jeremycai3721 0:09da8ea2d7a2 15
jeremycai3721 0:09da8ea2d7a2 16 class Watchdog
jeremycai3721 0:09da8ea2d7a2 17 {
jeremycai3721 0:09da8ea2d7a2 18 public:
jeremycai3721 0:09da8ea2d7a2 19 // Load timeout value in watchdog timer and enable
jeremycai3721 0:09da8ea2d7a2 20 void kick(float s) {
jeremycai3721 0:09da8ea2d7a2 21 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
jeremycai3721 0:09da8ea2d7a2 22 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
jeremycai3721 0:09da8ea2d7a2 23 LPC_WDT->WDTC = s * (float)clk;
jeremycai3721 0:09da8ea2d7a2 24 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
jeremycai3721 0:09da8ea2d7a2 25 kick();
jeremycai3721 0:09da8ea2d7a2 26 }
jeremycai3721 0:09da8ea2d7a2 27 // "kick" or "feed" the dog - reset the watchdog timer
jeremycai3721 0:09da8ea2d7a2 28 // by writing this required bit pattern
jeremycai3721 0:09da8ea2d7a2 29 void kick() {
jeremycai3721 0:09da8ea2d7a2 30 LPC_WDT->WDFEED = 0xAA;
jeremycai3721 0:09da8ea2d7a2 31 LPC_WDT->WDFEED = 0x55;
jeremycai3721 0:09da8ea2d7a2 32 }
jeremycai3721 0:09da8ea2d7a2 33 };
jeremycai3721 0:09da8ea2d7a2 34
jeremycai3721 0:09da8ea2d7a2 35 // Setup the watchdog timer
jeremycai3721 0:09da8ea2d7a2 36 Watchdog wdt;
jeremycai3721 0:09da8ea2d7a2 37
jeremycai3721 0:09da8ea2d7a2 38
jeremycai3721 0:09da8ea2d7a2 39 int main()
jeremycai3721 0:09da8ea2d7a2 40 {
jeremycai3721 0:09da8ea2d7a2 41 // Part 1 - Extra Credit , Watchdog timer
jeremycai3721 0:09da8ea2d7a2 42 int count = 0;
jeremycai3721 0:09da8ea2d7a2 43 // On reset, indicate a watchdog reset or a pushbutton reset on LED 4 or 3
jeremycai3721 0:09da8ea2d7a2 44 if ((LPC_WDT->WDMOD >> 2) & 1)
jeremycai3721 0:09da8ea2d7a2 45 myled4 = 1;
jeremycai3721 0:09da8ea2d7a2 46 else myled3 = 1;
jeremycai3721 0:09da8ea2d7a2 47
jeremycai3721 0:09da8ea2d7a2 48 // setup a 5 second timeout on watchdog timer hardware
jeremycai3721 0:09da8ea2d7a2 49 // needs to be longer than worst case main loop exection time
jeremycai3721 0:09da8ea2d7a2 50 wdt.kick(8.0);
jeremycai3721 0:09da8ea2d7a2 51
jeremycai3721 0:09da8ea2d7a2 52 // Main program loop - resets watchdog once each loop iteration
jeremycai3721 0:09da8ea2d7a2 53 // Would typically have a lot of code in loop with many calls
jeremycai3721 0:09da8ea2d7a2 54 while (1) {
jeremycai3721 0:09da8ea2d7a2 55 LED = 1; // LED is on
jeremycai3721 0:09da8ea2d7a2 56 wait(.05);
jeremycai3721 0:09da8ea2d7a2 57 LED = 0; // LED is off
jeremycai3721 0:09da8ea2d7a2 58 wait(.05);
jeremycai3721 0:09da8ea2d7a2 59 // Simulate a fault lock up with an infinite while loop, but only after 25 loop iterations
jeremycai3721 0:09da8ea2d7a2 60 if (count == 5) while (1) {};
jeremycai3721 0:09da8ea2d7a2 61 // LED 2 will stay on during the fault
jeremycai3721 0:09da8ea2d7a2 62 LED = 0;
jeremycai3721 0:09da8ea2d7a2 63 count ++;
jeremycai3721 0:09da8ea2d7a2 64 // End of main loop so "kick" to reset watchdog timer and avoid a reset
jeremycai3721 0:09da8ea2d7a2 65 wdt.kick();
jeremycai3721 0:09da8ea2d7a2 66 }
jeremycai3721 0:09da8ea2d7a2 67
jeremycai3721 0:09da8ea2d7a2 68
jeremycai3721 0:09da8ea2d7a2 69
jeremycai3721 0:09da8ea2d7a2 70 // // Part 2 - PWN
jeremycai3721 0:09da8ea2d7a2 71 // float dc = 0.5; // 50% duty cycle
jeremycai3721 0:09da8ea2d7a2 72 //
jeremycai3721 0:09da8ea2d7a2 73 // LED.period(0.01f); // 4 second period
jeremycai3721 0:09da8ea2d7a2 74 // LED.write(dc);
jeremycai3721 0:09da8ea2d7a2 75 //
jeremycai3721 0:09da8ea2d7a2 76 // while (1) {
jeremycai3721 0:09da8ea2d7a2 77 // if (!Button1) {
jeremycai3721 0:09da8ea2d7a2 78 // dc+=0.1;
jeremycai3721 0:09da8ea2d7a2 79 // LED.write(dc);
jeremycai3721 0:09da8ea2d7a2 80 // } else if (!Button2) {
jeremycai3721 0:09da8ea2d7a2 81 // dc-=0.1;
jeremycai3721 0:09da8ea2d7a2 82 // LED.write(dc);
jeremycai3721 0:09da8ea2d7a2 83 // }
jeremycai3721 0:09da8ea2d7a2 84 // wait(0.1);
jeremycai3721 0:09da8ea2d7a2 85 // }
jeremycai3721 0:09da8ea2d7a2 86 }