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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nz32s.h Source File

nz32s.h

00001 /**
00002  * File:      nz32s.h
00003  *
00004  * Author:    Modtronix Engineering - www.modtronix.com
00005  *
00006  * Description:
00007  *
00008  * Software License Agreement:
00009  * This software has been written or modified by Modtronix Engineering. The code
00010  * may be modified and can be used free of charge for commercial and non commercial
00011  * applications. If this is modified software, any license conditions from original
00012  * software also apply. Any redistribution must include reference to 'Modtronix
00013  * Engineering' and web link(www.modtronix.com) in the file header.
00014  *
00015  * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
00016  * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
00017  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
00018  * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
00019  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
00020  */
00021 #ifndef NZ32S_H_
00022 #define NZ32S_H_
00023 
00024 #include "nz32s_default_config.h"
00025 #include "mx_tick.h"
00026 #include "mx_helpers.h"
00027 #include "mx_circular_buffer.h"
00028 #include "mx_cmd_buffer.h"
00029 
00030 
00031 // Debugging //////////////////////////////////////////////////////////////////
00032 // To enable debug output from this file, define MX_DEBUG and DEBUG_ENABLE_NZ32S before
00033 // including this file.
00034 //
00035 //Defines for MXH_DEBUG - debugging for include file
00036 #if !defined(DEBUG_ENABLE_NZ32S)
00037     #define DEBUG_ENABLE_NZ32S          0
00038 #endif
00039 #if !defined(DEBUG_ENABLE_INFO_NZ32S)
00040     #define DEBUG_ENABLE_INFO_NZ32S     0
00041 #endif
00042 
00043 #if !defined(MXH_DEBUG)
00044     #if defined(MX_DEBUG) && (DEBUG_ENABLE_NZ32S==1)
00045         #define MXH_DEBUG MX_DEBUG
00046     #else
00047         #define MXH_DEBUG(format, args...) ((void)0)
00048     #endif
00049 #endif
00050 
00051 #if !defined(MXH_DEBUG_INFO)
00052     #if defined(MX_DEBUG) && (DEBUG_ENABLE_NZ32S==1) && (DEBUG_ENABLE_INFO_NZ32S==1)
00053         #define MXH_DEBUG_INFO MX_DEBUG
00054     #else
00055         #define MXH_DEBUG_INFO(format, args...) ((void)0)
00056     #endif
00057 #endif
00058 
00059 
00060 #ifdef __cplusplus
00061 extern "C" {
00062 #endif
00063 
00064 #ifndef WEAK
00065     #if defined (__ICCARM__)
00066         #define WEAK     __weak
00067     #else
00068         #define WEAK     __attribute__((weak))
00069     #endif
00070 #endif
00071 
00072 
00073 class NZ32S {
00074 public:
00075     /** Constructor
00076      */
00077     NZ32S();
00078 
00079     /** Toggle System LED1
00080     */
00081     static inline bool toggle_led1(void) {
00082         led1 = !led1;
00083         return (bool)led1.read();
00084     }
00085 
00086     /** Set System LED1
00087      */
00088     static inline void set_led1(void) {
00089         led1 = 1;
00090     }
00091 
00092     /** Set System LED1
00093      */
00094     static inline void clear_led1(void) {
00095         led1 = 0;
00096     }
00097 
00098     /** Set System LED1
00099      */
00100     static inline void write_led1(bool val) {
00101         led1 = val;
00102     }
00103 
00104     /** Get state of LED2
00105      */
00106     static inline bool get_led1(void) {
00107         return (bool)led1.read();
00108     }
00109 
00110     /** Get state of button1. Returns 1 when pressed
00111      */
00112     static inline bool get_btn1(void) {
00113         return (bool)btn1.read();
00114     }
00115 
00116     /** Enable fast charging
00117      * This function will enable fast charging of the battery. Ensure solder jumper labeled
00118      * "Fast Charge" (J13 on NZ32-SC151) on the back of the board is made!
00119      */
00120     static inline void enable_fast_charging(void) {
00121         #if (NZ32S_USE_A13_A14 == 1)
00122         enableFastCharge.output();
00123         enableFastCharge = 0; //Enable fast charging
00124         #endif
00125     }
00126 
00127 
00128     /** Disable fast charging
00129      */
00130     static inline void disable_fast_charging(void) {
00131         #if (NZ32S_USE_A13_A14 == 1)
00132         NZ32S::enableFastCharge.input();
00133         NZ32S::enableFastCharge.mode(PullNone);
00134         #endif
00135     }
00136 
00137     /** Get the battery voltage.
00138      */
00139     static uint16_t get_batt_mv();
00140 
00141     /**
00142      * Get Vusb or 5V supply voltage in millivolts.
00143      */
00144     static uint16_t get_supply_mv(void);
00145 
00146     /**
00147      * Reset I2C bus
00148      */
00149     static void i2c_reset(uint8_t busNumber, PinName sda, PinName scl);
00150 
00151     /** Initializes and start the IWDG Watchdog timer with given timerout.
00152      * After calling this function, watchdog_refresh() function must be called before the
00153      * timeout expires, else the MCU will be reset.
00154      *
00155      * @param timeout The timeout in ms. Must be a value from 1 to 32000 (1ms to 32 seconds)
00156      */
00157     static inline void watchdog_start(uint32_t timeout) {
00158 #if (NZ32S_USE_WWDG==1)
00159         //WWDG clock counter = (PCLK1 (32MHz)/4096)/8) = 976 Hz (~1024 us)
00160         //WWDG Window value = 80 means that the WWDG counter should be refreshed only
00161         //when the counter is below 80 (and greater than 64/0x40) otherwise a reset will
00162         //be generated.
00163         //WWDG Counter value = 127, WWDG timeout = ~1024 us * 64 = 65.57 ms */
00164         //
00165         //Min and Max for given Prescaler and WDGTB
00166         //For Prescaler=1 and WDGTB=0 is:
00167         // - Min TimeOut = 128uS, Max Timeout = 8.19ms
00168         //For Prescaler=2 and WDGTB=1 is:
00169         // - Min TimeOut = 256uS, Max Timeout = 16.38ms
00170         //For Prescaler=4 and WDGTB=2 is:
00171         // - Min TimeOut = 512uS, Max Timeout = 32.76ms
00172         //For Prescaler=8 and WDGTB=3 is:
00173         // - Min TimeOut = 1024uS, Max Timeout = 65.54ms
00174         NZ32S::hwwdg.Instance = WWDG;
00175 
00176         MXH_DEBUG("\r\nwatchdog_start() called!");
00177 
00178         //The refresh window is:between 48.1ms (~1024 * (127-80)) and 65.57 ms (~1024 * 64)
00179         NZ32S::hwwdg.Init.Prescaler = WWDG_PRESCALER_8;
00180         NZ32S::hwwdg.Init.Window    = 80;
00181         NZ32S::hwwdg.Init.Counter   = 127;
00182 
00183         HAL_WWDG_Init(&NZ32S::hwwdg);
00184 
00185         //Start the watchdog timer
00186         HAL_WWDG_Start(&NZ32S::hwwdg);
00187 #else
00188         //Watchdog frequency is always 32 kHz
00189         //Prescaler: Min_Value = 4 and Max_Value = 256
00190         //Reload: Min_Data = 0 and Max_Data = 0x0FFF(4000)
00191         //TimeOut in seconds = (Reload * Prescaler) / Freq.
00192         //MinTimeOut = (4 * 1) / 32000 = 0.000125 seconds (125 microseconds)
00193         //MaxTimeOut = (256 * 4096) / 32000 = 32.768 seconds
00194         NZ32S::hiwdg.Instance = IWDG;
00195 
00196         MXH_DEBUG("\r\nwatchdog_start() called!");
00197 
00198         //For values below 4000, use IWDG_PRESCALER_32. This will cause timeout period to be
00199         //equal to 'timeout' value in ms
00200         if(timeout < 4000) {
00201             NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
00202             NZ32S::hiwdg.Init.Reload = timeout;    //Timeout = (32 * timeout) / 32000 = (timeout) ms
00203         }
00204         else {
00205             NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
00206             NZ32S::hiwdg.Init.Reload = timeout/8;  //Timeout = (256 * (timeout/8)) / 32000
00207         }
00208         HAL_IWDG_Init(&NZ32S::hiwdg);
00209 
00210         //Start the watchdog timer
00211         HAL_IWDG_Start(&NZ32S::hiwdg);
00212 #endif
00213     }
00214 
00215 
00216     /** Refreshes the IWDG
00217      */
00218     static inline void watchdog_refresh(void) {
00219 #if (NZ32S_USE_WWDG==1)
00220         //Refresh WWDG: update counter value to 127, the refresh window is:
00221         //between 48.1ms (~1024 * (127-80)) and 65.57 ms (~1024 * 64) */
00222         HAL_WWDG_Refresh(&NZ32S::hwwdg, 127);
00223 #else
00224         HAL_IWDG_Refresh(&NZ32S::hiwdg);
00225 #endif
00226     }
00227 
00228 
00229     /** Return true if last reset was caused by watchdog timer
00230      *
00231      * @param clearFlags If set, will clear ALL reset flags! Note that not only the
00232      * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
00233      */
00234     static inline bool watchdog_caused_reset(bool clearFlags) {
00235         bool retVal;
00236         retVal = __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)==0?false:true;
00237         if (clearFlags==true) {
00238             __HAL_RCC_CLEAR_RESET_FLAGS(); // The flags cleared after use
00239         }
00240         return retVal;
00241     }
00242 
00243     /** Print the cause of last reset
00244      *
00245      * @param clearFlags If set, will clear ALL reset flags! Note that not only the
00246      * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
00247      */
00248     static inline void print_reset_cause(bool clearFlags);
00249 
00250 public:
00251     #if (NZ32S_USE_WWDG==1)
00252     static WWDG_HandleTypeDef hwwdg;   //Windowed Watchdog Timer, is stopped during low power mode
00253     #else
00254     static IWDG_HandleTypeDef hiwdg;   //Independent Watchdog Timer, is NOT stopped during low power mode!
00255     #endif
00256 
00257     static DigitalOut led1;
00258     static DigitalIn  btn1;
00259 
00260     #if (NZ32S_USE_A13_A14 == 1)
00261     static DigitalInOut enableFastCharge;
00262     #endif
00263 };
00264 
00265 #ifdef __cplusplus
00266 };
00267 #endif
00268 
00269 #if defined(MXH_DEBUG)
00270     #undef MXH_DEBUG
00271 #endif
00272 #if defined(MXH_DEBUG_INFO)
00273     #undef MXH_DEBUG_INFO
00274 #endif
00275 
00276 #endif //NZ32S_H_