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:
10:213d6350f0a8
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.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 5:e1297df8ee0d 45 IWDG_HandleTypeDef NZ32S::hiwdg; //Watchdog Timer
modtronix 5:e1297df8ee0d 46 #if (DONT_USE_A13_A14 == 0)
modtronix 5:e1297df8ee0d 47 DigitalInOut NZ32S::enableFastCharge(PA_14, PIN_INPUT, PullNone, 0);
modtronix 5:e1297df8ee0d 48 #endif
modtronix 5:e1297df8ee0d 49
modtronix 5:e1297df8ee0d 50
modtronix 2:cd263c5e86f2 51 // Function Prototypes ////////////////////////////////////////////////////////
modtronix 2:cd263c5e86f2 52
modtronix 2:cd263c5e86f2 53
modtronix-com 10:213d6350f0a8 54 /** Constructor
modtronix-com 10:213d6350f0a8 55 */
modtronix-com 10:213d6350f0a8 56 NZ32S::NZ32S() {
modtronix-com 10:213d6350f0a8 57 led1 = 0; //User LED off
modtronix-com 10:213d6350f0a8 58 }
modtronix-com 10:213d6350f0a8 59
modtronix-com 10:213d6350f0a8 60
modtronix 5:e1297df8ee0d 61 /** Get the battery voltage.
modtronix 5:e1297df8ee0d 62 */
modtronix 5:e1297df8ee0d 63 uint16_t NZ32S::get_batt_mv() {
modtronix 5:e1297df8ee0d 64 MX_DEBUG("\r\nnz32s_getBattMV()");
modtronix 5:e1297df8ee0d 65
modtronix 5:e1297df8ee0d 66 //uint16_t getBattMV(void) {
modtronix 5:e1297df8ee0d 67 // float fval = 0;
modtronix 5:e1297df8ee0d 68 //
modtronix-com 13:328bfac0e686 69 //#if (DONT_USE_A13_A14 == 0)
modtronix 5:e1297df8ee0d 70 //// uint16_t meas;
modtronix 5:e1297df8ee0d 71 ////
modtronix 5:e1297df8ee0d 72 //// //Measure Vbatt. It doesn't seem to make lots of a difference if we disable the DC/DC converter!
modtronix 5:e1297df8ee0d 73 //// ctrVBatt = 0;
modtronix 5:e1297df8ee0d 74 //// ctrVBatt.output();
modtronix 5:e1297df8ee0d 75 //// wait_ms(150);
modtronix 5:e1297df8ee0d 76 //// meas = ainVBatt.read_u16(); // Converts and read the analog input value
modtronix 5:e1297df8ee0d 77 //// ctrVBatt.input();
modtronix 5:e1297df8ee0d 78 //// //fval = ((float)meas / (float)0xffff) * 6600; //6600 = 3300*2, because resistor divider = 2
modtronix 5:e1297df8ee0d 79 //// fval = meas * ((float)6600.0 / (float)65535.0);
modtronix 5:e1297df8ee0d 80 //#endif
modtronix 5:e1297df8ee0d 81 //
modtronix 5:e1297df8ee0d 82 // return (uint16_t) fval;
modtronix 5:e1297df8ee0d 83 return 0;
modtronix 5:e1297df8ee0d 84 }
modtronix 5:e1297df8ee0d 85
modtronix 5:e1297df8ee0d 86
modtronix 5:e1297df8ee0d 87 /**
modtronix 5:e1297df8ee0d 88 * Get Vusb or 5V supply voltage in millivolts.
modtronix 5:e1297df8ee0d 89 */
modtronix 5:e1297df8ee0d 90 uint16_t NZ32S::get_supply_mv(void) {
modtronix 5:e1297df8ee0d 91 // float fval;
modtronix 5:e1297df8ee0d 92 // uint16_t meas;
modtronix 5:e1297df8ee0d 93 //
modtronix 5:e1297df8ee0d 94 // //Following voltages were measured at input of ADC:
modtronix 5:e1297df8ee0d 95 // //
modtronix 5:e1297df8ee0d 96 // //----- Vusb=0V & 5V supply=0V -----
modtronix 5:e1297df8ee0d 97 // //Value is typically 4mV
modtronix 5:e1297df8ee0d 98 // //
modtronix 5:e1297df8ee0d 99 // //----- Vusb=5V & 5V supply=0V -----
modtronix 5:e1297df8ee0d 100 // //When only VUSB is supplied, the value is typically 1.95V
modtronix 5:e1297df8ee0d 101 // //
modtronix 5:e1297df8ee0d 102 // //----- Vusb=0V & 5V supply=5V -----
modtronix 5:e1297df8ee0d 103 // //When only VUSB is supplied, the value is typically 1.95V
modtronix 5:e1297df8ee0d 104 // //
modtronix 5:e1297df8ee0d 105 // //The reason this circuit does not work, is because of the reverse voltage of the diodes. It is given
modtronix 5:e1297df8ee0d 106 // //as about 40 to 80uA. With a resistor value of 470K, this will put 5V on other side of diode.
modtronix 5:e1297df8ee0d 107 // //Thus, no matter if 5V is at Vusb, Vsupply, or both, the read value will always be 1.95V.
modtronix 5:e1297df8ee0d 108 // //To solve problem, resistors must be lowered to value where 80uA will no longer give more than 5V
modtronix 5:e1297df8ee0d 109 // //voltage drop = 62k. Using two 47k resistors should work.
modtronix 5:e1297df8ee0d 110 //
modtronix 5:e1297df8ee0d 111 // //Measure Vusb/5V sense input
modtronix 5:e1297df8ee0d 112 // meas = ainVSense.read_u16(); // Converts and read the analog input value
modtronix 5:e1297df8ee0d 113 // fval = ((float) meas / (float) 0xffff) * 3300;
modtronix 5:e1297df8ee0d 114 //
modtronix 5:e1297df8ee0d 115 // return (uint16_t) fval;
modtronix 5:e1297df8ee0d 116 return 0;
modtronix 5:e1297df8ee0d 117 }
modtronix 5:e1297df8ee0d 118
modtronix 2:cd263c5e86f2 119
modtronix 2:cd263c5e86f2 120 /**
modtronix 2:cd263c5e86f2 121 * Reset I2C bus
modtronix 2:cd263c5e86f2 122 */
modtronix 3:99cb87ee1792 123 void NZ32S::i2c_reset(uint8_t busNumber, PinName sda, PinName scl) {
modtronix 2:cd263c5e86f2 124 #if defined(STM32) || defined(STM32L1)
modtronix 2:cd263c5e86f2 125 i2c_t objI2C;
modtronix 2:cd263c5e86f2 126
modtronix 2:cd263c5e86f2 127 if (busNumber == 1) {
modtronix 2:cd263c5e86f2 128 objI2C.i2c = I2C_1;
modtronix 2:cd263c5e86f2 129 }
modtronix 2:cd263c5e86f2 130 else if (busNumber == 2) {
modtronix 2:cd263c5e86f2 131 objI2C.i2c = I2C_2;
modtronix 2:cd263c5e86f2 132 }
modtronix 2:cd263c5e86f2 133
modtronix 2:cd263c5e86f2 134 objI2C.slave = 0;
modtronix 2:cd263c5e86f2 135 i2c_init(&objI2C, sda, scl);
modtronix 2:cd263c5e86f2 136 //i2c_reset(&objI2C); //i2c_init() above calls i2c_reset()
modtronix 2:cd263c5e86f2 137 //pc.printf("\r\nI2C1 Reset");
modtronix 2:cd263c5e86f2 138
modtronix 2:cd263c5e86f2 139 MX_DEBUG("\r\nI2C1 Reset");
modtronix 2:cd263c5e86f2 140
modtronix 3:99cb87ee1792 141 #endif //#if defined(STM32) || defined(STM32L1)
modtronix 2:cd263c5e86f2 142 }
modtronix 3:99cb87ee1792 143
modtronix 3:99cb87ee1792 144 void NZ32S::print_reset_cause(bool clearFlags) {
modtronix 3:99cb87ee1792 145 #if defined(STM32) || defined(STM32L1)
modtronix 3:99cb87ee1792 146 if ( __HAL_RCC_GET_FLAG(PWR_FLAG_SB))
modtronix 3:99cb87ee1792 147 MX_DEBUG("\r\nSystem resumed from STANDBY mode");
modtronix 3:99cb87ee1792 148
modtronix 3:99cb87ee1792 149 if ( __HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST))
modtronix 3:99cb87ee1792 150 MX_DEBUG("\r\nSoftware Reset");
modtronix 3:99cb87ee1792 151
modtronix 3:99cb87ee1792 152 if ( __HAL_RCC_GET_FLAG(RCC_FLAG_PORRST))
modtronix 3:99cb87ee1792 153 MX_DEBUG("\r\nPower-On-Reset");
modtronix 3:99cb87ee1792 154
modtronix 3:99cb87ee1792 155 if ( __HAL_RCC_GET_FLAG(RCC_FLAG_PINRST)) // Always set, test other cases first
modtronix 3:99cb87ee1792 156 MX_DEBUG("\r\nExternal Pin Reset");
modtronix 3:99cb87ee1792 157
modtronix 3:99cb87ee1792 158 if ( __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET)
modtronix 3:99cb87ee1792 159 MX_DEBUG("\r\nWatchdog Reset");
modtronix 3:99cb87ee1792 160
modtronix 3:99cb87ee1792 161 if ( __HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) != RESET)
modtronix 3:99cb87ee1792 162 MX_DEBUG("\r\nWindow Watchdog Reset");
modtronix 3:99cb87ee1792 163
modtronix 3:99cb87ee1792 164 if ( __HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST) != RESET)
modtronix 3:99cb87ee1792 165 MX_DEBUG("\r\nLow Power Reset");
modtronix 3:99cb87ee1792 166
modtronix 3:99cb87ee1792 167 // if ( __HAL_RCC_GET_FLAG(RCC_FLAG_BORRST) != RESET) // F4 Usually set with POR
modtronix 3:99cb87ee1792 168 // MX_DEBUG("\r\nBrown-Out Reset");
modtronix 3:99cb87ee1792 169
modtronix 3:99cb87ee1792 170 if (clearFlags) {
modtronix 3:99cb87ee1792 171 __HAL_RCC_CLEAR_RESET_FLAGS(); // The flags cleared after use
modtronix 3:99cb87ee1792 172 }
modtronix 3:99cb87ee1792 173 #endif //#if defined(STM32) || defined(STM32L1)
modtronix 3:99cb87ee1792 174 }
modtronix 3:99cb87ee1792 175