diff options
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus')
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 84 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 50 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/i2c.rs | 78 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/spi.rs | 61 |
4 files changed, 273 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 2c762fe14..6f5f07051 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | |||
| @@ -7,6 +7,7 @@ use embedded_hal_1::i2c::blocking::{I2c, Operation}; | |||
| 7 | use embedded_hal_1::i2c::ErrorType; | 7 | use embedded_hal_1::i2c::ErrorType; |
| 8 | 8 | ||
| 9 | use crate::shared_bus::i2c::I2cBusDeviceError; | 9 | use crate::shared_bus::i2c::I2cBusDeviceError; |
| 10 | use crate::SetConfig; | ||
| 10 | 11 | ||
| 11 | pub struct I2cBusDevice<'a, M: RawMutex, BUS> { | 12 | pub struct I2cBusDevice<'a, M: RawMutex, BUS> { |
| 12 | bus: &'a Mutex<M, RefCell<BUS>>, | 13 | bus: &'a Mutex<M, RefCell<BUS>>, |
| @@ -82,3 +83,86 @@ where | |||
| 82 | todo!() | 83 | todo!() |
| 83 | } | 84 | } |
| 84 | } | 85 | } |
| 86 | |||
| 87 | pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { | ||
| 88 | bus: &'a Mutex<M, RefCell<BUS>>, | ||
| 89 | config: C, | ||
| 90 | } | ||
| 91 | |||
| 92 | impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { | ||
| 93 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, config: C) -> Self { | ||
| 94 | Self { bus, config } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | impl<'a, M: RawMutex, BUS, C> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> | ||
| 99 | where | ||
| 100 | BUS: ErrorType, | ||
| 101 | { | ||
| 102 | type Error = I2cBusDeviceError<BUS::Error>; | ||
| 103 | } | ||
| 104 | |||
| 105 | impl<M, BUS, C> I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> | ||
| 106 | where | ||
| 107 | M: RawMutex, | ||
| 108 | BUS: I2c + SetConfig<C>, | ||
| 109 | { | ||
| 110 | fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { | ||
| 111 | self.bus.lock(|bus| { | ||
| 112 | let mut bus = bus.borrow_mut(); | ||
| 113 | bus.set_config(&self.config); | ||
| 114 | bus.read(address, buffer).map_err(I2cBusDeviceError::I2c) | ||
| 115 | }) | ||
| 116 | } | ||
| 117 | |||
| 118 | fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 119 | self.bus.lock(|bus| { | ||
| 120 | let mut bus = bus.borrow_mut(); | ||
| 121 | bus.set_config(&self.config); | ||
| 122 | bus.write(address, bytes).map_err(I2cBusDeviceError::I2c) | ||
| 123 | }) | ||
| 124 | } | ||
| 125 | |||
| 126 | fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { | ||
| 127 | self.bus.lock(|bus| { | ||
| 128 | let mut bus = bus.borrow_mut(); | ||
| 129 | bus.set_config(&self.config); | ||
| 130 | bus.write_read(address, wr_buffer, rd_buffer) | ||
| 131 | .map_err(I2cBusDeviceError::I2c) | ||
| 132 | }) | ||
| 133 | } | ||
| 134 | |||
| 135 | fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> { | ||
| 136 | let _ = address; | ||
| 137 | let _ = operations; | ||
| 138 | todo!() | ||
| 139 | } | ||
| 140 | |||
| 141 | fn write_iter<B: IntoIterator<Item = u8>>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> { | ||
| 142 | let _ = addr; | ||
| 143 | let _ = bytes; | ||
| 144 | todo!() | ||
| 145 | } | ||
| 146 | |||
| 147 | fn write_iter_read<B: IntoIterator<Item = u8>>( | ||
| 148 | &mut self, | ||
| 149 | addr: u8, | ||
| 150 | bytes: B, | ||
| 151 | buffer: &mut [u8], | ||
| 152 | ) -> Result<(), Self::Error> { | ||
| 153 | let _ = addr; | ||
| 154 | let _ = bytes; | ||
| 155 | let _ = buffer; | ||
| 156 | todo!() | ||
| 157 | } | ||
| 158 | |||
| 159 | fn transaction_iter<'a, O: IntoIterator<Item = Operation<'a>>>( | ||
| 160 | &mut self, | ||
| 161 | address: u8, | ||
| 162 | operations: O, | ||
| 163 | ) -> Result<(), Self::Error> { | ||
| 164 | let _ = address; | ||
| 165 | let _ = operations; | ||
| 166 | todo!() | ||
| 167 | } | ||
| 168 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index c08bcbf62..d54ca6bfa 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs | |||
| @@ -8,6 +8,7 @@ use embedded_hal_1::spi; | |||
| 8 | use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; | 8 | use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; |
| 9 | 9 | ||
| 10 | use crate::shared_bus::spi::SpiBusDeviceError; | 10 | use crate::shared_bus::spi::SpiBusDeviceError; |
| 11 | use crate::SetConfig; | ||
| 11 | 12 | ||
| 12 | pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { | 13 | pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { |
| 13 | bus: &'a Mutex<M, RefCell<BUS>>, | 14 | bus: &'a Mutex<M, RefCell<BUS>>, |
| @@ -55,3 +56,52 @@ where | |||
| 55 | }) | 56 | }) |
| 56 | } | 57 | } |
| 57 | } | 58 | } |
| 59 | |||
| 60 | pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { | ||
| 61 | bus: &'a Mutex<M, RefCell<BUS>>, | ||
| 62 | cs: CS, | ||
| 63 | config: C, | ||
| 64 | } | ||
| 65 | |||
| 66 | impl<'a, M: RawMutex, BUS, CS, C> SpiBusDeviceWithConfig<'a, M, BUS, CS, C> { | ||
| 67 | pub fn new(bus: &'a Mutex<M, RefCell<BUS>>, cs: CS, config: C) -> Self { | ||
| 68 | Self { bus, cs, config } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C> | ||
| 73 | where | ||
| 74 | BUS: spi::ErrorType, | ||
| 75 | CS: OutputPin, | ||
| 76 | { | ||
| 77 | type Error = SpiBusDeviceError<BUS::Error, CS::Error>; | ||
| 78 | } | ||
| 79 | |||
| 80 | impl<BUS, M, CS, C> SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C> | ||
| 81 | where | ||
| 82 | M: RawMutex, | ||
| 83 | BUS: SpiBusFlush + SetConfig<C>, | ||
| 84 | CS: OutputPin, | ||
| 85 | { | ||
| 86 | type Bus = BUS; | ||
| 87 | |||
| 88 | fn transaction<R>(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result<R, BUS::Error>) -> Result<R, Self::Error> { | ||
| 89 | self.bus.lock(|bus| { | ||
| 90 | let mut bus = bus.borrow_mut(); | ||
| 91 | bus.set_config(&self.config); | ||
| 92 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 93 | |||
| 94 | let f_res = f(&mut bus); | ||
| 95 | |||
| 96 | // On failure, it's important to still flush and deassert CS. | ||
| 97 | let flush_res = bus.flush(); | ||
| 98 | let cs_res = self.cs.set_high(); | ||
| 99 | |||
| 100 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 101 | flush_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 102 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 103 | |||
| 104 | Ok(f_res) | ||
| 105 | }) | ||
| 106 | } | ||
| 107 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/i2c.rs index e8131288a..0e964773c 100644 --- a/embassy-embedded-hal/src/shared_bus/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/i2c.rs | |||
| @@ -29,6 +29,8 @@ use embassy::blocking_mutex::raw::RawMutex; | |||
| 29 | use embassy::mutex::Mutex; | 29 | use embassy::mutex::Mutex; |
| 30 | use embedded_hal_async::i2c; | 30 | use embedded_hal_async::i2c; |
| 31 | 31 | ||
| 32 | use crate::SetConfig; | ||
| 33 | |||
| 32 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | 34 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 33 | pub enum I2cBusDeviceError<BUS> { | 35 | pub enum I2cBusDeviceError<BUS> { |
| 34 | I2c(BUS), | 36 | I2c(BUS), |
| @@ -116,3 +118,79 @@ where | |||
| 116 | async move { todo!() } | 118 | async move { todo!() } |
| 117 | } | 119 | } |
| 118 | } | 120 | } |
| 121 | |||
| 122 | pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { | ||
| 123 | bus: &'a Mutex<M, BUS>, | ||
| 124 | config: C, | ||
| 125 | } | ||
| 126 | |||
| 127 | impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { | ||
| 128 | pub fn new(bus: &'a Mutex<M, BUS>, config: C) -> Self { | ||
| 129 | Self { bus, config } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | impl<'a, M: RawMutex, BUS, C> i2c::ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> | ||
| 134 | where | ||
| 135 | BUS: i2c::ErrorType, | ||
| 136 | { | ||
| 137 | type Error = I2cBusDeviceError<BUS::Error>; | ||
| 138 | } | ||
| 139 | |||
| 140 | impl<M, BUS, C> i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> | ||
| 141 | where | ||
| 142 | M: RawMutex + 'static, | ||
| 143 | BUS: i2c::I2c + SetConfig<C> + 'static, | ||
| 144 | { | ||
| 145 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 146 | |||
| 147 | fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 148 | async move { | ||
| 149 | let mut bus = self.bus.lock().await; | ||
| 150 | bus.set_config(&self.config); | ||
| 151 | bus.read(address, buffer).await.map_err(I2cBusDeviceError::I2c)?; | ||
| 152 | Ok(()) | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 157 | |||
| 158 | fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 159 | async move { | ||
| 160 | let mut bus = self.bus.lock().await; | ||
| 161 | bus.set_config(&self.config); | ||
| 162 | bus.write(address, bytes).await.map_err(I2cBusDeviceError::I2c)?; | ||
| 163 | Ok(()) | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 168 | |||
| 169 | fn write_read<'a>( | ||
| 170 | &'a mut self, | ||
| 171 | address: u8, | ||
| 172 | wr_buffer: &'a [u8], | ||
| 173 | rd_buffer: &'a mut [u8], | ||
| 174 | ) -> Self::WriteReadFuture<'a> { | ||
| 175 | async move { | ||
| 176 | let mut bus = self.bus.lock().await; | ||
| 177 | bus.set_config(&self.config); | ||
| 178 | bus.write_read(address, wr_buffer, rd_buffer) | ||
| 179 | .await | ||
| 180 | .map_err(I2cBusDeviceError::I2c)?; | ||
| 181 | Ok(()) | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a; | ||
| 186 | |||
| 187 | fn transaction<'a, 'b>( | ||
| 188 | &'a mut self, | ||
| 189 | address: u8, | ||
| 190 | operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], | ||
| 191 | ) -> Self::TransactionFuture<'a, 'b> { | ||
| 192 | let _ = address; | ||
| 193 | let _ = operations; | ||
| 194 | async move { todo!() } | ||
| 195 | } | ||
| 196 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/spi.rs index fd4b6d565..04378c330 100644 --- a/embassy-embedded-hal/src/shared_bus/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/spi.rs | |||
| @@ -34,6 +34,8 @@ use embedded_hal_1::digital::blocking::OutputPin; | |||
| 34 | use embedded_hal_1::spi::ErrorType; | 34 | use embedded_hal_1::spi::ErrorType; |
| 35 | use embedded_hal_async::spi; | 35 | use embedded_hal_async::spi; |
| 36 | 36 | ||
| 37 | use crate::SetConfig; | ||
| 38 | |||
| 37 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | 39 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 38 | pub enum SpiBusDeviceError<BUS, CS> { | 40 | pub enum SpiBusDeviceError<BUS, CS> { |
| 39 | Spi(BUS), | 41 | Spi(BUS), |
| @@ -109,3 +111,62 @@ where | |||
| 109 | } | 111 | } |
| 110 | } | 112 | } |
| 111 | } | 113 | } |
| 114 | |||
| 115 | pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { | ||
| 116 | bus: &'a Mutex<M, BUS>, | ||
| 117 | cs: CS, | ||
| 118 | config: C, | ||
| 119 | } | ||
| 120 | |||
| 121 | impl<'a, M: RawMutex, BUS, CS, C> SpiBusDeviceWithConfig<'a, M, BUS, CS, C> { | ||
| 122 | pub fn new(bus: &'a Mutex<M, BUS>, cs: CS, config: C) -> Self { | ||
| 123 | Self { bus, cs, config } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C> | ||
| 128 | where | ||
| 129 | BUS: spi::ErrorType, | ||
| 130 | CS: OutputPin, | ||
| 131 | { | ||
| 132 | type Error = SpiBusDeviceError<BUS::Error, CS::Error>; | ||
| 133 | } | ||
| 134 | |||
| 135 | impl<M, BUS, CS, C> spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C> | ||
| 136 | where | ||
| 137 | M: RawMutex + 'static, | ||
| 138 | BUS: spi::SpiBusFlush + SetConfig<C> + 'static, | ||
| 139 | CS: OutputPin, | ||
| 140 | { | ||
| 141 | type Bus = BUS; | ||
| 142 | |||
| 143 | type TransactionFuture<'a, R, F, Fut> = impl Future<Output = Result<R, Self::Error>> + 'a | ||
| 144 | where | ||
| 145 | Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 146 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a; | ||
| 147 | |||
| 148 | fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut> | ||
| 149 | where | ||
| 150 | R: 'a, | ||
| 151 | F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 152 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a, | ||
| 153 | { | ||
| 154 | async move { | ||
| 155 | let mut bus = self.bus.lock().await; | ||
| 156 | bus.set_config(&self.config); | ||
| 157 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 158 | |||
| 159 | let f_res = f(&mut *bus).await; | ||
| 160 | |||
| 161 | // On failure, it's important to still flush and deassert CS. | ||
| 162 | let flush_res = bus.flush().await; | ||
| 163 | let cs_res = self.cs.set_high(); | ||
| 164 | |||
| 165 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 166 | flush_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 167 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 168 | |||
| 169 | Ok(f_res) | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
