diff options
| author | Henrik Alsér <[email protected]> | 2022-05-26 18:54:58 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-05-26 18:54:58 +0200 |
| commit | e10fc2bada1c59420431f09a35f7aa09a5b45623 (patch) | |
| tree | 4b05a177db2a3e86c86058714d9a4ed014aea486 /embassy-embedded-hal | |
| parent | 36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff) | |
Async shared bus for SPI & I2C + rename embassy-traits (#769)
* Rename embassy-traits to embassy-embedded-hal
* Rename embassy-traits to embassy-embedded-hal
* Add shared bus for SPI and I2C
* rustfmt
* EHA alpha 1
* Rename embedded-traits in examples
* rustfmt
* rustfmt
Co-authored-by: Henrik Alsér <[email protected]>
Diffstat (limited to 'embassy-embedded-hal')
| -rw-r--r-- | embassy-embedded-hal/Cargo.toml | 17 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/adapter.rs | 248 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/lib.rs | 6 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/i2c.rs | 120 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/mod.rs | 4 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/spi.rs | 110 |
6 files changed, 505 insertions, 0 deletions
diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml new file mode 100644 index 000000000..14b73dce5 --- /dev/null +++ b/embassy-embedded-hal/Cargo.toml | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | [package] | ||
| 2 | name = "embassy-embedded-hal" | ||
| 3 | version = "0.1.0" | ||
| 4 | authors = ["Dario Nieuwenhuis <[email protected]>"] | ||
| 5 | edition = "2018" | ||
| 6 | |||
| 7 | [features] | ||
| 8 | std = [] | ||
| 9 | |||
| 10 | [dependencies] | ||
| 11 | embassy = { version = "0.1.0", path = "../embassy" } | ||
| 12 | embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } | ||
| 13 | embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } | ||
| 14 | embedded-hal-async = { version = "0.1.0-alpha.1" } | ||
| 15 | embedded-storage = "0.3.0" | ||
| 16 | embedded-storage-async = "0.3.0" | ||
| 17 | nb = "1.0.0" \ No newline at end of file | ||
diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter.rs new file mode 100644 index 000000000..033efb7ef --- /dev/null +++ b/embassy-embedded-hal/src/adapter.rs | |||
| @@ -0,0 +1,248 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | use embedded_hal_02::blocking; | ||
| 3 | use embedded_hal_02::serial; | ||
| 4 | |||
| 5 | /// BlockingAsync is a wrapper that implements async traits using blocking peripherals. This allows | ||
| 6 | /// driver writers to depend on the async traits while still supporting embedded-hal peripheral implementations. | ||
| 7 | /// | ||
| 8 | /// BlockingAsync will implement any async trait that maps to embedded-hal traits implemented for the wrapped driver. | ||
| 9 | /// | ||
| 10 | /// Driver users are then free to choose which implementation that is available to them. | ||
| 11 | pub struct BlockingAsync<T> { | ||
| 12 | wrapped: T, | ||
| 13 | } | ||
| 14 | |||
| 15 | impl<T> BlockingAsync<T> { | ||
| 16 | /// Create a new instance of a wrapper for a given peripheral. | ||
| 17 | pub fn new(wrapped: T) -> Self { | ||
| 18 | Self { wrapped } | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | // | ||
| 23 | // I2C implementations | ||
| 24 | // | ||
| 25 | impl<T, E> embedded_hal_1::i2c::ErrorType for BlockingAsync<T> | ||
| 26 | where | ||
| 27 | E: embedded_hal_1::i2c::Error + 'static, | ||
| 28 | T: blocking::i2c::WriteRead<Error = E> | ||
| 29 | + blocking::i2c::Read<Error = E> | ||
| 30 | + blocking::i2c::Write<Error = E>, | ||
| 31 | { | ||
| 32 | type Error = E; | ||
| 33 | } | ||
| 34 | |||
| 35 | impl<T, E> embedded_hal_async::i2c::I2c for BlockingAsync<T> | ||
| 36 | where | ||
| 37 | E: embedded_hal_1::i2c::Error + 'static, | ||
| 38 | T: blocking::i2c::WriteRead<Error = E> | ||
| 39 | + blocking::i2c::Read<Error = E> | ||
| 40 | + blocking::i2c::Write<Error = E>, | ||
| 41 | { | ||
| 42 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 43 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 44 | type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 45 | |||
| 46 | fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 47 | async move { self.wrapped.read(address, buffer) } | ||
| 48 | } | ||
| 49 | |||
| 50 | fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 51 | async move { self.wrapped.write(address, bytes) } | ||
| 52 | } | ||
| 53 | |||
| 54 | fn write_read<'a>( | ||
| 55 | &'a mut self, | ||
| 56 | address: u8, | ||
| 57 | bytes: &'a [u8], | ||
| 58 | buffer: &'a mut [u8], | ||
| 59 | ) -> Self::WriteReadFuture<'a> { | ||
| 60 | async move { self.wrapped.write_read(address, bytes, buffer) } | ||
| 61 | } | ||
| 62 | |||
| 63 | type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a; | ||
| 64 | |||
| 65 | fn transaction<'a, 'b>( | ||
| 66 | &'a mut self, | ||
| 67 | address: u8, | ||
| 68 | operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], | ||
| 69 | ) -> Self::TransactionFuture<'a, 'b> { | ||
| 70 | let _ = address; | ||
| 71 | let _ = operations; | ||
| 72 | async move { todo!() } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | // | ||
| 77 | // SPI implementatinos | ||
| 78 | // | ||
| 79 | |||
| 80 | impl<T, E> embedded_hal_async::spi::ErrorType for BlockingAsync<T> | ||
| 81 | where | ||
| 82 | E: embedded_hal_1::spi::Error, | ||
| 83 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 84 | { | ||
| 85 | type Error = E; | ||
| 86 | } | ||
| 87 | |||
| 88 | impl<T, E> embedded_hal_async::spi::SpiBus<u8> for BlockingAsync<T> | ||
| 89 | where | ||
| 90 | E: embedded_hal_1::spi::Error + 'static, | ||
| 91 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 92 | { | ||
| 93 | type TransferFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 94 | |||
| 95 | fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Self::TransferFuture<'a> { | ||
| 96 | async move { | ||
| 97 | // Ensure we write the expected bytes | ||
| 98 | for i in 0..core::cmp::min(read.len(), write.len()) { | ||
| 99 | read[i] = write[i].clone(); | ||
| 100 | } | ||
| 101 | self.wrapped.transfer(read)?; | ||
| 102 | Ok(()) | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | type TransferInPlaceFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 107 | |||
| 108 | fn transfer_in_place<'a>(&'a mut self, _: &'a mut [u8]) -> Self::TransferInPlaceFuture<'a> { | ||
| 109 | async move { todo!() } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | impl<T, E> embedded_hal_async::spi::SpiBusFlush for BlockingAsync<T> | ||
| 114 | where | ||
| 115 | E: embedded_hal_1::spi::Error + 'static, | ||
| 116 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 117 | { | ||
| 118 | type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 119 | |||
| 120 | fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { | ||
| 121 | async move { Ok(()) } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | impl<T, E> embedded_hal_async::spi::SpiBusWrite<u8> for BlockingAsync<T> | ||
| 126 | where | ||
| 127 | E: embedded_hal_1::spi::Error + 'static, | ||
| 128 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 129 | { | ||
| 130 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 131 | |||
| 132 | fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 133 | async move { | ||
| 134 | self.wrapped.write(data)?; | ||
| 135 | Ok(()) | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | impl<T, E> embedded_hal_async::spi::SpiBusRead<u8> for BlockingAsync<T> | ||
| 141 | where | ||
| 142 | E: embedded_hal_1::spi::Error + 'static, | ||
| 143 | T: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>, | ||
| 144 | { | ||
| 145 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 146 | |||
| 147 | fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 148 | async move { | ||
| 149 | self.wrapped.transfer(data)?; | ||
| 150 | Ok(()) | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | // Uart implementatinos | ||
| 156 | impl<T, E> embedded_hal_1::serial::ErrorType for BlockingAsync<T> | ||
| 157 | where | ||
| 158 | T: serial::Read<u8, Error = E>, | ||
| 159 | E: embedded_hal_1::serial::Error + 'static, | ||
| 160 | { | ||
| 161 | type Error = E; | ||
| 162 | } | ||
| 163 | |||
| 164 | #[cfg(feature = "_todo_embedded_hal_serial")] | ||
| 165 | impl<T, E> embedded_hal_async::serial::Read for BlockingAsync<T> | ||
| 166 | where | ||
| 167 | T: serial::Read<u8, Error = E>, | ||
| 168 | E: embedded_hal_1::serial::Error + 'static, | ||
| 169 | { | ||
| 170 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a; | ||
| 171 | fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 172 | async move { | ||
| 173 | let mut pos = 0; | ||
| 174 | while pos < buf.len() { | ||
| 175 | match self.wrapped.read() { | ||
| 176 | Err(nb::Error::WouldBlock) => {} | ||
| 177 | Err(nb::Error::Other(e)) => return Err(e), | ||
| 178 | Ok(b) => { | ||
| 179 | buf[pos] = b; | ||
| 180 | pos += 1; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | Ok(()) | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | #[cfg(feature = "_todo_embedded_hal_serial")] | ||
| 190 | impl<T, E> embedded_hal_async::serial::Write for BlockingAsync<T> | ||
| 191 | where | ||
| 192 | T: blocking::serial::Write<u8, Error = E> + serial::Read<u8, Error = E>, | ||
| 193 | E: embedded_hal_1::serial::Error + 'static, | ||
| 194 | { | ||
| 195 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a; | ||
| 196 | fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 197 | async move { self.wrapped.bwrite_all(buf) } | ||
| 198 | } | ||
| 199 | |||
| 200 | type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where T: 'a; | ||
| 201 | fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { | ||
| 202 | async move { self.wrapped.bflush() } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | /// NOR flash wrapper | ||
| 207 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | ||
| 208 | use embedded_storage_async::nor_flash::{AsyncNorFlash, AsyncReadNorFlash}; | ||
| 209 | |||
| 210 | impl<T> ErrorType for BlockingAsync<T> | ||
| 211 | where | ||
| 212 | T: ErrorType, | ||
| 213 | { | ||
| 214 | type Error = T::Error; | ||
| 215 | } | ||
| 216 | |||
| 217 | impl<T> AsyncNorFlash for BlockingAsync<T> | ||
| 218 | where | ||
| 219 | T: NorFlash, | ||
| 220 | { | ||
| 221 | const WRITE_SIZE: usize = <T as NorFlash>::WRITE_SIZE; | ||
| 222 | const ERASE_SIZE: usize = <T as NorFlash>::ERASE_SIZE; | ||
| 223 | |||
| 224 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 225 | fn write<'a>(&'a mut self, offset: u32, data: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 226 | async move { self.wrapped.write(offset, data) } | ||
| 227 | } | ||
| 228 | |||
| 229 | type EraseFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 230 | fn erase<'a>(&'a mut self, from: u32, to: u32) -> Self::EraseFuture<'a> { | ||
| 231 | async move { self.wrapped.erase(from, to) } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | impl<T> AsyncReadNorFlash for BlockingAsync<T> | ||
| 236 | where | ||
| 237 | T: ReadNorFlash, | ||
| 238 | { | ||
| 239 | const READ_SIZE: usize = <T as ReadNorFlash>::READ_SIZE; | ||
| 240 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 241 | fn read<'a>(&'a mut self, address: u32, data: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 242 | async move { self.wrapped.read(address, data) } | ||
| 243 | } | ||
| 244 | |||
| 245 | fn capacity(&self) -> usize { | ||
| 246 | self.wrapped.capacity() | ||
| 247 | } | ||
| 248 | } | ||
diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs new file mode 100644 index 000000000..27ffa7421 --- /dev/null +++ b/embassy-embedded-hal/src/lib.rs | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #![cfg_attr(not(feature = "std"), no_std)] | ||
| 2 | #![feature(generic_associated_types)] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | pub mod adapter; | ||
| 6 | pub mod shared_bus; | ||
diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/i2c.rs new file mode 100644 index 000000000..5a180e897 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/i2c.rs | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | //! Asynchronous shared I2C bus | ||
| 2 | //! | ||
| 3 | //! # Example (nrf52) | ||
| 4 | //! | ||
| 5 | //! ```rust | ||
| 6 | //! use embassy_embedded_hal::shared_bus::i2c::I2cBusDevice; | ||
| 7 | //! use embassy::mutex::Mutex; | ||
| 8 | //! use embassy::blocking_mutex::raw::ThreadModeRawMutex; | ||
| 9 | //! | ||
| 10 | //! static I2C_BUS: Forever<Mutex::<ThreadModeRawMutex, Twim<TWISPI0>>> = Forever::new(); | ||
| 11 | //! let config = twim::Config::default(); | ||
| 12 | //! let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); | ||
| 13 | //! let i2c = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, config); | ||
| 14 | //! let i2c_bus = Mutex::<ThreadModeRawMutex, _>::new(i2c); | ||
| 15 | //! let i2c_bus = I2C_BUS.put(i2c_bus); | ||
| 16 | //! | ||
| 17 | //! // Device 1, using embedded-hal-async compatible driver for QMC5883L compass | ||
| 18 | //! let i2c_dev1 = I2cBusDevice::new(i2c_bus); | ||
| 19 | //! let compass = QMC5883L::new(i2c_dev1).await.unwrap(); | ||
| 20 | //! | ||
| 21 | //! // Device 2, using embedded-hal-async compatible driver for Mpu6050 accelerometer | ||
| 22 | //! let i2c_dev2 = I2cBusDevice::new(i2c_bus); | ||
| 23 | //! let mpu = Mpu6050::new(i2c_dev2); | ||
| 24 | //! ``` | ||
| 25 | use core::{fmt::Debug, future::Future}; | ||
| 26 | use embassy::blocking_mutex::raw::RawMutex; | ||
| 27 | use embassy::mutex::Mutex; | ||
| 28 | use embedded_hal_async::i2c; | ||
| 29 | |||
| 30 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
| 31 | pub enum I2cBusDeviceError<BUS> { | ||
| 32 | I2c(BUS), | ||
| 33 | } | ||
| 34 | |||
| 35 | impl<BUS> i2c::Error for I2cBusDeviceError<BUS> | ||
| 36 | where | ||
| 37 | BUS: i2c::Error + Debug, | ||
| 38 | { | ||
| 39 | fn kind(&self) -> i2c::ErrorKind { | ||
| 40 | match self { | ||
| 41 | Self::I2c(e) => e.kind(), | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | pub struct I2cBusDevice<'a, M: RawMutex, BUS> { | ||
| 47 | bus: &'a Mutex<M, BUS>, | ||
| 48 | } | ||
| 49 | |||
| 50 | impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { | ||
| 51 | pub fn new(bus: &'a Mutex<M, BUS>) -> Self { | ||
| 52 | Self { bus } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | impl<'a, M: RawMutex, BUS> i2c::ErrorType for I2cBusDevice<'a, M, BUS> | ||
| 57 | where | ||
| 58 | BUS: i2c::ErrorType, | ||
| 59 | { | ||
| 60 | type Error = I2cBusDeviceError<BUS::Error>; | ||
| 61 | } | ||
| 62 | |||
| 63 | impl<M, BUS> i2c::I2c for I2cBusDevice<'_, M, BUS> | ||
| 64 | where | ||
| 65 | M: RawMutex + 'static, | ||
| 66 | BUS: i2c::I2c + 'static, | ||
| 67 | { | ||
| 68 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 69 | |||
| 70 | fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 71 | async move { | ||
| 72 | let mut bus = self.bus.lock().await; | ||
| 73 | bus.read(address, buffer) | ||
| 74 | .await | ||
| 75 | .map_err(I2cBusDeviceError::I2c)?; | ||
| 76 | Ok(()) | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 81 | |||
| 82 | fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 83 | async move { | ||
| 84 | let mut bus = self.bus.lock().await; | ||
| 85 | bus.write(address, bytes) | ||
| 86 | .await | ||
| 87 | .map_err(I2cBusDeviceError::I2c)?; | ||
| 88 | Ok(()) | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | type WriteReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 93 | |||
| 94 | fn write_read<'a>( | ||
| 95 | &'a mut self, | ||
| 96 | address: u8, | ||
| 97 | wr_buffer: &'a [u8], | ||
| 98 | rd_buffer: &'a mut [u8], | ||
| 99 | ) -> Self::WriteReadFuture<'a> { | ||
| 100 | async move { | ||
| 101 | let mut bus = self.bus.lock().await; | ||
| 102 | bus.write_read(address, wr_buffer, rd_buffer) | ||
| 103 | .await | ||
| 104 | .map_err(I2cBusDeviceError::I2c)?; | ||
| 105 | Ok(()) | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | type TransactionFuture<'a, 'b> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a, 'b: 'a; | ||
| 110 | |||
| 111 | fn transaction<'a, 'b>( | ||
| 112 | &'a mut self, | ||
| 113 | address: u8, | ||
| 114 | operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], | ||
| 115 | ) -> Self::TransactionFuture<'a, 'b> { | ||
| 116 | let _ = address; | ||
| 117 | let _ = operations; | ||
| 118 | async move { todo!() } | ||
| 119 | } | ||
| 120 | } | ||
diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs new file mode 100644 index 000000000..bd4fd2c31 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/mod.rs | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | //! Shared bus implementations for embedded-hal-async | ||
| 2 | |||
| 3 | pub mod i2c; | ||
| 4 | pub mod spi; | ||
diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/spi.rs new file mode 100644 index 000000000..3ec064ba7 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/spi.rs | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | //! Asynchronous shared SPI bus | ||
| 2 | //! | ||
| 3 | //! # Example (nrf52) | ||
| 4 | //! | ||
| 5 | //! ```rust | ||
| 6 | //! use embassy_embedded_hal::shared_bus::spi::SpiBusDevice; | ||
| 7 | //! use embassy::mutex::Mutex; | ||
| 8 | //! use embassy::blocking_mutex::raw::ThreadModeRawMutex; | ||
| 9 | //! | ||
| 10 | //! static SPI_BUS: Forever<Mutex<ThreadModeRawMutex, spim::Spim<SPI3>>> = Forever::new(); | ||
| 11 | //! let mut config = spim::Config::default(); | ||
| 12 | //! config.frequency = spim::Frequency::M32; | ||
| 13 | //! let irq = interrupt::take!(SPIM3); | ||
| 14 | //! let spi = spim::Spim::new_txonly(p.SPI3, irq, p.P0_15, p.P0_18, config); | ||
| 15 | //! let spi_bus = Mutex::<ThreadModeRawMutex, _>::new(spi); | ||
| 16 | //! let spi_bus = SPI_BUS.put(spi_bus); | ||
| 17 | //! | ||
| 18 | //! // Device 1, using embedded-hal-async compatible driver for ST7735 LCD display | ||
| 19 | //! let cs_pin1 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); | ||
| 20 | //! let spi_dev1 = SpiBusDevice::new(spi_bus, cs_pin1); | ||
| 21 | //! let display1 = ST7735::new(spi_dev1, dc1, rst1, Default::default(), 160, 128); | ||
| 22 | //! | ||
| 23 | //! // Device 2 | ||
| 24 | //! let cs_pin2 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); | ||
| 25 | //! let spi_dev2 = SpiBusDevice::new(spi_bus, cs_pin2); | ||
| 26 | //! let display2 = ST7735::new(spi_dev2, dc2, rst2, Default::default(), 160, 128); | ||
| 27 | //! ``` | ||
| 28 | use core::{fmt::Debug, future::Future}; | ||
| 29 | use embassy::blocking_mutex::raw::RawMutex; | ||
| 30 | use embassy::mutex::Mutex; | ||
| 31 | |||
| 32 | use embedded_hal_1::digital::blocking::OutputPin; | ||
| 33 | use embedded_hal_1::spi::ErrorType; | ||
| 34 | use embedded_hal_async::spi; | ||
| 35 | |||
| 36 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
| 37 | pub enum SpiBusDeviceError<BUS, CS> { | ||
| 38 | Spi(BUS), | ||
| 39 | Cs(CS), | ||
| 40 | } | ||
| 41 | |||
| 42 | impl<BUS, CS> spi::Error for SpiBusDeviceError<BUS, CS> | ||
| 43 | where | ||
| 44 | BUS: spi::Error + Debug, | ||
| 45 | CS: Debug, | ||
| 46 | { | ||
| 47 | fn kind(&self) -> spi::ErrorKind { | ||
| 48 | match self { | ||
| 49 | Self::Spi(e) => e.kind(), | ||
| 50 | Self::Cs(_) => spi::ErrorKind::Other, | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { | ||
| 56 | bus: &'a Mutex<M, BUS>, | ||
| 57 | cs: CS, | ||
| 58 | } | ||
| 59 | |||
| 60 | impl<'a, M: RawMutex, BUS, CS> SpiBusDevice<'a, M, BUS, CS> { | ||
| 61 | pub fn new(bus: &'a Mutex<M, BUS>, cs: CS) -> Self { | ||
| 62 | Self { bus, cs } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | impl<'a, M: RawMutex, BUS, CS> spi::ErrorType for SpiBusDevice<'a, M, BUS, CS> | ||
| 67 | where | ||
| 68 | BUS: spi::ErrorType, | ||
| 69 | CS: OutputPin, | ||
| 70 | { | ||
| 71 | type Error = SpiBusDeviceError<BUS::Error, CS::Error>; | ||
| 72 | } | ||
| 73 | |||
| 74 | impl<M, BUS, CS> spi::SpiDevice for SpiBusDevice<'_, M, BUS, CS> | ||
| 75 | where | ||
| 76 | M: RawMutex + 'static, | ||
| 77 | BUS: spi::SpiBusFlush + 'static, | ||
| 78 | CS: OutputPin, | ||
| 79 | { | ||
| 80 | type Bus = BUS; | ||
| 81 | |||
| 82 | type TransactionFuture<'a, R, F, Fut> = impl Future<Output = Result<R, Self::Error>> + 'a | ||
| 83 | where | ||
| 84 | Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 85 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a; | ||
| 86 | |||
| 87 | fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut> | ||
| 88 | where | ||
| 89 | R: 'a, | ||
| 90 | F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 91 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a, | ||
| 92 | { | ||
| 93 | async move { | ||
| 94 | let mut bus = self.bus.lock().await; | ||
| 95 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 96 | |||
| 97 | let f_res = f(&mut *bus).await; | ||
| 98 | |||
| 99 | // On failure, it's important to still flush and deassert CS. | ||
| 100 | let flush_res = bus.flush().await; | ||
| 101 | let cs_res = self.cs.set_high(); | ||
| 102 | |||
| 103 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 104 | flush_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 105 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 106 | |||
| 107 | Ok(f_res) | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
