Version of Watchdog timer for LPC4088 that uses the Singleton design pattern for easy access throughout a program.

Fork of WDT4088 by Kevin Braun

Files at this revision

API Documentation at this revision

Comitter:
tbronez
Date:
Sat Jan 23 19:01:31 2016 +0000
Parent:
0:fc62d045ca0c
Commit message:
Revised using Singleton design pattern to ease use throughout a program.

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
diff -r fc62d045ca0c -r 06813ae93fea Watchdog.cpp
--- a/Watchdog.cpp	Fri Dec 20 19:42:32 2013 +0000
+++ b/Watchdog.cpp	Sat Jan 23 19:01:31 2016 +0000
@@ -1,15 +1,34 @@
+// T. Bronez. rev. 2016-01-23
+// Based on https://developer.mbed.org/users/loopsva/code/WDT4088/
+// Put into Singleton design pattern for easy use throughout a program
+
 #include "mbed.h"
 #include "Watchdog.h"
 
-void Watchdog::kick(int WDTseconds) {
+// Global static point to singleton instance
+Watchdog* Watchdog::_pInstance = NULL;
+
+bool Watchdog::isWatchdogReset() {
+    return ((LPC_WDT->MOD >> 2) & 1);
+}
+
+Watchdog::Watchdog() {
+}
+
+Watchdog* Watchdog::getInstance() {
+    if(!_pInstance) { _pInstance = new Watchdog(); }
+    return _pInstance;
+}
+
+void Watchdog::enable(int WDTseconds) {
     if(WDTseconds < 1) WDTseconds = 1;      //minimum watchdog time is 1 sec
     if(WDTseconds > 134) WDTseconds = 134;  //maximum watchdog time is 134 sec
     LPC_WDT->TC = 125000 * WDTseconds;      //125000 * 8uS = 1.0 second tics
     LPC_WDT->MOD = 0x3;                     //enable and restart watchdog
-    Watchdog::kick();
+    Watchdog::feed();
 }
 
-void Watchdog::kick() {
-    LPC_WDT->FEED = 0xaa;                   //kick sequence into FEED register
+void Watchdog::feed() {
+    LPC_WDT->FEED = 0xaa;                   //put sequence into FEED register
     LPC_WDT->FEED = 0x55;
 }
diff -r fc62d045ca0c -r 06813ae93fea Watchdog.h
--- a/Watchdog.h	Fri Dec 20 19:42:32 2013 +0000
+++ b/Watchdog.h	Sat Jan 23 19:01:31 2016 +0000
@@ -1,57 +1,76 @@
+// T. Bronez. rev. 2016-01-23
+// Based on https://developer.mbed.org/users/loopsva/code/WDT4088/
+// Put into Singleton design pattern for easy use throughout a program
+
 #ifndef WATCHDOG_4088_H
 #define WATCHDOG_4088_H
 
 #include "mbed.h"
 
-/** Routines to set and kick the Watchdog timer for the LPC4088.
+/** Routines to enable and feed the Watchdog timer for the LPC4088.
  *
- *   The LPC4088 has a fixed, internal 500KHz oscillator which
- *   is divided by 4 to give an 8uS master clock to the Watchdog countdown timer.
+ *   The LPC4088 has a fixed, internal 500KHz oscillator which is divided
+ *   by 4 to give an 8uS master clock to the Watchdog countdown timer.
  *
  *   User inputs a value from 1 to 134 seconds when initializing the Watchdog
  *   timer. The user's input number is multiplied by 125k and then placed into 
  *   the countdown timer.
  *
- *   The user is responsible for "kicking" the Watchdog before the timeout interval 
- *   expires, otherwise the LPC4088 will automatically reboot.
+ *   The user is responsible for feeding the Watchdog before the timeout
+ *   interval expires, otherwise the LPC4088 will automatically reboot.
  *
  * @code
  * #include "mbed.h"
  * #include "Watchdog.h" 
  *
- * Watchdog wdt;
- *
  * int main() {
- *     initialization code....
- *     wdt.kick(20);           //init the watchdog for a 20 second timeout
- *     while(1) {
- *         wait_ms(1000);      //do some code
- *         wdt.kick();         //kick the watchdog before 20 seconds is up
+ *    Watchdog* pwd = Watchdog::getInstance();
+      pwd->enable(20);        // enable the watchdog with a 20 second timeout
+ *    while(1) {
+ *        wait_ms(1000);      // do some code
+ *        pwd->feed();        // feed the watchdog before 20 seconds is up
  *     }
  * }
  * @endcode
  *
  */
+ 
 /* Watchdog controller class
  */
 class Watchdog {
 
-public:
-    /** Create a Watchdog object and initialize timeout in seconds
-     *
-     * @param int timeout in seconds. Range (1 - 134)
-     */
-    void kick(int WDTseconds);
+    public:
+    
+        /* Get the singleton instance, creating it if needed
+         * @returns pointer to watchdog instance
+         */
+        static Watchdog* getInstance();
     
+        /** Enable the watchdog
+         * @param int timeout in seconds. Range (1 - 134)
+         */
+        void enable(int WDTseconds);
+        
+        /** Keep watchdog quiet by feeding
+         */
+        void feed();
     
-    /** Keep alive by kicking the Watchdog occasionally
-     *
-     * @param NONE
-     */
-    void kick();
-
-private:
-    int WDTseconds;
+        /** Identify reason for reset (watchdog or normal)
+         * @returns true if reset due to watchdog
+         */
+        static bool isWatchdogReset();
+        
+    private:
+        // Private constructor
+        Watchdog();
+        // Private copy constructor w/implementation
+        Watchdog(Watchdog const&);
+        // Private assignment operator w/implementation
+        Watchdog& operator=(Watchdog const&);
+        // Global static point to singleton instance
+        static Watchdog* _pInstance;
+            
+        int WDTseconds;
 };
 
 #endif