Skip to content

Latest commit

 

History

27 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

INA23x: INA238 & INA239 Power Sensor Rust Driver

A platform-agnostic `#![no_std]` Rust driver for the Texas Instruments "INA23x (INA238 & INA239)" 85V, 16-Bit High-Precision Power/Current/Voltage Monitor using `embedded-hal`.

CI Status Tests Coverage

Rust Edition embedded-hal embedded-hal License: MIT or Apache-2.0

Features

  • #![no_std] Support: Suitable for bare-metal microcontrollers (STM32, RP2040, ESP32, nRF, etc.).
  • embedded-hal Integration: Works with any I2C peripheral implementing embedded-hal 1.0 traits.
  • Dual Bus Support:
    • I2C (Ina238): Works with any I2C peripheral implementing embedded-hal 1.0 I2c traits.
    • SPI (Ina239): Works with any SPI peripheral implementing embedded-hal 1.0 SpiDevice traits.
  • Core Telemetry:
    • Bus Voltage Measurement
    • Shunt Voltage Measurement
    • Calculated Current and Power Readings
    • Temperature Sensing
  • Configuration & Calibration:
    • Custom Shunt Resistor (SHUNT_CAL) calibration setup
    • Adjustable conversion times and averaging settings
  • Diagnostics & Alerts:
    • Full read access to diagnostic flags (DIAG_ALRT)
    • Configurable overvoltage, undervoltage, power, and temperature threshold limits (Setters & Getters)

Getting Started

Add ina23x to your Cargo.toml:

[dependencies]
ina23x = "0.1.0"

To minimize code footprint, you can selectively enable or disable support for specific chips using Cargo features:

[features]
default = ["ina238", "ina239"]
ina238 = []
ina239 = []

Examples

INA238 (I2C) Example

use embedded_hal::i2c::I2c;
use ina23x::{Ina238, Address, AdcRange, ....};

// Create driver instance with default I2C address (0x40)
let mut sensor = Ina238::new(i2c, 0x40);

// Configure shunt calibration (e.g., for 2A max current & 10 mΩ shunt)
sensor.calibrate(2.0, 0.1).unwrap();

// Read bus voltage in Volts
if let Ok(bus_voltage) = sensor.bus_voltage() {
    // Process bus voltage...
}

// Read current in Amperes
if let Ok(current) = sensor.current() {
    // Process current...
}

INA239 (SPI) Example

use embedded_hal::spi::SpiDevice;
use ina23x::{Ina239, AdcRange};

// Create driver instance (SPI does not require an I2C slave address)
let mut sensor = Ina239::new(spi, 0.0001, 0.01, AdcRange::Range40mV);

// Read bus voltage in Volts
if let Ok(bus_voltage) = sensor.bus_voltage() {
    // Process bus voltage...
}

// Read current in Amperes
if let Ok(current) = sensor.current() {
    // Process current...
}

Supports

  • Basic Telemetry (Bus Voltage, Shunt Voltage, Current, Power, Temperature)
  • Core Configuration and Calibration
  • Diagnostic & Alert Mask Registers (DIAG_ALRT)
  • Alert Threshold Limits (SOVL, SUVL, BOVL, BUVL, TOL, POL)
  • Async support

Functional API features:

  • Core Telemetry & Monitoring API

    • bus_voltage(), shunt_voltage(), current(), power(), die_temperature()
  • Configuration & Calibration API

    • set_config(), set_adc_config(), set_shunt_calibrate()
  • Threshold API

    • Threshold Setters: set_shunt_overvoltage_th(), set_shunt_undervoltage_th(), set_bus_overvoltage_th(), set_bus_undervoltage_th(), set_temperature_limit(), set_power_limit()

    • Threshold Getters: get_shunt_overvoltage_th(), get_shunt_undervoltage_th(), get_bus_overvoltage_th(), get_bus_undervoltage_th(), get_temp_overlimit(), get_power_overlimit()

  • Status & Alert Config: diag_flags(), set_alert_config()

  • Device Identification & Maintenance API

    • manufacture_id(), device_id(), reset()

Testing

This crate includes a unit test suite covering register reads, writes, and bitwise operations using embedded-hal-mock. Run the tests on your host platform with:

cargo test

Supported devices

  • INA238, I²C 85-V, 16-Bit, High-Precision Power Monitor
  • INA239, SPI 85-V, 16-Bit, High-Precision Power Monitor

License

Licensed under either of:

at your option.

About

A `#![no_std]` Rust driver for the Texas Instruments "INA23x: INA238 & INA239" 85V, 16-Bit High-Precision Power/Current/Voltage

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages