Skip to content

Add I2C exclusive device implementation #401

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion embedded-hal-bus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

...
### Added
- `ExclusiveDevice` implementation for I2C.

## [v0.1.0-alpha.0] - 2022-08-17

Expand Down
3 changes: 2 additions & 1 deletion embedded-hal-bus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ repository = "https://github.com/rust-embedded/embedded-hal"
version = "0.1.0-alpha.0"

[dependencies]
embedded-hal = { version = "=1.0.0-alpha.8", path = ".." }
#embedded-hal = { version = "=1.0.0-alpha.8", path = ".." }
embedded-hal = {git = "http://github.com/Dirbaio/embedded-hal", branch="i2c-bus-device"}
57 changes: 57 additions & 0 deletions embedded-hal-bus/src/i2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! I2C bus sharing mechanisms.

/// I2C bus sharing with blocking traits
pub mod blocking {
use embedded_hal::i2c::{
blocking::{I2cBus, I2cDevice},
ErrorType,
};

/// [`I2cDevice`] implementation with exclusive access to the bus (not shared).
///
/// This is the most straightforward way of obtaining an [`I2cDevice`] from an [`I2cBus`],
/// ideal for when no sharing is required (only one I2C device is present on the bus).
#[derive(Debug)]
pub struct ExclusiveDevice<BUS> {
bus: BUS,
}

impl<BUS> ExclusiveDevice<BUS> {
/// Create a new `ExclusiveDevice`
pub fn new(bus: BUS) -> Self {
Self { bus }
}
}

impl<BUS> ErrorType for ExclusiveDevice<BUS>
where
BUS: ErrorType,
{
type Error = BUS::Error;
}

impl<BUS> I2cDevice for ExclusiveDevice<BUS>
where
BUS: I2cBus,
{
type Bus = BUS;

fn transaction<R>(
&mut self,
f: impl FnOnce(&mut Self::Bus) -> Result<R, <Self::Bus as ErrorType>::Error>,
) -> Result<R, Self::Error> {
let f_res = f(&mut self.bus);

// On failure, it's important to still stop and flush.
let stop_res = self.bus.stop();
let flush_res = self.bus.flush();

// Note that in the case of multiple failures, only one error
// will be returned, in the following order.
let f_ok = f_res?;
stop_res?;
flush_res?;
Ok(f_ok)
}
}
}
1 change: 1 addition & 0 deletions embedded-hal-bus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
#![warn(missing_docs)]
#![no_std]

pub mod i2c;
pub mod spi;