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

Fork of WDT4088 by Kevin Braun

Revision:
1:06813ae93fea
Parent:
0:fc62d045ca0c
--- 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