-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
44: Implement transactional I2C interface and add example r=ryankurte a=eldruin I implemented the transactional I2C interface from rust-embedded/embedded-hal#223 and added an example with a driver which does a combined Write/Read transaction. This is based on previous work from #33 by @ryankurte, rust-embedded/rust-i2cdev#51 and is similar to #35. Co-authored-by: Diego Barrios Romero <[email protected]>
- Loading branch information
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters