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

nz32s.h

Committer:
modtronix
Date:
2015-08-30
Revision:
3:99cb87ee1792
Parent:
2:cd263c5e86f2
Child:
5:e1297df8ee0d

File content as of revision 3:99cb87ee1792:

/**
 * File:      modtronix_nz32s.h
 *
 * Author:    Modtronix Engineering - www.modtronix.com
 *
 * Description:
 *
 * Software License Agreement:
 * This software has been written or modified by Modtronix Engineering. The code
 * may be modified and can be used free of charge for commercial and non commercial
 * applications. If this is modified software, any license conditions from original
 * software also apply. Any redistribution must include reference to 'Modtronix
 * Engineering' and web link(www.modtronix.com) in the file header.
 *
 * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
 * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
 * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */
#ifndef MODTRONIX_NZ32S_H_
#define MODTRONIX_NZ32S_H_

#include "mbed.h"

#ifndef WEAK
    #if defined (__ICCARM__)
        #define WEAK     __weak
    #else
        #define WEAK     __attribute__((weak))
    #endif
#endif


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_