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