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 } }
Watchdog.cpp@1:5a1ff72b5915, 2011-03-20 (annotated)
- Committer:
- WiredHome
- Date:
- Sun Mar 20 23:24:36 2011 +0000
- Revision:
- 1:5a1ff72b5915
- Parent:
- 0:7a316f14da9c
- Child:
- 2:2873f068f325
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 0:7a316f14da9c | 1 | |
WiredHome | 0:7a316f14da9c | 2 | #include "mbed.h" |
WiredHome | 0:7a316f14da9c | 3 | #include "Watchdog.h" |
WiredHome | 0:7a316f14da9c | 4 | |
WiredHome | 0:7a316f14da9c | 5 | |
WiredHome | 0:7a316f14da9c | 6 | |
WiredHome | 0:7a316f14da9c | 7 | /// Watchdog gets instantiated at the module level |
WiredHome | 0:7a316f14da9c | 8 | Watchdog::Watchdog() { |
WiredHome | 0:7a316f14da9c | 9 | wdreset = (LPC_WDT->WDMOD >> 2) & 1; |
WiredHome | 0:7a316f14da9c | 10 | } |
WiredHome | 0:7a316f14da9c | 11 | |
WiredHome | 0:7a316f14da9c | 12 | /// Load timeout value in watchdog timer and enable |
WiredHome | 0:7a316f14da9c | 13 | void Watchdog::Configure(float s) { |
WiredHome | 0:7a316f14da9c | 14 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
WiredHome | 0:7a316f14da9c | 15 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
WiredHome | 0:7a316f14da9c | 16 | LPC_WDT->WDTC = s * (float)clk; |
WiredHome | 0:7a316f14da9c | 17 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
WiredHome | 0:7a316f14da9c | 18 | Service(); |
WiredHome | 0:7a316f14da9c | 19 | } |
WiredHome | 0:7a316f14da9c | 20 | |
WiredHome | 0:7a316f14da9c | 21 | /// "Service", "kick" or "feed" the dog - reset the watchdog timer |
WiredHome | 0:7a316f14da9c | 22 | /// by writing this required bit pattern |
WiredHome | 0:7a316f14da9c | 23 | void Watchdog::Service() { |
WiredHome | 0:7a316f14da9c | 24 | LPC_WDT->WDFEED = 0xAA; |
WiredHome | 0:7a316f14da9c | 25 | LPC_WDT->WDFEED = 0x55; |
WiredHome | 0:7a316f14da9c | 26 | } |
WiredHome | 0:7a316f14da9c | 27 | |
WiredHome | 0:7a316f14da9c | 28 | /// get the flag to indicate if the watchdog causes the reset |
WiredHome | 0:7a316f14da9c | 29 | bool Watchdog::WatchdogCausedReset() { |
WiredHome | 0:7a316f14da9c | 30 | return wdreset; |
WiredHome | 0:7a316f14da9c | 31 | } |
WiredHome | 0:7a316f14da9c | 32 | |
WiredHome | 0:7a316f14da9c | 33 |