Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others

Revision:
15:3fd3c1ce01be
Parent:
13:328bfac0e686
Child:
17:86034c970ea0
--- a/nz32s.h	Sat Aug 13 11:51:46 2016 +1000
+++ b/nz32s.h	Sat Aug 13 12:57:48 2016 +1000
@@ -156,6 +156,36 @@
      * @param timeout The timeout in ms. Must be a value from 1 to 32000 (1ms to 32 seconds)
      */
     static inline void watchdog_start(uint32_t timeout) {
+#if (NZ32S_USE_WWDG==1)
+        //WWDG clock counter = (PCLK1 (32MHz)/4096)/8) = 976 Hz (~1024 us)
+        //WWDG Window value = 80 means that the WWDG counter should be refreshed only
+        //when the counter is below 80 (and greater than 64/0x40) otherwise a reset will
+        //be generated.
+        //WWDG Counter value = 127, WWDG timeout = ~1024 us * 64 = 65.57 ms */
+        //
+        //Min and Max for given Prescaler and WDGTB
+        //For Prescaler=1 and WDGTB=0 is:
+        // - Min TimeOut = 128uS, Max Timeout = 8.19ms
+        //For Prescaler=2 and WDGTB=1 is:
+        // - Min TimeOut = 256uS, Max Timeout = 16.38ms
+        //For Prescaler=4 and WDGTB=2 is:
+        // - Min TimeOut = 512uS, Max Timeout = 32.76ms
+        //For Prescaler=8 and WDGTB=3 is:
+        // - Min TimeOut = 1024uS, Max Timeout = 65.54ms
+        NZ32S::hwwdg.Instance = WWDG;
+
+        MXH_DEBUG("\r\nwatchdog_start() called!");
+
+        //The refresh window is:between 48.1ms (~1024 * (127-80)) and 65.57 ms (~1024 * 64)
+        NZ32S::hwwdg.Init.Prescaler = WWDG_PRESCALER_8;
+        NZ32S::hwwdg.Init.Window    = 80;
+        NZ32S::hwwdg.Init.Counter   = 127;
+
+        HAL_WWDG_Init(&NZ32S::hwwdg);
+
+        //Start the watchdog timer
+        HAL_WWDG_Start(&NZ32S::hwwdg);
+#else
         //Watchdog frequency is always 32 kHz
         //Prescaler: Min_Value = 4 and Max_Value = 256
         //Reload: Min_Data = 0 and Max_Data = 0x0FFF(4000)
@@ -180,13 +210,20 @@
 
         //Start the watchdog timer
         HAL_IWDG_Start(&NZ32S::hiwdg);
+#endif
     }
 
 
     /** Refreshes the IWDG
      */
     static inline void watchdog_refresh(void) {
+#if (NZ32S_USE_WWDG==1)
+        //Refresh WWDG: update counter value to 127, the refresh window is:
+        //between 48.1ms (~1024 * (127-80)) and 65.57 ms (~1024 * 64) */
+        HAL_WWDG_Refresh(&NZ32S::hwwdg, 127);
+#else
         HAL_IWDG_Refresh(&NZ32S::hiwdg);
+#endif
     }
 
 
@@ -212,13 +249,18 @@
     static inline void print_reset_cause(bool clearFlags);
 
 public:
-    static IWDG_HandleTypeDef hiwdg;   //Watchdog Timer
+    #if (NZ32S_USE_WWDG==1)
+    static WWDG_HandleTypeDef hwwdg;   //Windowed Watchdog Timer, is stopped during low power mode
+    #else
+    static IWDG_HandleTypeDef hiwdg;   //Independent Watchdog Timer, is NOT stopped during low power mode!
+    #endif
+
     static DigitalOut led1;
     static DigitalIn  btn1;
 
-#if (DONT_USE_A13_A14 == 0)
+    #if (DONT_USE_A13_A14 == 0)
     static DigitalInOut enableFastCharge;
-#endif
+    #endif
 };
 
 #ifdef __cplusplus