diff options
| author | Rasmus Melchior Jacobsen <[email protected]> | 2023-05-22 16:48:31 +0200 |
|---|---|---|
| committer | Rasmus Melchior Jacobsen <[email protected]> | 2023-05-22 16:48:31 +0200 |
| commit | cd1bf31fedbd33170507245eef1f7ae576aa3557 (patch) | |
| tree | 5e15fbe169c15ed603431f9222e6e9c99e23f713 /embassy-embedded-hal | |
| parent | d54eb1107ee45c5030449a0de0c259da7236ca05 (diff) | |
Add YieldingAsync adapter
Diffstat (limited to 'embassy-embedded-hal')
| -rw-r--r-- | embassy-embedded-hal/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/adapter/blocking_async.rs (renamed from embassy-embedded-hal/src/adapter.rs) | 79 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/adapter/mod.rs | 5 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/adapter/yielding_async.rs | 232 |
4 files changed, 241 insertions, 79 deletions
diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml index 81cece686..ad2f14568 100644 --- a/embassy-embedded-hal/Cargo.toml +++ b/embassy-embedded-hal/Cargo.toml | |||
| @@ -14,10 +14,10 @@ target = "x86_64-unknown-linux-gnu" | |||
| 14 | [features] | 14 | [features] |
| 15 | std = [] | 15 | std = [] |
| 16 | # Enable nightly-only features | 16 | # Enable nightly-only features |
| 17 | nightly = ["embedded-hal-async", "embedded-storage-async"] | 17 | nightly = ["embassy-futures", "embedded-hal-async", "embedded-storage-async"] |
| 18 | 18 | ||
| 19 | [dependencies] | 19 | [dependencies] |
| 20 | embassy-futures = { version = "0.1.0", path = "../embassy-futures" } | 20 | embassy-futures = { version = "0.1.0", path = "../embassy-futures", optional = true } |
| 21 | embassy-sync = { version = "0.2.0", path = "../embassy-sync" } | 21 | embassy-sync = { version = "0.2.0", path = "../embassy-sync" } |
| 22 | embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [ | 22 | embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [ |
| 23 | "unproven", | 23 | "unproven", |
diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter/blocking_async.rs index 169aad5e3..171ff6c9f 100644 --- a/embassy-embedded-hal/src/adapter.rs +++ b/embassy-embedded-hal/src/adapter/blocking_async.rs | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | //! Adapters between embedded-hal traits. | 1 | //! Adapters between embedded-hal traits. |
| 2 | 2 | ||
| 3 | use embassy_futures::yield_now; | ||
| 4 | use embedded_hal_02::{blocking, serial}; | 3 | use embedded_hal_02::{blocking, serial}; |
| 5 | 4 | ||
| 6 | /// Wrapper that implements async traits using blocking implementations. | 5 | /// Wrapper that implements async traits using blocking implementations. |
| @@ -151,18 +150,11 @@ where | |||
| 151 | const ERASE_SIZE: usize = <T as NorFlash>::ERASE_SIZE; | 150 | const ERASE_SIZE: usize = <T as NorFlash>::ERASE_SIZE; |
| 152 | 151 | ||
| 153 | async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> { | 152 | async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> { |
| 154 | self.wrapped.write(offset, data)?; | 153 | self.wrapped.write(offset, data) |
| 155 | yield_now().await; | ||
| 156 | Ok(()) | ||
| 157 | } | 154 | } |
| 158 | 155 | ||
| 159 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | 156 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { |
| 160 | for from in (from..to).step_by(T::ERASE_SIZE) { | 157 | self.wrapped.erase(from, to) |
| 161 | let to = core::cmp::min(from + T::ERASE_SIZE as u32, to); | ||
| 162 | self.wrapped.erase(from, to)?; | ||
| 163 | yield_now().await; | ||
| 164 | } | ||
| 165 | Ok(()) | ||
| 166 | } | 158 | } |
| 167 | } | 159 | } |
| 168 | 160 | ||
| @@ -179,70 +171,3 @@ where | |||
| 179 | self.wrapped.capacity() | 171 | self.wrapped.capacity() |
| 180 | } | 172 | } |
| 181 | } | 173 | } |
| 182 | |||
| 183 | #[cfg(test)] | ||
| 184 | mod tests { | ||
| 185 | use super::*; | ||
| 186 | |||
| 187 | extern crate std; | ||
| 188 | |||
| 189 | #[derive(Default)] | ||
| 190 | struct FakeFlash(Vec<(u32, u32)>); | ||
| 191 | |||
| 192 | impl embedded_storage::nor_flash::ErrorType for FakeFlash { | ||
| 193 | type Error = std::convert::Infallible; | ||
| 194 | } | ||
| 195 | |||
| 196 | impl embedded_storage::nor_flash::ReadNorFlash for FakeFlash { | ||
| 197 | const READ_SIZE: usize = 1; | ||
| 198 | |||
| 199 | fn read(&mut self, _offset: u32, _bytes: &mut [u8]) -> Result<(), Self::Error> { | ||
| 200 | unimplemented!() | ||
| 201 | } | ||
| 202 | |||
| 203 | fn capacity(&self) -> usize { | ||
| 204 | unimplemented!() | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | impl embedded_storage::nor_flash::NorFlash for FakeFlash { | ||
| 209 | const WRITE_SIZE: usize = 4; | ||
| 210 | const ERASE_SIZE: usize = 128; | ||
| 211 | |||
| 212 | fn write(&mut self, _offset: u32, _bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 213 | unimplemented!() | ||
| 214 | } | ||
| 215 | |||
| 216 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 217 | self.0.push((from, to)); | ||
| 218 | Ok(()) | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | #[futures_test::test] | ||
| 223 | async fn can_erase() { | ||
| 224 | let fake = FakeFlash::default(); | ||
| 225 | let mut yielding = BlockingAsync::new(fake); | ||
| 226 | |||
| 227 | yielding.erase(0, 256).await.unwrap(); | ||
| 228 | |||
| 229 | let fake = yielding.wrapped; | ||
| 230 | assert_eq!(2, fake.0.len()); | ||
| 231 | assert_eq!((0, 128), fake.0[0]); | ||
| 232 | assert_eq!((128, 256), fake.0[1]); | ||
| 233 | } | ||
| 234 | |||
| 235 | #[futures_test::test] | ||
| 236 | async fn can_erase_wrong_erase_size() { | ||
| 237 | let fake = FakeFlash::default(); | ||
| 238 | let mut yielding = BlockingAsync::new(fake); | ||
| 239 | |||
| 240 | yielding.erase(0, 257).await.unwrap(); | ||
| 241 | |||
| 242 | let fake = yielding.wrapped; | ||
| 243 | assert_eq!(3, fake.0.len()); | ||
| 244 | assert_eq!((0, 128), fake.0[0]); | ||
| 245 | assert_eq!((128, 256), fake.0[1]); | ||
| 246 | assert_eq!((256, 257), fake.0[2]); | ||
| 247 | } | ||
| 248 | } | ||
diff --git a/embassy-embedded-hal/src/adapter/mod.rs b/embassy-embedded-hal/src/adapter/mod.rs new file mode 100644 index 000000000..787ac2979 --- /dev/null +++ b/embassy-embedded-hal/src/adapter/mod.rs | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | mod blocking_async; | ||
| 2 | mod yielding_async; | ||
| 3 | |||
| 4 | pub use blocking_async::BlockingAsync; | ||
| 5 | pub use yielding_async::YieldingAsync; | ||
diff --git a/embassy-embedded-hal/src/adapter/yielding_async.rs b/embassy-embedded-hal/src/adapter/yielding_async.rs new file mode 100644 index 000000000..96d5cca8e --- /dev/null +++ b/embassy-embedded-hal/src/adapter/yielding_async.rs | |||
| @@ -0,0 +1,232 @@ | |||
| 1 | use embassy_futures::yield_now; | ||
| 2 | |||
| 3 | /// Wrapper that yields for each operation to the wrapped instance | ||
| 4 | /// | ||
| 5 | /// This can be used in combination with BlockingAsync<T> to enforce yields | ||
| 6 | /// between long running blocking operations. | ||
| 7 | pub struct YieldingAsync<T> { | ||
| 8 | wrapped: T, | ||
| 9 | } | ||
| 10 | |||
| 11 | impl<T> YieldingAsync<T> { | ||
| 12 | /// Create a new instance of a wrapper that yields after each operation. | ||
| 13 | pub fn new(wrapped: T) -> Self { | ||
| 14 | Self { wrapped } | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | // | ||
| 19 | // I2C implementations | ||
| 20 | // | ||
| 21 | impl<T> embedded_hal_1::i2c::ErrorType for YieldingAsync<T> | ||
| 22 | where | ||
| 23 | T: embedded_hal_1::i2c::ErrorType, | ||
| 24 | { | ||
| 25 | type Error = T::Error; | ||
| 26 | } | ||
| 27 | |||
| 28 | impl<T> embedded_hal_async::i2c::I2c for YieldingAsync<T> | ||
| 29 | where | ||
| 30 | T: embedded_hal_async::i2c::I2c, | ||
| 31 | { | ||
| 32 | async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { | ||
| 33 | self.wrapped.read(address, read).await?; | ||
| 34 | yield_now().await; | ||
| 35 | Ok(()) | ||
| 36 | } | ||
| 37 | |||
| 38 | async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { | ||
| 39 | self.wrapped.write(address, write).await?; | ||
| 40 | yield_now().await; | ||
| 41 | Ok(()) | ||
| 42 | } | ||
| 43 | |||
| 44 | async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { | ||
| 45 | self.wrapped.write_read(address, write, read).await?; | ||
| 46 | yield_now().await; | ||
| 47 | Ok(()) | ||
| 48 | } | ||
| 49 | |||
| 50 | async fn transaction( | ||
| 51 | &mut self, | ||
| 52 | address: u8, | ||
| 53 | operations: &mut [embedded_hal_1::i2c::Operation<'_>], | ||
| 54 | ) -> Result<(), Self::Error> { | ||
| 55 | self.wrapped.transaction(address, operations).await?; | ||
| 56 | yield_now().await; | ||
| 57 | Ok(()) | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | // | ||
| 62 | // SPI implementations | ||
| 63 | // | ||
| 64 | |||
| 65 | impl<T> embedded_hal_async::spi::ErrorType for YieldingAsync<T> | ||
| 66 | where | ||
| 67 | T: embedded_hal_async::spi::ErrorType, | ||
| 68 | { | ||
| 69 | type Error = T::Error; | ||
| 70 | } | ||
| 71 | |||
| 72 | impl<T> embedded_hal_async::spi::SpiBus<u8> for YieldingAsync<T> | ||
| 73 | where | ||
| 74 | T: embedded_hal_async::spi::SpiBus, | ||
| 75 | { | ||
| 76 | async fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Result<(), Self::Error> { | ||
| 77 | self.wrapped.transfer(read, write).await?; | ||
| 78 | yield_now().await; | ||
| 79 | Ok(()) | ||
| 80 | } | ||
| 81 | |||
| 82 | async fn transfer_in_place<'a>(&'a mut self, words: &'a mut [u8]) -> Result<(), Self::Error> { | ||
| 83 | self.wrapped.transfer_in_place(words).await?; | ||
| 84 | yield_now().await; | ||
| 85 | Ok(()) | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | impl<T> embedded_hal_async::spi::SpiBusFlush for YieldingAsync<T> | ||
| 90 | where | ||
| 91 | T: embedded_hal_async::spi::SpiBusFlush, | ||
| 92 | { | ||
| 93 | async fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 94 | self.wrapped.flush().await?; | ||
| 95 | yield_now().await; | ||
| 96 | Ok(()) | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | impl<T> embedded_hal_async::spi::SpiBusWrite<u8> for YieldingAsync<T> | ||
| 101 | where | ||
| 102 | T: embedded_hal_async::spi::SpiBusWrite<u8>, | ||
| 103 | { | ||
| 104 | async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> { | ||
| 105 | self.wrapped.write(data).await?; | ||
| 106 | yield_now().await; | ||
| 107 | Ok(()) | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | impl<T> embedded_hal_async::spi::SpiBusRead<u8> for YieldingAsync<T> | ||
| 112 | where | ||
| 113 | T: embedded_hal_async::spi::SpiBusRead<u8>, | ||
| 114 | { | ||
| 115 | async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> { | ||
| 116 | self.wrapped.read(data).await?; | ||
| 117 | yield_now().await; | ||
| 118 | Ok(()) | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | /// | ||
| 123 | /// NOR flash implementations | ||
| 124 | /// | ||
| 125 | impl<T: embedded_storage::nor_flash::ErrorType> embedded_storage::nor_flash::ErrorType for YieldingAsync<T> { | ||
| 126 | type Error = T::Error; | ||
| 127 | } | ||
| 128 | |||
| 129 | impl<T: embedded_storage_async::nor_flash::ReadNorFlash> embedded_storage_async::nor_flash::ReadNorFlash | ||
| 130 | for YieldingAsync<T> | ||
| 131 | { | ||
| 132 | const READ_SIZE: usize = T::READ_SIZE; | ||
| 133 | |||
| 134 | async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { | ||
| 135 | self.wrapped.read(offset, bytes).await?; | ||
| 136 | Ok(()) | ||
| 137 | } | ||
| 138 | |||
| 139 | fn capacity(&self) -> usize { | ||
| 140 | self.wrapped.capacity() | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | impl<T: embedded_storage_async::nor_flash::NorFlash> embedded_storage_async::nor_flash::NorFlash for YieldingAsync<T> { | ||
| 145 | const WRITE_SIZE: usize = T::WRITE_SIZE; | ||
| 146 | const ERASE_SIZE: usize = T::ERASE_SIZE; | ||
| 147 | |||
| 148 | async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 149 | self.wrapped.write(offset, bytes).await?; | ||
| 150 | yield_now().await; | ||
| 151 | Ok(()) | ||
| 152 | } | ||
| 153 | |||
| 154 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 155 | // Yield between each actual erase | ||
| 156 | for from in (from..to).step_by(T::ERASE_SIZE) { | ||
| 157 | let to = core::cmp::min(from + T::ERASE_SIZE as u32, to); | ||
| 158 | self.wrapped.erase(from, to).await?; | ||
| 159 | yield_now().await; | ||
| 160 | } | ||
| 161 | Ok(()) | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | #[cfg(test)] | ||
| 166 | mod tests { | ||
| 167 | use embedded_storage_async::nor_flash::NorFlash; | ||
| 168 | |||
| 169 | use super::*; | ||
| 170 | |||
| 171 | extern crate std; | ||
| 172 | |||
| 173 | #[derive(Default)] | ||
| 174 | struct FakeFlash(Vec<(u32, u32)>); | ||
| 175 | |||
| 176 | impl embedded_storage::nor_flash::ErrorType for FakeFlash { | ||
| 177 | type Error = std::convert::Infallible; | ||
| 178 | } | ||
| 179 | |||
| 180 | impl embedded_storage_async::nor_flash::ReadNorFlash for FakeFlash { | ||
| 181 | const READ_SIZE: usize = 1; | ||
| 182 | |||
| 183 | async fn read(&mut self, _offset: u32, _bytes: &mut [u8]) -> Result<(), Self::Error> { | ||
| 184 | unimplemented!() | ||
| 185 | } | ||
| 186 | |||
| 187 | fn capacity(&self) -> usize { | ||
| 188 | unimplemented!() | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | impl embedded_storage_async::nor_flash::NorFlash for FakeFlash { | ||
| 193 | const WRITE_SIZE: usize = 4; | ||
| 194 | const ERASE_SIZE: usize = 128; | ||
| 195 | |||
| 196 | async fn write(&mut self, _offset: u32, _bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 197 | unimplemented!() | ||
| 198 | } | ||
| 199 | |||
| 200 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 201 | self.0.push((from, to)); | ||
| 202 | Ok(()) | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | #[futures_test::test] | ||
| 207 | async fn can_erase() { | ||
| 208 | let fake = FakeFlash::default(); | ||
| 209 | let mut yielding = YieldingAsync::new(fake); | ||
| 210 | |||
| 211 | yielding.erase(0, 256).await.unwrap(); | ||
| 212 | |||
| 213 | let fake = yielding.wrapped; | ||
| 214 | assert_eq!(2, fake.0.len()); | ||
| 215 | assert_eq!((0, 128), fake.0[0]); | ||
| 216 | assert_eq!((128, 256), fake.0[1]); | ||
| 217 | } | ||
| 218 | |||
| 219 | #[futures_test::test] | ||
| 220 | async fn can_erase_wrong_erase_size() { | ||
| 221 | let fake = FakeFlash::default(); | ||
| 222 | let mut yielding = YieldingAsync::new(fake); | ||
| 223 | |||
| 224 | yielding.erase(0, 257).await.unwrap(); | ||
| 225 | |||
| 226 | let fake = yielding.wrapped; | ||
| 227 | assert_eq!(3, fake.0.len()); | ||
| 228 | assert_eq!((0, 128), fake.0[0]); | ||
| 229 | assert_eq!((128, 256), fake.0[1]); | ||
| 230 | assert_eq!((256, 257), fake.0[2]); | ||
| 231 | } | ||
| 232 | } | ||
