Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others
nz32s.cpp@19:42ae82a8f571, 2016-08-19 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
modtronix | 2:cd263c5e86f2 | 1 | /** |
modtronix | 5:e1297df8ee0d | 2 | * File: nz32s.cpp |
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 | #define THIS_IS_NZ32S_CPP |
modtronix | 5:e1297df8ee0d | 22 | |
modtronix | 2:cd263c5e86f2 | 23 | #include "mbed.h" |
modtronix | 2:cd263c5e86f2 | 24 | #include <stdarg.h> |
modtronix | 2:cd263c5e86f2 | 25 | #include <stdio.h> |
modtronix | 2:cd263c5e86f2 | 26 | |
modtronix | 3:99cb87ee1792 | 27 | #define DEBUG_ENABLE 1 |
modtronix | 3:99cb87ee1792 | 28 | #include "mx_default_debug.h" |
modtronix | 3:99cb87ee1792 | 29 | |
modtronix | 3:99cb87ee1792 | 30 | //Includes that use debugging - Include AFTER debug defines/includes above! It uses them for debugging! |
modtronix-com |
13:328bfac0e686 | 31 | #define DEBUG_ENABLE_NZ32S 0 |
modtronix-com |
13:328bfac0e686 | 32 | #define DEBUG_ENABLE_INFO_NZ32S 0 |
modtronix | 3:99cb87ee1792 | 33 | #include "nz32s.h" |
modtronix | 3:99cb87ee1792 | 34 | |
modtronix | 2:cd263c5e86f2 | 35 | |
modtronix | 2:cd263c5e86f2 | 36 | // DEFINES //////////////////////////////////////////////////////////////////// |
modtronix | 2:cd263c5e86f2 | 37 | |
modtronix | 2:cd263c5e86f2 | 38 | |
modtronix | 2:cd263c5e86f2 | 39 | // GLOBAL VARIABLES /////////////////////////////////////////////////////////// |
modtronix | 2:cd263c5e86f2 | 40 | |
modtronix | 2:cd263c5e86f2 | 41 | |
modtronix | 5:e1297df8ee0d | 42 | // STATIC OBJECTS ///////////////////////////////////////////////////////////// |
modtronix | 5:e1297df8ee0d | 43 | DigitalOut NZ32S::led1(LED1); |
modtronix | 5:e1297df8ee0d | 44 | DigitalIn NZ32S::btn1(USER_BUTTON, PullDown); |
modtronix-com |
15:3fd3c1ce01be | 45 | #if (NZ32S_USE_WWDG==1) |
modtronix-com |
15:3fd3c1ce01be | 46 | WWDG_HandleTypeDef NZ32S::hwwdg; //Windowed Watchdog Timer, is stopped during low power mode |
modtronix-com |
15:3fd3c1ce01be | 47 | #else |
modtronix-com |
15:3fd3c1ce01be | 48 | IWDG_HandleTypeDef NZ32S::hiwdg; //Independent Watchdog Timer, is NOT stopped during low power mode! |
modtronix-com |
15:3fd3c1ce01be | 49 | #endif |
modtronix-com |
17:86034c970ea0 | 50 | #if (NZ32S_USE_A13_A14 == 1) |
modtronix | 5:e1297df8ee0d | 51 | DigitalInOut NZ32S::enableFastCharge(PA_14, PIN_INPUT, PullNone, 0); |
modtronix | 5:e1297df8ee0d | 52 | #endif |
modtronix | 5:e1297df8ee0d | 53 | |
modtronix | 5:e1297df8ee0d | 54 | |
modtronix | 2:cd263c5e86f2 | 55 | // Function Prototypes //////////////////////////////////////////////////////// |
modtronix | 2:cd263c5e86f2 | 56 | |
modtronix | 2:cd263c5e86f2 | 57 | |
modtronix-com |
10:213d6350f0a8 | 58 | /** Constructor |
modtronix-com |
10:213d6350f0a8 | 59 | */ |
modtronix-com |
10:213d6350f0a8 | 60 | NZ32S::NZ32S() { |
modtronix-com |
10:213d6350f0a8 | 61 | led1 = 0; //User LED off |
modtronix-com |
10:213d6350f0a8 | 62 | } |
modtronix-com |
10:213d6350f0a8 | 63 | |
modtronix-com |
10:213d6350f0a8 | 64 | |
modtronix | 5:e1297df8ee0d | 65 | /** Get the battery voltage. |
modtronix | 5:e1297df8ee0d | 66 | */ |
modtronix | 5:e1297df8ee0d | 67 | uint16_t NZ32S::get_batt_mv() { |
modtronix | 5:e1297df8ee0d | 68 | MX_DEBUG("\r\nnz32s_getBattMV()"); |
modtronix | 5:e1297df8ee0d | 69 | |
modtronix | 5:e1297df8ee0d | 70 | //uint16_t getBattMV(void) { |
modtronix | 5:e1297df8ee0d | 71 | // float fval = 0; |
modtronix | 5:e1297df8ee0d | 72 | // |
modtronix-com |
17:86034c970ea0 | 73 | //#if (NZ32S_USE_A13_A14 == 1) |
modtronix | 5:e1297df8ee0d | 74 | //// uint16_t meas; |
modtronix | 5:e1297df8ee0d | 75 | //// |
modtronix | 5:e1297df8ee0d | 76 | //// //Measure Vbatt. It doesn't seem to make lots of a difference if we disable the DC/DC converter! |
modtronix | 5:e1297df8ee0d | 77 | //// ctrVBatt = 0; |
modtronix | 5:e1297df8ee0d | 78 | //// ctrVBatt.output(); |
modtronix | 5:e1297df8ee0d | 79 | //// wait_ms(150); |
modtronix | 5:e1297df8ee0d | 80 | //// meas = ainVBatt.read_u16(); // Converts and read the analog input value |
modtronix | 5:e1297df8ee0d | 81 | //// ctrVBatt.input(); |
modtronix | 5:e1297df8ee0d | 82 | //// //fval = ((float)meas / (float)0xffff) * 6600; //6600 = 3300*2, because resistor divider = 2 |
modtronix | 5:e1297df8ee0d | 83 | //// fval = meas * ((float)6600.0 / (float)65535.0); |
modtronix | 5:e1297df8ee0d | 84 | //#endif |
modtronix | 5:e1297df8ee0d | 85 | // |
modtronix | 5:e1297df8ee0d | 86 | // return (uint16_t) fval; |
modtronix | 5:e1297df8ee0d | 87 | return 0; |
modtronix | 5:e1297df8ee0d | 88 | } |
modtronix | 5:e1297df8ee0d | 89 | |
modtronix | 5:e1297df8ee0d | 90 | |
modtronix | 5:e1297df8ee0d | 91 | /** |
modtronix | 5:e1297df8ee0d | 92 | * Get Vusb or 5V supply voltage in millivolts. |
modtronix | 5:e1297df8ee0d | 93 | */ |
modtronix | 5:e1297df8ee0d | 94 | uint16_t NZ32S::get_supply_mv(void) { |
modtronix | 5:e1297df8ee0d | 95 | // float fval; |
modtronix | 5:e1297df8ee0d | 96 | // uint16_t meas; |
modtronix | 5:e1297df8ee0d | 97 | // |
modtronix | 5:e1297df8ee0d | 98 | // //Following voltages were measured at input of ADC: |
modtronix | 5:e1297df8ee0d | 99 | // // |
modtronix | 5:e1297df8ee0d | 100 | // //----- Vusb=0V & 5V supply=0V ----- |
modtronix | 5:e1297df8ee0d | 101 | // //Value is typically 4mV |
modtronix | 5:e1297df8ee0d | 102 | // // |
modtronix | 5:e1297df8ee0d | 103 | // //----- Vusb=5V & 5V supply=0V ----- |
modtronix | 5:e1297df8ee0d | 104 | // //When only VUSB is supplied, the value is typically 1.95V |
modtronix | 5:e1297df8ee0d | 105 | // // |
modtronix | 5:e1297df8ee0d | 106 | // //----- Vusb=0V & 5V supply=5V ----- |
modtronix | 5:e1297df8ee0d | 107 | // //When only VUSB is supplied, the value is typically 1.95V |
modtronix | 5:e1297df8ee0d | 108 | // // |
modtronix | 5:e1297df8ee0d | 109 | // //The reason this circuit does not work, is because of the reverse voltage of the diodes. It is given |
modtronix | 5:e1297df8ee0d | 110 | // //as about 40 to 80uA. With a resistor value of 470K, this will put 5V on other side of diode. |
modtronix | 5:e1297df8ee0d | 111 | // //Thus, no matter if 5V is at Vusb, Vsupply, or both, the read value will always be 1.95V. |
modtronix | 5:e1297df8ee0d | 112 | // //To solve problem, resistors must be lowered to value where 80uA will no longer give more than 5V |
modtronix | 5:e1297df8ee0d | 113 | // //voltage drop = 62k. Using two 47k resistors should work. |
modtronix | 5:e1297df8ee0d | 114 | // |
modtronix | 5:e1297df8ee0d | 115 | // //Measure Vusb/5V sense input |
modtronix | 5:e1297df8ee0d | 116 | // meas = ainVSense.read_u16(); // Converts and read the analog input value |
modtronix | 5:e1297df8ee0d | 117 | // fval = ((float) meas / (float) 0xffff) * 3300; |
modtronix | 5:e1297df8ee0d | 118 | // |
modtronix | 5:e1297df8ee0d | 119 | // return (uint16_t) fval; |
modtronix | 5:e1297df8ee0d | 120 | return 0; |
modtronix | 5:e1297df8ee0d | 121 | } |
modtronix | 5:e1297df8ee0d | 122 | |
modtronix | 2:cd263c5e86f2 | 123 | |
modtronix | 2:cd263c5e86f2 | 124 | /** |
modtronix | 2:cd263c5e86f2 | 125 | * Reset I2C bus |
modtronix | 2:cd263c5e86f2 | 126 | */ |
modtronix | 3:99cb87ee1792 | 127 | void NZ32S::i2c_reset(uint8_t busNumber, PinName sda, PinName scl) { |
modtronix | 2:cd263c5e86f2 | 128 | #if defined(STM32) || defined(STM32L1) |
modtronix | 2:cd263c5e86f2 | 129 | i2c_t objI2C; |
modtronix | 2:cd263c5e86f2 | 130 | |
modtronix | 2:cd263c5e86f2 | 131 | if (busNumber == 1) { |
modtronix | 2:cd263c5e86f2 | 132 | objI2C.i2c = I2C_1; |
modtronix | 2:cd263c5e86f2 | 133 | } |
modtronix | 2:cd263c5e86f2 | 134 | else if (busNumber == 2) { |
modtronix | 2:cd263c5e86f2 | 135 | objI2C.i2c = I2C_2; |
modtronix | 2:cd263c5e86f2 | 136 | } |
modtronix | 2:cd263c5e86f2 | 137 | |
modtronix | 2:cd263c5e86f2 | 138 | objI2C.slave = 0; |
modtronix | 2:cd263c5e86f2 | 139 | i2c_init(&objI2C, sda, scl); |
modtronix | 2:cd263c5e86f2 | 140 | //i2c_reset(&objI2C); //i2c_init() above calls i2c_reset() |
modtronix | 2:cd263c5e86f2 | 141 | //pc.printf("\r\nI2C1 Reset"); |
modtronix | 2:cd263c5e86f2 | 142 | |
modtronix | 2:cd263c5e86f2 | 143 | MX_DEBUG("\r\nI2C1 Reset"); |
modtronix | 2:cd263c5e86f2 | 144 | |
modtronix | 3:99cb87ee1792 | 145 | #endif //#if defined(STM32) || defined(STM32L1) |
modtronix | 2:cd263c5e86f2 | 146 | } |
modtronix | 3:99cb87ee1792 | 147 | |
modtronix | 3:99cb87ee1792 | 148 | void NZ32S::print_reset_cause(bool clearFlags) { |
modtronix | 3:99cb87ee1792 | 149 | #if defined(STM32) || defined(STM32L1) |
modtronix | 3:99cb87ee1792 | 150 | if ( __HAL_RCC_GET_FLAG(PWR_FLAG_SB)) |
modtronix | 3:99cb87ee1792 | 151 | MX_DEBUG("\r\nSystem resumed from STANDBY mode"); |
modtronix | 3:99cb87ee1792 | 152 | |
modtronix | 3:99cb87ee1792 | 153 | if ( __HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST)) |
modtronix | 3:99cb87ee1792 | 154 | MX_DEBUG("\r\nSoftware Reset"); |
modtronix | 3:99cb87ee1792 | 155 | |
modtronix | 3:99cb87ee1792 | 156 | if ( __HAL_RCC_GET_FLAG(RCC_FLAG_PORRST)) |
modtronix | 3:99cb87ee1792 | 157 | MX_DEBUG("\r\nPower-On-Reset"); |
modtronix | 3:99cb87ee1792 | 158 | |
modtronix | 3:99cb87ee1792 | 159 | if ( __HAL_RCC_GET_FLAG(RCC_FLAG_PINRST)) // Always set, test other cases first |
modtronix | 3:99cb87ee1792 | 160 | MX_DEBUG("\r\nExternal Pin Reset"); |
modtronix | 3:99cb87ee1792 | 161 | |
modtronix | 3:99cb87ee1792 | 162 | if ( __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) |
modtronix | 3:99cb87ee1792 | 163 | MX_DEBUG("\r\nWatchdog Reset"); |
modtronix | 3:99cb87ee1792 | 164 | |
modtronix | 3:99cb87ee1792 | 165 | if ( __HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) != RESET) |
modtronix | 3:99cb87ee1792 | 166 | MX_DEBUG("\r\nWindow Watchdog Reset"); |
modtronix | 3:99cb87ee1792 | 167 | |
modtronix | 3:99cb87ee1792 | 168 | if ( __HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST) != RESET) |
modtronix | 3:99cb87ee1792 | 169 | MX_DEBUG("\r\nLow Power Reset"); |
modtronix | 3:99cb87ee1792 | 170 | |
modtronix | 3:99cb87ee1792 | 171 | // if ( __HAL_RCC_GET_FLAG(RCC_FLAG_BORRST) != RESET) // F4 Usually set with POR |
modtronix | 3:99cb87ee1792 | 172 | // MX_DEBUG("\r\nBrown-Out Reset"); |
modtronix | 3:99cb87ee1792 | 173 | |
modtronix | 3:99cb87ee1792 | 174 | if (clearFlags) { |
modtronix | 3:99cb87ee1792 | 175 | __HAL_RCC_CLEAR_RESET_FLAGS(); // The flags cleared after use |
modtronix | 3:99cb87ee1792 | 176 | } |
modtronix | 3:99cb87ee1792 | 177 | #endif //#if defined(STM32) || defined(STM32L1) |
modtronix | 3:99cb87ee1792 | 178 | } |
modtronix | 3:99cb87ee1792 | 179 |