aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal
diff options
context:
space:
mode:
authorRasmus Melchior Jacobsen <[email protected]>2023-05-22 15:57:20 +0200
committerRasmus Melchior Jacobsen <[email protected]>2023-05-22 15:57:20 +0200
commitd54eb1107ee45c5030449a0de0c259da7236ca05 (patch)
treebe08e06cd7923abb57901085aed8b96b44c2b0fb /embassy-embedded-hal
parentab7d129e152a9450b2a6445397365bcb3a3ce183 (diff)
Yield between BlockingAsync NorFlash write and erase operations
Diffstat (limited to 'embassy-embedded-hal')
-rw-r--r--embassy-embedded-hal/Cargo.toml8
-rw-r--r--embassy-embedded-hal/src/adapter.rs79
2 files changed, 84 insertions, 3 deletions
diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml
index 19d512585..81cece686 100644
--- a/embassy-embedded-hal/Cargo.toml
+++ b/embassy-embedded-hal/Cargo.toml
@@ -17,8 +17,11 @@ std = []
17nightly = ["embedded-hal-async", "embedded-storage-async"] 17nightly = ["embedded-hal-async", "embedded-storage-async"]
18 18
19[dependencies] 19[dependencies]
20embassy-futures = { version = "0.1.0", path = "../embassy-futures" }
20embassy-sync = { version = "0.2.0", path = "../embassy-sync" } 21embassy-sync = { version = "0.2.0", path = "../embassy-sync" }
21embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } 22embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [
23 "unproven",
24] }
22embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" } 25embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" }
23embedded-hal-async = { version = "=0.2.0-alpha.1", optional = true } 26embedded-hal-async = { version = "=0.2.0-alpha.1", optional = true }
24embedded-storage = "0.3.0" 27embedded-storage = "0.3.0"
@@ -26,3 +29,6 @@ embedded-storage-async = { version = "0.4.0", optional = true }
26nb = "1.0.0" 29nb = "1.0.0"
27 30
28defmt = { version = "0.3", optional = true } 31defmt = { version = "0.3", optional = true }
32
33[dev-dependencies]
34futures-test = "0.3.17"
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}