aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/adapter.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-embedded-hal/src/adapter.rs')
-rw-r--r--embassy-embedded-hal/src/adapter.rs79
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
3use embassy_futures::yield_now;
3use embedded_hal_02::{blocking, serial}; 4use 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)]
184mod 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}