Skip to main content

Ina226

Struct Ina226 

Source
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>

Source

pub fn new(bus: I2C, address: u8, delay: D) -> Self

Wraps an I2C bus and the part’s address.

§Arguments
  • bus - the I2C bus the part is on.
  • address - the address the A1 and A0 pins select, from address.
  • delay - the timer that paces the conversions.
§Returns

The driver, with the reset configuration and a 100 mΩ shunt sized for 3.2 A.

Source

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.

Source

pub fn with_current_lsb(self, microamps: u32) -> Self

Sets the current resolution outright, for a round figure such as 100 µA.

§Arguments
  • microamps - microamps per count of the Current register.
§Returns

The driver.

Source

pub fn with_configuration(self, configuration: Configuration) -> Self

Sets the averaging and conversion times; the mode is chosen per conversion.

§Arguments
  • configuration - the settings to write.
§Returns

The driver.

Source

pub fn current_lsb_microamps(&self) -> u32

Returns the current resolution in microamps per count.

Source

pub fn calibration(&self) -> u16

Returns the calibration word the driver programs.

Source

pub fn die_id(&self) -> Option<DieId>

Returns the die id read at initialization, if it has happened.

Source

pub fn release(self) -> (I2C, D)

Gives back the bus and the delay.

§Returns

The bus and delay the driver was built from.

Source§

impl<I2C: I2c, D: DelayNs> Ina226<I2C, D>

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<I2C: Debug, D: Debug> Debug for Ina226<I2C, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I2C, D> Sensor for Ina226<I2C, D>
where I2C: I2c + Send, I2C::Error: Debug, D: DelayNs + Send,

Source§

type Reading = Reading

The value produced by a single read, for example a temperature or a fix.
Source§

async fn read(&mut self) -> Result<Reading>

Takes a single reading from the sensor. Read more
§

fn map<F, T>(self, select: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Reading) -> T,

Wraps this sensor so every reading passes through select first. Read more

Auto Trait Implementations§

§

impl<I2C, D> Freeze for Ina226<I2C, D>
where I2C: Freeze, D: Freeze,

§

impl<I2C, D> RefUnwindSafe for Ina226<I2C, D>

§

impl<I2C, D> Send for Ina226<I2C, D>
where I2C: Send, D: Send,

§

impl<I2C, D> Sync for Ina226<I2C, D>
where I2C: Sync, D: Sync,

§

impl<I2C, D> Unpin for Ina226<I2C, D>
where I2C: Unpin, D: Unpin,

§

impl<I2C, D> UnsafeUnpin for Ina226<I2C, D>
where I2C: UnsafeUnpin, D: UnsafeUnpin,

§

impl<I2C, D> UnwindSafe for Ina226<I2C, D>
where I2C: UnwindSafe, D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.