Modified version of the Watchdog library with improved support for STM32
Dependents: LightSaber iot_water_monitor_v2
Fork of Watchdog by
Diff: Watchdog.cpp
- Revision:
- 9:84f7c088c261
- Parent:
- 8:3c7d083f26b5
--- 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
