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:
5:e1297df8ee0d
Parent:
3:99cb87ee1792
Child:
7:709130701ac7

File content as of revision 5:e1297df8ee0d:

/**
 * File:      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 NZ32S_H_
#define NZ32S_H_

#include "mbed.h"
#include "nz32s_default_config.h"

#ifdef __cplusplus
extern "C" {
#endif

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


class NZ32S {
public:
    /** Toggle System LED1
    */
    static inline void toggle_led1(void) {
        led1 = !led1;
    }

    /** Set System LED1
     */
    static inline void set_led1(void) {
        led1 = 1;
    }

    /** Set System LED1
     */
    static inline void clear_led1(void) {
        led1 = 0;
    }

    /** Set System LED1
     */
    static inline void write_led1(bool val) {
        led1 = val;
    }


    /** Get state of button1
     */
    static inline bool get_btn1(void) {
        return (bool)btn1.read();
    }

    /** Enable fast charging
     * This function will enable fast charging of the battery. Ensure solder jumper labeled
     * "Fast Charge" (J13 on NZ32-SC151) on the back of the board is made!
     */
    static inline void enable_fast_charging(void) {
        #if (DONT_USE_A13_A14 == 0)
        asdf
        enableFastCharge.output();
        enableFastCharge = 0;
        #endif
    }


    /** Disable fast charging
     */
    static inline void disable_fast_charging(void) {
        #if (DONT_USE_A13_A14 == 0)
        NZ32S::enableFastCharge.input();
        NZ32S::enableFastCharge.mode(PullNone);
        #endif
    }

    /** Get the battery voltage.
     */
    static uint16_t get_batt_mv();

    /**
     * Get Vusb or 5V supply voltage in millivolts.
     */
    static uint16_t get_supply_mv(void);

    /**
     * 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);

public:
    static IWDG_HandleTypeDef hiwdg;   //Watchdog Timer
    static DigitalOut led1;
    static DigitalIn  btn1;

#if (DONT_USE_A13_A14 == 0)
    static DigitalInOut enableFastCharge;
#endif
};

#ifdef __cplusplus
};
#endif

#endif //NZ32S_H_