ARM Mbed library for TI INA226. High-Side or Low-Side Measurement, Bi-Directional Current and Power Monitor with I2C Compatible Interface.

Committer:
Branilson Luiz
Date:
Mon Sep 09 02:34:29 2019 -0300
Revision:
0:ed5e54b4383d
First commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Branilson Luiz 0:ed5e54b4383d 1 /*
Branilson Luiz 0:ed5e54b4383d 2 * Copyright (c) 2019 Branilson Luiz
Branilson Luiz 0:ed5e54b4383d 3 * INA226.hpp - Header file for the INA226 Bi-directional Current/Power Monitor
Branilson Luiz 0:ed5e54b4383d 4 * Mbed Library.
Branilson Luiz 0:ed5e54b4383d 5 * Version: 1.0.0
Branilson Luiz 0:ed5e54b4383d 6 *
Branilson Luiz 0:ed5e54b4383d 7 * branilson (at) gmail dot com
Branilson Luiz 0:ed5e54b4383d 8 * Github: https://github.com/branilson/ina226_mbed_library
Branilson Luiz 0:ed5e54b4383d 9 *
Branilson Luiz 0:ed5e54b4383d 10 * This program is free software: you can redistribute it and/or modify it un-
Branilson Luiz 0:ed5e54b4383d 11 * der the terms of the version 3 GNU General Public License as published by
Branilson Luiz 0:ed5e54b4383d 12 * the Free Software Foundation.
Branilson Luiz 0:ed5e54b4383d 13 * This program is distributed in the hope that it will be useful, but WITHOUT
Branilson Luiz 0:ed5e54b4383d 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT-
Branilson Luiz 0:ed5e54b4383d 15 * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Branilson Luiz 0:ed5e54b4383d 16 * details.
Branilson Luiz 0:ed5e54b4383d 17 * You should have received a copy of the GNU General Public License along with
Branilson Luiz 0:ed5e54b4383d 18 * this program. If not, see <http://www.gnu.org/licenses/>.
Branilson Luiz 0:ed5e54b4383d 19 */
Branilson Luiz 0:ed5e54b4383d 20
Branilson Luiz 0:ed5e54b4383d 21 #ifndef INA226_HPP_
Branilson Luiz 0:ed5e54b4383d 22 #define INA226_HPP_
Branilson Luiz 0:ed5e54b4383d 23
Branilson Luiz 0:ed5e54b4383d 24 #include "mbed.h"
Branilson Luiz 0:ed5e54b4383d 25
Branilson Luiz 0:ed5e54b4383d 26 #define BIT_SOL (0x8000) // Shunt Voltage Over-Voltage
Branilson Luiz 0:ed5e54b4383d 27 #define BIT_SUL (0x4000) // Shunt Voltage Under-Voltage
Branilson Luiz 0:ed5e54b4383d 28 #define BIT_BOL (0x2000) // Bus Voltage Over-Voltage
Branilson Luiz 0:ed5e54b4383d 29 #define BIT_BUL (0x1000) // Bus Voltage Under-Voltage
Branilson Luiz 0:ed5e54b4383d 30 #define BIT_POL (0x0800) // Power Over-Limit
Branilson Luiz 0:ed5e54b4383d 31 #define BIT_CNVR (0x0400) // Conversion Ready
Branilson Luiz 0:ed5e54b4383d 32 #define BIT_AFF (0x0010) // Alert Function Flag
Branilson Luiz 0:ed5e54b4383d 33 #define BIT_CVRF (0x0008) // Conversion Ready Flag
Branilson Luiz 0:ed5e54b4383d 34 #define BIT_OVF (0x0004) // Math Overflow Flag
Branilson Luiz 0:ed5e54b4383d 35 #define BIT_APOL (0x0002) // Alert Polarity bit; sets the Alert pin polaritY
Branilson Luiz 0:ed5e54b4383d 36 #define BIT_LEN (0x0001) // Alert Latch Enable; configures the latching feature of the Alert pin and Alert Flag bits
Branilson Luiz 0:ed5e54b4383d 37
Branilson Luiz 0:ed5e54b4383d 38 typedef enum {
Branilson Luiz 0:ed5e54b4383d 39 AVERAGES_1 = 0b000,
Branilson Luiz 0:ed5e54b4383d 40 AVERAGES_4 = 0b001,
Branilson Luiz 0:ed5e54b4383d 41 AVERAGES_16 = 0b010,
Branilson Luiz 0:ed5e54b4383d 42 AVERAGES_64 = 0b011,
Branilson Luiz 0:ed5e54b4383d 43 AVERAGES_128 = 0b100,
Branilson Luiz 0:ed5e54b4383d 44 AVERAGES_256 = 0b101,
Branilson Luiz 0:ed5e54b4383d 45 AVERAGES_512 = 0b110,
Branilson Luiz 0:ed5e54b4383d 46 AVERAGES_1024 = 0b111
Branilson Luiz 0:ed5e54b4383d 47 } ina226_averages_t;
Branilson Luiz 0:ed5e54b4383d 48
Branilson Luiz 0:ed5e54b4383d 49 typedef enum {
Branilson Luiz 0:ed5e54b4383d 50 BUS_CONV_TIME_140US = 0b000,
Branilson Luiz 0:ed5e54b4383d 51 BUS_CONV_TIME_204US = 0b001,
Branilson Luiz 0:ed5e54b4383d 52 BUS_CONV_TIME_332US = 0b010,
Branilson Luiz 0:ed5e54b4383d 53 BUS_CONV_TIME_588US = 0b011,
Branilson Luiz 0:ed5e54b4383d 54 BUS_CONV_TIME_1100US = 0b100,
Branilson Luiz 0:ed5e54b4383d 55 BUS_CONV_TIME_2116US = 0b101,
Branilson Luiz 0:ed5e54b4383d 56 BUS_CONV_TIME_4156US = 0b110,
Branilson Luiz 0:ed5e54b4383d 57 BUS_CONV_TIME_8244US = 0b111
Branilson Luiz 0:ed5e54b4383d 58 } ina226_busConvTime_t;
Branilson Luiz 0:ed5e54b4383d 59
Branilson Luiz 0:ed5e54b4383d 60 typedef enum {
Branilson Luiz 0:ed5e54b4383d 61 SHUNT_CONV_TIME_140US = 0b000,
Branilson Luiz 0:ed5e54b4383d 62 SHUNT_CONV_TIME_204US = 0b001,
Branilson Luiz 0:ed5e54b4383d 63 SHUNT_CONV_TIME_332US = 0b010,
Branilson Luiz 0:ed5e54b4383d 64 SHUNT_CONV_TIME_588US = 0b011,
Branilson Luiz 0:ed5e54b4383d 65 SHUNT_CONV_TIME_1100US = 0b100,
Branilson Luiz 0:ed5e54b4383d 66 SHUNT_CONV_TIME_2116US = 0b101,
Branilson Luiz 0:ed5e54b4383d 67 SHUNT_CONV_TIME_4156US = 0b110,
Branilson Luiz 0:ed5e54b4383d 68 SHUNT_CONV_TIME_8244US = 0b111
Branilson Luiz 0:ed5e54b4383d 69 } ina226_shuntConvTime_t;
Branilson Luiz 0:ed5e54b4383d 70
Branilson Luiz 0:ed5e54b4383d 71 typedef enum {
Branilson Luiz 0:ed5e54b4383d 72 MODE_POWER_DOWN = 0b000,
Branilson Luiz 0:ed5e54b4383d 73 MODE_SHUNT_TRIG = 0b001,
Branilson Luiz 0:ed5e54b4383d 74 MODE_BUS_TRIG = 0b010,
Branilson Luiz 0:ed5e54b4383d 75 MODE_SHUNT_BUS_TRIG = 0b011,
Branilson Luiz 0:ed5e54b4383d 76 MODE_ADC_OFF = 0b100,
Branilson Luiz 0:ed5e54b4383d 77 MODE_SHUNT_CONT = 0b101,
Branilson Luiz 0:ed5e54b4383d 78 MODE_BUS_CONT = 0b110,
Branilson Luiz 0:ed5e54b4383d 79 MODE_SHUNT_BUS_CONT = 0b111,
Branilson Luiz 0:ed5e54b4383d 80 } ina226_mode_t;
Branilson Luiz 0:ed5e54b4383d 81
Branilson Luiz 0:ed5e54b4383d 82 class ina226 {
Branilson Luiz 0:ed5e54b4383d 83 public:
Branilson Luiz 0:ed5e54b4383d 84 ina226(I2C& i2c, uint8_t address, int frequency);
Branilson Luiz 0:ed5e54b4383d 85 int setConfig(ina226_averages_t avg = AVERAGES_64,
Branilson Luiz 0:ed5e54b4383d 86 ina226_busConvTime_t busConvTime = BUS_CONV_TIME_1100US,
Branilson Luiz 0:ed5e54b4383d 87 ina226_shuntConvTime_t shuntConvTime = SHUNT_CONV_TIME_1100US,
Branilson Luiz 0:ed5e54b4383d 88 ina226_mode_t mode = MODE_SHUNT_BUS_CONT);
Branilson Luiz 0:ed5e54b4383d 89 int setCalibration(float rShuntValue = 0.01, float iMaxExpected = 8.191);
Branilson Luiz 0:ed5e54b4383d 90 // int setAlert(uint16_t val);
Branilson Luiz 0:ed5e54b4383d 91 float readShuntVoltage(void);
Branilson Luiz 0:ed5e54b4383d 92 float readCurrent(void);
Branilson Luiz 0:ed5e54b4383d 93 float readPower(void);
Branilson Luiz 0:ed5e54b4383d 94 float readBusVoltage(void);
Branilson Luiz 0:ed5e54b4383d 95 int readManufacturerID(void);
Branilson Luiz 0:ed5e54b4383d 96 int readDieID(void);
Branilson Luiz 0:ed5e54b4383d 97 int readCalibration(void);
Branilson Luiz 0:ed5e54b4383d 98 ina226_averages_t getAverages(void);
Branilson Luiz 0:ed5e54b4383d 99 ina226_busConvTime_t getBusConversionTime(void);
Branilson Luiz 0:ed5e54b4383d 100 ina226_shuntConvTime_t getShuntConversionTime(void);
Branilson Luiz 0:ed5e54b4383d 101 ina226_mode_t getMode(void);
Branilson Luiz 0:ed5e54b4383d 102 void enableShuntOverVoltageAlert(void);
Branilson Luiz 0:ed5e54b4383d 103 void enableShuntUnderVoltageAlert(void);
Branilson Luiz 0:ed5e54b4383d 104 void enableBusOvertVoltageAlert(void);
Branilson Luiz 0:ed5e54b4383d 105 void enableBusUnderVoltageAlert(void);
Branilson Luiz 0:ed5e54b4383d 106 void enableOverPowerAlert(void);
Branilson Luiz 0:ed5e54b4383d 107 void enableConversionReadyAlert(void);
Branilson Luiz 0:ed5e54b4383d 108 void setOverCurrentLimit(float current);
Branilson Luiz 0:ed5e54b4383d 109 void setBusVoltageLimit(float voltage);
Branilson Luiz 0:ed5e54b4383d 110 void setShuntVoltageLimit(float voltage);
Branilson Luiz 0:ed5e54b4383d 111 void setPowerLimit(float watts);
Branilson Luiz 0:ed5e54b4383d 112 void setAlertInvertedPolarity(bool inverted);
Branilson Luiz 0:ed5e54b4383d 113 void setAlertLatch(bool latch);
Branilson Luiz 0:ed5e54b4383d 114 bool isMathOverflow(void);
Branilson Luiz 0:ed5e54b4383d 115 bool isAlert(void);
Branilson Luiz 0:ed5e54b4383d 116
Branilson Luiz 0:ed5e54b4383d 117 private:
Branilson Luiz 0:ed5e54b4383d 118 I2C& _i2c;
Branilson Luiz 0:ed5e54b4383d 119 const uint8_t i2c_addr;
Branilson Luiz 0:ed5e54b4383d 120 const int i2c_freq;
Branilson Luiz 0:ed5e54b4383d 121 float currentLSB, powerLSB, rShunt;
Branilson Luiz 0:ed5e54b4383d 122 const float vShuntMax = 0.08192f;
Branilson Luiz 0:ed5e54b4383d 123 const uint8_t REG_CONFIG = 0x00;
Branilson Luiz 0:ed5e54b4383d 124 const uint8_t REG_SHUNT_VOLTAGE = 0x01;
Branilson Luiz 0:ed5e54b4383d 125 const uint8_t REG_BUS_VOLTAGE = 0x02;
Branilson Luiz 0:ed5e54b4383d 126 const uint8_t REG_POWER = 0x03;
Branilson Luiz 0:ed5e54b4383d 127 const uint8_t REG_CURRENT = 0x04;
Branilson Luiz 0:ed5e54b4383d 128 const uint8_t REG_CALIBRATION = 0x05;
Branilson Luiz 0:ed5e54b4383d 129 const uint8_t REG_MASK = 0x06;
Branilson Luiz 0:ed5e54b4383d 130 const uint8_t REG_ALERT = 0x07;
Branilson Luiz 0:ed5e54b4383d 131 const uint8_t REG_MANUFACTURER_ID = 0xFE;
Branilson Luiz 0:ed5e54b4383d 132 const uint8_t REG_DIE_ID = 0xFF;
Branilson Luiz 0:ed5e54b4383d 133 int writeRegister16(uint8_t reg, uint16_t val);
Branilson Luiz 0:ed5e54b4383d 134 int16_t readRegister16(uint8_t reg);
Branilson Luiz 0:ed5e54b4383d 135 void setMask(uint16_t mask);
Branilson Luiz 0:ed5e54b4383d 136 uint16_t getMask(void);
Branilson Luiz 0:ed5e54b4383d 137 };
Branilson Luiz 0:ed5e54b4383d 138
Branilson Luiz 0:ed5e54b4383d 139 #endif // INA226_HPP_