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

Committer:
modtronix-com
Date:
Sat Aug 13 12:57:48 2016 +1000
Revision:
15:3fd3c1ce01be
Parent:
13:328bfac0e686
Child:
17:86034c970ea0
Added support for Windowed and Independent WDT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 2:cd263c5e86f2 1 /**
modtronix 5:e1297df8ee0d 2 * File: nz32s.h
modtronix 2:cd263c5e86f2 3 *
modtronix 2:cd263c5e86f2 4 * Author: Modtronix Engineering - www.modtronix.com
modtronix 2:cd263c5e86f2 5 *
modtronix 2:cd263c5e86f2 6 * Description:
modtronix 2:cd263c5e86f2 7 *
modtronix 2:cd263c5e86f2 8 * Software License Agreement:
modtronix 2:cd263c5e86f2 9 * This software has been written or modified by Modtronix Engineering. The code
modtronix 2:cd263c5e86f2 10 * may be modified and can be used free of charge for commercial and non commercial
modtronix 2:cd263c5e86f2 11 * applications. If this is modified software, any license conditions from original
modtronix 2:cd263c5e86f2 12 * software also apply. Any redistribution must include reference to 'Modtronix
modtronix 2:cd263c5e86f2 13 * Engineering' and web link(www.modtronix.com) in the file header.
modtronix 2:cd263c5e86f2 14 *
modtronix 2:cd263c5e86f2 15 * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
modtronix 2:cd263c5e86f2 16 * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
modtronix 2:cd263c5e86f2 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
modtronix 2:cd263c5e86f2 18 * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
modtronix 2:cd263c5e86f2 19 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
modtronix 2:cd263c5e86f2 20 */
modtronix 5:e1297df8ee0d 21 #ifndef NZ32S_H_
modtronix 5:e1297df8ee0d 22 #define NZ32S_H_
modtronix 2:cd263c5e86f2 23
modtronix 2:cd263c5e86f2 24 #include "mbed.h"
modtronix-com 7:709130701ac7 25 #include "mx_tick.h"
modtronix-com 7:709130701ac7 26 #include "mx_helpers.h"
modtronix-com 13:328bfac0e686 27 #include "mx_circular_buffer.h"
modtronix-com 13:328bfac0e686 28 #include "mx_cmd_buffer.h"
modtronix 5:e1297df8ee0d 29 #include "nz32s_default_config.h"
modtronix 5:e1297df8ee0d 30
modtronix-com 13:328bfac0e686 31
modtronix-com 13:328bfac0e686 32 // Debugging //////////////////////////////////////////////////////////////////
modtronix-com 13:328bfac0e686 33 // To enable debug output from this file, define MX_DEBUG and DEBUG_ENABLE_NZ32S before
modtronix-com 13:328bfac0e686 34 // including this file.
modtronix-com 13:328bfac0e686 35 //
modtronix-com 13:328bfac0e686 36 //Defines for MXH_DEBUG - debugging for include file
modtronix-com 13:328bfac0e686 37 #if !defined(DEBUG_ENABLE_NZ32S)
modtronix-com 13:328bfac0e686 38 #define DEBUG_ENABLE_NZ32S 0
modtronix-com 13:328bfac0e686 39 #endif
modtronix-com 13:328bfac0e686 40 #if !defined(DEBUG_ENABLE_INFO_NZ32S)
modtronix-com 13:328bfac0e686 41 #define DEBUG_ENABLE_INFO_NZ32S 0
modtronix-com 13:328bfac0e686 42 #endif
modtronix-com 13:328bfac0e686 43
modtronix-com 13:328bfac0e686 44 #if !defined(MXH_DEBUG)
modtronix-com 13:328bfac0e686 45 #if defined(MX_DEBUG) && (DEBUG_ENABLE_NZ32S==1)
modtronix-com 13:328bfac0e686 46 #define MXH_DEBUG MX_DEBUG
modtronix-com 13:328bfac0e686 47 #else
modtronix-com 13:328bfac0e686 48 #define MXH_DEBUG(format, args...) ((void)0)
modtronix-com 13:328bfac0e686 49 #endif
modtronix-com 13:328bfac0e686 50 #endif
modtronix-com 13:328bfac0e686 51
modtronix-com 13:328bfac0e686 52 #if !defined(MXH_DEBUG_INFO)
modtronix-com 13:328bfac0e686 53 #if defined(MX_DEBUG) && (DEBUG_ENABLE_NZ32S==1) && (DEBUG_ENABLE_INFO_NZ32S==1)
modtronix-com 13:328bfac0e686 54 #define MXH_DEBUG_INFO MX_DEBUG
modtronix-com 13:328bfac0e686 55 #else
modtronix-com 13:328bfac0e686 56 #define MXH_DEBUG_INFO(format, args...) ((void)0)
modtronix-com 13:328bfac0e686 57 #endif
modtronix-com 13:328bfac0e686 58 #endif
modtronix-com 13:328bfac0e686 59
modtronix-com 13:328bfac0e686 60
modtronix 5:e1297df8ee0d 61 #ifdef __cplusplus
modtronix 5:e1297df8ee0d 62 extern "C" {
modtronix 5:e1297df8ee0d 63 #endif
modtronix 2:cd263c5e86f2 64
modtronix 2:cd263c5e86f2 65 #ifndef WEAK
modtronix 2:cd263c5e86f2 66 #if defined (__ICCARM__)
modtronix 2:cd263c5e86f2 67 #define WEAK __weak
modtronix 2:cd263c5e86f2 68 #else
modtronix 2:cd263c5e86f2 69 #define WEAK __attribute__((weak))
modtronix 2:cd263c5e86f2 70 #endif
modtronix 2:cd263c5e86f2 71 #endif
modtronix 2:cd263c5e86f2 72
modtronix 3:99cb87ee1792 73
modtronix 3:99cb87ee1792 74 class NZ32S {
modtronix 3:99cb87ee1792 75 public:
modtronix-com 10:213d6350f0a8 76 /** Constructor
modtronix-com 10:213d6350f0a8 77 */
modtronix-com 10:213d6350f0a8 78 NZ32S();
modtronix-com 10:213d6350f0a8 79
modtronix 5:e1297df8ee0d 80 /** Toggle System LED1
modtronix 5:e1297df8ee0d 81 */
modtronix-com 13:328bfac0e686 82 static inline bool toggle_led1(void) {
modtronix 5:e1297df8ee0d 83 led1 = !led1;
modtronix-com 13:328bfac0e686 84 return (bool)led1.read();
modtronix 5:e1297df8ee0d 85 }
modtronix 5:e1297df8ee0d 86
modtronix 5:e1297df8ee0d 87 /** Set System LED1
modtronix 5:e1297df8ee0d 88 */
modtronix 5:e1297df8ee0d 89 static inline void set_led1(void) {
modtronix 5:e1297df8ee0d 90 led1 = 1;
modtronix 5:e1297df8ee0d 91 }
modtronix 5:e1297df8ee0d 92
modtronix 5:e1297df8ee0d 93 /** Set System LED1
modtronix 5:e1297df8ee0d 94 */
modtronix 5:e1297df8ee0d 95 static inline void clear_led1(void) {
modtronix 5:e1297df8ee0d 96 led1 = 0;
modtronix 5:e1297df8ee0d 97 }
modtronix 5:e1297df8ee0d 98
modtronix 5:e1297df8ee0d 99 /** Set System LED1
modtronix 5:e1297df8ee0d 100 */
modtronix 5:e1297df8ee0d 101 static inline void write_led1(bool val) {
modtronix 5:e1297df8ee0d 102 led1 = val;
modtronix 5:e1297df8ee0d 103 }
modtronix 5:e1297df8ee0d 104
modtronix-com 13:328bfac0e686 105 /** Get state of LED2
modtronix-com 13:328bfac0e686 106 */
modtronix-com 13:328bfac0e686 107 static inline bool get_led1(void) {
modtronix-com 13:328bfac0e686 108 return (bool)led1.read();
modtronix-com 13:328bfac0e686 109 }
modtronix 5:e1297df8ee0d 110
modtronix-com 13:328bfac0e686 111 /** Get state of button1. Returns 1 when pressed
modtronix 5:e1297df8ee0d 112 */
modtronix 5:e1297df8ee0d 113 static inline bool get_btn1(void) {
modtronix 5:e1297df8ee0d 114 return (bool)btn1.read();
modtronix 5:e1297df8ee0d 115 }
modtronix 3:99cb87ee1792 116
modtronix 5:e1297df8ee0d 117 /** Enable fast charging
modtronix 5:e1297df8ee0d 118 * This function will enable fast charging of the battery. Ensure solder jumper labeled
modtronix 5:e1297df8ee0d 119 * "Fast Charge" (J13 on NZ32-SC151) on the back of the board is made!
modtronix 5:e1297df8ee0d 120 */
modtronix 5:e1297df8ee0d 121 static inline void enable_fast_charging(void) {
modtronix 5:e1297df8ee0d 122 #if (DONT_USE_A13_A14 == 0)
modtronix 5:e1297df8ee0d 123 enableFastCharge.output();
modtronix 5:e1297df8ee0d 124 enableFastCharge = 0;
modtronix 5:e1297df8ee0d 125 #endif
modtronix 5:e1297df8ee0d 126 }
modtronix 5:e1297df8ee0d 127
modtronix 5:e1297df8ee0d 128
modtronix 5:e1297df8ee0d 129 /** Disable fast charging
modtronix 5:e1297df8ee0d 130 */
modtronix 5:e1297df8ee0d 131 static inline void disable_fast_charging(void) {
modtronix 5:e1297df8ee0d 132 #if (DONT_USE_A13_A14 == 0)
modtronix 5:e1297df8ee0d 133 NZ32S::enableFastCharge.input();
modtronix 5:e1297df8ee0d 134 NZ32S::enableFastCharge.mode(PullNone);
modtronix 5:e1297df8ee0d 135 #endif
modtronix 5:e1297df8ee0d 136 }
modtronix 5:e1297df8ee0d 137
modtronix 5:e1297df8ee0d 138 /** Get the battery voltage.
modtronix 5:e1297df8ee0d 139 */
modtronix 5:e1297df8ee0d 140 static uint16_t get_batt_mv();
modtronix 5:e1297df8ee0d 141
modtronix 5:e1297df8ee0d 142 /**
modtronix 5:e1297df8ee0d 143 * Get Vusb or 5V supply voltage in millivolts.
modtronix 5:e1297df8ee0d 144 */
modtronix 5:e1297df8ee0d 145 static uint16_t get_supply_mv(void);
modtronix 5:e1297df8ee0d 146
modtronix 3:99cb87ee1792 147 /**
modtronix 3:99cb87ee1792 148 * Reset I2C bus
modtronix 3:99cb87ee1792 149 */
modtronix 3:99cb87ee1792 150 static void i2c_reset(uint8_t busNumber, PinName sda, PinName scl);
modtronix 3:99cb87ee1792 151
modtronix 3:99cb87ee1792 152 /** Initializes and start the IWDG Watchdog timer with given timerout.
modtronix 3:99cb87ee1792 153 * After calling this function, watchdog_refresh() function must be called before the
modtronix 3:99cb87ee1792 154 * timeout expires, else the MCU will be reset.
modtronix 3:99cb87ee1792 155 *
modtronix 3:99cb87ee1792 156 * @param timeout The timeout in ms. Must be a value from 1 to 32000 (1ms to 32 seconds)
modtronix 3:99cb87ee1792 157 */
modtronix 3:99cb87ee1792 158 static inline void watchdog_start(uint32_t timeout) {
modtronix-com 15:3fd3c1ce01be 159 #if (NZ32S_USE_WWDG==1)
modtronix-com 15:3fd3c1ce01be 160 //WWDG clock counter = (PCLK1 (32MHz)/4096)/8) = 976 Hz (~1024 us)
modtronix-com 15:3fd3c1ce01be 161 //WWDG Window value = 80 means that the WWDG counter should be refreshed only
modtronix-com 15:3fd3c1ce01be 162 //when the counter is below 80 (and greater than 64/0x40) otherwise a reset will
modtronix-com 15:3fd3c1ce01be 163 //be generated.
modtronix-com 15:3fd3c1ce01be 164 //WWDG Counter value = 127, WWDG timeout = ~1024 us * 64 = 65.57 ms */
modtronix-com 15:3fd3c1ce01be 165 //
modtronix-com 15:3fd3c1ce01be 166 //Min and Max for given Prescaler and WDGTB
modtronix-com 15:3fd3c1ce01be 167 //For Prescaler=1 and WDGTB=0 is:
modtronix-com 15:3fd3c1ce01be 168 // - Min TimeOut = 128uS, Max Timeout = 8.19ms
modtronix-com 15:3fd3c1ce01be 169 //For Prescaler=2 and WDGTB=1 is:
modtronix-com 15:3fd3c1ce01be 170 // - Min TimeOut = 256uS, Max Timeout = 16.38ms
modtronix-com 15:3fd3c1ce01be 171 //For Prescaler=4 and WDGTB=2 is:
modtronix-com 15:3fd3c1ce01be 172 // - Min TimeOut = 512uS, Max Timeout = 32.76ms
modtronix-com 15:3fd3c1ce01be 173 //For Prescaler=8 and WDGTB=3 is:
modtronix-com 15:3fd3c1ce01be 174 // - Min TimeOut = 1024uS, Max Timeout = 65.54ms
modtronix-com 15:3fd3c1ce01be 175 NZ32S::hwwdg.Instance = WWDG;
modtronix-com 15:3fd3c1ce01be 176
modtronix-com 15:3fd3c1ce01be 177 MXH_DEBUG("\r\nwatchdog_start() called!");
modtronix-com 15:3fd3c1ce01be 178
modtronix-com 15:3fd3c1ce01be 179 //The refresh window is:between 48.1ms (~1024 * (127-80)) and 65.57 ms (~1024 * 64)
modtronix-com 15:3fd3c1ce01be 180 NZ32S::hwwdg.Init.Prescaler = WWDG_PRESCALER_8;
modtronix-com 15:3fd3c1ce01be 181 NZ32S::hwwdg.Init.Window = 80;
modtronix-com 15:3fd3c1ce01be 182 NZ32S::hwwdg.Init.Counter = 127;
modtronix-com 15:3fd3c1ce01be 183
modtronix-com 15:3fd3c1ce01be 184 HAL_WWDG_Init(&NZ32S::hwwdg);
modtronix-com 15:3fd3c1ce01be 185
modtronix-com 15:3fd3c1ce01be 186 //Start the watchdog timer
modtronix-com 15:3fd3c1ce01be 187 HAL_WWDG_Start(&NZ32S::hwwdg);
modtronix-com 15:3fd3c1ce01be 188 #else
modtronix 3:99cb87ee1792 189 //Watchdog frequency is always 32 kHz
modtronix 3:99cb87ee1792 190 //Prescaler: Min_Value = 4 and Max_Value = 256
modtronix 3:99cb87ee1792 191 //Reload: Min_Data = 0 and Max_Data = 0x0FFF(4000)
modtronix 3:99cb87ee1792 192 //TimeOut in seconds = (Reload * Prescaler) / Freq.
modtronix 3:99cb87ee1792 193 //MinTimeOut = (4 * 1) / 32000 = 0.000125 seconds (125 microseconds)
modtronix 3:99cb87ee1792 194 //MaxTimeOut = (256 * 4096) / 32000 = 32.768 seconds
modtronix 3:99cb87ee1792 195 NZ32S::hiwdg.Instance = IWDG;
modtronix 3:99cb87ee1792 196
modtronix-com 13:328bfac0e686 197 MXH_DEBUG("\r\nwatchdog_start() called!");
modtronix 3:99cb87ee1792 198
modtronix 3:99cb87ee1792 199 //For values below 4000, use IWDG_PRESCALER_32. This will cause timeout period to be
modtronix 3:99cb87ee1792 200 //equal to 'timeout' value in ms
modtronix 3:99cb87ee1792 201 if(timeout < 4000) {
modtronix 3:99cb87ee1792 202 NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
modtronix 3:99cb87ee1792 203 NZ32S::hiwdg.Init.Reload = timeout; //Timeout = (32 * timeout) / 32000 = (timeout) ms
modtronix 3:99cb87ee1792 204 }
modtronix 3:99cb87ee1792 205 else {
modtronix 3:99cb87ee1792 206 NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
modtronix 3:99cb87ee1792 207 NZ32S::hiwdg.Init.Reload = timeout/8; //Timeout = (256 * (timeout/8)) / 32000
modtronix 3:99cb87ee1792 208 }
modtronix 3:99cb87ee1792 209 HAL_IWDG_Init(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 210
modtronix 3:99cb87ee1792 211 //Start the watchdog timer
modtronix 3:99cb87ee1792 212 HAL_IWDG_Start(&NZ32S::hiwdg);
modtronix-com 15:3fd3c1ce01be 213 #endif
modtronix 3:99cb87ee1792 214 }
modtronix 3:99cb87ee1792 215
modtronix 3:99cb87ee1792 216
modtronix 3:99cb87ee1792 217 /** Refreshes the IWDG
modtronix 3:99cb87ee1792 218 */
modtronix 3:99cb87ee1792 219 static inline void watchdog_refresh(void) {
modtronix-com 15:3fd3c1ce01be 220 #if (NZ32S_USE_WWDG==1)
modtronix-com 15:3fd3c1ce01be 221 //Refresh WWDG: update counter value to 127, the refresh window is:
modtronix-com 15:3fd3c1ce01be 222 //between 48.1ms (~1024 * (127-80)) and 65.57 ms (~1024 * 64) */
modtronix-com 15:3fd3c1ce01be 223 HAL_WWDG_Refresh(&NZ32S::hwwdg, 127);
modtronix-com 15:3fd3c1ce01be 224 #else
modtronix 3:99cb87ee1792 225 HAL_IWDG_Refresh(&NZ32S::hiwdg);
modtronix-com 15:3fd3c1ce01be 226 #endif
modtronix 3:99cb87ee1792 227 }
modtronix 3:99cb87ee1792 228
modtronix 3:99cb87ee1792 229
modtronix 3:99cb87ee1792 230 /** Return true if last reset was caused by watchdog timer
modtronix 3:99cb87ee1792 231 *
modtronix 3:99cb87ee1792 232 * @param clearFlags If set, will clear ALL reset flags! Note that not only the
modtronix 3:99cb87ee1792 233 * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
modtronix 3:99cb87ee1792 234 */
modtronix 3:99cb87ee1792 235 static inline bool watchdog_caused_reset(bool clearFlags) {
modtronix 3:99cb87ee1792 236 bool retVal;
modtronix 3:99cb87ee1792 237 retVal = __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)==0?false:true;
modtronix 3:99cb87ee1792 238 if (clearFlags==true) {
modtronix 3:99cb87ee1792 239 __HAL_RCC_CLEAR_RESET_FLAGS(); // The flags cleared after use
modtronix 3:99cb87ee1792 240 }
modtronix 3:99cb87ee1792 241 return retVal;
modtronix 3:99cb87ee1792 242 }
modtronix 3:99cb87ee1792 243
modtronix 3:99cb87ee1792 244 /** Print the cause of last reset
modtronix 3:99cb87ee1792 245 *
modtronix 3:99cb87ee1792 246 * @param clearFlags If set, will clear ALL reset flags! Note that not only the
modtronix 3:99cb87ee1792 247 * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
modtronix 3:99cb87ee1792 248 */
modtronix 3:99cb87ee1792 249 static inline void print_reset_cause(bool clearFlags);
modtronix 3:99cb87ee1792 250
modtronix 5:e1297df8ee0d 251 public:
modtronix-com 15:3fd3c1ce01be 252 #if (NZ32S_USE_WWDG==1)
modtronix-com 15:3fd3c1ce01be 253 static WWDG_HandleTypeDef hwwdg; //Windowed Watchdog Timer, is stopped during low power mode
modtronix-com 15:3fd3c1ce01be 254 #else
modtronix-com 15:3fd3c1ce01be 255 static IWDG_HandleTypeDef hiwdg; //Independent Watchdog Timer, is NOT stopped during low power mode!
modtronix-com 15:3fd3c1ce01be 256 #endif
modtronix-com 15:3fd3c1ce01be 257
modtronix 5:e1297df8ee0d 258 static DigitalOut led1;
modtronix 5:e1297df8ee0d 259 static DigitalIn btn1;
modtronix 3:99cb87ee1792 260
modtronix-com 15:3fd3c1ce01be 261 #if (DONT_USE_A13_A14 == 0)
modtronix 5:e1297df8ee0d 262 static DigitalInOut enableFastCharge;
modtronix-com 15:3fd3c1ce01be 263 #endif
modtronix 3:99cb87ee1792 264 };
modtronix 3:99cb87ee1792 265
modtronix 5:e1297df8ee0d 266 #ifdef __cplusplus
modtronix 5:e1297df8ee0d 267 };
modtronix 5:e1297df8ee0d 268 #endif
modtronix 2:cd263c5e86f2 269
modtronix-com 13:328bfac0e686 270 #if defined(MXH_DEBUG)
modtronix-com 13:328bfac0e686 271 #undef MXH_DEBUG
modtronix-com 13:328bfac0e686 272 #endif
modtronix-com 13:328bfac0e686 273 #if defined(MXH_DEBUG_INFO)
modtronix-com 13:328bfac0e686 274 #undef MXH_DEBUG_INFO
modtronix-com 13:328bfac0e686 275 #endif
modtronix-com 13:328bfac0e686 276
modtronix 5:e1297df8ee0d 277 #endif //NZ32S_H_