WatchDog Timer library, support for LPC1768 and LPC11U24

Dependents:   ECGAFE_copy WatchDog

Fork of WatchDog by Christian Lerche

Files at this revision

API Documentation at this revision

Comitter:
Lerche
Date:
Sun Jul 01 22:43:25 2012 +0000
Child:
1:f64e5d18c374
Child:
2:a6661a79bf7b
Commit message:
Added support for LPC11U24.

Changed in this revision

WatchDog.cpp Show annotated file Show diff for this revision Revisions of this file
WatchDog.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WatchDog.cpp	Sun Jul 01 22:43:25 2012 +0000
@@ -0,0 +1,97 @@
+/*
+*           Source code is free to use and distribute. 
+*           Software originally written by Simon Ford, 
+*           altered by Lerche. 
+*
+*           02-07-2012 - Added LPC11U24 Watchdog functions. Lerche
+*           
+*/
+
+#include "mbed.h"
+#include "WatchDog.h"
+
+
+WatchDog::WatchDog(float s) {
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    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;         // Load WD Timer Constant with value determined by float s
+    LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset        
+    feed();    
+#elif defined(TARGET_LPC11U24)
+    LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000;
+    LPC_SYSCON->PDRUNCFG &= 0xFFBF;
+    LPC_SYSCON->WDTOSCCTRL = 0x40;
+    uint32_t clk = 100000;
+    LPC_WWDT->TC = s * (float)clk;
+    LPC_WWDT->CLKSEL = 0x1;
+    LPC_WWDT->MOD = 0x3;
+    feed();
+#endif
+}
+
+WatchDog_ms::WatchDog_ms(int ms) {
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    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 = ((float)ms * (float)clk) /1000;     // Load WD Timer Constant with value determined by int ms    
+    LPC_WDT->WDMOD = 0x3;                               // Enabled and Reset        
+    feed();
+#elif defined(TARGET_LPC11U24)
+    LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000;
+    LPC_SYSCON->PDRUNCFG &= 0xFFBF;
+    LPC_SYSCON->WDTOSCCTRL = 0x40;
+    uint32_t clk = 100000;
+    LPC_WWDT->TC = ((float)ms * (float)clk) / 1000;    
+    LPC_WWDT->CLKSEL = 0x1;
+    LPC_WWDT->MOD = 0x3;
+    feed();
+#endif    
+}
+
+WatchDog_us::WatchDog_us(int us) {
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    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 = ((float)us * (float)clk) /1000000;  // Load WD Timer Constant with value determined by int us       
+    LPC_WDT->WDMOD = 0x3;                               // Enabled and Reset        
+    feed();
+#elif defined(TARGET_LPC11U24)
+    LPC_SYSCON->SYSAHBCLKCTRL |= 0x8000;
+    LPC_SYSCON->PDRUNCFG &= 0xFFBF;
+    LPC_SYSCON->WDTOSCCTRL = 0x40;
+    uint32_t clk = 100000;
+    LPC_WWDT->TC = ((float)us * (float)clk) / 1000000;    
+    LPC_WWDT->CLKSEL = 0x1;
+    LPC_WWDT->MOD = 0x3;
+    feed();
+#endif   
+}
+
+void WatchDog::feed() {
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    LPC_WDT->WDFEED = 0xAA;         // Routine to feed the WatchDog
+    LPC_WDT->WDFEED = 0x55;         // according to user manual 
+#elif defined(TARGET_LPC11U24)
+    LPC_WWDT->FEED = 0xAA;
+    LPC_WWDT->FEED = 0x55;
+#endif    
+}
+void WatchDog_ms::feed() {
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    LPC_WDT->WDFEED = 0xAA;         // Routine to feed the WatchDog
+    LPC_WDT->WDFEED = 0x55;         // according to user manual
+#elif defined(TARGET_LPC11U24)
+    LPC_WWDT->FEED = 0xAA;
+    LPC_WWDT->FEED = 0x55;
+#endif    
+}
+void WatchDog_us::feed() {
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    LPC_WDT->WDFEED = 0xAA;         // Routine to feed the WatchDog
+    LPC_WDT->WDFEED = 0x55;         // according to user manual
+#elif defined(TARGET_LPC11U24)
+    LPC_WWDT->FEED = 0xAA;
+    LPC_WWDT->FEED = 0x55;
+#endif    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WatchDog.h	Sun Jul 01 22:43:25 2012 +0000
@@ -0,0 +1,57 @@
+/*
+*           Using code Simon Ford written in his example
+*           to implement WDT. 
+*           It can run in seconds, milliseconds, and microseconds. 
+*           All there is to do, is keep feeding the watchdog. 
+*/
+
+#ifndef WATCHDOG_H
+#define WATCHDOG_H
+
+#include "mbed.h"
+
+/** Watchdog timer implementation (in seconds) */
+class WatchDog {
+public: 
+    /** Creates Watchdog timer
+    * 
+    * @param Sets the WDT in seconds
+    */
+    WatchDog(float s);
+    
+    /** Keep feeding the watchdog in routine
+    *
+    * xxx.feed(); does the trick
+    */
+    void feed();
+};
+/** Watchdog timer implementation (in milliseconds) */
+class WatchDog_ms {
+public: 
+    /** Creates Watchdog timer
+    * 
+    * @param Sets the WDT in milliseconds
+    */
+    WatchDog_ms(int ms);
+    /** Keep feeding the watchdog in routine
+    *
+    * xxx.feed(); does the trick
+    */
+    void feed();
+};
+/** Watchdog timer implementation (in microseconds) */
+class WatchDog_us {
+public:
+    /** Creates Watchdog timer
+    * 
+    * @param Sets the WDT in microseconds
+    */
+    WatchDog_us(int us);
+    /** Keep feeding the watchdog in routine
+    *
+    * xxx.feed(); does the trick
+    */    
+    void feed();
+};
+
+#endif
\ No newline at end of file