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

Committer:
modtronix-com
Date:
Mon Sep 14 15:20:50 2015 +1000
Revision:
10:213d6350f0a8
Parent:
5:e1297df8ee0d
Child:
13:328bfac0e686
added constructor

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