Skip to content
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

Implement i2c proxy for embedded-hal 1.0.0-alpha.8 #36

Merged
merged 1 commit into from
Jun 6, 2022
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ xtensa-lx = { version = "0.6.0", optional = true }
spin = { version = "0.9.2", optional = true }
atomic-polyfill = { version = "0.1.6", optional = true }

embedded-hal-alpha = { package = "embedded-hal", version = "=1.0.0-alpha.8", optional = true }

[dev-dependencies]
embedded-hal-mock = "0.8"

[features]
std = ["once_cell"]
xtensa = ["xtensa-lx", "spin"]
cortex-m = ["dep:cortex-m", "atomic-polyfill"]
eh-alpha = ["embedded-hal-alpha"]
73 changes: 73 additions & 0 deletions src/proxies.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "eh-alpha")]
use embedded_hal_alpha::i2c as i2c_alpha;

use embedded_hal::adc;
use embedded_hal::blocking::i2c;
use embedded_hal::blocking::spi;
Expand Down Expand Up @@ -61,6 +64,76 @@ where
}
}

// Implementations for the embedded_hal alpha

#[cfg(feature = "eh-alpha")]
impl<'a, M: crate::BusMutex> i2c_alpha::ErrorType for I2cProxy<'a, M>
where
M::Bus: i2c_alpha::ErrorType,
{
type Error = <M::Bus as i2c_alpha::ErrorType>::Error;
}

#[cfg(feature = "eh-alpha")]
impl<'a, M: crate::BusMutex> i2c_alpha::blocking::I2c for I2cProxy<'a, M>
where
M::Bus: i2c_alpha::blocking::I2c,
{
fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
self.mutex.lock(|bus| bus.read(address, buffer))
}

fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
self.mutex.lock(|bus| bus.write(address, bytes))
}

fn write_iter<B>(&mut self, address: u8, bytes: B) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
self.mutex.lock(|bus| bus.write_iter(address, bytes))
}

fn write_read(
&mut self,
address: u8,
bytes: &[u8],
buffer: &mut [u8],
) -> Result<(), Self::Error> {
self.mutex
.lock(|bus| bus.write_read(address, bytes, buffer))
}

fn write_iter_read<B>(
&mut self,
address: u8,
bytes: B,
buffer: &mut [u8],
) -> Result<(), Self::Error>
where
B: IntoIterator<Item = u8>,
{
self.mutex
.lock(|bus| bus.write_iter_read(address, bytes, buffer))
}

fn transaction<'b>(
&mut self,
address: u8,
operations: &mut [i2c_alpha::blocking::Operation<'b>],
) -> Result<(), Self::Error> {
self.mutex.lock(|bus| bus.transaction(address, operations))
}

fn transaction_iter<'b, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error>
where
O: IntoIterator<Item = i2c_alpha::blocking::Operation<'b>>,
{
self.mutex
.lock(|bus| bus.transaction_iter(address, operations))
}
}

/// Proxy type for SPI bus sharing.
///
/// The `SpiProxy` implements all (blocking) SPI traits so it can be passed to drivers instead of
Expand Down