Timer code

Dependencies:   mbed

Committer:
DeanArm7
Date:
Tue Nov 28 15:10:54 2017 +0000
Revision:
0:8ac51305f7f7
Timer code

Who changed what in which revision?

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