Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement transactional I2C interface and add example #44

Merged
merged 1 commit into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ gpio_cdev = ["gpio-cdev"]
default = [ "gpio_cdev", "gpio_sysfs" ]

[dependencies]
embedded-hal = "=1.0.0-alpha.3"
embedded-hal = "=1.0.0-alpha.4"
gpio-cdev = { version = "0.3", optional = true }
sysfs_gpio = { version = "0.5", optional = true }

Expand Down
35 changes: 35 additions & 0 deletions examples/transactional-i2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
extern crate embedded_hal;
extern crate linux_embedded_hal;
use embedded_hal::blocking::i2c::{Operation as I2cOperation, Transactional};
use linux_embedded_hal::I2cdev;

const ADDR: u8 = 0x12;

struct Driver<I2C> {
i2c: I2C,
}

impl<I2C> Driver<I2C>
where
I2C: Transactional,
{
pub fn new(i2c: I2C) -> Self {
Driver { i2c }
}

fn read_something(&mut self) -> Result<u8, I2C::Error> {
let mut read_buffer = [0];
let mut ops = [
I2cOperation::Write(&[0xAB]),
I2cOperation::Read(&mut read_buffer),
];
self.i2c.try_exec(ADDR, &mut ops).and(Ok(read_buffer[0]))
}
}

fn main() {
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut driver = Driver::new(dev);
let value = driver.read_something().unwrap();
println!("Read value: {}", value);
}
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use std::time::Duration;
use std::{ops, thread};

use cast::{u32, u64};
use hal::blocking::i2c::Operation as I2cOperation;
use i2cdev::core::{I2CDevice, I2CMessage, I2CTransfer};
use i2cdev::linux::LinuxI2CMessage;
use spidev::SpidevTransfer;
Expand Down Expand Up @@ -207,6 +208,26 @@ impl hal::blocking::i2c::WriteRead for I2cdev {
}
}

impl hal::blocking::i2c::Transactional for I2cdev {
type Error = i2cdev::linux::LinuxI2CError;

fn try_exec(&mut self, address: u8, operations: &mut [I2cOperation]) -> Result<(), Self::Error>
{
// Map operations from generic to linux objects
let mut messages: Vec<_> = operations
.as_mut()
.iter_mut()
.map(|a| match a {
I2cOperation::Write(w) => LinuxI2CMessage::write(w),
I2cOperation::Read(r) => LinuxI2CMessage::read(r),
})
.collect();

self.set_address(address)?;
self.inner.transfer(&mut messages).map(drop)
}
}

impl ops::Deref for I2cdev {
type Target = i2cdev::linux::LinuxI2CDevice;

Expand Down