Compare commits

..

No commits in common. "main" and "fixabug" have entirely different histories.

11 changed files with 419 additions and 1063 deletions

View file

@ -1,13 +0,0 @@
Language: Cpp
BasedOnStyle: Google
IndentWidth: 2
ColumnLimit: 80
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BinPackArguments: true
BinPackParameters: true
BreakBeforeBraces: Attach
DerivePointerAlignment: false
PointerAlignment: Left
SpacesBeforeTrailingComments: 1

View file

@ -19,12 +19,12 @@ jobs:
- name: pre-install
run: bash ci/actions_install.sh
- name: clang
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r .
- name: test platforms
run: python3 ci/build_platform.py main_platforms
- name: clang
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r .
- name: doxygen
env:
GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }}

4
.gitignore vendored
View file

@ -2,7 +2,3 @@ html/
Doxyfile
.vscode/
.pio/
**/.claude/settings.local.json
*.bak
*.txt
*.pdf

View file

@ -1,39 +1,40 @@
/*!
* @file Adafruit_INA228.cpp
*
* @section ina228_intro Introduction
* @mainpage Adafruit INA228 I2C Current and Power sensor
*
* @section intro_sec Introduction
*
* I2C Driver for the INA228 I2C Current and Power sensor
*
* This is a library for the Adafruit INA228 breakout:
* http://www.adafruit.com/products/5832
* http://www.adafruit.com/products/4226
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing products from
* Adafruit!
*
* @section ina228_dependencies Dependencies
* @section dependencies Dependencies
*
* This library depends on the Adafruit BusIO library
*
* @section ina228_author Author
* @section author Author
*
* Bryan Siepert for Adafruit Industries
*
* @section ina228_license License
* @section license License
*
* BSD (see license.txt)
*
* @section ina228_history HISTORY
* @section HISTORY
*
* v1.0 - First release
*/
#include "Adafruit_INA228.h"
#include "Arduino.h"
#include <Wire.h>
#include "Arduino.h"
#include "Adafruit_INA228.h"
/*!
* @brief Instantiates a new INA228 class
@ -46,51 +47,145 @@ Adafruit_INA228::Adafruit_INA228(void) {}
* The I2C address to be used.
* @param theWire
* The Wire object to be used for I2C connections.
* @param skipReset
* When set to true, will omit resetting all INA228 registers to
* their default values. Default: false.
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_INA228::begin(uint8_t i2c_address, TwoWire* theWire,
bool skipReset) {
if (!Adafruit_INA2xx::begin(i2c_address, theWire, skipReset)) {
bool Adafruit_INA228::begin(uint8_t i2c_address, TwoWire *theWire) {
i2c_dev = new Adafruit_I2CDevice(i2c_address, theWire);
if (!i2c_dev->begin()) {
return false;
}
Adafruit_I2CRegister *device_register =
new Adafruit_I2CRegister(i2c_dev, INA228_REG_DVC_UID, 2, MSBFIRST);
Adafruit_I2CRegister *mfg_register =
new Adafruit_I2CRegister(i2c_dev, INA228_REG_MFG_UID, 2, MSBFIRST);
Adafruit_I2CRegisterBits *device_id =
new Adafruit_I2CRegisterBits(device_register, 12, 4);
// make sure we're talking to the right chip
if (_device_id != INA228_DEVICE_ID) {
if ((mfg_register->read() != 0x5449) || (device_id->read() != 0x228)) {
return false;
}
Config = new Adafruit_I2CRegister(i2c_dev, INA228_REG_CONFIG, 2, MSBFIRST);
ADC_Config = new Adafruit_I2CRegister(i2c_dev, INA228_REG_ADCCFG, 2, MSBFIRST);
Diag_Alert = new Adafruit_I2CRegister(i2c_dev, INA228_REG_DIAGALRT, 2, MSBFIRST);
reset();
delay(2); // delay 2ms to give time for first measurement to finish
return true;
}
/**************************************************************************/
/*!
@brief Resets the hardware specific to INA228
@brief Resets the harware. All registers are set to default values,
the same as a power-on reset.
*/
/**************************************************************************/
void Adafruit_INA228::reset(void) {
// Perform base class reset
Adafruit_INA2xx::reset();
// Any additional INA228 specific reset actions would go here
Adafruit_I2CRegisterBits reset = Adafruit_I2CRegisterBits(Config, 1, 15);
reset.write(1);
Adafruit_I2CRegisterBits alert_conv =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 14);
alert_conv.write(1);
setMode(INA228_MODE_CONTINUOUS);
}
/**************************************************************************/
/*!
@brief Sets the shunt calibration by resistor
@param shunt_res Resistance of the shunt in ohms (floating point)
*/
/**************************************************************************/
void Adafruit_INA228::setShunt(float shunt_res, float max_current) {
_current_lsb = max_current / (float)(1UL << 19);
//Serial.print("current lsb is (uA) ");
//Serial.println(_current_lsb * 1000000, 8);
bool adcrange = (Config->read() >> 4) & 1;
float scale = 1;
if (adcrange) {
scale = 4;
}
float shunt_cal = 13107.2 * 1000000.0 * shunt_res * _current_lsb * scale;
//Serial.print("shunt cal is ");
//Serial.println(shunt_cal);
Adafruit_I2CRegister shunt =
Adafruit_I2CRegister(i2c_dev, INA228_REG_SHUNTCAL, 2, MSBFIRST);
shunt.write(shunt_cal);
}
/**************************************************************************/
/*!
@brief Updates the shunt calibration value to the INA228 register.
@brief Reads the die temperature
@return The current die temp in deg C
*/
/**************************************************************************/
void Adafruit_INA228::_updateShuntCalRegister() {
float scale = 1;
if (getADCRange()) {
scale = 4;
}
float shunt_cal = 13107.2 * 1000000.0 * _shunt_res * _current_lsb * scale;
float Adafruit_INA228::readDieTemp(void) {
Adafruit_I2CRegister temp =
Adafruit_I2CRegister(i2c_dev, INA228_REG_DIETEMP, 2, MSBFIRST);
return (float)temp.read() * 7.8125 / 1000.0;
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Current register.
@return The current current measurement in mA
*/
/**************************************************************************/
float Adafruit_INA228::readCurrent(void) {
Adafruit_I2CRegister current =
Adafruit_I2CRegister(i2c_dev, INA228_REG_CURRENT, 3, MSBFIRST);
int32_t i = current.read();
if (i & 0x800000) i |= 0xFF000000;
return (float)i / 16.0 * _current_lsb * 1000.0;
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Bus Voltage register.
@return The current bus voltage measurement in mV
*/
/**************************************************************************/
float Adafruit_INA228::readBusVoltage(void) {
Adafruit_I2CRegister shunt =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_SHUNTCAL, 2, MSBFIRST);
shunt.write(shunt_cal);
Adafruit_I2CRegister bus_voltage =
Adafruit_I2CRegister(i2c_dev, INA228_REG_VBUS, 3, MSBFIRST);
return (float)((uint32_t)bus_voltage.read() >> 4) * 195.3125 / 1000.0;
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Shunt Voltage register.
@return The current shunt voltage measurement in mV
*/
/**************************************************************************/
float Adafruit_INA228::readShuntVoltage(void) {
bool adcrange = (Config->read() >> 4) & 1;
float scale = 312.5;
if (adcrange) {
scale = 78.125;
}
Adafruit_I2CRegister shunt_voltage =
Adafruit_I2CRegister(i2c_dev, INA228_REG_VSHUNT, 3, MSBFIRST);
int32_t v = shunt_voltage.read();
if (v & 0x800000) v |= 0xFF000000;
return v / 16.0 * scale / 1000000.0;
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Power register.
@return The current Power calculation in mW
*/
/**************************************************************************/
float Adafruit_INA228::readPower(void) {
Adafruit_I2CRegister power =
Adafruit_I2CRegister(i2c_dev, INA228_REG_POWER, 3, MSBFIRST);
return (float)power.read() * 3.2 * _current_lsb * 1000;
}
/**************************************************************************/
@ -101,11 +196,11 @@ void Adafruit_INA228::_updateShuntCalRegister() {
/**************************************************************************/
float Adafruit_INA228::readEnergy(void) {
Adafruit_I2CRegister energy =
Adafruit_I2CRegister(i2c_dev, INA228_REG_ENERGY, 5, MSBFIRST);
Adafruit_I2CRegister(i2c_dev, INA228_REG_ENERGY, 5, MSBFIRST);
uint8_t buff[5];
energy.read(buff, 5);
float e = 0;
for (int i = 0; i < 5; i++) {
for (int i=0; i<5; i++) {
e *= 256;
e += buff[i];
}
@ -114,104 +209,162 @@ float Adafruit_INA228::readEnergy(void) {
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Charge register.
@return The current Charge calculation in Coulombs
@brief Returns the current measurement mode
@return The current mode
*/
/**************************************************************************/
float Adafruit_INA228::readCharge(void) {
Adafruit_I2CRegister charge =
Adafruit_I2CRegister(i2c_dev, INA228_REG_CHARGE, 5, MSBFIRST);
uint8_t buff[5];
charge.read(buff, 5);
// Convert 40-bit two's complement value
int64_t c = 0;
for (int i = 0; i < 5; i++) {
c = (c << 8) | buff[i];
}
// Handle sign extension for 40-bit two's complement
if (c & ((int64_t)1 << 39)) {
c |= 0xFFFFFF0000000000; // Sign extend to 64 bits
}
return (float)c * _current_lsb;
INA228_MeasurementMode Adafruit_INA228::getMode(void) {
Adafruit_I2CRegisterBits mode = Adafruit_I2CRegisterBits(Config, 3, 0);
return (INA228_MeasurementMode)mode.read();
}
/**************************************************************************/
/*!
@brief Sets a new measurement mode
@param new_mode
The new mode to be set
*/
/**************************************************************************/
void Adafruit_INA228::setMode(INA228_MeasurementMode new_mode) {
Adafruit_I2CRegisterBits mode = Adafruit_I2CRegisterBits(ADC_Config, 4, 12);
mode.write(new_mode);
}
/**************************************************************************/
/*!
@brief Reads the current number of averaging samples
@return The current number of averaging samples
*/
/**************************************************************************/
INA228_AveragingCount Adafruit_INA228::getAveragingCount(void) {
Adafruit_I2CRegisterBits averaging_count =
Adafruit_I2CRegisterBits(ADC_Config, 3, 0);
return (INA228_AveragingCount)averaging_count.read();
}
/**************************************************************************/
/*!
@brief Sets the number of averaging samples
@param count
The number of samples to be averaged
*/
/**************************************************************************/
void Adafruit_INA228::setAveragingCount(INA228_AveragingCount count) {
Adafruit_I2CRegisterBits averaging_count =
Adafruit_I2CRegisterBits(ADC_Config, 3, 0);
averaging_count.write(count);
}
/**************************************************************************/
/*!
@brief Reads the current current conversion time
@return The current current conversion time
*/
/**************************************************************************/
INA228_ConversionTime Adafruit_INA228::getCurrentConversionTime(void) {
Adafruit_I2CRegisterBits current_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 6);
return (INA228_ConversionTime)current_conversion_time.read();
}
/**************************************************************************/
/*!
@brief Sets the current conversion time
@param time
The new current conversion time
*/
/**************************************************************************/
void Adafruit_INA228::setCurrentConversionTime(INA228_ConversionTime time) {
Adafruit_I2CRegisterBits current_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 6);
current_conversion_time.write(time);
}
/**************************************************************************/
/*!
@brief Reads the current bus voltage conversion time
@return The current bus voltage conversion time
*/
/**************************************************************************/
INA228_ConversionTime Adafruit_INA228::getVoltageConversionTime(void) {
Adafruit_I2CRegisterBits voltage_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 9);
return (INA228_ConversionTime)voltage_conversion_time.read();
}
/**************************************************************************/
/*!
@brief Sets the bus voltage conversion time
@param time
The new bus voltage conversion time
*/
/**************************************************************************/
void Adafruit_INA228::setVoltageConversionTime(INA228_ConversionTime time) {
Adafruit_I2CRegisterBits voltage_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 9);
voltage_conversion_time.write(time);
}
/**************************************************************************/
/*!
@brief Returns the current alert type
@return The current alert type
@brief Checks if the most recent one shot measurement has completed
@return true if the conversion has completed
*/
/**************************************************************************/
INA228_AlertType Adafruit_INA228::getAlertType(void) {
Adafruit_I2CRegisterBits alert_type =
Adafruit_I2CRegisterBits(Diag_Alert, 6, 8);
return (INA228_AlertType)alert_type.read();
bool Adafruit_INA228::conversionReady(void) {
Adafruit_I2CRegisterBits conversion_ready =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 1);
return conversion_ready.read();
}
/**************************************************************************/
/*!
@brief Sets a new alert type
@param alert
The new alert type to be set
@brief Reads the current alert polarity setting
@return The current bus alert polarity setting
*/
/**************************************************************************/
void Adafruit_INA228::setAlertType(INA228_AlertType alert) {
Adafruit_I2CRegisterBits alert_type =
Adafruit_I2CRegisterBits(Diag_Alert, 6, 8);
alert_type.write(alert);
INA228_AlertPolarity Adafruit_INA228::getAlertPolarity(void) {
Adafruit_I2CRegisterBits alert_polarity =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 12);
return (INA228_AlertPolarity)alert_polarity.read();
}
/**************************************************************************/
/*!
@brief Resets the energy and charge accumulators
@brief Sets Alert Polarity Bit
@param polarity
The polarity of the alert pin
*/
/**************************************************************************/
void Adafruit_INA228::resetAccumulators(void) {
Adafruit_I2CRegisterBits reset_accumulators =
Adafruit_I2CRegisterBits(Config, 1, 14);
reset_accumulators.write(1);
void Adafruit_INA228::setAlertPolarity(INA228_AlertPolarity polarity) {
Adafruit_I2CRegisterBits alert_polarity =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 12);
alert_polarity.write(polarity);
}
/**************************************************************************/
/*!
@brief Reads the die temperature with the INA228-specific conversion factor
@return The current die temp in deg C
@brief Reads the current alert latch setting
@return The current bus alert latch setting
*/
/**************************************************************************/
float Adafruit_INA228::readDieTemp(void) {
Adafruit_I2CRegister temp =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_DIETEMP, 2, MSBFIRST);
int16_t t = temp.read();
// INA228 uses 16 bits for temperature with 7.8125 m°C/LSB
return (float)t * 7.8125 / 1000.0;
INA228_AlertLatch Adafruit_INA228::getAlertLatch(void) {
Adafruit_I2CRegisterBits alert_latch =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 15);
return (INA228_AlertLatch)alert_latch.read();
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Bus Voltage register
using INA228-specific conversion factor.
@return The current bus voltage measurement in V
@brief Sets Alert Latch Bit
@param state
The parameter which asserts the ALERT pin
*/
/**************************************************************************/
float Adafruit_INA228::readBusVoltage(void) {
Adafruit_I2CRegister bus_voltage =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_VBUS, 3, MSBFIRST);
// INA228 uses 195.3125 µV/LSB for bus voltage
return (float)((uint32_t)bus_voltage.read() >> 4) * 195.3125 / 1e6;
void Adafruit_INA228::setAlertLatch(INA228_AlertLatch state) {
Adafruit_I2CRegisterBits alert_latch =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 15);
alert_latch.write(state);
}
/**************************************************************************/
/*!
@brief Sets the shunt calibration by resistor for INA228.
@param shunt_res Resistance of the shunt in ohms (floating point)
@param max_current Maximum expected current in A (floating point)
@brief Reads the 12 possible alert reason bits from DIAG_ALRT
@return 10 bits that indiccate MEMSTAT (bit 0), CONVRF, POL, BUSUL, BUSOL, SHNTUL, SHNTOL,
TMPOL, reserved, MATHOF, CHARGEOF, ENERGYOF (bit 11)
*/
/**************************************************************************/
void Adafruit_INA228::setShunt(float shunt_res, float max_current) {
_shunt_res = shunt_res;
// INA228 uses 2^19 as the divisor
_current_lsb = max_current / (float)(1UL << 19);
_updateShuntCalRegister();
uint16_t Adafruit_INA228::alertFunctionFlags(void) {
Adafruit_I2CRegisterBits alert_flags =
Adafruit_I2CRegisterBits(Diag_Alert, 12, 0);
return alert_flags.read();
}

View file

@ -4,7 +4,7 @@
* I2C Driver for INA228 Current and Power sensor
*
* This is a library for the Adafruit INA228 breakout:
* http://www.adafruit.com/products/5832
* http://www.adafruit.com/products/xxxx
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing products from
@ -17,112 +17,84 @@
#ifndef _ADAFRUIT_INA228_H
#define _ADAFRUIT_INA228_H
#include "Adafruit_INA2xx.h"
#include "Arduino.h"
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Wire.h>
#define INA228_I2CADDR_DEFAULT 0x40 ///< INA228 default i2c address
#define INA228_DEVICE_ID 0x228 ///< INA228 device ID
#define INA228_REG_CONFIG 0x00 ///< Configuration register
#define INA228_REG_ADCCFG 0x01
#define INA228_REG_SHUNTCAL 0x02
#define INA228_REG_SHUNTTEMPCO 0x03
#define INA228_REG_VSHUNT 0x04
#define INA228_REG_VBUS 0x05
#define INA228_REG_DIETEMP 0x06
#define INA228_REG_CURRENT 0x07
#define INA228_REG_POWER 0x08
#define INA228_REG_ENERGY 0x09
#define INA228_REG_CHARGE 0x0A
#define INA228_REG_DIAGALRT 0x0B
#define INA228_REG_SOVL 0x0C
#define INA228_REG_SUVL 0x0D
#define INA228_REG_BOVL 0x0E
#define INA228_REG_BUVL 0x0F
#define INA228_REG_TEMPLIMIT 0x10
#define INA228_REG_PWRLIMIT 0x10
#define INA228_REG_MFG_UID 0x3E ///< Manufacturer ID Register
#define INA228_REG_DVC_UID 0x3F ///< Device ID and Revision Register
// INA228 specific registers
#define INA228_REG_ENERGY 0x09 ///< Energy result register
#define INA228_REG_CHARGE 0x0A ///< Charge result register (40-bit)
#define INA228_REG_SHUNTTEMPCO 0x03 ///< Shunt temperature coefficient register
///@{
/**
* @name Legacy compatibility macros
* @brief Mode options.
*
* These macros provide backward compatibility with earlier versions of the
* library
* Allowed values for setMode.
*/
#define INA228_MODE_SHUTDOWN \
INA2XX_MODE_SHUTDOWN ///< Alias for INA2XX_MODE_SHUTDOWN
#define INA228_MODE_TRIG_BUS \
INA2XX_MODE_TRIG_BUS ///< Alias for INA2XX_MODE_TRIG_BUS
#define INA228_MODE_TRIG_SHUNT \
INA2XX_MODE_TRIG_SHUNT ///< Alias for INA2XX_MODE_TRIG_SHUNT
#define INA228_MODE_TRIG_BUS_SHUNT \
INA2XX_MODE_TRIG_BUS_SHUNT ///< Alias for INA2XX_MODE_TRIG_BUS_SHUNT
#define INA228_MODE_TRIG_TEMP \
INA2XX_MODE_TRIG_TEMP ///< Alias for INA2XX_MODE_TRIG_TEMP
#define INA228_MODE_TRIG_TEMP_BUS \
INA2XX_MODE_TRIG_TEMP_BUS ///< Alias for INA2XX_MODE_TRIG_TEMP_BUS
#define INA228_MODE_TRIG_TEMP_SHUNT \
INA2XX_MODE_TRIG_TEMP_SHUNT ///< Alias for INA2XX_MODE_TRIG_TEMP_SHUNT
#define INA228_MODE_TRIG_TEMP_BUS_SHUNT \
INA2XX_MODE_TRIG_TEMP_BUS_SHUNT ///< Alias for INA2XX_MODE_TRIG_TEMP_BUS_SHUNT
#define INA228_MODE_SHUTDOWN2 \
INA2XX_MODE_SHUTDOWN2 ///< Alias for INA2XX_MODE_SHUTDOWN2
#define INA228_MODE_CONT_BUS \
INA2XX_MODE_CONT_BUS ///< Alias for INA2XX_MODE_CONT_BUS
#define INA228_MODE_CONT_SHUNT \
INA2XX_MODE_CONT_SHUNT ///< Alias for INA2XX_MODE_CONT_SHUNT
#define INA228_MODE_CONT_BUS_SHUNT \
INA2XX_MODE_CONT_BUS_SHUNT ///< Alias for INA2XX_MODE_CONT_BUS_SHUNT
#define INA228_MODE_CONT_TEMP \
INA2XX_MODE_CONT_TEMP ///< Alias for INA2XX_MODE_CONT_TEMP
#define INA228_MODE_CONT_TEMP_BUS \
INA2XX_MODE_CONT_TEMP_BUS ///< Alias for INA2XX_MODE_CONT_TEMP_BUS
#define INA228_MODE_CONT_TEMP_SHUNT \
INA2XX_MODE_CONT_TEMP_SHUNT ///< Alias for INA2XX_MODE_CONT_TEMP_SHUNT
#define INA228_MODE_CONT_TEMP_BUS_SHUNT \
INA2XX_MODE_CONT_TEMP_BUS_SHUNT ///< Alias for INA2XX_MODE_CONT_TEMP_BUS_SHUNT
#define INA228_MODE_TRIGGERED \
INA2XX_MODE_TRIGGERED ///< Alias for INA2XX_MODE_TRIGGERED
#define INA228_MODE_CONTINUOUS \
INA2XX_MODE_CONTINUOUS ///< Alias for INA2XX_MODE_CONTINUOUS
typedef enum _mode {
INA228_MODE_SHUTDOWN = 0x00, /**< SHUTDOWN: Minimize quiescient current and
turn off current into the device inputs. Set
another mode to exit shutown mode **/
INA228_MODE_TRIGGERED = 0x07, /**< TRIGGERED: Trigger a one-shot measurement
of temp, current and bus voltage. Set the TRIGGERED
mode again to take a new measurement **/
INA228_MODE_CONTINUOUS = 0x0F, /**< CONTINUOUS: (Default) Continuously update
the temp, current, bus voltage and power
registers with new measurements **/
} INA228_MeasurementMode;
#define INA228_TIME_50_us INA2XX_TIME_50_us ///< Alias for INA2XX_TIME_50_us
#define INA228_TIME_84_us INA2XX_TIME_84_us ///< Alias for INA2XX_TIME_84_us
#define INA228_TIME_150_us INA2XX_TIME_150_us ///< Alias for INA2XX_TIME_150_us
#define INA228_TIME_280_us INA2XX_TIME_280_us ///< Alias for INA2XX_TIME_280_us
#define INA228_TIME_540_us INA2XX_TIME_540_us ///< Alias for INA2XX_TIME_540_us
#define INA228_TIME_1052_us \
INA2XX_TIME_1052_us ///< Alias for INA2XX_TIME_1052_us
#define INA228_TIME_2074_us \
INA2XX_TIME_2074_us ///< Alias for INA2XX_TIME_2074_us
#define INA228_TIME_4120_us \
INA2XX_TIME_4120_us ///< Alias for INA2XX_TIME_4120_us
#define INA228_COUNT_1 INA2XX_COUNT_1 ///< Alias for INA2XX_COUNT_1
#define INA228_COUNT_4 INA2XX_COUNT_4 ///< Alias for INA2XX_COUNT_4
#define INA228_COUNT_16 INA2XX_COUNT_16 ///< Alias for INA2XX_COUNT_16
#define INA228_COUNT_64 INA2XX_COUNT_64 ///< Alias for INA2XX_COUNT_64
#define INA228_COUNT_128 INA2XX_COUNT_128 ///< Alias for INA2XX_COUNT_128
#define INA228_COUNT_256 INA2XX_COUNT_256 ///< Alias for INA2XX_COUNT_256
#define INA228_COUNT_512 INA2XX_COUNT_512 ///< Alias for INA2XX_COUNT_512
#define INA228_COUNT_1024 INA2XX_COUNT_1024 ///< Alias for INA2XX_COUNT_1024
#define INA228_ALERT_POLARITY_NORMAL \
INA2XX_ALERT_POLARITY_NORMAL ///< Alias for INA2XX_ALERT_POLARITY_NORMAL
#define INA228_ALERT_POLARITY_INVERTED \
INA2XX_ALERT_POLARITY_INVERTED ///< Alias for INA2XX_ALERT_POLARITY_INVERTED
#define INA228_ALERT_LATCH_ENABLED \
INA2XX_ALERT_LATCH_ENABLED ///< Alias for INA2XX_ALERT_LATCH_ENABLED
#define INA228_ALERT_LATCH_TRANSPARENT \
INA2XX_ALERT_LATCH_TRANSPARENT ///< Alias for INA2XX_ALERT_LATCH_TRANSPARENT
///@}
///@{
/**
* @name Legacy type aliases
* @brief Conversion Time options.
*
* These typedefs provide backward compatibility with earlier versions of the
* library
* Allowed values for setCurrentConversionTime and setVoltageConversionTime.
*/
typedef INA2XX_MeasurementMode
INA228_MeasurementMode; ///< Alias for INA2XX_MeasurementMode
typedef INA2XX_ConversionTime
INA228_ConversionTime; ///< Alias for INA2XX_ConversionTime
typedef INA2XX_AveragingCount
INA228_AveragingCount; ///< Alias for INA2XX_AveragingCount
typedef INA2XX_AlertPolarity
INA228_AlertPolarity; ///< Alias for INA2XX_AlertPolarity
typedef INA2XX_AlertLatch INA228_AlertLatch; ///< Alias for INA2XX_AlertLatch
///@}
typedef enum _conversion_time {
INA228_TIME_50_us, ///< Measurement time: 50us
INA228_TIME_84_us, ///< Measurement time: 84us
INA228_TIME_150_us, ///< Measurement time: 150us
INA228_TIME_280_us, ///< Measurement time: 280us
INA228_TIME_540_us, ///< Measurement time: 540us
INA228_TIME_1052_us, ///< Measurement time: 1052us
INA228_TIME_2074_us, ///< Measurement time: 2074us
INA228_TIME_4120_us, ///< Measurement time: 4120us
} INA228_ConversionTime;
/**
* @brief Alert trigger options specific to INA228.
* @brief Averaging Count options.
*
* Allowed values forsetAveragingCount.
*/
typedef enum _count {
INA228_COUNT_1, ///< Window size: 1 sample (Default)
INA228_COUNT_4, ///< Window size: 4 samples
INA228_COUNT_16, ///< Window size: 16 samples
INA228_COUNT_64, ///< Window size: 64 samples
INA228_COUNT_128, ///< Window size: 128 samples
INA228_COUNT_256, ///< Window size: 256 samples
INA228_COUNT_512, ///< Window size: 512 samples
INA228_COUNT_1024, ///< Window size: 1024 samples
} INA228_AveragingCount;
/**
* @brief Alert trigger options.
*
* Allowed values for setAlertType.
*/
@ -136,32 +108,78 @@ typedef enum _alert_type {
INA228_ALERT_NONE = 0x0, ///< Do not trigger alert pin (Default)
} INA228_AlertType;
/**
* @brief Alert pin polarity options.
*
* Allowed values for setAlertPolarity.
*/
typedef enum _alert_polarity {
INA228_ALERT_POLARITY_NORMAL = 0x0, ///< Active high open-collector (Default)
INA228_ALERT_POLARITY_INVERTED = 0x1, ///< Active low open-collector
} INA228_AlertPolarity;
/**
* @brief Alert pin latch options.
*
* Allowed values for setAlertLatch.
*/
typedef enum _alert_latch {
INA228_ALERT_LATCH_ENABLED = 0x1, /**< Alert will latch until Mask/Enable
register is read **/
INA228_ALERT_LATCH_TRANSPARENT = 0x0, /**< Alert will reset when fault is
cleared **/
} INA228_AlertLatch;
/*!
* @brief Class that stores state and functions for interacting with
* INA228 Current and Power Sensor
*/
class Adafruit_INA228 : public Adafruit_INA2xx {
public:
class Adafruit_INA228 {
public:
Adafruit_INA228();
bool begin(uint8_t i2c_addr = INA228_I2CADDR_DEFAULT,
TwoWire* theWire = &Wire, bool skipReset = false);
void reset(void) override;
TwoWire *theWire = &Wire);
void reset(void);
// INA228 specific functions
void setShunt(float shunt_res = 0.1, float max_current = 3.2);
float readDieTemp(void);
float readCurrent(void);
float readBusVoltage(void);
float readShuntVoltage(void);
float readPower(void);
float readEnergy(void);
float readCharge(void);
void setMode(INA228_MeasurementMode mode);
INA228_MeasurementMode getMode(void);
bool conversionReady(void);
uint16_t alertFunctionFlags(void);
float getAlertLimit(void);
void setAlertLimit(float limit);
INA228_AlertLatch getAlertLatch(void);
void setAlertLatch(INA228_AlertLatch state);
INA228_AlertPolarity getAlertPolarity(void);
void setAlertPolarity(INA228_AlertPolarity polarity);
INA228_AlertType getAlertType(void);
void setAlertType(INA228_AlertType alert);
void resetAccumulators(void);
float readDieTemp(void) override;
float readBusVoltage(void) override;
void setShunt(float shunt_res = 0.1, float max_current = 3.2) override;
// INA228 specific register pointer
Adafruit_I2CRegister* AlertLimit; ///< BusIO Register for AlertLimit
INA228_ConversionTime getCurrentConversionTime(void);
void setCurrentConversionTime(INA228_ConversionTime time);
INA228_ConversionTime getVoltageConversionTime(void);
void setVoltageConversionTime(INA228_ConversionTime time);
INA228_AveragingCount getAveragingCount(void);
void setAveragingCount(INA228_AveragingCount count);
protected:
void _updateShuntCalRegister(void) override;
Adafruit_I2CRegister *Config, ///< BusIO Register for Config
*ADC_Config, ///< BusIO Register for Config
*Diag_Alert, ///< BusIO Register for MaskEnable
*AlertLimit; ///< BusIO Register for AlertLimit
private:
float _current_lsb;
Adafruit_I2CDevice *i2c_dev;
};
#endif

View file

@ -1,459 +0,0 @@
/*!
* @file Adafruit_INA2xx.cpp
*
* @section ina2xx_intro Introduction
*
* I2C Driver base class for the INA2xx I2C Current and Power sensors
*
* This is a library for the Adafruit INA228 breakout:
* http://www.adafruit.com/products/5832
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing products from
* Adafruit!
*
* @section ina2xx_dependencies Dependencies
*
* This library depends on the Adafruit BusIO library
*
* @section ina2xx_author Author
*
* Bryan Siepert for Adafruit Industries
*
* @section ina2xx_license License
*
* BSD (see license.txt)
*
* @section ina2xx_history HISTORY
*
* v1.0 - First release
*/
#include "Adafruit_INA2xx.h"
#include <Wire.h>
#include "Arduino.h"
/*!
* @brief Instantiates a new INA2xx class
*/
Adafruit_INA2xx::Adafruit_INA2xx(void) {}
/*!
* @brief Sets up the HW
* @param i2c_address
* The I2C address to be used.
* @param theWire
* The Wire object to be used for I2C connections.
* @param skipReset
* When set to true, will omit resetting all registers to
* their default values. Default: false.
* @return True if initialization was successful (including verifying
* the manufacturer ID is Texas Instruments: 0x5449), otherwise
* false.
*/
bool Adafruit_INA2xx::begin(uint8_t i2c_address, TwoWire* theWire,
bool skipReset) {
i2c_dev = new Adafruit_I2CDevice(i2c_address, theWire);
if (!i2c_dev->begin()) {
return false;
}
Adafruit_I2CRegister* device_register =
new Adafruit_I2CRegister(i2c_dev, INA2XX_REG_DVC_UID, 2, MSBFIRST);
Adafruit_I2CRegister* mfg_register =
new Adafruit_I2CRegister(i2c_dev, INA2XX_REG_MFG_UID, 2, MSBFIRST);
Adafruit_I2CRegisterBits* device_id =
new Adafruit_I2CRegisterBits(device_register, 12, 4);
// Check manufacturer ID (should be 0x5449 for Texas Instruments)
uint16_t mfg_id = mfg_register->read();
if (mfg_id != 0x5449) {
return false;
}
// Store device ID for validation in derived classes
_device_id = device_id->read();
Config = new Adafruit_I2CRegister(i2c_dev, INA2XX_REG_CONFIG, 2, MSBFIRST);
ADC_Config =
new Adafruit_I2CRegister(i2c_dev, INA2XX_REG_ADCCFG, 2, MSBFIRST);
Diag_Alert =
new Adafruit_I2CRegister(i2c_dev, INA2XX_REG_DIAGALRT, 2, MSBFIRST);
if (!skipReset) {
reset();
delay(2); // delay 2ms to give time for first measurement to finish
}
return true;
}
/**************************************************************************/
/*!
@brief Resets the hardware. All registers are set to default values,
the same as a power-on reset.
*/
/**************************************************************************/
void Adafruit_INA2xx::reset(void) {
Adafruit_I2CRegisterBits reset = Adafruit_I2CRegisterBits(Config, 1, 15);
reset.write(1);
Adafruit_I2CRegisterBits alert_conv =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 14);
alert_conv.write(1);
setMode(INA2XX_MODE_CONTINUOUS);
}
/**************************************************************************/
/*!
@brief Updates the shunt calibration value to the register.
This is implemented in the derived classes due to different calculations
*/
/**************************************************************************/
void Adafruit_INA2xx::_updateShuntCalRegister() {
// Implemented in derived classes
}
/**************************************************************************/
/*!
@brief Sets the shunt calibration by resistor.
@note This base implementation uses INA228 settings.
Derived classes override this method for their specific settings.
@param shunt_res Resistance of the shunt in ohms (floating point)
@param max_current Maximum expected current in A (floating point)
*/
/**************************************************************************/
void Adafruit_INA2xx::setShunt(float shunt_res, float max_current) {
_shunt_res = shunt_res;
// Default to INA228 behavior (2^19 divisor)
_current_lsb = max_current / (float)(1UL << 19);
_updateShuntCalRegister();
}
/**************************************************************************/
/*!
@brief Sets the shunt full scale ADC range across IN+ and IN-.
@param adc_range
Shunt full scale ADC range (0: +/-163.84 mV or 1: +/-40.96 mV)
*/
/**************************************************************************/
void Adafruit_INA2xx::setADCRange(uint8_t adc_range) {
Adafruit_I2CRegisterBits adc_range_bit =
Adafruit_I2CRegisterBits(Config, 1, 4);
adc_range_bit.write(adc_range);
_updateShuntCalRegister();
}
/**************************************************************************/
/*!
@brief Reads the shunt full scale ADC range across IN+ and IN-.
@return Shunt full scale ADC range (0: +/-163.84 mV or 1: +/-40.96 mV)
*/
/**************************************************************************/
uint8_t Adafruit_INA2xx::getADCRange() {
return (Config->read() >> 4) & 1;
}
/**************************************************************************/
/*!
@brief Reads the die temperature (using INA228 scale factor by default)
@note This base implementation uses the INA228 conversion factor of 7.8125
m°C/LSB. Derived classes override this method as needed (e.g., INA237).
@return The current die temp in deg C
*/
/**************************************************************************/
float Adafruit_INA2xx::readDieTemp(void) {
Adafruit_I2CRegister temp =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_DIETEMP, 2, MSBFIRST);
int16_t t = temp.read();
// INA228 uses 16 bits for temperature with 7.8125 m°C/LSB
return (float)t * 7.8125 / 1000.0;
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Current register.
@return The current current measurement in mA
*/
/**************************************************************************/
float Adafruit_INA2xx::readCurrent(void) {
Adafruit_I2CRegister current =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_CURRENT, 3, MSBFIRST);
int32_t i = current.read();
if (i & 0x800000)
i |= 0xFF000000;
return (float)i / 16.0 * _current_lsb * 1000.0;
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Current register.
@return The current current measurement in mA
*/
/**************************************************************************/
float Adafruit_INA2xx::getCurrent_mA(void) {
return readCurrent();
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Bus Voltage register.
@note This base implementation uses the INA228 conversion factor.
Derived classes override this method for their specific conversion
factors.
@return The current bus voltage measurement in V
*/
/**************************************************************************/
float Adafruit_INA2xx::readBusVoltage(void) {
Adafruit_I2CRegister bus_voltage =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_VBUS, 3, MSBFIRST);
// INA228 uses 195.3125 µV/LSB (microvolts) for bus voltage,
// so we need to divide by 1e6 to get Volts
return (float)((uint32_t)bus_voltage.read() >> 4) * 195.3125 / 1e6;
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Bus Voltage register.
@return The current bus voltage measurement in V
*/
/**************************************************************************/
float Adafruit_INA2xx::getBusVoltage_V(void) {
return readBusVoltage();
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Shunt Voltage register.
@return The current shunt voltage measurement in mV
*/
/**************************************************************************/
float Adafruit_INA2xx::readShuntVoltage(void) {
float scale = 312.5;
if (getADCRange()) {
scale = 78.125;
}
Adafruit_I2CRegister shunt_voltage =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_VSHUNT, 3, MSBFIRST);
int32_t v = shunt_voltage.read();
if (v & 0x800000)
v |= 0xFF000000;
return (float)v / 16.0 * scale / 1000000.0;
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Shunt Voltage register.
@return The current shunt voltage measurement in mV
*/
/**************************************************************************/
float Adafruit_INA2xx::getShuntVoltage_mV(void) {
return readShuntVoltage();
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Power register.
@return The current Power calculation in mW
*/
/**************************************************************************/
float Adafruit_INA2xx::readPower(void) {
Adafruit_I2CRegister power =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_POWER, 3, MSBFIRST);
return (float)power.read() * 3.2 * _current_lsb * 1000;
}
/**************************************************************************/
/*!
@brief Reads and scales the current value of the Power register.
@return The current Power calculation in mW
*/
/**************************************************************************/
float Adafruit_INA2xx::getPower_mW(void) {
return readPower();
}
/**************************************************************************/
/*!
@brief Returns the current measurement mode
@return The current mode
*/
/**************************************************************************/
INA2XX_MeasurementMode Adafruit_INA2xx::getMode(void) {
Adafruit_I2CRegisterBits mode = Adafruit_I2CRegisterBits(ADC_Config, 4, 12);
return (INA2XX_MeasurementMode)mode.read();
}
/**************************************************************************/
/*!
@brief Sets a new measurement mode
@param new_mode
The new mode to be set
*/
/**************************************************************************/
void Adafruit_INA2xx::setMode(INA2XX_MeasurementMode new_mode) {
Adafruit_I2CRegisterBits mode = Adafruit_I2CRegisterBits(ADC_Config, 4, 12);
mode.write(new_mode);
}
/**************************************************************************/
/*!
@brief Reads the current number of averaging samples
@return The current number of averaging samples
*/
/**************************************************************************/
INA2XX_AveragingCount Adafruit_INA2xx::getAveragingCount(void) {
Adafruit_I2CRegisterBits averaging_count =
Adafruit_I2CRegisterBits(ADC_Config, 3, 0);
return (INA2XX_AveragingCount)averaging_count.read();
}
/**************************************************************************/
/*!
@brief Sets the number of averaging samples
@param count
The number of samples to be averaged
*/
/**************************************************************************/
void Adafruit_INA2xx::setAveragingCount(INA2XX_AveragingCount count) {
Adafruit_I2CRegisterBits averaging_count =
Adafruit_I2CRegisterBits(ADC_Config, 3, 0);
averaging_count.write(count);
}
/**************************************************************************/
/*!
@brief Reads the current current conversion time
@return The current current conversion time
*/
/**************************************************************************/
INA2XX_ConversionTime Adafruit_INA2xx::getCurrentConversionTime(void) {
Adafruit_I2CRegisterBits current_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 6);
return (INA2XX_ConversionTime)current_conversion_time.read();
}
/**************************************************************************/
/*!
@brief Sets the current conversion time
@param time
The new current conversion time
*/
/**************************************************************************/
void Adafruit_INA2xx::setCurrentConversionTime(INA2XX_ConversionTime time) {
Adafruit_I2CRegisterBits current_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 6);
current_conversion_time.write(time);
}
/**************************************************************************/
/*!
@brief Reads the current bus voltage conversion time
@return The current bus voltage conversion time
*/
/**************************************************************************/
INA2XX_ConversionTime Adafruit_INA2xx::getVoltageConversionTime(void) {
Adafruit_I2CRegisterBits voltage_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 9);
return (INA2XX_ConversionTime)voltage_conversion_time.read();
}
/**************************************************************************/
/*!
@brief Sets the bus voltage conversion time
@param time
The new bus voltage conversion time
*/
/**************************************************************************/
void Adafruit_INA2xx::setVoltageConversionTime(INA2XX_ConversionTime time) {
Adafruit_I2CRegisterBits voltage_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 9);
voltage_conversion_time.write(time);
}
/**************************************************************************/
/*!
@brief Reads the temperature conversion time
@return The temperature conversion time
*/
/**************************************************************************/
INA2XX_ConversionTime Adafruit_INA2xx::getTemperatureConversionTime(void) {
Adafruit_I2CRegisterBits temperature_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 3);
return (INA2XX_ConversionTime)temperature_conversion_time.read();
}
/**************************************************************************/
/*!
@brief Sets the temperature conversion time
@param time
The new temperature conversion time
*/
/**************************************************************************/
void Adafruit_INA2xx::setTemperatureConversionTime(INA2XX_ConversionTime time) {
Adafruit_I2CRegisterBits temperature_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 3);
temperature_conversion_time.write(time);
}
/**************************************************************************/
/*!
@brief Checks if the most recent one shot measurement has completed
@return true if the conversion has completed
*/
/**************************************************************************/
bool Adafruit_INA2xx::conversionReady(void) {
Adafruit_I2CRegisterBits conversion_ready =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 1);
return conversion_ready.read();
}
/**************************************************************************/
/*!
@brief Reads the current alert polarity setting
@return The current bus alert polarity setting
*/
/**************************************************************************/
INA2XX_AlertPolarity Adafruit_INA2xx::getAlertPolarity(void) {
Adafruit_I2CRegisterBits alert_polarity =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 12);
return (INA2XX_AlertPolarity)alert_polarity.read();
}
/**************************************************************************/
/*!
@brief Sets Alert Polarity Bit
@param polarity
The polarity of the alert pin
*/
/**************************************************************************/
void Adafruit_INA2xx::setAlertPolarity(INA2XX_AlertPolarity polarity) {
Adafruit_I2CRegisterBits alert_polarity =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 12);
alert_polarity.write(polarity);
}
/**************************************************************************/
/*!
@brief Reads the current alert latch setting
@return The current bus alert latch setting
*/
/**************************************************************************/
INA2XX_AlertLatch Adafruit_INA2xx::getAlertLatch(void) {
Adafruit_I2CRegisterBits alert_latch =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 15);
return (INA2XX_AlertLatch)alert_latch.read();
}
/**************************************************************************/
/*!
@brief Sets Alert Latch Bit
@param state
The parameter which asserts the ALERT pin
*/
/**************************************************************************/
void Adafruit_INA2xx::setAlertLatch(INA2XX_AlertLatch state) {
Adafruit_I2CRegisterBits alert_latch =
Adafruit_I2CRegisterBits(Diag_Alert, 1, 15);
alert_latch.write(state);
}
/**************************************************************************/
/*!
@brief Reads the alert function flags from DIAG_ALRT
@return Bits that indicate alert flags
*/
/**************************************************************************/
uint16_t Adafruit_INA2xx::alertFunctionFlags(void) {
Adafruit_I2CRegisterBits alert_flags =
Adafruit_I2CRegisterBits(Diag_Alert, 12, 0);
return alert_flags.read();
}

View file

@ -1,213 +0,0 @@
/*!
* @file Adafruit_INA2xx.h
*
* I2C Driver base class for INA2xx Current and Power sensors
*
* This is a library for the Adafruit INA228 breakout:
* http://www.adafruit.com/products/5832
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing products from
* Adafruit!
*
*
* BSD license (see license.txt)
*/
#ifndef _ADAFRUIT_INA2XX_H
#define _ADAFRUIT_INA2XX_H
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Wire.h>
#include "Arduino.h"
// Common registers for INA2xx family
#define INA2XX_REG_CONFIG 0x00 ///< Configuration register
#define INA2XX_REG_ADCCFG 0x01 ///< ADC configuration register
#define INA2XX_REG_SHUNTCAL 0x02 ///< Shunt calibration register
#define INA2XX_REG_VSHUNT 0x04 ///< Shunt voltage measurement register
#define INA2XX_REG_VBUS 0x05 ///< Bus voltage measurement register
#define INA2XX_REG_DIETEMP 0x06 ///< Temperature measurement register
#define INA2XX_REG_CURRENT 0x07 ///< Current result register
#define INA2XX_REG_POWER 0x08 ///< Power result register
#define INA2XX_REG_DIAGALRT 0x0B ///< Diagnostic flags and alert register
#define INA2XX_REG_SOVL 0x0C ///< Shunt overvoltage threshold register
#define INA2XX_REG_SUVL 0x0D ///< Shunt undervoltage threshold register
#define INA2XX_REG_BOVL 0x0E ///< Bus overvoltage threshold register
#define INA2XX_REG_BUVL 0x0F ///< Bus undervoltage threshold register
#define INA2XX_REG_TEMPLIMIT 0x10 ///< Temperature over-limit threshold register
#define INA2XX_REG_PWRLIMIT 0x11 ///< Power over-limit threshold register
#define INA2XX_REG_MFG_UID 0x3E ///< Manufacturer ID register
#define INA2XX_REG_DVC_UID 0x3F ///< Device ID and revision register
#define INA2XX_I2CADDR_DEFAULT 0x40 ///< INA2xx default i2c address
/**
* @brief Mode options.
*
* Allowed values for setMode.
*/
typedef enum _mode {
/**< SHUTDOWN: Minimize quiescient current and turn off current into the
device inputs. Set another mode to exit shutown mode **/
INA2XX_MODE_SHUTDOWN = 0x00,
/**< Triggered bus voltage, single shot **/
INA2XX_MODE_TRIG_BUS = 0x01,
/**< Triggered shunt voltage, single shot **/
INA2XX_MODE_TRIG_SHUNT = 0x02,
/**< Triggered shunt voltage and bus voltage, single shot **/
INA2XX_MODE_TRIG_BUS_SHUNT = 0x03,
/**< Triggered temperature, single shot **/
INA2XX_MODE_TRIG_TEMP = 0x04,
/**< Triggered temperature and bus voltage, single shot **/
INA2XX_MODE_TRIG_TEMP_BUS = 0x05,
/**< Triggered temperature and shunt voltage, single shot **/
INA2XX_MODE_TRIG_TEMP_SHUNT = 0x06,
/**< Triggered bus voltage, shunt voltage and temperature, single shot **/
INA2XX_MODE_TRIG_TEMP_BUS_SHUNT = 0x07,
/**< Shutdown **/
INA2XX_MODE_SHUTDOWN2 = 0x08,
/**< Continuous bus voltage only **/
INA2XX_MODE_CONT_BUS = 0x09,
/**< Continuous shunt voltage only **/
INA2XX_MODE_CONT_SHUNT = 0x0A,
/**< Continuous shunt and bus voltage **/
INA2XX_MODE_CONT_BUS_SHUNT = 0x0B,
/**< Continuous temperature only **/
INA2XX_MODE_CONT_TEMP = 0x0C,
/**< Continuous bus voltage and temperature **/
INA2XX_MODE_CONT_TEMP_BUS = 0x0D,
/**< Continuous temperature and shunt voltage **/
INA2XX_MODE_CONT_TEMP_SHUNT = 0x0E,
/**< Continuous bus voltage, shunt voltage and temperature **/
INA2XX_MODE_CONT_TEMP_BUS_SHUNT = 0x0F,
/**< TRIGGERED: Trigger a one-shot measurement of temp, current and bus
voltage. Set the TRIGGERED mode again to take a new measurement **/
INA2XX_MODE_TRIGGERED = INA2XX_MODE_TRIG_TEMP_BUS_SHUNT,
/**< CONTINUOUS: (Default) Continuously update the temp, current, bus
voltage and power registers with new measurements **/
INA2XX_MODE_CONTINUOUS = INA2XX_MODE_CONT_TEMP_BUS_SHUNT
} INA2XX_MeasurementMode;
/**
* @brief Conversion Time options.
*
* Allowed values for setCurrentConversionTime and setVoltageConversionTime.
*/
typedef enum _conversion_time {
INA2XX_TIME_50_us, ///< Measurement time: 50us
INA2XX_TIME_84_us, ///< Measurement time: 84us
INA2XX_TIME_150_us, ///< Measurement time: 150us
INA2XX_TIME_280_us, ///< Measurement time: 280us
INA2XX_TIME_540_us, ///< Measurement time: 540us
INA2XX_TIME_1052_us, ///< Measurement time: 1052us
INA2XX_TIME_2074_us, ///< Measurement time: 2074us
INA2XX_TIME_4120_us, ///< Measurement time: 4120us
} INA2XX_ConversionTime;
/**
* @brief Averaging Count options.
*
* Allowed values forsetAveragingCount.
*/
typedef enum _count {
INA2XX_COUNT_1, ///< Window size: 1 sample (Default)
INA2XX_COUNT_4, ///< Window size: 4 samples
INA2XX_COUNT_16, ///< Window size: 16 samples
INA2XX_COUNT_64, ///< Window size: 64 samples
INA2XX_COUNT_128, ///< Window size: 128 samples
INA2XX_COUNT_256, ///< Window size: 256 samples
INA2XX_COUNT_512, ///< Window size: 512 samples
INA2XX_COUNT_1024, ///< Window size: 1024 samples
} INA2XX_AveragingCount;
/**
* @brief Alert pin polarity options.
*
* Allowed values for setAlertPolarity.
*/
typedef enum _alert_polarity {
INA2XX_ALERT_POLARITY_NORMAL = 0x0, ///< Active high open-collector (Default)
INA2XX_ALERT_POLARITY_INVERTED = 0x1, ///< Active low open-collector
} INA2XX_AlertPolarity;
/**
* @brief Alert pin latch options.
*
* Allowed values for setAlertLatch.
*/
typedef enum _alert_latch {
INA2XX_ALERT_LATCH_ENABLED = 0x1, /**< Alert will latch until Mask/Enable
register is read **/
INA2XX_ALERT_LATCH_TRANSPARENT = 0x0, /**< Alert will reset when fault is
cleared **/
} INA2XX_AlertLatch;
/*!
* @brief Class that stores state and functions for interacting with
* INA2xx Current and Power Sensor
*/
class Adafruit_INA2xx {
public:
Adafruit_INA2xx();
virtual bool begin(uint8_t i2c_addr = INA2XX_I2CADDR_DEFAULT,
TwoWire* theWire = &Wire, bool skipReset = false);
virtual void reset(void);
virtual void setShunt(float shunt_res = 0.1, float max_current = 3.2);
void setADCRange(uint8_t);
uint8_t getADCRange(void);
virtual float readDieTemp(void);
virtual float readBusVoltage(void);
// Common INA interface:
float getBusVoltage_V(void);
float getShuntVoltage_mV(void);
float getCurrent_mA(void);
float getPower_mW(void);
//
virtual float readCurrent(void);
virtual float readShuntVoltage(void);
virtual float readPower(void);
void setMode(INA2XX_MeasurementMode mode);
INA2XX_MeasurementMode getMode(void);
bool conversionReady(void);
uint16_t alertFunctionFlags(void);
INA2XX_AlertLatch getAlertLatch(void);
void setAlertLatch(INA2XX_AlertLatch state);
INA2XX_AlertPolarity getAlertPolarity(void);
void setAlertPolarity(INA2XX_AlertPolarity polarity);
INA2XX_ConversionTime getCurrentConversionTime(void);
void setCurrentConversionTime(INA2XX_ConversionTime time);
INA2XX_ConversionTime getVoltageConversionTime(void);
void setVoltageConversionTime(INA2XX_ConversionTime time);
INA2XX_ConversionTime getTemperatureConversionTime(void);
void setTemperatureConversionTime(INA2XX_ConversionTime time);
INA2XX_AveragingCount getAveragingCount(void);
void setAveragingCount(INA2XX_AveragingCount count);
Adafruit_I2CRegister *Config, ///< BusIO Register for Config
*ADC_Config, ///< BusIO Register for ADC Config
*Diag_Alert; ///< BusIO Register for Diagnostic Alerts
protected:
virtual void _updateShuntCalRegister(
void); ///< Updates the shunt calibration register based on
///< device-specific calculations
float _shunt_res; ///< Shunt resistance value in ohms
float _current_lsb; ///< Current LSB value used for calculations
Adafruit_I2CDevice* i2c_dev; ///< I2C device interface
uint16_t _device_id; ///< Device ID for chip verification
};
#endif

View file

@ -1,11 +1,9 @@
Adafruit_INA228
===============
[![Build Status](https://github.com/adafruit/Adafruit_INA228/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_INA228/actions)
[![Documentation](https://raw.githubusercontent.com/adafruit/ci-arduino/master/assets/doxygen_badge.svg)](https://adafruit.github.io/Adafruit_INA228/html/index.html)
Adafruit_INA228 [![Build Status](https://github.com/adafruit/Adafruit_INA228/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_INA228/actions)[![Documentation](https://github.com/adafruit/ci-arduino/blob/master/assets/doxygen_badge.svg)](http://adafruit.github.io/Adafruit_INA228/html/index.html)
================
This is the Adafruit INA228 Current and Power sensor library
Tested and works great with the [Adafruit INA228 Breakout Board](https://www.adafruit.com/product/5832)
Tested and works great with the [Adafruit INA228 Breakout Board](http://www.adafruit.com/products)
This chip uses I2C to communicate, 2 pins are required to interface
@ -18,6 +16,3 @@ BSD license, check license.txt for more information
All text above must be included in any redistribution
To install, use the Arduino Library Manager and search for "Adafruit INA228" and install the library.
Note: For INA237 and INA238 support, please use the separate [Adafruit INA237 and INA238 Library](https://github.com/adafruit/Adafruit_INA237_INA238)

View file

@ -5,20 +5,17 @@ Adafruit_INA228 ina228 = Adafruit_INA228();
void setup() {
Serial.begin(115200);
// Wait until serial port is opened
while (!Serial) {
delay(10);
}
while (!Serial) { delay(10); }
Serial.println("Adafruit INA228 Test");
if (!ina228.begin()) {
Serial.println("Couldn't find INA228 chip");
while (1)
;
while (1);
}
Serial.println("Found INA228 chip");
// set shunt resistance and max current
ina228.setShunt(0.015, 10.0);
// we need to set the resistance (default 0.1 ohm) and our max expected current (no greater than 3.2A)
ina228.setShunt(0.1, 1.0);
ina228.setAveragingCount(INA228_COUNT_16);
uint16_t counts[] = {1, 4, 16, 64, 128, 256, 512, 1024};
@ -29,66 +26,32 @@ void setup() {
ina228.setVoltageConversionTime(INA228_TIME_150_us);
Serial.print("Voltage conversion time: ");
switch (ina228.getVoltageConversionTime()) {
case INA228_TIME_50_us:
Serial.print("50");
break;
case INA228_TIME_84_us:
Serial.print("84");
break;
case INA228_TIME_150_us:
Serial.print("150");
break;
case INA228_TIME_280_us:
Serial.print("280");
break;
case INA228_TIME_540_us:
Serial.print("540");
break;
case INA228_TIME_1052_us:
Serial.print("1052");
break;
case INA228_TIME_2074_us:
Serial.print("2074");
break;
case INA228_TIME_4120_us:
Serial.print("4120");
break;
case INA228_TIME_50_us: Serial.print("50"); break;
case INA228_TIME_84_us: Serial.print("84"); break;
case INA228_TIME_150_us: Serial.print("150"); break;
case INA228_TIME_280_us: Serial.print("280"); break;
case INA228_TIME_1052_us: Serial.print("1052"); break;
case INA228_TIME_2074_us: Serial.print("2074"); break;
case INA228_TIME_4120_us: Serial.print("4120"); break;
}
Serial.println(" uS");
ina228.setCurrentConversionTime(INA228_TIME_280_us);
Serial.print("Current conversion time: ");
switch (ina228.getCurrentConversionTime()) {
case INA228_TIME_50_us:
Serial.print("50");
break;
case INA228_TIME_84_us:
Serial.print("84");
break;
case INA228_TIME_150_us:
Serial.print("150");
break;
case INA228_TIME_280_us:
Serial.print("280");
break;
case INA228_TIME_540_us:
Serial.print("540");
break;
case INA228_TIME_1052_us:
Serial.print("1052");
break;
case INA228_TIME_2074_us:
Serial.print("2074");
break;
case INA228_TIME_4120_us:
Serial.print("4120");
break;
case INA228_TIME_50_us: Serial.print("50"); break;
case INA228_TIME_84_us: Serial.print("84"); break;
case INA228_TIME_150_us: Serial.print("150"); break;
case INA228_TIME_280_us: Serial.print("280"); break;
case INA228_TIME_1052_us: Serial.print("1052"); break;
case INA228_TIME_2074_us: Serial.print("2074"); break;
case INA228_TIME_4120_us: Serial.print("4120"); break;
}
Serial.println(" uS");
// default polarity for the alert is low on ready, but
// it can be inverted!
// ina228.setAlertPolarity(1);
//ina228.setAlertPolarity(1);
}
void loop() {
@ -97,34 +60,30 @@ void loop() {
// we can set to triggered mode. to do that, we have to set
// the mode to trigger a new reading, then wait for a conversion
// either by checking the ALERT pin or reading the ready register
// ina228.setMode(INA228_MODE_TRIGGERED);
// while (!ina228.conversionReady())
//ina228.setMode(INA228_MODE_TRIGGERED);
//while (!ina228.conversionReady())
// delay(1);
Serial.print("Current: ");
Serial.print(ina228.getCurrent_mA());
Serial.print(ina228.readCurrent());
Serial.println(" mA");
Serial.print("Bus Voltage: ");
Serial.print(ina228.getBusVoltage_V());
Serial.println(" V");
Serial.print(ina228.readBusVoltage());
Serial.println(" mV");
Serial.print("Shunt Voltage: ");
Serial.print(ina228.getShuntVoltage_mV());
Serial.print(ina228.readShuntVoltage());
Serial.println(" mV");
Serial.print("Power: ");
Serial.print(ina228.getPower_mW());
Serial.print(ina228.readPower());
Serial.println(" mW");
Serial.print("Energy: ");
Serial.print(ina228.readEnergy());
Serial.println(" J");
Serial.print("Charge: ");
Serial.print(ina228.readCharge());
Serial.println(" C");
Serial.print("Temperature: ");
Serial.print(ina228.readDieTemp());
Serial.println(" *C");

View file

@ -1,80 +0,0 @@
#######################################
# Syntax Coloring Map for Adafruit_INA228 Library
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Adafruit_INA2xx KEYWORD1
Adafruit_INA228 KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
reset KEYWORD2
setShunt KEYWORD2
setADCRange KEYWORD2
getADCRange KEYWORD2
readDieTemp KEYWORD2
readCurrent KEYWORD2
readBusVoltage KEYWORD2
readShuntVoltage KEYWORD2
readPower KEYWORD2
readEnergy KEYWORD2
readCharge KEYWORD2
setMode KEYWORD2
getMode KEYWORD2
conversionReady KEYWORD2
resetAccumulators KEYWORD2
getAlertType KEYWORD2
setAlertType KEYWORD2
getBusVoltage_V KEYWORD2
getShuntVoltage_mV KEYWORD2
getCurrent_mA KEYWORD2
getPower_mW KEYWORD2
getCurrentConversionTime KEYWORD2
setCurrentConversionTime KEYWORD2
getVoltageConversionTime KEYWORD2
setVoltageConversionTime KEYWORD2
getTemperatureConversionTime KEYWORD2
setTemperatureConversionTime KEYWORD2
getAveragingCount KEYWORD2
setAveragingCount KEYWORD2
getAlertLatch KEYWORD2
setAlertLatch KEYWORD2
getAlertPolarity KEYWORD2
setAlertPolarity KEYWORD2
alertFunctionFlags KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
INA228_I2CADDR_DEFAULT LITERAL1
INA228_DEVICE_ID LITERAL1
INA2XX_MODE_SHUTDOWN LITERAL1
INA2XX_MODE_TRIGGERED LITERAL1
INA2XX_MODE_CONTINUOUS LITERAL1
INA2XX_TIME_50_us LITERAL1
INA2XX_TIME_84_us LITERAL1
INA2XX_TIME_150_us LITERAL1
INA2XX_TIME_280_us LITERAL1
INA2XX_TIME_540_us LITERAL1
INA2XX_TIME_1052_us LITERAL1
INA2XX_TIME_2074_us LITERAL1
INA2XX_TIME_4120_us LITERAL1
INA2XX_COUNT_1 LITERAL1
INA2XX_COUNT_4 LITERAL1
INA2XX_COUNT_16 LITERAL1
INA2XX_COUNT_64 LITERAL1
INA2XX_COUNT_128 LITERAL1
INA2XX_COUNT_256 LITERAL1
INA2XX_COUNT_512 LITERAL1
INA2XX_COUNT_1024 LITERAL1
INA2XX_ALERT_POLARITY_NORMAL LITERAL1
INA2XX_ALERT_POLARITY_INVERTED LITERAL1
INA2XX_ALERT_LATCH_ENABLED LITERAL1
INA2XX_ALERT_LATCH_TRANSPARENT LITERAL1

View file

@ -1,5 +1,5 @@
name=Adafruit INA228 Library
version=3.0.0
version=1.0.0
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for the INA228 sensors in the Adafruit shop