pub struct Ina226<I2C, D> { /* private fields */ }Expand description
An INA226 on an I2C bus, measuring shunt and bus voltage, current, and power on demand.
init resets the part, checks the manufacturer and die id
registers, writes the configuration, programs the calibration register for the
shunt and the current resolution, and reads the calibration back.
measure writes the triggered shunt-and-bus mode, which
starts one conversion of each, waits the conversion time for the averaging and
conversion-time settings, polls the conversion-ready flag in the Mask/Enable
register, then reads the four data registers. Reading the Mask/Enable register
clears the flag, which is why the poll is the only place it is read. As a
[Sensor] its reading is the Reading; one quantity is selected with
[Sensor::map].
§Examples
A battery bank’s draw, read through an INA226 that is not plugged in yet: 24 V on the bus and 1 A through a 100 mΩ shunt, with the part calibrated for up to 3.2 A.
use pamoja_core::Sensor;
use pamoja_hal::script::{block_on, DelayLog};
use pamoja_sensors::ina226::{sim, Ina226, BASE_ADDRESS};
let part = sim::reporting(BASE_ADDRESS, 100, 3_200_000, 24_000_000, 1_000_000);
let mut bank = Ina226::new(part, BASE_ADDRESS, DelayLog::new()).with_shunt(100, 3_200_000);
let reading = block_on(bank.read())?;
assert_eq!(reading.bus_microvolts(), 24_000_000);
assert!((reading.current_amps() - 1.0).abs() < 0.001);Implementations§
Source§impl<I2C, D> Ina226<I2C, D>
impl<I2C, D> Ina226<I2C, D>
Sourcepub fn with_shunt(self, milliohms: u32, max_microamps: u32) -> Self
pub fn with_shunt(self, milliohms: u32, max_microamps: u32) -> Self
Sets the shunt and the largest current expected through it.
The current resolution is the smallest the datasheet allows for that maximum; the calibration register follows from the two by the datasheet’s equation.
§Arguments
milliohms- the shunt resistance.max_microamps- the largest current the shunt will carry.
§Returns
The driver.
Sourcepub fn with_current_lsb(self, microamps: u32) -> Self
pub fn with_current_lsb(self, microamps: u32) -> Self
Sourcepub fn with_configuration(self, configuration: Configuration) -> Self
pub fn with_configuration(self, configuration: Configuration) -> Self
Sourcepub fn current_lsb_microamps(&self) -> u32
pub fn current_lsb_microamps(&self) -> u32
Returns the current resolution in microamps per count.
Sourcepub fn calibration(&self) -> u16
pub fn calibration(&self) -> u16
Returns the calibration word the driver programs.
Source§impl<I2C: I2c, D: DelayNs> Ina226<I2C, D>
impl<I2C: I2c, D: DelayNs> Ina226<I2C, D>
Sourcepub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
pub fn init(&mut self) -> Result<(), DriverError<I2C::Error>>
Resets the part, checks its identity, writes the configuration and calibration, and reads the calibration back.
§Errors
Returns DriverError::Bus if the bus fails, and DriverError::Sensor
with SensorError::Identity if the id registers are not an INA226’s or the
calibration register does not hold what was written.
Sourcepub fn measure(&mut self) -> Result<Reading, DriverError<I2C::Error>>
pub fn measure(&mut self) -> Result<Reading, DriverError<I2C::Error>>
Triggers one shunt and bus conversion and returns every result.
Initializes the part first if init has not run.
§Returns
The four registers with the current resolution they scale by.
§Errors
Returns DriverError::Bus if the bus fails, DriverError::Timeout if
the conversion-ready flag never sets, or any error of init.
Sourcepub fn set_alert(
&mut self,
mask: MaskEnable,
limit: u16,
) -> Result<(), DriverError<I2C::Error>>
pub fn set_alert( &mut self, mask: MaskEnable, limit: u16, ) -> Result<(), DriverError<I2C::Error>>
Programs the alert pin: which limit it watches and the limit itself.
§Arguments
mask- the Mask/Enable settings; one alert function at a time.limit- the Alert Limit register value, in the units of the register the function compares against.
§Errors
Returns DriverError::Bus if the bus fails.