diff options
| author | Rasmus Melchior Jacobsen <[email protected]> | 2023-05-22 15:57:20 +0200 |
|---|---|---|
| committer | Rasmus Melchior Jacobsen <[email protected]> | 2023-05-22 15:57:20 +0200 |
| commit | d54eb1107ee45c5030449a0de0c259da7236ca05 (patch) | |
| tree | be08e06cd7923abb57901085aed8b96b44c2b0fb /embassy-embedded-hal/src/adapter.rs | |
| parent | ab7d129e152a9450b2a6445397365bcb3a3ce183 (diff) | |
Yield between BlockingAsync NorFlash write and erase operations
Diffstat (limited to 'embassy-embedded-hal/src/adapter.rs')
| -rw-r--r-- | embassy-embedded-hal/src/adapter.rs | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter.rs index 171ff6c9f..169aad5e3 100644 --- a/embassy-embedded-hal/src/adapter.rs +++ b/embassy-embedded-hal/src/adapter.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | //! Adapters between embedded-hal traits. | 1 | //! Adapters between embedded-hal traits. |
| 2 | 2 | ||
| 3 | use embassy_futures::yield_now; | ||
| 3 | use embedded_hal_02::{blocking, serial}; | 4 | use embedded_hal_02::{blocking, serial}; |
| 4 | 5 | ||
| 5 | /// Wrapper that implements async traits using blocking implementations. | 6 | /// Wrapper that implements async traits using blocking implementations. |
| @@ -150,11 +151,18 @@ where | |||
| 150 | const ERASE_SIZE: usize = <T as NorFlash>::ERASE_SIZE; | 151 | const ERASE_SIZE: usize = <T as NorFlash>::ERASE_SIZE; |
| 151 | 152 | ||
| 152 | async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> { | 153 | async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> { |
| 153 | self.wrapped.write(offset, data) | 154 | self.wrapped.write(offset, data)?; |
| 155 | yield_now().await; | ||
| 156 | Ok(()) | ||
| 154 | } | 157 | } |
| 155 | 158 | ||
| 156 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | 159 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { |
| 157 | self.wrapped.erase(from, to) | 160 | for from in (from..to).step_by(T::ERASE_SIZE) { |
| 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(()) | ||
| 158 | } | 166 | } |
| 159 | } | 167 | } |
| 160 | 168 | ||
| @@ -171,3 +179,70 @@ where | |||
| 171 | self.wrapped.capacity() | 179 | self.wrapped.capacity() |
| 172 | } | 180 | } |
| 173 | } | 181 | } |
| 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 | } | ||
