commit

Dependencies:   mbed

Committer:
kaushalpkk
Date:
Thu Aug 30 13:17:29 2018 +0000
Revision:
0:c1e30d3d8237
commit

Who changed what in which revision?

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