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

Committer:
modtronix-com
Date:
Fri Aug 19 15:52:51 2016 +1000
Revision:
19:42ae82a8f571
Parent:
17:86034c970ea0
Added tag v1.1 for changeset 37e7c8fac8c7

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