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

Committer:
modtronix
Date:
Sun Aug 30 11:59:06 2015 +1000
Revision:
5:e1297df8ee0d
Parent:
3:99cb87ee1792
Child:
7:709130701ac7
Added functions and macros

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 5:e1297df8ee0d 25 #include "nz32s_default_config.h"
modtronix 5:e1297df8ee0d 26
modtronix 5:e1297df8ee0d 27 #ifdef __cplusplus
modtronix 5:e1297df8ee0d 28 extern "C" {
modtronix 5:e1297df8ee0d 29 #endif
modtronix 2:cd263c5e86f2 30
modtronix 2:cd263c5e86f2 31 #ifndef WEAK
modtronix 2:cd263c5e86f2 32 #if defined (__ICCARM__)
modtronix 2:cd263c5e86f2 33 #define WEAK __weak
modtronix 2:cd263c5e86f2 34 #else
modtronix 2:cd263c5e86f2 35 #define WEAK __attribute__((weak))
modtronix 2:cd263c5e86f2 36 #endif
modtronix 2:cd263c5e86f2 37 #endif
modtronix 2:cd263c5e86f2 38
modtronix 3:99cb87ee1792 39
modtronix 3:99cb87ee1792 40 class NZ32S {
modtronix 3:99cb87ee1792 41 public:
modtronix 5:e1297df8ee0d 42 /** Toggle System LED1
modtronix 5:e1297df8ee0d 43 */
modtronix 5:e1297df8ee0d 44 static inline void toggle_led1(void) {
modtronix 5:e1297df8ee0d 45 led1 = !led1;
modtronix 5:e1297df8ee0d 46 }
modtronix 5:e1297df8ee0d 47
modtronix 5:e1297df8ee0d 48 /** Set System LED1
modtronix 5:e1297df8ee0d 49 */
modtronix 5:e1297df8ee0d 50 static inline void set_led1(void) {
modtronix 5:e1297df8ee0d 51 led1 = 1;
modtronix 5:e1297df8ee0d 52 }
modtronix 5:e1297df8ee0d 53
modtronix 5:e1297df8ee0d 54 /** Set System LED1
modtronix 5:e1297df8ee0d 55 */
modtronix 5:e1297df8ee0d 56 static inline void clear_led1(void) {
modtronix 5:e1297df8ee0d 57 led1 = 0;
modtronix 5:e1297df8ee0d 58 }
modtronix 5:e1297df8ee0d 59
modtronix 5:e1297df8ee0d 60 /** Set System LED1
modtronix 5:e1297df8ee0d 61 */
modtronix 5:e1297df8ee0d 62 static inline void write_led1(bool val) {
modtronix 5:e1297df8ee0d 63 led1 = val;
modtronix 5:e1297df8ee0d 64 }
modtronix 5:e1297df8ee0d 65
modtronix 5:e1297df8ee0d 66
modtronix 5:e1297df8ee0d 67 /** Get state of button1
modtronix 5:e1297df8ee0d 68 */
modtronix 5:e1297df8ee0d 69 static inline bool get_btn1(void) {
modtronix 5:e1297df8ee0d 70 return (bool)btn1.read();
modtronix 5:e1297df8ee0d 71 }
modtronix 3:99cb87ee1792 72
modtronix 5:e1297df8ee0d 73 /** Enable fast charging
modtronix 5:e1297df8ee0d 74 * This function will enable fast charging of the battery. Ensure solder jumper labeled
modtronix 5:e1297df8ee0d 75 * "Fast Charge" (J13 on NZ32-SC151) on the back of the board is made!
modtronix 5:e1297df8ee0d 76 */
modtronix 5:e1297df8ee0d 77 static inline void enable_fast_charging(void) {
modtronix 5:e1297df8ee0d 78 #if (DONT_USE_A13_A14 == 0)
modtronix 5:e1297df8ee0d 79 asdf
modtronix 5:e1297df8ee0d 80 enableFastCharge.output();
modtronix 5:e1297df8ee0d 81 enableFastCharge = 0;
modtronix 5:e1297df8ee0d 82 #endif
modtronix 5:e1297df8ee0d 83 }
modtronix 5:e1297df8ee0d 84
modtronix 5:e1297df8ee0d 85
modtronix 5:e1297df8ee0d 86 /** Disable fast charging
modtronix 5:e1297df8ee0d 87 */
modtronix 5:e1297df8ee0d 88 static inline void disable_fast_charging(void) {
modtronix 5:e1297df8ee0d 89 #if (DONT_USE_A13_A14 == 0)
modtronix 5:e1297df8ee0d 90 NZ32S::enableFastCharge.input();
modtronix 5:e1297df8ee0d 91 NZ32S::enableFastCharge.mode(PullNone);
modtronix 5:e1297df8ee0d 92 #endif
modtronix 5:e1297df8ee0d 93 }
modtronix 5:e1297df8ee0d 94
modtronix 5:e1297df8ee0d 95 /** Get the battery voltage.
modtronix 5:e1297df8ee0d 96 */
modtronix 5:e1297df8ee0d 97 static uint16_t get_batt_mv();
modtronix 5:e1297df8ee0d 98
modtronix 5:e1297df8ee0d 99 /**
modtronix 5:e1297df8ee0d 100 * Get Vusb or 5V supply voltage in millivolts.
modtronix 5:e1297df8ee0d 101 */
modtronix 5:e1297df8ee0d 102 static uint16_t get_supply_mv(void);
modtronix 5:e1297df8ee0d 103
modtronix 3:99cb87ee1792 104 /**
modtronix 3:99cb87ee1792 105 * Reset I2C bus
modtronix 3:99cb87ee1792 106 */
modtronix 3:99cb87ee1792 107 static void i2c_reset(uint8_t busNumber, PinName sda, PinName scl);
modtronix 3:99cb87ee1792 108
modtronix 3:99cb87ee1792 109 /** Initializes and start the IWDG Watchdog timer with given timerout.
modtronix 3:99cb87ee1792 110 * After calling this function, watchdog_refresh() function must be called before the
modtronix 3:99cb87ee1792 111 * timeout expires, else the MCU will be reset.
modtronix 3:99cb87ee1792 112 *
modtronix 3:99cb87ee1792 113 * @param timeout The timeout in ms. Must be a value from 1 to 32000 (1ms to 32 seconds)
modtronix 3:99cb87ee1792 114 */
modtronix 3:99cb87ee1792 115 static inline void watchdog_start(uint32_t timeout) {
modtronix 3:99cb87ee1792 116 //Watchdog frequency is always 32 kHz
modtronix 3:99cb87ee1792 117 //Prescaler: Min_Value = 4 and Max_Value = 256
modtronix 3:99cb87ee1792 118 //Reload: Min_Data = 0 and Max_Data = 0x0FFF(4000)
modtronix 3:99cb87ee1792 119 //TimeOut in seconds = (Reload * Prescaler) / Freq.
modtronix 3:99cb87ee1792 120 //MinTimeOut = (4 * 1) / 32000 = 0.000125 seconds (125 microseconds)
modtronix 3:99cb87ee1792 121 //MaxTimeOut = (256 * 4096) / 32000 = 32.768 seconds
modtronix 3:99cb87ee1792 122 NZ32S::hiwdg.Instance = IWDG;
modtronix 3:99cb87ee1792 123
modtronix 3:99cb87ee1792 124 //MXH_DEBUG("\r\nwatchdog_start() called!!!!!!!!!!!!!!");
modtronix 3:99cb87ee1792 125
modtronix 3:99cb87ee1792 126 //For values below 4000, use IWDG_PRESCALER_32. This will cause timeout period to be
modtronix 3:99cb87ee1792 127 //equal to 'timeout' value in ms
modtronix 3:99cb87ee1792 128 if(timeout < 4000) {
modtronix 3:99cb87ee1792 129 NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
modtronix 3:99cb87ee1792 130 NZ32S::hiwdg.Init.Reload = timeout; //Timeout = (32 * timeout) / 32000 = (timeout) ms
modtronix 3:99cb87ee1792 131 }
modtronix 3:99cb87ee1792 132 else {
modtronix 3:99cb87ee1792 133 NZ32S::hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
modtronix 3:99cb87ee1792 134 NZ32S::hiwdg.Init.Reload = timeout/8; //Timeout = (256 * (timeout/8)) / 32000
modtronix 3:99cb87ee1792 135 }
modtronix 3:99cb87ee1792 136 HAL_IWDG_Init(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 137
modtronix 3:99cb87ee1792 138 //Start the watchdog timer
modtronix 3:99cb87ee1792 139 HAL_IWDG_Start(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 140 }
modtronix 3:99cb87ee1792 141
modtronix 3:99cb87ee1792 142
modtronix 3:99cb87ee1792 143 /** Refreshes the IWDG
modtronix 3:99cb87ee1792 144 */
modtronix 3:99cb87ee1792 145 static inline void watchdog_refresh(void) {
modtronix 3:99cb87ee1792 146 HAL_IWDG_Refresh(&NZ32S::hiwdg);
modtronix 3:99cb87ee1792 147 }
modtronix 3:99cb87ee1792 148
modtronix 3:99cb87ee1792 149
modtronix 3:99cb87ee1792 150 /** Return true if last reset was caused by watchdog timer
modtronix 3:99cb87ee1792 151 *
modtronix 3:99cb87ee1792 152 * @param clearFlags If set, will clear ALL reset flags! Note that not only the
modtronix 3:99cb87ee1792 153 * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
modtronix 3:99cb87ee1792 154 */
modtronix 3:99cb87ee1792 155 static inline bool watchdog_caused_reset(bool clearFlags) {
modtronix 3:99cb87ee1792 156 bool retVal;
modtronix 3:99cb87ee1792 157 retVal = __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)==0?false:true;
modtronix 3:99cb87ee1792 158 if (clearFlags==true) {
modtronix 3:99cb87ee1792 159 __HAL_RCC_CLEAR_RESET_FLAGS(); // The flags cleared after use
modtronix 3:99cb87ee1792 160 }
modtronix 3:99cb87ee1792 161 return retVal;
modtronix 3:99cb87ee1792 162 }
modtronix 3:99cb87ee1792 163
modtronix 3:99cb87ee1792 164 /** Print the cause of last reset
modtronix 3:99cb87ee1792 165 *
modtronix 3:99cb87ee1792 166 * @param clearFlags If set, will clear ALL reset flags! Note that not only the
modtronix 3:99cb87ee1792 167 * watchdog flag, but ALL reset flags located in RCC_CSR register are cleared!
modtronix 3:99cb87ee1792 168 */
modtronix 3:99cb87ee1792 169 static inline void print_reset_cause(bool clearFlags);
modtronix 3:99cb87ee1792 170
modtronix 5:e1297df8ee0d 171 public:
modtronix 5:e1297df8ee0d 172 static IWDG_HandleTypeDef hiwdg; //Watchdog Timer
modtronix 5:e1297df8ee0d 173 static DigitalOut led1;
modtronix 5:e1297df8ee0d 174 static DigitalIn btn1;
modtronix 3:99cb87ee1792 175
modtronix 5:e1297df8ee0d 176 #if (DONT_USE_A13_A14 == 0)
modtronix 5:e1297df8ee0d 177 static DigitalInOut enableFastCharge;
modtronix 5:e1297df8ee0d 178 #endif
modtronix 3:99cb87ee1792 179 };
modtronix 3:99cb87ee1792 180
modtronix 5:e1297df8ee0d 181 #ifdef __cplusplus
modtronix 5:e1297df8ee0d 182 };
modtronix 5:e1297df8ee0d 183 #endif
modtronix 2:cd263c5e86f2 184
modtronix 5:e1297df8ee0d 185 #endif //NZ32S_H_