The common example for running a watchdog timer on mbed processors doesn't work for the LPC1114, a common and easily-breadboardable ARM chip. My program provides a watchdog class that works on the LPC1114 but which is also similar to the common example found in https://mbed.org/forum/mbed/topic/508/

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 /*
00004 Main code copied from: http://mbed.org/cookbook/WatchDog-Timer
00005 Modified for use with LPC1114 with source code found here:
00006 https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/targets/cmsis/TARGET_NXP/TARGET_LPC11XX_11CXX/LPC11xx.h
00007 With some inspiration from:
00008 https://github.com/Mrjohns42/RSL/blob/master/LPC1114/nRF24L01/wdt.c
00009 */
00010 
00011 // LEDs used to indicate code activity and reset source
00012 DigitalOut led1(dp1);
00013 DigitalOut led2(dp4);
00014  
00015 // Simon's Watchdog code from
00016 // http://mbed.org/forum/mbed/topic/508/
00017 class Watchdog {
00018 public:
00019 // Load timeout value in watchdog timer and enable
00020     void kick(float s) {
00021         LPC_SYSCON->SYSAHBCLKCTRL = LPC_SYSCON->SYSAHBCLKCTRL|(1<<15);
00022         LPC_SYSCON->WDTCLKSEL = 0x1;                // Set CLK src to Main Clock
00023         LPC_SYSCON->WDTCLKUEN = 0x01;       /* Update clock */
00024         LPC_SYSCON->WDTCLKUEN = 0x00;       /* Toggle update register once */
00025         LPC_SYSCON->WDTCLKUEN = 0x01;
00026         LPC_SYSCON->WDTCLKDIV = 0x10;
00027         uint32_t clk = SystemCoreClock/16;    // WD has a fixed /4 prescaler, PCLK default is /4
00028         LPC_WDT->TC = s * (float)clk;
00029         LPC_WDT->MOD = 0x3;                   // Enabled and Reset
00030         kick();
00031     }
00032 // "kick" or "feed" the dog - reset the watchdog timer
00033 // by writing this required bit pattern
00034     void kick() {
00035         LPC_WDT->FEED = 0xAA;
00036         LPC_WDT->FEED = 0x55;
00037     }
00038 };
00039  
00040 // Setup the watchdog timer
00041 Watchdog wdt;
00042  
00043 int main() {
00044     int count = 0;
00045     // On reset, indicate a watchdog reset or a pushbutton reset on LED 4 or 3
00046     if ((LPC_WDT->MOD >> 2) & 1)
00047         led1 = 1; else led1 = 0;
00048         
00049     // setup a 10 second timeout on watchdog timer hardware
00050     // needs to be longer than worst case main loop exection time
00051     wdt.kick(10.0);  
00052  
00053     // Main program loop - resets watchdog once each loop iteration
00054     // Would typically have a lot of code in loop with many calls
00055     while (1) {
00056         wait(.1);
00057         led2 = 1;
00058         wait(.1);
00059         // Simulate a fault lock up with an infinite while loop, but only after 25 loop iterations
00060         if (count == 25) while (1) {};
00061         
00062         // LED 2 will stay on during the fault
00063         led2 = 0;
00064         count ++;
00065         // End of main loop so "kick" to reset watchdog timer and avoid a reset
00066         wdt.kick();
00067     }
00068 }