Skip to content

Commit 909db99

Browse files
authored
Merge branch 'master' into feature/spi-transactions
2 parents 687ab98 + dfc0434 commit 909db99

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111

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

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

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

4244
### Added
4345

44-
- implementation of the unproven `embedded_hal::::digital::InputPin` trait.
46+
- implementation of the unproven `embedded_hal::::digital::InputPin` trait.
4547

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ gpio_cdev = ["gpio-cdev"]
1919
default = [ "gpio_cdev", "gpio_sysfs" ]
2020

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

2626
i2cdev = "0.4.3"
27-
nb = "0.1.1"
27+
nb = "1"
2828
serial-core = "0.4.0"
2929
serial-unix = "0.4.0"
3030
spidev = "0.4"

examples/transactional-i2c.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
extern crate embedded_hal;
2+
extern crate linux_embedded_hal;
3+
use embedded_hal::blocking::i2c::{Operation as I2cOperation, Transactional};
4+
use linux_embedded_hal::I2cdev;
5+
6+
const ADDR: u8 = 0x12;
7+
8+
struct Driver<I2C> {
9+
i2c: I2C,
10+
}
11+
12+
impl<I2C> Driver<I2C>
13+
where
14+
I2C: Transactional,
15+
{
16+
pub fn new(i2c: I2C) -> Self {
17+
Driver { i2c }
18+
}
19+
20+
fn read_something(&mut self) -> Result<u8, I2C::Error> {
21+
let mut read_buffer = [0];
22+
let mut ops = [
23+
I2cOperation::Write(&[0xAB]),
24+
I2cOperation::Read(&mut read_buffer),
25+
];
26+
self.i2c.try_exec(ADDR, &mut ops).and(Ok(read_buffer[0]))
27+
}
28+
}
29+
30+
fn main() {
31+
let dev = I2cdev::new("/dev/i2c-1").unwrap();
32+
let mut driver = Driver::new(dev);
33+
let value = driver.read_something().unwrap();
34+
println!("Read value: {}", value);
35+
}

src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use std::time::Duration;
3333
use std::{ops, thread};
3434

3535
use cast::{u32, u64};
36+
use hal::blocking::i2c::Operation as I2cOperation;
3637
use i2cdev::core::{I2CDevice, I2CMessage, I2CTransfer};
3738
use i2cdev::linux::LinuxI2CMessage;
3839
use spidev::SpidevTransfer;
@@ -209,6 +210,26 @@ impl embedded_hal::blocking::i2c::WriteRead for I2cdev {
209210
}
210211
}
211212

213+
impl hal::blocking::i2c::Transactional for I2cdev {
214+
type Error = i2cdev::linux::LinuxI2CError;
215+
216+
fn try_exec(&mut self, address: u8, operations: &mut [I2cOperation]) -> Result<(), Self::Error>
217+
{
218+
// Map operations from generic to linux objects
219+
let mut messages: Vec<_> = operations
220+
.as_mut()
221+
.iter_mut()
222+
.map(|a| match a {
223+
I2cOperation::Write(w) => LinuxI2CMessage::write(w),
224+
I2cOperation::Read(r) => LinuxI2CMessage::read(r),
225+
})
226+
.collect();
227+
228+
self.set_address(address)?;
229+
self.inner.transfer(&mut messages).map(drop)
230+
}
231+
}
232+
212233
impl ops::Deref for I2cdev {
213234
type Target = i2cdev::linux::LinuxI2CDevice;
214235

0 commit comments

Comments
 (0)