Alef Sin / Watchdog

Dependents:   LightSaber iot_water_monitor_v2

Fork of Watchdog by David Smart

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Watchdog.cpp Source File

Watchdog.cpp

Go to the documentation of this file.
00001 /// @file Watchdog.cpp provides the interface to the Watchdog module
00002 ///
00003 /// This provides basic Watchdog service for the mbed. You can configure
00004 /// various timeout intervals that meet your system needs. Additionally,
00005 /// it is possible to identify if the Watchdog was the cause of any 
00006 /// system restart.
00007 /// 
00008 /// Adapted from Simon's Watchdog code from http://mbed.org/forum/mbed/topic/508/
00009 ///
00010 /// @note Copyright © 2011 by Smartware Computing, all rights reserved.
00011 ///     This software may be used to derive new software, as long as
00012 ///     this copyright statement remains in the source file.
00013 /// @author David Smart
00014 ///
00015 #include "mbed.h"
00016 #include "Watchdog.h"
00017 
00018 #if defined( TARGET_LPC1768 )
00019 /// Watchdog gets instantiated at the module level
00020 Watchdog::Watchdog() {
00021     wdreset = (LPC_WDT->WDMOD >> 2) & 1;    // capture the cause of the previous reset
00022 }
00023 
00024 /// Load timeout value in watchdog timer and enable
00025 void Watchdog::Configure(float s) {
00026     LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
00027     uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
00028     LPC_WDT->WDTC = (uint32_t)(s * (float)clk);
00029     LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
00030     Service();
00031 }
00032 
00033 /// "Service", "kick" or "feed" the dog - reset the watchdog timer
00034 /// by writing this required bit pattern
00035 void Watchdog::Service() {
00036     LPC_WDT->WDFEED = 0xAA;
00037     LPC_WDT->WDFEED = 0x55;
00038 }
00039 
00040 /// get the flag to indicate if the watchdog causes the reset
00041 bool Watchdog::WatchdogCausedReset() {
00042     return wdreset;
00043 }
00044 #elif defined( TARGET_LPC4088 )
00045 // from Gesotec Gesotec
00046 /// Watchdog gets instantiated at the module level
00047 Watchdog::Watchdog() {
00048     wdreset = (LPC_WDT->MOD >> 2) & 1;    // capture the cause of the previous reset
00049 }
00050  
00051 /// Load timeout value in watchdog timer and enable
00052 void Watchdog::Configure(float s) {
00053     //LPC_WDT->CLKSEL = 0x1;                // Set CLK src to PCLK
00054     uint32_t clk = 500000 / 4;    // WD has a fixed /4 prescaler, and a 500khz oscillator
00055     LPC_WDT->TC = (uint32_t)(s * (float)clk);
00056     LPC_WDT->MOD = 0x3;                   // Enabled and Reset
00057     Service();
00058 }
00059  
00060 /// "Service", "kick" or "feed" the dog - reset the watchdog timer
00061 /// by writing this required bit pattern
00062 void Watchdog::Service() {
00063     LPC_WDT->FEED = 0xAA;
00064     LPC_WDT->FEED = 0x55;
00065 }
00066  
00067 /// get the flag to indicate if the watchdog causes the reset
00068 bool Watchdog::WatchdogCausedReset() {
00069     return wdreset;
00070 }
00071 #elif defined( TARGET_STM )
00072 // Derived from Chau Vo
00073 /// Watchdog gets instantiated at the module level
00074 Watchdog::Watchdog() {
00075     wdreset = (RCC->CSR & (1<<29)) ? true : false;  // read the IWDGRSTF (Independent WD, not the windows WD)
00076 }
00077 
00078 // Compute the log2 of an integer. This is the simplest algorithm but probably is a bit slow.
00079 int Watchdog::log2(unsigned v)
00080 {
00081     unsigned r = 0;             
00082     
00083     while (v >>= 1)
00084       r++;
00085                           
00086     return r;                        
00087 }
00088 
00089 
00090 /// Load timeout value in watchdog timer and enable
00091 void Watchdog::Configure(float s) {
00092     // http://www.st.com/web/en/resource/technical/document/reference_manual/CD00171190.pdf
00093     
00094     s = s * 32768;                  // Newer Nucleo boards have 32.768 kHz crystal. Without it, the internal 
00095                                     // RC clock would have an average frequency of 40 kHz (variable between 30 and 60 kHz)
00096                                     
00097     int scale = 1 + log2(s / 4096); // The RLR register is 12 bits and beyond that a prescaler should be used
00098     int residual = s / (1 << scale); // The value for the RLR register
00099     
00100     if (scale > 8) {                 //STM32 allows a maximum time of around 26.2 seconds for the Watchdog timer
00101         scale = 8;
00102         residual = 0xFFF;
00103     }
00104            
00105     IWDG->KR  = 0x5555;         // enable write to PR, RLR
00106     IWDG->PR  = scale - 2;      // Prescaler has values of multiples of 4 (i.e. 2 ^2), page 486 Reference Manual
00107     IWDG->RLR = residual;       // Init RLR
00108     IWDG->KR  = 0xAAAA;         // Reload the watchdog
00109     IWDG->KR  = 0xCCCC;         // Starts the WD
00110 }
00111 
00112 /// "Service", "kick" or "feed" the dog - reset the watchdog timer
00113 void Watchdog::Service() {
00114     IWDG->KR  = 0xAAAA;
00115 }
00116 
00117 /// get the flag to indicate if the watchdog causes the reset
00118 bool Watchdog::WatchdogCausedReset() {
00119     if (wdreset) {
00120         RCC->CSR |= (1<<24); // clear reset flag
00121     }
00122     return wdreset;
00123 }
00124 #endif