Skip to content

Initial INA4230 quad-channel power monitor driver implementation#1

Open
LTAFlight wants to merge 11 commits into
OpenDevicePartnership:mainfrom
LTAFlight:initial-driver-implementation
Open

Initial INA4230 quad-channel power monitor driver implementation#1
LTAFlight wants to merge 11 commits into
OpenDevicePartnership:mainfrom
LTAFlight:initial-driver-implementation

Conversation

@LTAFlight
Copy link
Copy Markdown

Platform-agnostic async Rust driver for the Texas Instruments INA4230
quad-channel power and energy sense monitor.

What's included

  • Full register coverage via pre-generated src/device.rs from INA4230.toml
  • Async I²C interface using embedded-hal-async
  • VoltageSensor, CurrentSensor, PowerSensor, and EnergySensor trait implementations
  • ADC range selection (AdcRange::Range0 / AdcRange::Range1)
  • Calibration helpers with correct SHUNT_CAL formula
  • Optional defmt-03 logging support
  • CI workflows: check, nostd, device-driver-pregen

Testing

  • Unit tests passing (cargo test)
  • Validated on real hardware (MSPM0G3519 + Embassy) measuring bus voltage,
    shunt voltage, current, power, and energy

@LTAFlight LTAFlight requested a review from a team as a code owner April 23, 2026 01:27
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 23, 2026

Cargo Vet Audit Passed

cargo vet has passed in this PR. No new unvetted dependencies were found.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces an async, platform-agnostic Rust driver crate for the TI INA4230 quad-channel power/energy monitor, with generated register accessors and higher-level sensor trait APIs built on embedded-hal-async / embedded-sensors.

Changes:

  • Added high-level Ina4230 driver with async I²C interface adapter and sensor-trait measurement APIs (V/I/P/E).
  • Added calibration helpers and unit conversion utilities, plus basic async I²C mock-based tests.
  • Added register manifest (INA4230.toml), CI workflow to verify generated src/device.rs, and updated crate metadata/docs for publishing.

Reviewed changes

Copilot reviewed 5 out of 8 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/lib.rs Core driver API, async I²C interface adapter, calibration + unit conversions, and unit tests.
README.md Driver documentation, usage examples, calibration guidance, and regeneration instructions.
INA4230.toml Device register manifest used to generate src/device.rs.
Cargo.toml Crate rename/metadata for publishing, dependencies, features, and lint configuration.
Cargo.lock Lockfile update reflecting new dependencies.
.gitignore Ignore rule updates (notably /target and Cargo.lock).
.github/workflows/device-driver.yml CI job to regenerate and diff-check src/device.rs against the manifest.

Comment thread .github/workflows/device-driver.yml Outdated
Comment thread Cargo.toml Outdated
Comment thread Cargo.toml
Comment thread src/lib.rs Outdated
Comment thread src/lib.rs
Comment thread src/lib.rs Outdated
Comment thread src/lib.rs Outdated
Comment thread src/lib.rs Outdated
Comment thread src/lib.rs
Comment thread src/lib.rs
Comment thread src/lib.rs Outdated
- Fix filename casing: ina4230.toml -> INA4230.toml in workflows and Cargo.toml
- Add rust-version = 1.88 to Cargo.toml
- Clamp SHUNT_CAL to 15-bit max (0x7FFF)
- Per-channel calibration storage ([Option<f32>; 4], [AdcRange; 4])
- Write CONFIG2.RANGE in calibrate() for hardware ADC range selection
- Return NotCalibrated error when current/power/energy read before calibrate()
- Replace INA4230_ADDR constant with A0/A1 enums and i2c_address() function
- Add MathOverflow and EnergyOverflow error variants with check_flags() method
- Make device field private
- Add set_channel_active() method
- Update calibrate_all() to take per-channel params [(f32, f32, AdcRange); 4]
- Remove src/main.rs template leftover
- Update README with new API, error handling, and channel management sections
- Expand test suite to 9 tests
@LTAFlight
Copy link
Copy Markdown
Author

LTAFlight commented Apr 23, 2026

Addressed all review comments so far:

  • Fixed INA4230.toml filename casing throughout
  • Added rust-version = "1.88" to Cargo.toml
  • Per-channel calibration with independent parameters
  • CONFIG2.RANGE now written to hardware in calibrate()
  • NotCalibrated, MathOverflow, EnergyOverflow error variants
  • A0/A1 enums replace INA4230_ADDR constant
  • device field made private
  • set_channel_active() method added
  • calibrate_all() now takes independent per-channel params
  • Updated README and expanded test suite to 9 tests

Comment thread src/lib.rs
Comment thread src/lib.rs Outdated
Comment thread src/lib.rs Outdated
Comment thread src/lib.rs Outdated
Comment thread src/lib.rs
Comment thread src/lib.rs
Comment thread src/lib.rs Outdated
Comment thread src/lib.rs
Comment thread Cargo.toml Outdated
Comment thread Cargo.toml Outdated
LTAFlight and others added 6 commits April 23, 2026 07:51
Co-authored-by: Felipe Balbi <felipe@balbi.sh>
Co-authored-by: Felipe Balbi <felipe@balbi.sh>
Co-authored-by: Felipe Balbi <felipe@balbi.sh>
Co-authored-by: Felipe Balbi <felipe@balbi.sh>
- Rename defmt-03 feature to defmt, update to defmt 1.0
- Update INA4230.toml defmt_feature to defmt
- Regenerate src/device.rs with updated defmt feature name
- Add Sda and Scl variants to A0 and A1 enums
- Replace i2c_address() free function with ToAddress trait
- Update ToAddress impl to cover all 16 address combinations
- Update i2c_address_all_combinations test to verify all 16 addresses
@LTAFlight
Copy link
Copy Markdown
Author

Renamed defmt-03 feature to defmt, updated to defmt 1.0
Updated INA4230.toml defmt_feature to match
Regenerated src/device.rs to eliminate defmt-03 warnings
Replaced A0/A1 enums with single AddrPinState enum (reviewer-approved pattern)
Implemented ToAddress trait for (AddrPinState, AddrPinState)
Fixed all 16 address combinations with correct hex values
Fixed Channel::to_bit() match arms for set_channel_active()
Fixed check_flags() semicolon errors
All 9 tests passing

Updated Readme:
Features — defmt-03 → defmt
Usage example — A0, A1 → AddrPinState
I²C Addresses section — updated constructor example to use AddrPinState, expanded table to all 16 combinations, added column headers as A0 and A1 to clarify the argument order

- Remove [lib] doctest = false — doctests now enabled
- Pin tokio to =1.45.0
- Fix math formula blocks to use text fence in README
- Change rust,no_run to rust,ignore for examples that cannot compile in doctest context
- Keep simple const declaration blocks as runnable doctests
- Fix all clippy errors in lib.rs
- Restore device module allow attributes matching bq25723 approved pattern
- Regenerate src/device.rs with updated defmt feature name
- Remove [lib] doctest = false, enable doctests
- Change rust,no_run to rust,ignore in README
- Fix math formula blocks in README
- Pin tokio to =1.45.0
- Add cargo-vet supply chain with ODP shared audits
- Add exemptions for embedded-sensors-hal, embedded-sensors-hal-async,
  libc and paste
felipebalbi
felipebalbi previously approved these changes Apr 27, 2026
Copy link
Copy Markdown

@felipebalbi felipebalbi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just missing integration of the traits in embedded-sensors and the subsequent update after that happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants