Modified version of the Watchdog library with improved support for STM32
Dependents: LightSaber iot_water_monitor_v2
Fork of Watchdog by
Revision 9:84f7c088c261, committed 2015-11-11
- Comitter:
- salarian
- Date:
- Wed Nov 11 16:05:42 2015 +0000
- Parent:
- 8:3c7d083f26b5
- Commit message:
- Refactored the STM32 bit variant of the functions and set the default clock to 32.768 kHz (crystal oscillator)
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 |
diff -r 3c7d083f26b5 -r 84f7c088c261 Watchdog.cpp --- a/Watchdog.cpp Tue Oct 27 17:04:16 2015 +0000 +++ b/Watchdog.cpp Wed Nov 11 16:05:42 2015 +0000 @@ -76,7 +76,7 @@ } // Compute the log2 of an integer. This is the simplest algorithm but probably is a bit slow. -int log2(unsigned v) +int Watchdog::log2(unsigned v) { unsigned r = 0; @@ -91,7 +91,9 @@ void Watchdog::Configure(float s) { // http://www.st.com/web/en/resource/technical/document/reference_manual/CD00171190.pdf - s = s * 40000; // The average frequency of STM32 watchdog timer is 40 kHz but it can vary between 30 and 60 kHz + s = s * 32768; // Newer Nucleo boards have 32.768 kHz crystal. Without it, the internal + // RC clock would have an average frequency of 40 kHz (variable between 30 and 60 kHz) + int scale = 1 + log2(s / 4096); // The RLR register is 12 bits and beyond that a prescaler should be used int residual = s / (1 << scale); // The value for the RLR register @@ -99,7 +101,7 @@ scale = 8; residual = 0xFFF; } - + IWDG->KR = 0x5555; // enable write to PR, RLR IWDG->PR = scale - 2; // Prescaler has values of multiples of 4 (i.e. 2 ^2), page 486 Reference Manual IWDG->RLR = residual; // Init RLR
diff -r 3c7d083f26b5 -r 84f7c088c261 Watchdog.h --- a/Watchdog.h Tue Oct 27 17:04:16 2015 +0000 +++ b/Watchdog.h Wed Nov 11 16:05:42 2015 +0000 @@ -102,6 +102,17 @@ bool WatchdogCausedReset(); private: bool wdreset; + + #if defined( TARGET_STM ) + /// Logarithm in base 2 + /// + /// This function is needed for the STM32 platforms to compute the prescaler + /// value for the watchdog timer + /// + /// @param[in] the input value as an unsigned integer (i.e. always positive) + /// @returns the computed log2 + int log2(unsigned v); + #endif }; #endif // WATCHDOG_H \ No newline at end of file