From 5ad34404af9b7cfecfed5075d987c4c03ff2ca27 Mon Sep 17 00:00:00 2001 From: xoviat Date: Sun, 1 Oct 2023 09:37:20 -0500 Subject: [PATCH] eh: update set_config and add get_config --- embassy-embedded-hal/src/lib.rs | 14 +++++++++++++- embassy-embedded-hal/src/shared_bus/asynch/i2c.rs | 8 ++++---- embassy-embedded-hal/src/shared_bus/asynch/spi.rs | 2 +- .../src/shared_bus/blocking/i2c.rs | 6 +++--- .../src/shared_bus/blocking/spi.rs | 2 +- embassy-embedded-hal/src/shared_bus/mod.rs | 6 ++++++ 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index 3aad838bd..8872b3d61 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs @@ -26,6 +26,18 @@ pub trait SetConfig { /// The configuration type used by this driver. type Config; + /// The error type that can occur if `set_config` fails. + type ConfigError; + /// Set the configuration of the driver. - fn set_config(&mut self, config: &Self::Config); + fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError>; +} + +/// Get the configuration of a peripheral driver. +pub trait GetConfig { + /// The configuration type used by this driver. + type Config; + + /// Get the configuration of the driver. + fn get_config(&self) -> Self::Config; } diff --git a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs index 87e8a4304..1053d3849 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs @@ -125,14 +125,14 @@ where { async fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), I2cDeviceError> { let mut bus = self.bus.lock().await; - bus.set_config(&self.config); + bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?; Ok(()) } async fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), I2cDeviceError> { let mut bus = self.bus.lock().await; - bus.set_config(&self.config); + bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?; Ok(()) } @@ -144,7 +144,7 @@ where rd_buffer: &mut [u8], ) -> Result<(), I2cDeviceError> { let mut bus = self.bus.lock().await; - bus.set_config(&self.config); + bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; bus.write_read(address, wr_buffer, rd_buffer) .await .map_err(I2cDeviceError::I2c)?; @@ -153,7 +153,7 @@ where async fn transaction(&mut self, address: u8, operations: &mut [i2c::Operation<'_>]) -> Result<(), Self::Error> { let mut bus = self.bus.lock().await; - bus.set_config(&self.config); + bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; bus.transaction(address, operations) .await .map_err(I2cDeviceError::I2c)?; diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs index 030392183..b2a9f1e33 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs @@ -130,7 +130,7 @@ where { async fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> { let mut bus = self.bus.lock().await; - bus.set_config(&self.config); + bus.set_config(&self.config).map_err(|_| SpiDeviceError::Config)?; self.cs.set_low().map_err(SpiDeviceError::Cs)?; let op_res: Result<(), BUS::Error> = try { diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index af73df059..233c9e1fd 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -148,7 +148,7 @@ where fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { self.bus.lock(|bus| { let mut bus = bus.borrow_mut(); - bus.set_config(&self.config); + bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; bus.read(address, buffer).map_err(I2cDeviceError::I2c) }) } @@ -156,7 +156,7 @@ where fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { self.bus.lock(|bus| { let mut bus = bus.borrow_mut(); - bus.set_config(&self.config); + bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; bus.write(address, bytes).map_err(I2cDeviceError::I2c) }) } @@ -164,7 +164,7 @@ where fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { self.bus.lock(|bus| { let mut bus = bus.borrow_mut(); - bus.set_config(&self.config); + bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; bus.write_read(address, wr_buffer, rd_buffer) .map_err(I2cDeviceError::I2c) }) diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index 6d03d6263..feb0f5b7d 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -163,7 +163,7 @@ where fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> { self.bus.lock(|bus| { let mut bus = bus.borrow_mut(); - bus.set_config(&self.config); + bus.set_config(&self.config).map_err(|_| SpiDeviceError::Config)?; self.cs.set_low().map_err(SpiDeviceError::Cs)?; let op_res = operations.iter_mut().try_for_each(|op| match op { diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs index 79a90bd52..b0159ac09 100644 --- a/embassy-embedded-hal/src/shared_bus/mod.rs +++ b/embassy-embedded-hal/src/shared_bus/mod.rs @@ -14,6 +14,8 @@ pub mod blocking; pub enum I2cDeviceError { /// An operation on the inner I2C bus failed. I2c(BUS), + /// Configuration of the inner I2C bus failed. + Config, } impl i2c::Error for I2cDeviceError @@ -23,6 +25,7 @@ where fn kind(&self) -> i2c::ErrorKind { match self { Self::I2c(e) => e.kind(), + Self::Config => i2c::ErrorKind::Other, } } } @@ -38,6 +41,8 @@ pub enum SpiDeviceError { Cs(CS), /// DelayUs operations are not supported when the `time` Cargo feature is not enabled. DelayUsNotSupported, + /// The SPI bus could not be configured. + Config, } impl spi::Error for SpiDeviceError @@ -50,6 +55,7 @@ where Self::Spi(e) => e.kind(), Self::Cs(_) => spi::ErrorKind::Other, Self::DelayUsNotSupported => spi::ErrorKind::Other, + Self::Config => spi::ErrorKind::Other, } } }