Skip to content

Commit 0d7a4ad

Browse files
bors[bot]eldruin
andauthored
26: Transactional WriteRead r=ryankurte a=eldruin This fixes rust-embedded#25 by implementing a transactional write-read without STOP in the middle. It also fixes a type cast which was the only warning present. Co-authored-by: Diego Barrios Romero <eldruin@gmail.com>
2 parents 55f7f90 + 7cd65d6 commit 0d7a4ad

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Do write and read in one transaction in WriteRead implementation.
13+
14+
### Changed
15+
16+
- updated to i2cdev 0.4.3 (necessary for trasactional write-read).
17+
1018
## [v0.2.2] - 2018-12-21
1119

1220
### Changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ version = "0.2.2"
1010

1111
[dependencies]
1212
embedded-hal = { version = "0.2.0", features = ["unproven"] }
13-
i2cdev = "0.4"
13+
i2cdev = "0.4.3"
1414
spidev = "0.4"
1515
sysfs_gpio = "0.5"
1616
serial-unix = "0.4.0"

src/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use std::time::Duration;
2727
use std::{ops, thread};
2828

2929
use cast::{u32, u64};
30-
use i2cdev::core::I2CDevice;
30+
use i2cdev::core::{I2CDevice, I2CMessage, I2CTransfer};
31+
use i2cdev::linux::LinuxI2CMessage;
3132
use spidev::SpidevTransfer;
3233

3334
mod serial;
@@ -184,7 +185,7 @@ impl I2cdev {
184185

185186
fn set_address(&mut self, address: u8) -> Result<(), i2cdev::linux::LinuxI2CError> {
186187
if self.address != Some(address) {
187-
self.inner = i2cdev::linux::LinuxI2CDevice::new(&self.path, address as u16)?;
188+
self.inner = i2cdev::linux::LinuxI2CDevice::new(&self.path, u16::from(address))?;
188189
self.address = Some(address);
189190
}
190191
Ok(())
@@ -219,8 +220,11 @@ impl hal::blocking::i2c::WriteRead for I2cdev {
219220
buffer: &mut [u8],
220221
) -> Result<(), Self::Error> {
221222
self.set_address(address)?;
222-
self.inner.write(bytes)?;
223-
self.inner.read(buffer)
223+
let mut messages = [
224+
LinuxI2CMessage::write(bytes),
225+
LinuxI2CMessage::read(buffer),
226+
];
227+
self.inner.transfer(&mut messages).map(drop)
224228
}
225229
}
226230

0 commit comments

Comments
 (0)