This provides a basic Watchdog service, and includes a startup detection to determine if the reset was caused by the WD.

Dependents:   A_CANAdapter LeonardoMbos AVC_2012 RT_CAN ... more

Using this Watchdog Class

#include "Watchdog.h"

Watchdog wd;

...
void main() {
   if (wd.WatchdogCausedReset())
       pc.printf("Watchdog caused reset.\r\n");
      
   wd.Configure(3.0);       // sets the timeout interval
   for (;;) {
        wd.Service();       // kick the dog before the timeout
        // do other work
   }
}

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Mon Mar 16 01:00:03 2015 +0000
Parent:
4:22c5c4aa4661
Child:
6:e0f547e22dd5
Commit message:
Enhanced support from TARGET_LPC1768 to TARGET_LPC4088, TARGET_STM

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
--- a/Watchdog.cpp	Sat Oct 11 17:28:07 2014 +0000
+++ b/Watchdog.cpp	Mon Mar 16 01:00:03 2015 +0000
@@ -15,7 +15,7 @@
 #include "mbed.h"
 #include "Watchdog.h"
 
-
+#if defined( TARGET_LPC1768 )
 /// Watchdog gets instantiated at the module level
 Watchdog::Watchdog() {
     wdreset = (LPC_WDT->WDMOD >> 2) & 1;    // capture the cause of the previous reset
@@ -41,5 +41,60 @@
 bool Watchdog::WatchdogCausedReset() {
     return wdreset;
 }
+#elif defined( TARGET_LPC4088 )
+// from Gesotec Gesotec
+/// Watchdog gets instantiated at the module level
+Watchdog::Watchdog() {
+    wdreset = (LPC_WDT->MOD >> 2) & 1;    // capture the cause of the previous reset
+}
+ 
+/// Load timeout value in watchdog timer and enable
+void Watchdog::Configure(float s) {
+    //LPC_WDT->CLKSEL = 0x1;                // Set CLK src to PCLK
+    uint32_t clk = 500000 / 4;    // WD has a fixed /4 prescaler, and a 500khz oscillator
+    LPC_WDT->TC = (uint32_t)(s * (float)clk);
+    LPC_WDT->MOD = 0x3;                   // Enabled and Reset
+    Service();
+}
+ 
+/// "Service", "kick" or "feed" the dog - reset the watchdog timer
+/// by writing this required bit pattern
+void Watchdog::Service() {
+    LPC_WDT->FEED = 0xAA;
+    LPC_WDT->FEED = 0x55;
+}
+ 
+/// get the flag to indicate if the watchdog causes the reset
+bool Watchdog::WatchdogCausedReset() {
+    return wdreset;
+}
+#elif defined( TARGET_STM )
+// Derived from Chau Vo
+/// Watchdog gets instantiated at the module level
+Watchdog::Watchdog() {
+    wdreset = (RCC->CSR & (1<<29)) ? true : false;  // read the IWDGRSTF (Independent WD, not the windows WD)
+}
 
+/// Load timeout value in watchdog timer and enable
+void Watchdog::Configure(int pr) {
+    // http://www.st.com/web/en/resource/technical/document/reference_manual/CD00171190.pdf
+    IWDG->KR  = 0x5555;         // enable write to PR, RLR
+    IWDG->PR  = pr;             // Init prescaler, page 486 Reference Manual
+    IWDG->RLR = 0xFFF;          // Init RLR
+    IWDG->KR  = 0xAAAA;         // Reload the watchdog
+    IWDG->KR  = 0xCCCC;         // Starts the WD
+}
 
+/// "Service", "kick" or "feed" the dog - reset the watchdog timer
+void Watchdog::Service() {
+    IWDG->KR  = 0xAAAA;
+}
+
+/// get the flag to indicate if the watchdog causes the reset
+bool Watchdog::WatchdogCausedReset() {
+    if (wdreset) {
+        RCC->CSR |= (1<<24); // clear reset flag
+    }
+    return wdreset;
+}
+#endif
--- a/Watchdog.h	Sat Oct 11 17:28:07 2014 +0000
+++ b/Watchdog.h	Mon Mar 16 01:00:03 2015 +0000
@@ -14,6 +14,7 @@
 /// @author David Smart
 ///
 /// History
+/// \li v2.00 - 20150315: Enhanced beyond TARGET_LPC1768 to TARGET_LPC4088, TARGET_STM
 /// \li v1.00 - 20110616: initial release with some documentation improvements
 ///
 #ifndef WATCHDOG_H
@@ -31,6 +32,11 @@
 /// however this one also provides a method for the application software
 /// to determine the cause of the reset - watchdog or otherwise.
 ///
+/// Supports:
+/// \li TARGET_LPC1768 
+/// \li TARGET_LPC4088
+/// \li TARGET_STM
+///
 /// example:
 /// @code
 /// Watchdog wd;