Cameron Causer
/
4180_L1_P1_Watchdog
Revision 0:846bdf8c596c, committed 2018-10-16
- Comitter:
- kzar
- Date:
- Tue Oct 16 19:00:04 2018 +0000
- Commit message:
- Watchdog timer example using led
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 846bdf8c596c main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Oct 16 19:00:04 2018 +0000 @@ -0,0 +1,56 @@ +#include "mbed.h" +// LEDs used to indicate code activity and reset source +DigitalOut myled3(LED3); //The pushbutton or power on caused a reset +DigitalOut myled4(LED4); //The watchdog timer caused a reset + +//Pushbutton and led +DigitalIn pb(p21); +DigitalOut led(p23); + +// Simon's Watchdog code from +// http://mbed.org/forum/mbed/topic/508/ +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; + } +}; + +// Setup the watchdog timer +Watchdog wdt; + +int main() { + 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(3.0); + + + // Main program loop - resets watchdog once each loop iteration + // Would typically have a lot of code in loop with many calls + while (1) { + led = pb; + wait(.1); + if (count == 50) { + while(1) {} + } + count++; + // End of main loop so "kick" to reset watchdog timer and avoid a reset + wdt.kick(); + } +}
diff -r 000000000000 -r 846bdf8c596c mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Oct 16 19:00:04 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/a7c7b631e539 \ No newline at end of file