Version of Watchdog timer for LPC4088 that uses the Singleton design pattern for easy access throughout a program.
Fork of WDT4088 by
Watchdog.h@1:06813ae93fea, 2016-01-23 (annotated)
- Committer:
- tbronez
- Date:
- Sat Jan 23 19:01:31 2016 +0000
- Revision:
- 1:06813ae93fea
- Parent:
- 0:fc62d045ca0c
Revised using Singleton design pattern to ease use throughout a program.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tbronez | 1:06813ae93fea | 1 | // T. Bronez. rev. 2016-01-23 |
tbronez | 1:06813ae93fea | 2 | // Based on https://developer.mbed.org/users/loopsva/code/WDT4088/ |
tbronez | 1:06813ae93fea | 3 | // Put into Singleton design pattern for easy use throughout a program |
tbronez | 1:06813ae93fea | 4 | |
loopsva | 0:fc62d045ca0c | 5 | #ifndef WATCHDOG_4088_H |
loopsva | 0:fc62d045ca0c | 6 | #define WATCHDOG_4088_H |
loopsva | 0:fc62d045ca0c | 7 | |
loopsva | 0:fc62d045ca0c | 8 | #include "mbed.h" |
loopsva | 0:fc62d045ca0c | 9 | |
tbronez | 1:06813ae93fea | 10 | /** Routines to enable and feed the Watchdog timer for the LPC4088. |
loopsva | 0:fc62d045ca0c | 11 | * |
tbronez | 1:06813ae93fea | 12 | * The LPC4088 has a fixed, internal 500KHz oscillator which is divided |
tbronez | 1:06813ae93fea | 13 | * by 4 to give an 8uS master clock to the Watchdog countdown timer. |
loopsva | 0:fc62d045ca0c | 14 | * |
loopsva | 0:fc62d045ca0c | 15 | * User inputs a value from 1 to 134 seconds when initializing the Watchdog |
loopsva | 0:fc62d045ca0c | 16 | * timer. The user's input number is multiplied by 125k and then placed into |
loopsva | 0:fc62d045ca0c | 17 | * the countdown timer. |
loopsva | 0:fc62d045ca0c | 18 | * |
tbronez | 1:06813ae93fea | 19 | * The user is responsible for feeding the Watchdog before the timeout |
tbronez | 1:06813ae93fea | 20 | * interval expires, otherwise the LPC4088 will automatically reboot. |
loopsva | 0:fc62d045ca0c | 21 | * |
loopsva | 0:fc62d045ca0c | 22 | * @code |
loopsva | 0:fc62d045ca0c | 23 | * #include "mbed.h" |
loopsva | 0:fc62d045ca0c | 24 | * #include "Watchdog.h" |
loopsva | 0:fc62d045ca0c | 25 | * |
loopsva | 0:fc62d045ca0c | 26 | * int main() { |
tbronez | 1:06813ae93fea | 27 | * Watchdog* pwd = Watchdog::getInstance(); |
tbronez | 1:06813ae93fea | 28 | pwd->enable(20); // enable the watchdog with a 20 second timeout |
tbronez | 1:06813ae93fea | 29 | * while(1) { |
tbronez | 1:06813ae93fea | 30 | * wait_ms(1000); // do some code |
tbronez | 1:06813ae93fea | 31 | * pwd->feed(); // feed the watchdog before 20 seconds is up |
loopsva | 0:fc62d045ca0c | 32 | * } |
loopsva | 0:fc62d045ca0c | 33 | * } |
loopsva | 0:fc62d045ca0c | 34 | * @endcode |
loopsva | 0:fc62d045ca0c | 35 | * |
loopsva | 0:fc62d045ca0c | 36 | */ |
tbronez | 1:06813ae93fea | 37 | |
loopsva | 0:fc62d045ca0c | 38 | /* Watchdog controller class |
loopsva | 0:fc62d045ca0c | 39 | */ |
loopsva | 0:fc62d045ca0c | 40 | class Watchdog { |
loopsva | 0:fc62d045ca0c | 41 | |
tbronez | 1:06813ae93fea | 42 | public: |
tbronez | 1:06813ae93fea | 43 | |
tbronez | 1:06813ae93fea | 44 | /* Get the singleton instance, creating it if needed |
tbronez | 1:06813ae93fea | 45 | * @returns pointer to watchdog instance |
tbronez | 1:06813ae93fea | 46 | */ |
tbronez | 1:06813ae93fea | 47 | static Watchdog* getInstance(); |
loopsva | 0:fc62d045ca0c | 48 | |
tbronez | 1:06813ae93fea | 49 | /** Enable the watchdog |
tbronez | 1:06813ae93fea | 50 | * @param int timeout in seconds. Range (1 - 134) |
tbronez | 1:06813ae93fea | 51 | */ |
tbronez | 1:06813ae93fea | 52 | void enable(int WDTseconds); |
tbronez | 1:06813ae93fea | 53 | |
tbronez | 1:06813ae93fea | 54 | /** Keep watchdog quiet by feeding |
tbronez | 1:06813ae93fea | 55 | */ |
tbronez | 1:06813ae93fea | 56 | void feed(); |
loopsva | 0:fc62d045ca0c | 57 | |
tbronez | 1:06813ae93fea | 58 | /** Identify reason for reset (watchdog or normal) |
tbronez | 1:06813ae93fea | 59 | * @returns true if reset due to watchdog |
tbronez | 1:06813ae93fea | 60 | */ |
tbronez | 1:06813ae93fea | 61 | static bool isWatchdogReset(); |
tbronez | 1:06813ae93fea | 62 | |
tbronez | 1:06813ae93fea | 63 | private: |
tbronez | 1:06813ae93fea | 64 | // Private constructor |
tbronez | 1:06813ae93fea | 65 | Watchdog(); |
tbronez | 1:06813ae93fea | 66 | // Private copy constructor w/implementation |
tbronez | 1:06813ae93fea | 67 | Watchdog(Watchdog const&); |
tbronez | 1:06813ae93fea | 68 | // Private assignment operator w/implementation |
tbronez | 1:06813ae93fea | 69 | Watchdog& operator=(Watchdog const&); |
tbronez | 1:06813ae93fea | 70 | // Global static point to singleton instance |
tbronez | 1:06813ae93fea | 71 | static Watchdog* _pInstance; |
tbronez | 1:06813ae93fea | 72 | |
tbronez | 1:06813ae93fea | 73 | int WDTseconds; |
loopsva | 0:fc62d045ca0c | 74 | }; |
loopsva | 0:fc62d045ca0c | 75 | |
loopsva | 0:fc62d045ca0c | 76 | #endif |