A compilation of some hardware sensors and their shared programming interfaces.

Committer:
mgottscho
Date:
Wed Mar 19 00:35:31 2014 +0000
Revision:
1:15396cab58d1
Parent:
0:8d34cc2ff388
Updated for most recent UtilityLib.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mgottscho 0:8d34cc2ff388 1 /* INA219.h
mgottscho 0:8d34cc2ff388 2 * Tested with mbed board: FRDM-KL46Z
mgottscho 0:8d34cc2ff388 3 * Author: Mark Gottscho
mgottscho 0:8d34cc2ff388 4 * mgottscho@ucla.edu
mgottscho 0:8d34cc2ff388 5 */
mgottscho 0:8d34cc2ff388 6
mgottscho 0:8d34cc2ff388 7 #ifndef INA219_H
mgottscho 0:8d34cc2ff388 8 #define INA219_H
mgottscho 0:8d34cc2ff388 9
mgottscho 0:8d34cc2ff388 10 #include "mbed.h"
mgottscho 0:8d34cc2ff388 11 #include "I2CSensor.h"
mgottscho 0:8d34cc2ff388 12 #include "PeriodicSensor.h"
mgottscho 0:8d34cc2ff388 13
mgottscho 0:8d34cc2ff388 14 /**
mgottscho 0:8d34cc2ff388 15 * This class allows for easy control over a INA219 current/power sensing IC.
mgottscho 0:8d34cc2ff388 16 */
mgottscho 0:8d34cc2ff388 17 class INA219 : public I2CSensor, public PeriodicSensor {
mgottscho 0:8d34cc2ff388 18 public:
mgottscho 0:8d34cc2ff388 19 /**
mgottscho 0:8d34cc2ff388 20 * @param sda the pin identifier for SDA I2C signal
mgottscho 0:8d34cc2ff388 21 * @param scl the pin identifier for SCL I2C signal
mgottscho 0:8d34cc2ff388 22 * @param i2c_addr the 8-bit I2C address for this device. Note that LSB is a don't care.
mgottscho 0:8d34cc2ff388 23 */
mgottscho 0:8d34cc2ff388 24 INA219(PinName sda, PinName scl, int i2c_addr);
mgottscho 0:8d34cc2ff388 25
mgottscho 0:8d34cc2ff388 26 /**
mgottscho 0:8d34cc2ff388 27 * Destroys this object
mgottscho 0:8d34cc2ff388 28 */
mgottscho 0:8d34cc2ff388 29 ~INA219();
mgottscho 0:8d34cc2ff388 30
mgottscho 0:8d34cc2ff388 31 /**
mgottscho 0:8d34cc2ff388 32 * Initializes the device to some nice state.
mgottscho 0:8d34cc2ff388 33 */
mgottscho 0:8d34cc2ff388 34 void selfInit();
mgottscho 0:8d34cc2ff388 35
mgottscho 0:8d34cc2ff388 36 /**
mgottscho 0:8d34cc2ff388 37 * Performs a software reset of the device.
mgottscho 0:8d34cc2ff388 38 */
mgottscho 0:8d34cc2ff388 39 void reset();
mgottscho 0:8d34cc2ff388 40
mgottscho 0:8d34cc2ff388 41 /**
mgottscho 0:8d34cc2ff388 42 * Sets the bus voltage range to either 16V or 32V.
mgottscho 0:8d34cc2ff388 43 * @param enable if true, sets the bus voltage range to 32V. Else, sets it to 16V.
mgottscho 0:8d34cc2ff388 44 */
mgottscho 0:8d34cc2ff388 45 void setBusVoltageRange32V(bool enable);
mgottscho 0:8d34cc2ff388 46
mgottscho 0:8d34cc2ff388 47 /**
mgottscho 0:8d34cc2ff388 48 * @returns true if the bus voltage range is set to 32V. if false, bus voltage range is 16V.
mgottscho 0:8d34cc2ff388 49 */
mgottscho 0:8d34cc2ff388 50 bool isBusVoltageRange32V();
mgottscho 0:8d34cc2ff388 51
mgottscho 0:8d34cc2ff388 52 /**
mgottscho 0:8d34cc2ff388 53 * Sets the PGA amplifier gain for shunt voltage measurement.
mgottscho 0:8d34cc2ff388 54 * @param gain the gain level to set. Allowed values: 1, 2, 4, 8. Default is 8. Any other values will have no effect.
mgottscho 0:8d34cc2ff388 55 */
mgottscho 0:8d34cc2ff388 56 void setShuntAmpGain(unsigned int gain);
mgottscho 0:8d34cc2ff388 57
mgottscho 0:8d34cc2ff388 58 /**
mgottscho 0:8d34cc2ff388 59 * Gets the PGA amplifier gain for shunt voltage measurement.
mgottscho 0:8d34cc2ff388 60 * @returns the gain level
mgottscho 0:8d34cc2ff388 61 */
mgottscho 0:8d34cc2ff388 62 unsigned int getShuntAmpGain();
mgottscho 0:8d34cc2ff388 63
mgottscho 0:8d34cc2ff388 64 /**
mgottscho 0:8d34cc2ff388 65 * Sets the resolution and sample averaging of the bus or shunt ADC.
mgottscho 0:8d34cc2ff388 66 * If resolution is set, sample averaging is disabled.
mgottscho 0:8d34cc2ff388 67 * If sample averaging is set, resolution setting is disabled.
mgottscho 0:8d34cc2ff388 68 * @param shunt if true, sets for shunt ADC. if false, sets for bus ADC.
mgottscho 0:8d34cc2ff388 69 * @param resolution if true, sets ADC resolution using the third parameter. if false, sets the ADC sample averaging
mgottscho 0:8d34cc2ff388 70 * using the third parameter.
mgottscho 0:8d34cc2ff388 71 * @param value if resolution is true, then this is the resolution in bits to use. Allowed values:
mgottscho 0:8d34cc2ff388 72 * 9, 10, 11, or 12 bits.
mgottscho 0:8d34cc2ff388 73 * If resolution is false, then this is the number of samples to average in each data sample. Allowed values:
mgottscho 0:8d34cc2ff388 74 * 2, 4, 8, 16, 32, 64, or 128 samples.
mgottscho 0:8d34cc2ff388 75 * Any other values will cause this method to have no effect. Default is 12-bit resolution mode.
mgottscho 0:8d34cc2ff388 76 */
mgottscho 0:8d34cc2ff388 77 void setADCResolutionAndAveraging(bool shunt, bool resolution, unsigned int value);
mgottscho 0:8d34cc2ff388 78
mgottscho 0:8d34cc2ff388 79 /**
mgottscho 0:8d34cc2ff388 80 * Gets the resolution or number of samples in an average of the bus or shunt ADC.
mgottscho 0:8d34cc2ff388 81 * @param shunt if true, gets for shunt ADC. if false, gets for bus ADC.
mgottscho 0:8d34cc2ff388 82 * @param resolution sets this to true if the return value represents resolution in bits. else, sets it to false if return
mgottscho 0:8d34cc2ff388 83 * value represents sample averaging in # samples.
mgottscho 0:8d34cc2ff388 84 */
mgottscho 0:8d34cc2ff388 85 unsigned int getADCResolutionAndAveraging(bool shunt, bool &resolution);
mgottscho 0:8d34cc2ff388 86
mgottscho 0:8d34cc2ff388 87 /**
mgottscho 0:8d34cc2ff388 88 * Sets the operating mode of the device. If all parameters are false, the device powers down.
mgottscho 0:8d34cc2ff388 89 * If shuntVoltage and busVoltage are false but continuous is true, the device remains on but
mgottscho 0:8d34cc2ff388 90 * the ADC is powered down.
mgottscho 0:8d34cc2ff388 91 * @param shuntVoltage if true, samples the shunt voltage.
mgottscho 0:8d34cc2ff388 92 * @param busVoltage if true, samples the bus voltage.
mgottscho 0:8d34cc2ff388 93 * @param continuous if true, samples continuously, otherwise only on triggered reads.
mgottscho 0:8d34cc2ff388 94 */
mgottscho 0:8d34cc2ff388 95 void setMode(bool shuntVoltage, bool busVoltage, bool continuous);
mgottscho 0:8d34cc2ff388 96
mgottscho 0:8d34cc2ff388 97 /**
mgottscho 0:8d34cc2ff388 98 * Gets the operating mode of the device. If all parameters are false, the device is powered down.
mgottscho 0:8d34cc2ff388 99 * If shuntVoltage and busVoltage are false but continuous is true, the device is on but
mgottscho 0:8d34cc2ff388 100 * the ADC is powered down.
mgottscho 0:8d34cc2ff388 101 * @param shuntVoltage sets true if shunt voltage is being measured.
mgottscho 0:8d34cc2ff388 102 * @param busVoltage sets true if bus voltage is being measured.
mgottscho 0:8d34cc2ff388 103 * @param continuous sets true if continuous readings are being done.
mgottscho 0:8d34cc2ff388 104 */
mgottscho 0:8d34cc2ff388 105 void getMode(bool &shuntVoltage, bool &busVoltage, bool &continuous);
mgottscho 0:8d34cc2ff388 106
mgottscho 0:8d34cc2ff388 107 /**
mgottscho 0:8d34cc2ff388 108 * Gets the shunt voltage across the current sensing resistor.
mgottscho 0:8d34cc2ff388 109 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
mgottscho 0:8d34cc2ff388 110 * The latter is preferred if this object is set up to sample using interrupts.
mgottscho 0:8d34cc2ff388 111 * @returns 16-bit integer representing the shunt voltage.
mgottscho 0:8d34cc2ff388 112 */
mgottscho 0:8d34cc2ff388 113 int16_t getShuntVoltage(bool sampleNow);
mgottscho 0:8d34cc2ff388 114
mgottscho 0:8d34cc2ff388 115 /**
mgottscho 0:8d34cc2ff388 116 * Gets the shunt voltage across the current sensing resistor.
mgottscho 0:8d34cc2ff388 117 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
mgottscho 0:8d34cc2ff388 118 * The latter is preferred if this object is set up to sample using interrupts.
mgottscho 0:8d34cc2ff388 119 * @returns the shunt voltage in Volts
mgottscho 0:8d34cc2ff388 120 */
mgottscho 0:8d34cc2ff388 121 float getShuntVoltageFloat(bool sampleNow);
mgottscho 0:8d34cc2ff388 122
mgottscho 0:8d34cc2ff388 123
mgottscho 0:8d34cc2ff388 124 /**
mgottscho 0:8d34cc2ff388 125 * Gets the bus voltage at the negative terminal of the current sensing resistor (V-).
mgottscho 0:8d34cc2ff388 126 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
mgottscho 0:8d34cc2ff388 127 * The latter is preferred if this object is set up to sample using interrupts.
mgottscho 0:8d34cc2ff388 128 * @returns 16-bit integer representing the bus voltage.
mgottscho 0:8d34cc2ff388 129 */
mgottscho 0:8d34cc2ff388 130 int16_t getBusVoltage(bool sampleNow);
mgottscho 0:8d34cc2ff388 131
mgottscho 0:8d34cc2ff388 132 /**
mgottscho 0:8d34cc2ff388 133 * Gets the bus voltage across the current sensing resistor.
mgottscho 0:8d34cc2ff388 134 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
mgottscho 0:8d34cc2ff388 135 * The latter is preferred if this object is set up to sample using interrupts.
mgottscho 0:8d34cc2ff388 136 * @returns the bus voltage in Volts
mgottscho 0:8d34cc2ff388 137 */
mgottscho 0:8d34cc2ff388 138 float getBusVoltageFloat(bool sampleNow);
mgottscho 0:8d34cc2ff388 139
mgottscho 0:8d34cc2ff388 140 /**
mgottscho 0:8d34cc2ff388 141 * Gets the power consumed by the load. Note that calling this method with sampleNow == true
mgottscho 0:8d34cc2ff388 142 * will also implicitly sample current as well, which can be retrieved with a call to
mgottscho 0:8d34cc2ff388 143 * getCurrent(false) after calling getPower(true).
mgottscho 0:8d34cc2ff388 144 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
mgottscho 0:8d34cc2ff388 145 * The latter is preferred if this object is set up to sample using interrupts.
mgottscho 0:8d34cc2ff388 146 * @returns 16-bit integer representing power consumption of the load.
mgottscho 0:8d34cc2ff388 147 */
mgottscho 0:8d34cc2ff388 148 int16_t getPower(bool sampleNow);
mgottscho 0:8d34cc2ff388 149
mgottscho 0:8d34cc2ff388 150 /**
mgottscho 0:8d34cc2ff388 151 * Gets the power consumed by the load. Note that calling this method with sampleNow == true
mgottscho 0:8d34cc2ff388 152 * will also implicitly sample current as well, which can be retrieved with a call to
mgottscho 0:8d34cc2ff388 153 * getCurrentFloat(false) after calling getPowerFloat(true).
mgottscho 0:8d34cc2ff388 154 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
mgottscho 0:8d34cc2ff388 155 * The latter is preferred if this object is set up to sample using interrupts.
mgottscho 0:8d34cc2ff388 156 * @returns the load power in Watts
mgottscho 0:8d34cc2ff388 157 */
mgottscho 0:8d34cc2ff388 158 float getPowerFloat(bool sampleNow);
mgottscho 0:8d34cc2ff388 159
mgottscho 0:8d34cc2ff388 160 /**
mgottscho 0:8d34cc2ff388 161 * Gets the current sunk by the load.
mgottscho 0:8d34cc2ff388 162 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
mgottscho 0:8d34cc2ff388 163 * The latter is preferred if this object is set up to sample using interrupts.
mgottscho 0:8d34cc2ff388 164 * @returns 16-bit integer representing current sunk by the load.
mgottscho 0:8d34cc2ff388 165 */
mgottscho 0:8d34cc2ff388 166 int16_t getCurrent(bool sampleNow);
mgottscho 0:8d34cc2ff388 167
mgottscho 0:8d34cc2ff388 168 /**
mgottscho 0:8d34cc2ff388 169 * Gets the current delivered to the load
mgottscho 0:8d34cc2ff388 170 * @param sampleNow if true, queries the device for the sample and returns it. if false, gets the last queried value.
mgottscho 0:8d34cc2ff388 171 * The latter is preferred if this object is set up to sample using interrupts.
mgottscho 0:8d34cc2ff388 172 * @returns the load current in Amperes
mgottscho 0:8d34cc2ff388 173 */
mgottscho 0:8d34cc2ff388 174 float getCurrentFloat(bool sampleNow);
mgottscho 0:8d34cc2ff388 175
mgottscho 0:8d34cc2ff388 176 /**
mgottscho 0:8d34cc2ff388 177 * Gets the calibration configuration setting for current range.
mgottscho 0:8d34cc2ff388 178 * @returns 16-bit unsigned integer representing calibration register value
mgottscho 0:8d34cc2ff388 179 */
mgottscho 0:8d34cc2ff388 180 uint16_t getCalibration();
mgottscho 0:8d34cc2ff388 181
mgottscho 0:8d34cc2ff388 182 private:
mgottscho 0:8d34cc2ff388 183 /**
mgottscho 0:8d34cc2ff388 184 * Interrupt service routine for fetching power data from the device.
mgottscho 0:8d34cc2ff388 185 */
mgottscho 0:8d34cc2ff388 186 virtual void __sample_data_ISR();
mgottscho 0:8d34cc2ff388 187
mgottscho 0:8d34cc2ff388 188 /**
mgottscho 0:8d34cc2ff388 189 * Sets the calibration register of the device so that current and power measurements can be made. NOTE: This is a custom method for my project.
mgottscho 0:8d34cc2ff388 190 */
mgottscho 0:8d34cc2ff388 191 void __calibrate();
mgottscho 0:8d34cc2ff388 192
mgottscho 0:8d34cc2ff388 193 /////////////////////// VARIABLES //////////////////////////
mgottscho 0:8d34cc2ff388 194 bool __bus_voltage_range_32V;
mgottscho 0:8d34cc2ff388 195 unsigned int __shunt_amp_gain;
mgottscho 0:8d34cc2ff388 196 unsigned int __shunt_resolution;
mgottscho 0:8d34cc2ff388 197 unsigned int __shunt_num_samples_in_average;
mgottscho 0:8d34cc2ff388 198 unsigned int __bus_resolution;
mgottscho 0:8d34cc2ff388 199 unsigned int __bus_num_samples_in_average;
mgottscho 0:8d34cc2ff388 200 bool __active;
mgottscho 0:8d34cc2ff388 201 bool __measure_shunt_voltage;
mgottscho 0:8d34cc2ff388 202 bool __measure_bus_voltage;
mgottscho 0:8d34cc2ff388 203 bool __continuous_measurements;
mgottscho 0:8d34cc2ff388 204
mgottscho 0:8d34cc2ff388 205 volatile int16_t __shunt_voltage;
mgottscho 0:8d34cc2ff388 206 volatile int16_t __bus_voltage;
mgottscho 0:8d34cc2ff388 207 volatile int16_t __power;
mgottscho 0:8d34cc2ff388 208 volatile int16_t __shunt_current;
mgottscho 0:8d34cc2ff388 209 volatile uint16_t __calibration;
mgottscho 0:8d34cc2ff388 210
mgottscho 0:8d34cc2ff388 211 float __current_div; //Amps/level
mgottscho 0:8d34cc2ff388 212 float __power_div; //Watts/level
mgottscho 0:8d34cc2ff388 213
mgottscho 0:8d34cc2ff388 214
mgottscho 0:8d34cc2ff388 215
mgottscho 0:8d34cc2ff388 216 /////////////////////// CONSTANTS //////////////////////////
mgottscho 0:8d34cc2ff388 217
mgottscho 0:8d34cc2ff388 218 //Device register addresses
mgottscho 0:8d34cc2ff388 219 const static uint8_t CONFIG = 0x00;
mgottscho 0:8d34cc2ff388 220 const static uint8_t SHUNT_VOLTAGE = 0x01;
mgottscho 0:8d34cc2ff388 221 const static uint8_t BUS_VOLTAGE = 0x02;
mgottscho 0:8d34cc2ff388 222 const static uint8_t POWER = 0x03;
mgottscho 0:8d34cc2ff388 223 const static uint8_t CURRENT = 0x04;
mgottscho 0:8d34cc2ff388 224 const static uint8_t CALIBRATION = 0x05;
mgottscho 0:8d34cc2ff388 225
mgottscho 0:8d34cc2ff388 226 //Register masks
mgottscho 0:8d34cc2ff388 227 const static uint16_t CONFIG_RST_MASK = 0x8000; //b1000 0000 0000 0000
mgottscho 0:8d34cc2ff388 228 const static uint16_t CONFIG_BRNG_MASK = 0x2000; //b0010 0000 0000 0000
mgottscho 0:8d34cc2ff388 229 const static uint16_t CONFIG_PG_MASK = 0x1800; //b0001 1000 0000 0000
mgottscho 0:8d34cc2ff388 230 const static uint16_t CONFIG_BADC_MASK = 0x0780; //b0000 0111 1000 0000
mgottscho 0:8d34cc2ff388 231 const static uint16_t CONFIG_SADC_MASK = 0x0078; //b0000 0000 0111 1000
mgottscho 0:8d34cc2ff388 232 const static uint16_t CONFIG_MODE_MASK = 0x0007; //b0000 0000 0000 0111
mgottscho 0:8d34cc2ff388 233
mgottscho 0:8d34cc2ff388 234 //No masks needed for SHUNT_VOLTAGE register
mgottscho 0:8d34cc2ff388 235
mgottscho 0:8d34cc2ff388 236 const static uint16_t BUS_VOLTAGE_BD_MASK = 0xFFF8; //b1111 1111 1111 1000
mgottscho 0:8d34cc2ff388 237 const static uint16_t BUS_VOLTAGE_CNVR_MASK = 0x0002; //b0000 0000 0000 0010
mgottscho 0:8d34cc2ff388 238 const static uint16_t BUS_VOLTAGE_OVF_MASK = 0x0001; //b0000 0000 0000 0001
mgottscho 0:8d34cc2ff388 239
mgottscho 0:8d34cc2ff388 240 //No masks needed for POWER register
mgottscho 0:8d34cc2ff388 241
mgottscho 0:8d34cc2ff388 242 const static uint16_t CURRENT_CSIGN_MASK = 0x8000; //b1000 0000 0000 0000
mgottscho 0:8d34cc2ff388 243 const static uint16_t CURRENT_CD_MASK = 0x7FFF; //b0111 1111 1111 1111
mgottscho 0:8d34cc2ff388 244
mgottscho 0:8d34cc2ff388 245 const static uint16_t CALIBRATION_FS_MASK = 0xFFFE; //b1111 1111 1111 1110
mgottscho 0:8d34cc2ff388 246
mgottscho 0:8d34cc2ff388 247 const static float SHUNT_VOLTAGE_DIV = 0.00001; //10uV / level
mgottscho 0:8d34cc2ff388 248 const static float BUS_VOLTAGE_DIV = 0.004; //4mV / level
mgottscho 0:8d34cc2ff388 249 };
mgottscho 0:8d34cc2ff388 250
mgottscho 0:8d34cc2ff388 251 #endif