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:
Sun Mar 20 23:22:50 2011 +0000
Child:
1:5a1ff72b5915
Commit message:

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 Mar 20 23:22:50 2011 +0000
@@ -0,0 +1,33 @@
+
+#include "mbed.h"
+#include "Watchdog.h"
+
+
+
+/// Watchdog gets instantiated at the module level
+Watchdog::Watchdog() {
+    wdreset = (LPC_WDT->WDMOD >> 2) & 1;
+}
+
+/// Load timeout value in watchdog timer and enable
+void Watchdog::Configure(float s) {
+    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;
+    LPC_WDT->WDMOD = 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->WDFEED = 0xAA;
+    LPC_WDT->WDFEED = 0x55;
+}
+
+/// get the flag to indicate if the watchdog causes the reset
+bool Watchdog::WatchdogCausedReset() {
+    return wdreset;
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Watchdog.h	Sun Mar 20 23:22:50 2011 +0000
@@ -0,0 +1,81 @@
+/// @file Watchdog.h provides the interface to the Watchdog module
+///
+/// This provides basic Watchdog service for the mbed. You can configure
+/// various timeout intervals that meet your system needs. Additionally,
+/// it is possible to identify if the Watchdog was the cause of any 
+/// system restart.
+/// 
+/// Adapted from Simon's Watchdog code from http://mbed.org/forum/mbed/topic/508/
+///
+/// @note Copyright © 2011 by Smartware Computing, all rights reserved.
+///     This software may be used to derive new software, as long as
+///     this copyright statement remains in the source file.
+/// @author David Smart
+///
+#ifndef WATCHDOG_H
+#define WATCHDOG_H
+#include "mbed.h"
+
+/// Watchdog provides the interface to the Watchdog feature
+///
+/// example:
+/// @code
+/// Watchdog wd;
+///
+/// ...
+/// main() {
+///    if (wd.WatchdogCausedReset())
+///        pc.printf("Watchdog caused reset. WD being armed for 30s cycle.\r\n");
+///      
+///    wd.Configure(30.0);      // sets the timeout interval
+///    for (;;) {
+///         wd.Service;       // kick the dog before the timeout
+/// @endcode
+///
+class Watchdog {
+public:
+    /// Create a Watchdog object
+    ///
+    /// example:
+    /// @code
+    /// Watchdog wd;    // placed before main
+    /// @endcode
+    Watchdog();
+    
+    /// Configure the timeout for the Watchdog
+    ///
+    /// This configures the Watchdog service and starts it. It must
+    /// be serviced before the timeout, or the system will be restarted.
+    ///
+    /// example:
+    /// @code
+    ///     ...
+    ///     wd.Configure(1.4);  // configure for a 1.4 second timeout
+    ///     ...
+    /// @endcode
+    ///
+    /// @param timeout in seconds, as a floating point number
+    /// @returns none
+    ///
+    void Configure(float timeout);
+    
+    /// Service the Watchdog so it does not cause a system reset
+    ///
+    /// @returns none
+    void Service();
+    
+    /// WatchdogCausedReset identifies if the cause of the system
+    /// reset was the Watchdog
+    ///
+    /// example:
+    /// @code
+    ///    if (wd.WatchdogCausedReset())) {
+    /// @endcode
+    ///
+    /// @returns true if the Watchdog was the cause of the reset
+    bool WatchdogCausedReset();
+private:
+    bool wdreset;
+};
+
+#endif // WATCHDOG_H
\ No newline at end of file