diff options
| author | xoviat <[email protected]> | 2023-10-01 09:37:20 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-10-01 09:37:20 -0500 |
| commit | 5ad34404af9b7cfecfed5075d987c4c03ff2ca27 (patch) | |
| tree | 72f07beb474ef99a94408aba6b9c0362aa454889 /embassy-embedded-hal/src | |
| parent | a7b1e516504bb0df9f7af42361e4280adcc30417 (diff) | |
eh: update set_config and add get_config
Diffstat (limited to 'embassy-embedded-hal/src')
| -rw-r--r-- | embassy-embedded-hal/src/lib.rs | 14 | ||||
| -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 | 2 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 6 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 2 | ||||
| -rw-r--r-- | 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 { | |||
| 26 | /// The configuration type used by this driver. | 26 | /// The configuration type used by this driver. |
| 27 | type Config; | 27 | type Config; |
| 28 | 28 | ||
| 29 | /// The error type that can occur if `set_config` fails. | ||
| 30 | type ConfigError; | ||
| 31 | |||
| 29 | /// Set the configuration of the driver. | 32 | /// Set the configuration of the driver. |
| 30 | fn set_config(&mut self, config: &Self::Config); | 33 | fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError>; |
| 34 | } | ||
| 35 | |||
| 36 | /// Get the configuration of a peripheral driver. | ||
| 37 | pub trait GetConfig { | ||
| 38 | /// The configuration type used by this driver. | ||
| 39 | type Config; | ||
| 40 | |||
| 41 | /// Get the configuration of the driver. | ||
| 42 | fn get_config(&self) -> Self::Config; | ||
| 31 | } | 43 | } |
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 | |||
| 125 | { | 125 | { |
| 126 | async fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> { | 126 | async fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), I2cDeviceError<BUS::Error>> { |
| 127 | let mut bus = self.bus.lock().await; | 127 | let mut bus = self.bus.lock().await; |
| 128 | bus.set_config(&self.config); | 128 | bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; |
| 129 | bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?; | 129 | bus.read(address, buffer).await.map_err(I2cDeviceError::I2c)?; |
| 130 | Ok(()) | 130 | Ok(()) |
| 131 | } | 131 | } |
| 132 | 132 | ||
| 133 | async fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), I2cDeviceError<BUS::Error>> { | 133 | async fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), I2cDeviceError<BUS::Error>> { |
| 134 | let mut bus = self.bus.lock().await; | 134 | let mut bus = self.bus.lock().await; |
| 135 | bus.set_config(&self.config); | 135 | bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; |
| 136 | bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?; | 136 | bus.write(address, bytes).await.map_err(I2cDeviceError::I2c)?; |
| 137 | Ok(()) | 137 | Ok(()) |
| 138 | } | 138 | } |
| @@ -144,7 +144,7 @@ where | |||
| 144 | rd_buffer: &mut [u8], | 144 | rd_buffer: &mut [u8], |
| 145 | ) -> Result<(), I2cDeviceError<BUS::Error>> { | 145 | ) -> Result<(), I2cDeviceError<BUS::Error>> { |
| 146 | let mut bus = self.bus.lock().await; | 146 | let mut bus = self.bus.lock().await; |
| 147 | bus.set_config(&self.config); | 147 | bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; |
| 148 | bus.write_read(address, wr_buffer, rd_buffer) | 148 | bus.write_read(address, wr_buffer, rd_buffer) |
| 149 | .await | 149 | .await |
| 150 | .map_err(I2cDeviceError::I2c)?; | 150 | .map_err(I2cDeviceError::I2c)?; |
| @@ -153,7 +153,7 @@ where | |||
| 153 | 153 | ||
| 154 | async fn transaction(&mut self, address: u8, operations: &mut [i2c::Operation<'_>]) -> Result<(), Self::Error> { | 154 | async fn transaction(&mut self, address: u8, operations: &mut [i2c::Operation<'_>]) -> Result<(), Self::Error> { |
| 155 | let mut bus = self.bus.lock().await; | 155 | let mut bus = self.bus.lock().await; |
| 156 | bus.set_config(&self.config); | 156 | bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; |
| 157 | bus.transaction(address, operations) | 157 | bus.transaction(address, operations) |
| 158 | .await | 158 | .await |
| 159 | .map_err(I2cDeviceError::I2c)?; | 159 | .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 | |||
| 130 | { | 130 | { |
| 131 | async fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> { | 131 | async fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> { |
| 132 | let mut bus = self.bus.lock().await; | 132 | let mut bus = self.bus.lock().await; |
| 133 | bus.set_config(&self.config); | 133 | bus.set_config(&self.config).map_err(|_| SpiDeviceError::Config)?; |
| 134 | self.cs.set_low().map_err(SpiDeviceError::Cs)?; | 134 | self.cs.set_low().map_err(SpiDeviceError::Cs)?; |
| 135 | 135 | ||
| 136 | let op_res: Result<(), BUS::Error> = try { | 136 | 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 | |||
| 148 | fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { | 148 | fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { |
| 149 | self.bus.lock(|bus| { | 149 | self.bus.lock(|bus| { |
| 150 | let mut bus = bus.borrow_mut(); | 150 | let mut bus = bus.borrow_mut(); |
| 151 | bus.set_config(&self.config); | 151 | bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; |
| 152 | bus.read(address, buffer).map_err(I2cDeviceError::I2c) | 152 | bus.read(address, buffer).map_err(I2cDeviceError::I2c) |
| 153 | }) | 153 | }) |
| 154 | } | 154 | } |
| @@ -156,7 +156,7 @@ where | |||
| 156 | fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { | 156 | fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { |
| 157 | self.bus.lock(|bus| { | 157 | self.bus.lock(|bus| { |
| 158 | let mut bus = bus.borrow_mut(); | 158 | let mut bus = bus.borrow_mut(); |
| 159 | bus.set_config(&self.config); | 159 | bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; |
| 160 | bus.write(address, bytes).map_err(I2cDeviceError::I2c) | 160 | bus.write(address, bytes).map_err(I2cDeviceError::I2c) |
| 161 | }) | 161 | }) |
| 162 | } | 162 | } |
| @@ -164,7 +164,7 @@ where | |||
| 164 | fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { | 164 | fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { |
| 165 | self.bus.lock(|bus| { | 165 | self.bus.lock(|bus| { |
| 166 | let mut bus = bus.borrow_mut(); | 166 | let mut bus = bus.borrow_mut(); |
| 167 | bus.set_config(&self.config); | 167 | bus.set_config(&self.config).map_err(|_| I2cDeviceError::Config)?; |
| 168 | bus.write_read(address, wr_buffer, rd_buffer) | 168 | bus.write_read(address, wr_buffer, rd_buffer) |
| 169 | .map_err(I2cDeviceError::I2c) | 169 | .map_err(I2cDeviceError::I2c) |
| 170 | }) | 170 | }) |
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 | |||
| 163 | fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> { | 163 | fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> { |
| 164 | self.bus.lock(|bus| { | 164 | self.bus.lock(|bus| { |
| 165 | let mut bus = bus.borrow_mut(); | 165 | let mut bus = bus.borrow_mut(); |
| 166 | bus.set_config(&self.config); | 166 | bus.set_config(&self.config).map_err(|_| SpiDeviceError::Config)?; |
| 167 | self.cs.set_low().map_err(SpiDeviceError::Cs)?; | 167 | self.cs.set_low().map_err(SpiDeviceError::Cs)?; |
| 168 | 168 | ||
| 169 | let op_res = operations.iter_mut().try_for_each(|op| match op { | 169 | 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; | |||
| 14 | pub enum I2cDeviceError<BUS> { | 14 | pub enum I2cDeviceError<BUS> { |
| 15 | /// An operation on the inner I2C bus failed. | 15 | /// An operation on the inner I2C bus failed. |
| 16 | I2c(BUS), | 16 | I2c(BUS), |
| 17 | /// Configuration of the inner I2C bus failed. | ||
| 18 | Config, | ||
| 17 | } | 19 | } |
| 18 | 20 | ||
| 19 | impl<BUS> i2c::Error for I2cDeviceError<BUS> | 21 | impl<BUS> i2c::Error for I2cDeviceError<BUS> |
| @@ -23,6 +25,7 @@ where | |||
| 23 | fn kind(&self) -> i2c::ErrorKind { | 25 | fn kind(&self) -> i2c::ErrorKind { |
| 24 | match self { | 26 | match self { |
| 25 | Self::I2c(e) => e.kind(), | 27 | Self::I2c(e) => e.kind(), |
| 28 | Self::Config => i2c::ErrorKind::Other, | ||
| 26 | } | 29 | } |
| 27 | } | 30 | } |
| 28 | } | 31 | } |
| @@ -38,6 +41,8 @@ pub enum SpiDeviceError<BUS, CS> { | |||
| 38 | Cs(CS), | 41 | Cs(CS), |
| 39 | /// DelayUs operations are not supported when the `time` Cargo feature is not enabled. | 42 | /// DelayUs operations are not supported when the `time` Cargo feature is not enabled. |
| 40 | DelayUsNotSupported, | 43 | DelayUsNotSupported, |
| 44 | /// The SPI bus could not be configured. | ||
| 45 | Config, | ||
| 41 | } | 46 | } |
| 42 | 47 | ||
| 43 | impl<BUS, CS> spi::Error for SpiDeviceError<BUS, CS> | 48 | impl<BUS, CS> spi::Error for SpiDeviceError<BUS, CS> |
| @@ -50,6 +55,7 @@ where | |||
| 50 | Self::Spi(e) => e.kind(), | 55 | Self::Spi(e) => e.kind(), |
| 51 | Self::Cs(_) => spi::ErrorKind::Other, | 56 | Self::Cs(_) => spi::ErrorKind::Other, |
| 52 | Self::DelayUsNotSupported => spi::ErrorKind::Other, | 57 | Self::DelayUsNotSupported => spi::ErrorKind::Other, |
| 58 | Self::Config => spi::ErrorKind::Other, | ||
| 53 | } | 59 | } |
| 54 | } | 60 | } |
| 55 | } | 61 | } |
