Skip to content

Transactional WriteRead #26

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

Merged
merged 4 commits into from
Dec 3, 2019
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- Do write and read in one transaction in WriteRead implementation.

### Changed

- updated to i2cdev 0.4.3 (necessary for trasactional write-read).

## [v0.2.2] - 2018-12-21

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ version = "0.2.2"

[dependencies]
embedded-hal = { version = "0.2.0", features = ["unproven"] }
i2cdev = "0.4"
i2cdev = "0.4.3"
spidev = "0.4"
sysfs_gpio = "0.5"
serial-unix = "0.4.0"
Expand Down
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use std::time::Duration;
use std::{ops, thread};

use cast::{u32, u64};
use i2cdev::core::I2CDevice;
use i2cdev::core::{I2CDevice, I2CMessage, I2CTransfer};
use i2cdev::linux::LinuxI2CMessage;
use spidev::SpidevTransfer;

mod serial;
Expand Down Expand Up @@ -184,7 +185,7 @@ impl I2cdev {

fn set_address(&mut self, address: u8) -> Result<(), i2cdev::linux::LinuxI2CError> {
if self.address != Some(address) {
self.inner = i2cdev::linux::LinuxI2CDevice::new(&self.path, address as u16)?;
self.inner = i2cdev::linux::LinuxI2CDevice::new(&self.path, u16::from(address))?;
self.address = Some(address);
}
Ok(())
Expand Down Expand Up @@ -219,8 +220,11 @@ impl hal::blocking::i2c::WriteRead for I2cdev {
buffer: &mut [u8],
) -> Result<(), Self::Error> {
self.set_address(address)?;
self.inner.write(bytes)?;
self.inner.read(buffer)
let mut messages = [
LinuxI2CMessage::write(bytes),
LinuxI2CMessage::read(buffer),
];
self.inner.transfer(&mut messages).map(drop)
}
}

Expand Down