diff options
| -rw-r--r-- | embassy-embedded-hal/src/adapter.rs | 7 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/lib.rs | 17 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/asynch/i2c.rs | 8 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/asynch/spi.rs | 8 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 8 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 8 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/mod.rs | 5 |
7 files changed, 59 insertions, 2 deletions
diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter.rs index 7d25d89fc..1c43f015f 100644 --- a/embassy-embedded-hal/src/adapter.rs +++ b/embassy-embedded-hal/src/adapter.rs | |||
| @@ -1,9 +1,12 @@ | |||
| 1 | //! Adapters between embedded-hal traits. | ||
| 2 | |||
| 1 | use core::future::Future; | 3 | use core::future::Future; |
| 2 | 4 | ||
| 3 | use embedded_hal_02::{blocking, serial}; | 5 | use embedded_hal_02::{blocking, serial}; |
| 4 | 6 | ||
| 5 | /// BlockingAsync is a wrapper that implements async traits using blocking peripherals. This allows | 7 | /// Wrapper that implements async traits using blocking implementations. |
| 6 | /// driver writers to depend on the async traits while still supporting embedded-hal peripheral implementations. | 8 | /// |
| 9 | /// This allows driver writers to depend on the async traits while still supporting embedded-hal peripheral implementations. | ||
| 7 | /// | 10 | /// |
| 8 | /// BlockingAsync will implement any async trait that maps to embedded-hal traits implemented for the wrapped driver. | 11 | /// BlockingAsync will implement any async trait that maps to embedded-hal traits implemented for the wrapped driver. |
| 9 | /// | 12 | /// |
diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index d77c2d635..0c6f2786a 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs | |||
| @@ -1,12 +1,29 @@ | |||
| 1 | #![cfg_attr(not(feature = "std"), no_std)] | 1 | #![cfg_attr(not(feature = "std"), no_std)] |
| 2 | #![cfg_attr(feature = "nightly", feature(generic_associated_types, type_alias_impl_trait))] | 2 | #![cfg_attr(feature = "nightly", feature(generic_associated_types, type_alias_impl_trait))] |
| 3 | #![warn(missing_docs)] | ||
| 4 | |||
| 5 | //! Utilities to use `embedded-hal` traits with Embassy. | ||
| 3 | 6 | ||
| 4 | #[cfg(feature = "nightly")] | 7 | #[cfg(feature = "nightly")] |
| 5 | pub mod adapter; | 8 | pub mod adapter; |
| 6 | 9 | ||
| 7 | pub mod shared_bus; | 10 | pub mod shared_bus; |
| 8 | 11 | ||
| 12 | /// Set the configuration of a peripheral driver. | ||
| 13 | /// | ||
| 14 | /// This trait is intended to be implemented by peripheral drivers such as SPI | ||
| 15 | /// and I2C. It allows changing the configuration at runtime. | ||
| 16 | /// | ||
| 17 | /// The exact type of the "configuration" is defined by each individual driver, since different | ||
| 18 | /// drivers support different options. Therefore it is defined as an associated type. | ||
| 19 | /// | ||
| 20 | /// For example, it is used by [`SpiDeviceWithConfig`](crate::shared_bus::asynch::spi::SpiDeviceWithConfig) and | ||
| 21 | /// [`I2cDeviceWithConfig`](crate::shared_bus::asynch::i2c::I2cDeviceWithConfig) to allow different | ||
| 22 | /// devices on the same bus to use different communication settings. | ||
| 9 | pub trait SetConfig { | 23 | pub trait SetConfig { |
| 24 | /// The configuration type used by this driver. | ||
| 10 | type Config; | 25 | type Config; |
| 26 | |||
| 27 | /// Set the configuration of the driver. | ||
| 11 | fn set_config(&mut self, config: &Self::Config); | 28 | fn set_config(&mut self, config: &Self::Config); |
| 12 | } | 29 | } |
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs index 5bd3d9bc5..fa77a06d3 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs | |||
| @@ -31,11 +31,13 @@ use embedded_hal_async::i2c; | |||
| 31 | use crate::shared_bus::I2cDeviceError; | 31 | use crate::shared_bus::I2cDeviceError; |
| 32 | use crate::SetConfig; | 32 | use crate::SetConfig; |
| 33 | 33 | ||
| 34 | /// I2C device on a shared bus. | ||
| 34 | pub struct I2cDevice<'a, M: RawMutex, BUS> { | 35 | pub struct I2cDevice<'a, M: RawMutex, BUS> { |
| 35 | bus: &'a Mutex<M, BUS>, | 36 | bus: &'a Mutex<M, BUS>, |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | impl<'a, M: RawMutex, BUS> I2cDevice<'a, M, BUS> { | 39 | impl<'a, M: RawMutex, BUS> I2cDevice<'a, M, BUS> { |
| 40 | /// Create a new `I2cDevice`. | ||
| 39 | pub fn new(bus: &'a Mutex<M, BUS>) -> Self { | 41 | pub fn new(bus: &'a Mutex<M, BUS>) -> Self { |
| 40 | Self { bus } | 42 | Self { bus } |
| 41 | } | 43 | } |
| @@ -103,12 +105,18 @@ where | |||
| 103 | } | 105 | } |
| 104 | } | 106 | } |
| 105 | 107 | ||
| 108 | /// I2C device on a shared bus, with its own configuration. | ||
| 109 | /// | ||
| 110 | /// This is like [`I2cDevice`], with an additional bus configuration that's applied | ||
| 111 | /// to the bus before each use using [`SetConfig`]. This allows different | ||
| 112 | /// devices on the same bus to use different communication settings. | ||
| 106 | pub struct I2cDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> { | 113 | pub struct I2cDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> { |
| 107 | bus: &'a Mutex<M, BUS>, | 114 | bus: &'a Mutex<M, BUS>, |
| 108 | config: BUS::Config, | 115 | config: BUS::Config, |
| 109 | } | 116 | } |
| 110 | 117 | ||
| 111 | impl<'a, M: RawMutex, BUS: SetConfig> I2cDeviceWithConfig<'a, M, BUS> { | 118 | impl<'a, M: RawMutex, BUS: SetConfig> I2cDeviceWithConfig<'a, M, BUS> { |
| 119 | /// Create a new `I2cDeviceWithConfig`. | ||
| 112 | pub fn new(bus: &'a Mutex<M, BUS>, config: BUS::Config) -> Self { | 120 | pub fn new(bus: &'a Mutex<M, BUS>, config: BUS::Config) -> Self { |
| 113 | Self { bus, config } | 121 | Self { bus, config } |
| 114 | } | 122 | } |
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs index 343184d12..a08eaa82d 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs | |||
| @@ -36,12 +36,14 @@ use embedded_hal_async::spi; | |||
| 36 | use crate::shared_bus::SpiDeviceError; | 36 | use crate::shared_bus::SpiDeviceError; |
| 37 | use crate::SetConfig; | 37 | use crate::SetConfig; |
| 38 | 38 | ||
| 39 | /// SPI device on a shared bus. | ||
| 39 | pub struct SpiDevice<'a, M: RawMutex, BUS, CS> { | 40 | pub struct SpiDevice<'a, M: RawMutex, BUS, CS> { |
| 40 | bus: &'a Mutex<M, BUS>, | 41 | bus: &'a Mutex<M, BUS>, |
| 41 | cs: CS, | 42 | cs: CS, |
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | impl<'a, M: RawMutex, BUS, CS> SpiDevice<'a, M, BUS, CS> { | 45 | impl<'a, M: RawMutex, BUS, CS> SpiDevice<'a, M, BUS, CS> { |
| 46 | /// Create a new `SpiDevice`. | ||
| 45 | pub fn new(bus: &'a Mutex<M, BUS>, cs: CS) -> Self { | 47 | pub fn new(bus: &'a Mutex<M, BUS>, cs: CS) -> Self { |
| 46 | Self { bus, cs } | 48 | Self { bus, cs } |
| 47 | } | 49 | } |
| @@ -93,6 +95,11 @@ where | |||
| 93 | } | 95 | } |
| 94 | } | 96 | } |
| 95 | 97 | ||
| 98 | /// SPI device on a shared bus, with its own configuration. | ||
| 99 | /// | ||
| 100 | /// This is like [`SpiDevice`], with an additional bus configuration that's applied | ||
| 101 | /// to the bus before each use using [`SetConfig`]. This allows different | ||
| 102 | /// devices on the same bus to use different communication settings. | ||
| 96 | pub struct SpiDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> { | 103 | pub struct SpiDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> { |
| 97 | bus: &'a Mutex<M, BUS>, | 104 | bus: &'a Mutex<M, BUS>, |
| 98 | cs: CS, | 105 | cs: CS, |
| @@ -100,6 +107,7 @@ pub struct SpiDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> { | |||
| 100 | } | 107 | } |
| 101 | 108 | ||
| 102 | impl<'a, M: RawMutex, BUS: SetConfig, CS> SpiDeviceWithConfig<'a, M, BUS, CS> { | 109 | impl<'a, M: RawMutex, BUS: SetConfig, CS> SpiDeviceWithConfig<'a, M, BUS, CS> { |
| 110 | /// Create a new `SpiDeviceWithConfig`. | ||
| 103 | pub fn new(bus: &'a Mutex<M, BUS>, cs: CS, config: BUS::Config) -> Self { | 111 | pub fn new(bus: &'a Mutex<M, BUS>, cs: CS, config: BUS::Config) -> Self { |
| 104 | Self { bus, cs, config } | 112 | Self { bus, cs, config } |
| 105 | } | 113 | } |
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 1ec480c0c..c8b5e30f6 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | |||
| @@ -26,11 +26,13 @@ use embedded_hal_1::i2c::ErrorType; | |||
| 26 | use crate::shared_bus::I2cDeviceError; | 26 | use crate::shared_bus::I2cDeviceError; |
| 27 | use crate::SetConfig; | 27 | use crate::SetConfig; |
| 28 | 28 | ||
| 29 | /// I2C device on a shared bus. | ||
| 29 | pub struct I2cDevice<'a, M: RawMutex, BUS> { | 30 | pub struct I2cDevice<'a, M: RawMutex, BUS> { |
| 30 | bus: &'a Mutex<M, RefCell<BUS>>, | 31 | bus: &'a Mutex<M, RefCell<BUS>>, |
| 31 | } | 32 | } |
| 32 | 33 | ||
| 33 | impl<'a, M: RawMutex, BUS> I2cDevice<'a, M, BUS> { | 34 | impl<'a, M: RawMutex, BUS> I2cDevice<'a, M, BUS> { |
| 35 | /// Create a new `I2cDevice`. | ||
| 34 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>) -> Self { | 36 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>) -> Self { |
| 35 | Self { bus } | 37 | Self { bus } |
| 36 | } | 38 | } |
| @@ -143,12 +145,18 @@ where | |||
| 143 | } | 145 | } |
| 144 | } | 146 | } |
| 145 | 147 | ||
| 148 | /// I2C device on a shared bus, with its own configuration. | ||
| 149 | /// | ||
| 150 | /// This is like [`I2cDevice`], with an additional bus configuration that's applied | ||
| 151 | /// to the bus before each use using [`SetConfig`]. This allows different | ||
| 152 | /// devices on the same bus to use different communication settings. | ||
| 146 | pub struct I2cDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> { | 153 | pub struct I2cDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> { |
| 147 | bus: &'a Mutex<M, RefCell<BUS>>, | 154 | bus: &'a Mutex<M, RefCell<BUS>>, |
| 148 | config: BUS::Config, | 155 | config: BUS::Config, |
| 149 | } | 156 | } |
| 150 | 157 | ||
| 151 | impl<'a, M: RawMutex, BUS: SetConfig> I2cDeviceWithConfig<'a, M, BUS> { | 158 | impl<'a, M: RawMutex, BUS: SetConfig> I2cDeviceWithConfig<'a, M, BUS> { |
| 159 | /// Create a new `I2cDeviceWithConfig`. | ||
| 152 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: BUS::Config) -> Self { | 160 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: BUS::Config) -> Self { |
| 153 | Self { bus, config } | 161 | Self { bus, config } |
| 154 | } | 162 | } |
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index ff92e1a7e..d0648f59a 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs | |||
| @@ -29,12 +29,14 @@ use embedded_hal_1::spi::blocking::SpiBusFlush; | |||
| 29 | use crate::shared_bus::SpiDeviceError; | 29 | use crate::shared_bus::SpiDeviceError; |
| 30 | use crate::SetConfig; | 30 | use crate::SetConfig; |
| 31 | 31 | ||
| 32 | /// SPI device on a shared bus. | ||
| 32 | pub struct SpiDevice<'a, M: RawMutex, BUS, CS> { | 33 | pub struct SpiDevice<'a, M: RawMutex, BUS, CS> { |
| 33 | bus: &'a Mutex<M, RefCell<BUS>>, | 34 | bus: &'a Mutex<M, RefCell<BUS>>, |
| 34 | cs: CS, | 35 | cs: CS, |
| 35 | } | 36 | } |
| 36 | 37 | ||
| 37 | impl<'a, M: RawMutex, BUS, CS> SpiDevice<'a, M, BUS, CS> { | 38 | impl<'a, M: RawMutex, BUS, CS> SpiDevice<'a, M, BUS, CS> { |
| 39 | /// Create a new `SpiDevice`. | ||
| 38 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, cs: CS) -> Self { | 40 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, cs: CS) -> Self { |
| 39 | Self { bus, cs } | 41 | Self { bus, cs } |
| 40 | } | 42 | } |
| @@ -117,6 +119,11 @@ where | |||
| 117 | } | 119 | } |
| 118 | } | 120 | } |
| 119 | 121 | ||
| 122 | /// SPI device on a shared bus, with its own configuration. | ||
| 123 | /// | ||
| 124 | /// This is like [`SpiDevice`], with an additional bus configuration that's applied | ||
| 125 | /// to the bus before each use using [`SetConfig`]. This allows different | ||
| 126 | /// devices on the same bus to use different communication settings. | ||
| 120 | pub struct SpiDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> { | 127 | pub struct SpiDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> { |
| 121 | bus: &'a Mutex<M, RefCell<BUS>>, | 128 | bus: &'a Mutex<M, RefCell<BUS>>, |
| 122 | cs: CS, | 129 | cs: CS, |
| @@ -124,6 +131,7 @@ pub struct SpiDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> { | |||
| 124 | } | 131 | } |
| 125 | 132 | ||
| 126 | impl<'a, M: RawMutex, BUS: SetConfig, CS> SpiDeviceWithConfig<'a, M, BUS, CS> { | 133 | impl<'a, M: RawMutex, BUS: SetConfig, CS> SpiDeviceWithConfig<'a, M, BUS, CS> { |
| 134 | /// Create a new `SpiDeviceWithConfig`. | ||
| 127 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, cs: CS, config: BUS::Config) -> Self { | 135 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, cs: CS, config: BUS::Config) -> Self { |
| 128 | Self { bus, cs, config } | 136 | Self { bus, cs, config } |
| 129 | } | 137 | } |
diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs index 3cd380f72..76ee413d9 100644 --- a/embassy-embedded-hal/src/shared_bus/mod.rs +++ b/embassy-embedded-hal/src/shared_bus/mod.rs | |||
| @@ -8,8 +8,10 @@ pub mod asynch; | |||
| 8 | 8 | ||
| 9 | pub mod blocking; | 9 | pub mod blocking; |
| 10 | 10 | ||
| 11 | /// Error returned by I2C device implementations in this crate. | ||
| 11 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | 12 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 12 | pub enum I2cDeviceError<BUS> { | 13 | pub enum I2cDeviceError<BUS> { |
| 14 | /// An operation on the inner I2C bus failed. | ||
| 13 | I2c(BUS), | 15 | I2c(BUS), |
| 14 | } | 16 | } |
| 15 | 17 | ||
| @@ -24,9 +26,12 @@ where | |||
| 24 | } | 26 | } |
| 25 | } | 27 | } |
| 26 | 28 | ||
| 29 | /// Error returned by SPI device implementations in this crate. | ||
| 27 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | 30 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 28 | pub enum SpiDeviceError<BUS, CS> { | 31 | pub enum SpiDeviceError<BUS, CS> { |
| 32 | /// An operation on the inner SPI bus failed. | ||
| 29 | Spi(BUS), | 33 | Spi(BUS), |
| 34 | /// Setting the value of the Chip Select (CS) pin failed. | ||
| 30 | Cs(CS), | 35 | Cs(CS), |
| 31 | } | 36 | } |
| 32 | 37 | ||
