From ef24faf2df594d0eca1542ac02834af6aafa3853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sun, 10 Jul 2022 00:05:57 +0200 Subject: [PATCH] Add asynch mod to shared_bus --- embassy-embedded-hal/src/lib.rs | 2 ++ .../src/shared_bus/{ => asynch}/i2c.rs | 25 +++++-------- .../src/shared_bus/asynch/mod.rs | 3 ++ .../src/shared_bus/{ => asynch}/spi.rs | 36 ++++++++----------- .../src/shared_bus/blocking/i2c.rs | 2 +- .../src/shared_bus/blocking/spi.rs | 2 +- embassy-embedded-hal/src/shared_bus/mod.rs | 18 +++++++--- 7 files changed, 44 insertions(+), 44 deletions(-) rename embassy-embedded-hal/src/shared_bus/{ => asynch}/i2c.rs (97%) create mode 100644 embassy-embedded-hal/src/shared_bus/asynch/mod.rs rename embassy-embedded-hal/src/shared_bus/{ => asynch}/spi.rs (96%) diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index 688d0b48d..f57d83b5c 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs @@ -2,7 +2,9 @@ #![feature(generic_associated_types)] #![feature(type_alias_impl_trait)] +#[cfg(feature = "nightly")] pub mod adapter; + pub mod shared_bus; pub trait SetConfig { diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs similarity index 97% rename from embassy-embedded-hal/src/shared_bus/i2c.rs rename to embassy-embedded-hal/src/shared_bus/asynch/i2c.rs index f63190e6a..408317490 100644 --- a/embassy-embedded-hal/src/shared_bus/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs @@ -27,14 +27,19 @@ use core::future::Future; use embassy::blocking_mutex::raw::RawMutex; use embassy::mutex::Mutex; -#[cfg(feature = "nightly")] use embedded_hal_async::i2c; +use crate::shared_bus::I2cBusDeviceError; use crate::SetConfig; -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -pub enum I2cBusDeviceError { - I2c(BUS), +pub struct I2cBusDevice<'a, M: RawMutex, BUS> { + bus: &'a Mutex, +} + +impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { + pub fn new(bus: &'a Mutex) -> Self { + Self { bus } + } } impl i2c::Error for I2cBusDeviceError @@ -48,16 +53,6 @@ where } } -pub struct I2cBusDevice<'a, M: RawMutex, BUS> { - bus: &'a Mutex, -} - -impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { - pub fn new(bus: &'a Mutex) -> Self { - Self { bus } - } -} - impl<'a, M: RawMutex, BUS> i2c::ErrorType for I2cBusDevice<'a, M, BUS> where BUS: i2c::ErrorType, @@ -65,7 +60,6 @@ where type Error = I2cBusDeviceError; } -#[cfg(feature = "nightly")] impl i2c::I2c for I2cBusDevice<'_, M, BUS> where M: RawMutex + 'static, @@ -141,7 +135,6 @@ where type Error = I2cBusDeviceError; } -#[cfg(feature = "nightly")] impl i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS> where M: RawMutex + 'static, diff --git a/embassy-embedded-hal/src/shared_bus/asynch/mod.rs b/embassy-embedded-hal/src/shared_bus/asynch/mod.rs new file mode 100644 index 000000000..2e660b724 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/asynch/mod.rs @@ -0,0 +1,3 @@ +//! Asynchronous shared bus implementations for embedded-hal-async +pub mod i2c; +pub mod spi; diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs similarity index 96% rename from embassy-embedded-hal/src/shared_bus/spi.rs rename to embassy-embedded-hal/src/shared_bus/asynch/spi.rs index 136352e0a..f3795bb19 100644 --- a/embassy-embedded-hal/src/shared_bus/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs @@ -32,30 +32,11 @@ use embassy::blocking_mutex::raw::RawMutex; use embassy::mutex::Mutex; use embedded_hal_1::digital::blocking::OutputPin; use embedded_hal_1::spi::ErrorType; -#[cfg(feature = "nightly")] use embedded_hal_async::spi; +use crate::shared_bus::SpiBusDeviceError; use crate::SetConfig; -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -pub enum SpiBusDeviceError { - Spi(BUS), - Cs(CS), -} - -impl spi::Error for SpiBusDeviceError -where - BUS: spi::Error + Debug, - CS: Debug, -{ - fn kind(&self) -> spi::ErrorKind { - match self { - Self::Spi(e) => e.kind(), - Self::Cs(_) => spi::ErrorKind::Other, - } - } -} - pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { bus: &'a Mutex, cs: CS, @@ -75,7 +56,19 @@ where type Error = SpiBusDeviceError; } -#[cfg(feature = "nightly")] +impl spi::Error for SpiBusDeviceError +where + BUS: spi::Error + Debug, + CS: Debug, +{ + fn kind(&self) -> spi::ErrorKind { + match self { + Self::Spi(e) => e.kind(), + Self::Cs(_) => spi::ErrorKind::Other, + } + } +} + impl spi::SpiDevice for SpiBusDevice<'_, M, BUS, CS> where M: RawMutex + 'static, @@ -135,7 +128,6 @@ where type Error = SpiBusDeviceError; } -#[cfg(feature = "nightly")] impl spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS> where M: RawMutex + 'static, diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 0c8338c73..ac361a786 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -23,7 +23,7 @@ use embassy::blocking_mutex::Mutex; use embedded_hal_1::i2c::blocking::{I2c, Operation}; use embedded_hal_1::i2c::ErrorType; -use crate::shared_bus::i2c::I2cBusDeviceError; +use crate::shared_bus::I2cBusDeviceError; use crate::SetConfig; pub struct I2cBusDevice<'a, M: RawMutex, BUS> { diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index 456da8859..704858cdb 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -26,7 +26,7 @@ use embedded_hal_1::digital::blocking::OutputPin; use embedded_hal_1::spi; use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; -use crate::shared_bus::spi::SpiBusDeviceError; +use crate::shared_bus::SpiBusDeviceError; use crate::SetConfig; pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs index cd748cac8..2309a6c35 100644 --- a/embassy-embedded-hal/src/shared_bus/mod.rs +++ b/embassy-embedded-hal/src/shared_bus/mod.rs @@ -1,6 +1,16 @@ //! Shared bus implementations +#[cfg(feature = "nightly")] +pub mod asynch; + pub mod blocking; -/// Shared i2c bus implementation for embedded-hal-async -pub mod i2c; -/// Shared SPI bus implementation for embedded-hal-async -pub mod spi; + +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +pub enum I2cBusDeviceError { + I2c(BUS), +} + +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +pub enum SpiBusDeviceError { + Spi(BUS), + Cs(CS), +}