A platform-agnostic `#![no_std]` Rust driver for the Texas Instruments "INA238" 85V, 16-Bit High-Precision Power/Current/Voltage Monitor using `embedded-hal`.
More about INA238(../ina238/Docs/ina238.pdf)
#![no_std]Support: Suitable for bare-metal microcontrollers (STM32, RP2040, ESP32, nRF, etc.).embedded-halIntegration: Works with any I2C peripheral implementingembedded-hal1.0 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
- Custom Shunt Resistor (
- Diagnostics & Alerts:
- Full read access to diagnostic flags (
DIAG_ALRT) - Configurable overvoltage, undervoltage, power, and temperature threshold limits (Setters & Getters)
- Full read access to diagnostic flags (
- Tested & Verified: Covered by unit tests using
embedded-hal-mockto ensure accurate byte sequencing and register interaction logic.
Add ina238 to your Cargo.toml:
[dependencies]
ina238 = "0.1.0"use embedded_hal::i2c::I2c;
use ina238::Ina238;
// 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...
}- 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()
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
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.