Initial commit
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
commit
bf5ee0ec78
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
82
.vscode/launch.json
vendored
Normal file
82
.vscode/launch.json
vendored
Normal file
@ -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}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
63
Cargo.lock
generated
Normal file
63
Cargo.lock
generated
Normal file
@ -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",
|
||||||
|
]
|
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "ina3221"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
i2cdev = "0.6.1"
|
10
examples/current.rs
Normal file
10
examples/current.rs
Normal file
@ -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);
|
||||||
|
}
|
10
examples/voltage.rs
Normal file
10
examples/voltage.rs
Normal file
@ -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);
|
||||||
|
}
|
72
src/lib.rs
Normal file
72
src/lib.rs
Normal file
@ -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<u16, LinuxI2CError> {
|
||||||
|
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<f64, LinuxI2CError> {
|
||||||
|
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<f64, LinuxI2CError> {
|
||||||
|
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<f64, LinuxI2CError> {
|
||||||
|
let tmp: f64 = self.shunt_voltage(channel)?;
|
||||||
|
let res: f64 = tmp * self.shunt_resistor;
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user