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

Committer:
modtronix-com
Date:
Wed Oct 21 14:53:20 2015 +1100
Revision:
13:328bfac0e686
Parent:
11:86288b5c75cb
Child:
15:3fd3c1ce01be
Many improvements, and added dependency for modtronix_config.h file.

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 3:99cb87ee1792 159 //Watchdog frequency is always 32 kHz
modtronix 3:99cb87ee1792 160 //Prescaler: Min_Value = 4 and Max_Value = 256
modtronix 3:99cb87ee1792 161 //Reload: Min_Data = 0 and Max_Data = 0x0FFF(4000)
modtronix 3:99cb87ee1792 162 //TimeOut in seconds = (Reload * Prescaler) / Freq.
modtronix 3:99cb87ee1792 163 //MinTimeOut = (4 * 1) / 32000 = 0.000125 seconds (125 microseconds)
modtronix 3:99cb87ee1792 164 //MaxTimeOut = (256 * 4096) / 32000 = 32.768 seconds
modtronix 3:99cb87ee1792 165 NZ32S::hiwdg.Instance = IWDG;
modtronix 3:99cb87ee1792 166
modtronix-com 13:328bfac0e686 167 MXH_DEBUG("\r\nwatchdog_start() called!");
modtronix 3:99cb87ee1792 168
modtronix 3:99cb87ee1792 169 //For values below 4000, use IWDG_PRESCALER_32. This will cause timeout period to be
modtronix 3:99cb87ee1792 170 //equal to 'timeout' value in ms
modtronix 3:99cb87ee1792 171 if(timeout < 4000) {
modtronix 3:99cb87ee1792 172 NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
modtronix 3:99cb87ee1792 173 NZ32S::hiwdg.Init.Reload = timeout; //Timeout = (32 * timeout) / 32000 = (timeout) ms
modtronix 3:99cb87ee1792 174 }
modtronix 3:99cb87ee1792 175 else {
modtronix 3:99cb87ee1792 176 NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
modtronix 3:99cb87ee1792 177 NZ32S::hiwdg.Init.Reload = timeout/8; //Timeout = (256 * (timeout/8)) / 32000
modtronix 3:99cb87ee1792 178 }
modtronix 3:99cb87ee1792 179 HAL_IWDG_Init(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 180
modtronix 3:99cb87ee1792 181 //Start the watchdog timer
modtronix 3:99cb87ee1792 182 HAL_IWDG_Start(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 183 }
modtronix 3:99cb87ee1792 184
modtronix 3:99cb87ee1792 185
modtronix 3:99cb87ee1792 186 /** Refreshes the IWDG
modtronix 3:99cb87ee1792 187 */
modtronix 3:99cb87ee1792 188 static inline void watchdog_refresh(void) {
modtronix 3:99cb87ee1792 189 HAL_IWDG_Refresh(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 190 }
modtronix 3:99cb87ee1792 191
modtronix 3:99cb87ee1792 192
modtronix 3:99cb87ee1792 193 /** Return true if last reset was caused by watchdog timer
modtronix 3:99cb87ee1792 194 *
modtronix 3:99cb87ee1792 195 * @param clearFlags If set, will clear ALL reset flags! Note that not only the
modtronix 3:99cb87ee1792 196 * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
modtronix 3:99cb87ee1792 197 */
modtronix 3:99cb87ee1792 198 static inline bool watchdog_caused_reset(bool clearFlags) {
modtronix 3:99cb87ee1792 199 bool retVal;
modtronix 3:99cb87ee1792 200 retVal = __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)==0?false:true;
modtronix 3:99cb87ee1792 201 if (clearFlags==true) {
modtronix 3:99cb87ee1792 202 __HAL_RCC_CLEAR_RESET_FLAGS(); // The flags cleared after use
modtronix 3:99cb87ee1792 203 }
modtronix 3:99cb87ee1792 204 return retVal;
modtronix 3:99cb87ee1792 205 }
modtronix 3:99cb87ee1792 206
modtronix 3:99cb87ee1792 207 /** Print the cause of last reset
modtronix 3:99cb87ee1792 208 *
modtronix 3:99cb87ee1792 209 * @param clearFlags If set, will clear ALL reset flags! Note that not only the
modtronix 3:99cb87ee1792 210 * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
modtronix 3:99cb87ee1792 211 */
modtronix 3:99cb87ee1792 212 static inline void print_reset_cause(bool clearFlags);
modtronix 3:99cb87ee1792 213
modtronix 5:e1297df8ee0d 214 public:
modtronix 5:e1297df8ee0d 215 static IWDG_HandleTypeDef hiwdg; //Watchdog Timer
modtronix 5:e1297df8ee0d 216 static DigitalOut led1;
modtronix 5:e1297df8ee0d 217 static DigitalIn btn1;
modtronix 3:99cb87ee1792 218
modtronix 5:e1297df8ee0d 219 #if (DONT_USE_A13_A14 == 0)
modtronix 5:e1297df8ee0d 220 static DigitalInOut enableFastCharge;
modtronix 5:e1297df8ee0d 221 #endif
modtronix 3:99cb87ee1792 222 };
modtronix 3:99cb87ee1792 223
modtronix 5:e1297df8ee0d 224 #ifdef __cplusplus
modtronix 5:e1297df8ee0d 225 };
modtronix 5:e1297df8ee0d 226 #endif
modtronix 2:cd263c5e86f2 227
modtronix-com 13:328bfac0e686 228 #if defined(MXH_DEBUG)
modtronix-com 13:328bfac0e686 229 #undef MXH_DEBUG
modtronix-com 13:328bfac0e686 230 #endif
modtronix-com 13:328bfac0e686 231 #if defined(MXH_DEBUG_INFO)
modtronix-com 13:328bfac0e686 232 #undef MXH_DEBUG_INFO
modtronix-com 13:328bfac0e686 233 #endif
modtronix-com 13:328bfac0e686 234
modtronix 5:e1297df8ee0d 235 #endif //NZ32S_H_