Skip to content

Commit

Permalink
Merge branch 'master' into feature/spi-transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankurte authored Nov 15, 2020
2 parents 687ab98 + dfc0434 commit 909db99
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Set default features to build both sysfs and cdev pin types
- Removed `Pin` export, use `CdevPin` or `SysfsPin`
- Adapted to `embedded-hal` `1.0.0-alpha.3` release.
- Updated `nb` to version `1`.

## [v0.3.0] - 2019-11-25

Expand Down Expand Up @@ -41,7 +43,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- implementation of the unproven `embedded_hal::::digital::InputPin` trait.
- implementation of the unproven `embedded_hal::::digital::InputPin` trait.

## [v0.2.0] - 2018-05-14

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ 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 }

i2cdev = "0.4.3"
nb = "0.1.1"
nb = "1"
serial-core = "0.4.0"
serial-unix = "0.4.0"
spidev = "0.4"
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 @@ -33,6 +33,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 @@ -209,6 +210,26 @@ impl embedded_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

0 comments on commit 909db99

Please sign in to comment.