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

Revision:
3:99cb87ee1792
Parent:
2:cd263c5e86f2
Child:
5:e1297df8ee0d
--- a/nz32s.h	Sat Aug 29 13:54:00 2015 +1000
+++ b/nz32s.h	Sun Aug 30 09:23:05 2015 +1000
@@ -31,9 +31,81 @@
     #endif
 #endif
 
-/**
- * Reset I2C bus
- */
-void nz32s_resetI2C(uint8_t busNumber, PinName sda, PinName scl);
+
+class NZ32S {
+public:
+    static IWDG_HandleTypeDef hiwdg;   //Watchdog Timer
+
+public:
+    /**
+     * Reset I2C bus
+     */
+    static void i2c_reset(uint8_t busNumber, PinName sda, PinName scl);
+
+    /** Initializes and start the IWDG Watchdog timer with given timerout.
+     * After calling this function, watchdog_refresh() function must be called before the
+     * timeout expires, else the MCU will be reset.
+     *
+     * @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) {
+        //Watchdog frequency is always 32 kHz
+        //Prescaler: Min_Value = 4 and Max_Value = 256
+        //Reload: Min_Data = 0 and Max_Data = 0x0FFF(4000)
+        //TimeOut in seconds = (Reload * Prescaler) / Freq.
+        //MinTimeOut = (4 * 1) / 32000 = 0.000125 seconds (125 microseconds)
+        //MaxTimeOut = (256 * 4096) / 32000 = 32.768 seconds
+        NZ32S::hiwdg.Instance = IWDG;
+
+        //MXH_DEBUG("\r\nwatchdog_start() called!!!!!!!!!!!!!!");
+
+        //For values below 4000, use IWDG_PRESCALER_32. This will cause timeout period to be
+        //equal to 'timeout' value in ms
+        if(timeout < 4000) {
+            NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
+            NZ32S::hiwdg.Init.Reload = timeout;    //Timeout = (32 * timeout) / 32000 = (timeout) ms
+        }
+        else {
+            NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
+            NZ32S::hiwdg.Init.Reload = timeout/8;  //Timeout = (256 * (timeout/8)) / 32000
+        }
+        HAL_IWDG_Init(&NZ32S::hiwdg);
+
+        //Start the watchdog timer
+        HAL_IWDG_Start(&NZ32S::hiwdg);
+    }
+
+
+    /** Refreshes the IWDG
+     */
+    static inline void watchdog_refresh(void) {
+        HAL_IWDG_Refresh(&NZ32S::hiwdg);
+    }
+
+
+    /** Return true if last reset was caused by watchdog timer
+     *
+     * @param clearFlags If set, will clear ALL reset flags! Note that not only the
+     * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
+     */
+    static inline bool watchdog_caused_reset(bool clearFlags) {
+        bool retVal;
+        retVal = __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)==0?false:true;
+        if (clearFlags==true) {
+            __HAL_RCC_CLEAR_RESET_FLAGS(); // The flags cleared after use
+        }
+        return retVal;
+    }
+
+    /** Print the cause of last reset
+     *
+     * @param clearFlags If set, will clear ALL reset flags! Note that not only the
+     * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
+     */
+    static inline void print_reset_cause(bool clearFlags);
+
+
+};
+
 
 #endif //MODTRONIX_NZ32S_H_