From bf5ee0ec7839d3d4f8e98b49f43f0c38b7f9a2be Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Tue, 14 Jan 2025 14:42:18 +0100 Subject: [PATCH] Initial commit Signed-off-by: Thomas Klaehn --- .gitignore | 1 + .vscode/launch.json | 82 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.lock | 63 ++++++++++++++++++++++++++++++++++ Cargo.toml | 7 ++++ examples/current.rs | 10 ++++++ examples/voltage.rs | 10 ++++++ src/lib.rs | 72 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 245 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 examples/current.rs create mode 100644 examples/voltage.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d77c7ec --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,82 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in library 'ina3221'", + "cargo": { + "args": [ + "test", + "--no-run", + "--lib", + "--package=ina3221" + ], + "filter": { + "name": "ina3221", + "kind": "lib" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug example 'voltage'", + "cargo": { + "args": [ + "build", + "--example=voltage", + "--package=ina3221" + ], + "filter": { + "name": "voltage", + "kind": "example" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug example 'current'", + "cargo": { + "args": [ + "build", + "--example=current", + "--package=ina3221" + ], + "filter": { + "name": "current", + "kind": "example" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in example 'voltage'", + "cargo": { + "args": [ + "test", + "--no-run", + "--example=voltage", + "--package=ina3221" + ], + "filter": { + "name": "voltage", + "kind": "example" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bb1a395 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,63 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1be3f42a67d6d345ecd59f675f3f012d6974981560836e938c22b424b85ce1be" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "i2cdev" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a01f563561b53e2def844167c92984da13779a53ca409e948a03400633cb8228" +dependencies = [ + "bitflags 2.7.0", + "byteorder", + "libc", + "nix", +] + +[[package]] +name = "ina3221" +version = "0.1.0" +dependencies = [ + "i2cdev", +] + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + +[[package]] +name = "nix" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" +dependencies = [ + "bitflags 1.3.2", + "cfg-if", + "libc", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c42f6a2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "ina3221" +version = "0.1.0" +edition = "2021" + +[dependencies] +i2cdev = "0.6.1" diff --git a/examples/current.rs b/examples/current.rs new file mode 100644 index 0000000..aaba131 --- /dev/null +++ b/examples/current.rs @@ -0,0 +1,10 @@ +use ina3221; + +fn main() { + let mut ina = ina3221::Ina3221::new(0x40); + let res = match ina.current(ina3221::Channel::One) { + Ok(value) => value, + Err(error) => panic!("Can't read i2c: {error:?}"), + }; + println!("Channel one current: {:?} mA", res); +} diff --git a/examples/voltage.rs b/examples/voltage.rs new file mode 100644 index 0000000..4d64483 --- /dev/null +++ b/examples/voltage.rs @@ -0,0 +1,10 @@ +use ina3221; + +fn main() { + let mut ina = ina3221::Ina3221::new(0x40); + let res = match ina.bus_voltage(ina3221::Channel::One) { + Ok(value) => value, + Err(error) => panic!("Can't read i2c: {error:?}"), + }; + println!("Channel one bus voltage: {:?} V.", res); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..13113d9 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,72 @@ +extern crate i2cdev; + +use i2cdev::core::*; +use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError, LinuxI2CMessage}; + +pub enum Channel { + One, + Two, + Three, +} + +pub struct Ina3221 { + slave_addr: u16, + i2c_device: String, + shunt_resistor: f64, +} + +impl Ina3221 { + + pub fn new(slave_address: u16) -> Ina3221 { + Ina3221 { + slave_addr: slave_address, + i2c_device: format!("/dev/i2c-1"), + shunt_resistor: 1.0, + } + } + + fn write_read_transaction(&mut self, register: u8) -> Result { + let mut dev = LinuxI2CDevice::new(&self.i2c_device, self.slave_addr)?; + + let binding = [register]; + let mut read_data = [0; 2]; + let mut msgs = [ + LinuxI2CMessage::write(&binding), + LinuxI2CMessage::read(&mut read_data) + ]; + dev.transfer(&mut msgs)?; + + let tmp: u16 = (read_data[0] as u16) << 8 | (read_data[1] as u16); + Ok(tmp) + } + + pub fn bus_voltage(&mut self, channel: Channel) -> Result { + let register: u8 = match channel { + Channel::One => 2, + Channel::Two => 4, + Channel::Three => 6, + }; + let mut tmp = self.write_read_transaction(register)?; + tmp = tmp >> 3; + let res: f64 = (tmp as f64) * 8.0 / 1000.0; + Ok(res) + } + + pub fn shunt_voltage(&mut self, channel: Channel) -> Result { + let register: u8 = match channel { + Channel::One => 1, + Channel::Two => 3, + Channel::Three => 5, + }; + let mut tmp = self.write_read_transaction(register)?; + tmp = tmp >> 3; + let res: f64 = (tmp as f64) * 40.0 / 1000000.0; + Ok(res) + } + + pub fn current(&mut self, channel: Channel) -> Result { + let tmp: f64 = self.shunt_voltage(channel)?; + let res: f64 = tmp * self.shunt_resistor; + Ok(res) + } +} \ No newline at end of file