aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-embedded-hal/src')
-rw-r--r--embassy-embedded-hal/src/adapter/yielding_async.rs62
-rw-r--r--embassy-embedded-hal/src/flash/concat_flash.rs (renamed from embassy-embedded-hal/src/flash.rs)86
-rw-r--r--embassy-embedded-hal/src/flash/mem_flash.rs127
-rw-r--r--embassy-embedded-hal/src/flash/mod.rs9
-rw-r--r--embassy-embedded-hal/src/flash/partition.rs150
5 files changed, 307 insertions, 127 deletions
diff --git a/embassy-embedded-hal/src/adapter/yielding_async.rs b/embassy-embedded-hal/src/adapter/yielding_async.rs
index 96d5cca8e..f51e4076f 100644
--- a/embassy-embedded-hal/src/adapter/yielding_async.rs
+++ b/embassy-embedded-hal/src/adapter/yielding_async.rs
@@ -167,66 +167,18 @@ mod tests {
167 use embedded_storage_async::nor_flash::NorFlash; 167 use embedded_storage_async::nor_flash::NorFlash;
168 168
169 use super::*; 169 use super::*;
170 170 use crate::flash::mem_flash::MemFlash;
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 171
206 #[futures_test::test] 172 #[futures_test::test]
207 async fn can_erase() { 173 async fn can_erase() {
208 let fake = FakeFlash::default(); 174 let flash = MemFlash::<1024, 128, 4>::new(0x00);
209 let mut yielding = YieldingAsync::new(fake); 175 let mut yielding = YieldingAsync::new(flash);
210 176
211 yielding.erase(0, 256).await.unwrap(); 177 yielding.erase(0, 256).await.unwrap();
212 178
213 let fake = yielding.wrapped; 179 let flash = yielding.wrapped;
214 assert_eq!(2, fake.0.len()); 180 assert_eq!(2, flash.erases.len());
215 assert_eq!((0, 128), fake.0[0]); 181 assert_eq!((0, 128), flash.erases[0]);
216 assert_eq!((128, 256), fake.0[1]); 182 assert_eq!((128, 256), flash.erases[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 } 183 }
232} 184}
diff --git a/embassy-embedded-hal/src/flash.rs b/embassy-embedded-hal/src/flash/concat_flash.rs
index 9a6e4bd92..1ea84269c 100644
--- a/embassy-embedded-hal/src/flash.rs
+++ b/embassy-embedded-hal/src/flash/concat_flash.rs
@@ -1,5 +1,3 @@
1//! Utilities related to flash.
2
3use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, ReadNorFlash}; 1use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, ReadNorFlash};
4#[cfg(feature = "nightly")] 2#[cfg(feature = "nightly")]
5use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash}; 3use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
@@ -192,18 +190,21 @@ where
192 190
193#[cfg(test)] 191#[cfg(test)]
194mod tests { 192mod tests {
195 use super::*; 193 use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
194
195 use super::ConcatFlash;
196 use crate::flash::mem_flash::MemFlash;
196 197
197 #[test] 198 #[test]
198 fn can_write_and_read_across_flashes() { 199 fn can_write_and_read_across_flashes() {
199 let first = MemFlash::<64, 16, 4>::new(); 200 let first = MemFlash::<64, 16, 4>::default();
200 let second = MemFlash::<64, 64, 4>::new(); 201 let second = MemFlash::<64, 64, 4>::default();
201 let mut f = ConcatFlash::new(first, second); 202 let mut f = ConcatFlash::new(first, second);
202 203
203 f.write(60, &[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]).unwrap(); 204 f.write(60, &[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]).unwrap();
204 205
205 assert_eq!(&[0x11, 0x22, 0x33, 0x44], &f.0 .0[60..]); 206 assert_eq!(&[0x11, 0x22, 0x33, 0x44], &f.0.mem[60..]);
206 assert_eq!(&[0x55, 0x66, 0x77, 0x88], &f.1 .0[0..4]); 207 assert_eq!(&[0x55, 0x66, 0x77, 0x88], &f.1.mem[0..4]);
207 208
208 let mut read_buf = [0; 8]; 209 let mut read_buf = [0; 8];
209 f.read(60, &mut read_buf).unwrap(); 210 f.read(60, &mut read_buf).unwrap();
@@ -213,74 +214,15 @@ mod tests {
213 214
214 #[test] 215 #[test]
215 fn can_erase_across_flashes() { 216 fn can_erase_across_flashes() {
216 let mut first = MemFlash::<128, 16, 4>::new(); 217 let first = MemFlash::<128, 16, 4>::new(0x00);
217 let mut second = MemFlash::<128, 64, 4>::new(); 218 let second = MemFlash::<128, 64, 4>::new(0x00);
218 first.0.fill(0x00);
219 second.0.fill(0x00);
220
221 let mut f = ConcatFlash::new(first, second); 219 let mut f = ConcatFlash::new(first, second);
222 220
223 f.erase(64, 192).unwrap(); 221 f.erase(64, 192).unwrap();
224 222
225 assert_eq!(&[0x00; 64], &f.0 .0[0..64]); 223 assert_eq!(&[0x00; 64], &f.0.mem[0..64]);
226 assert_eq!(&[0xff; 64], &f.0 .0[64..128]); 224 assert_eq!(&[0xff; 64], &f.0.mem[64..128]);
227 assert_eq!(&[0xff; 64], &f.1 .0[0..64]); 225 assert_eq!(&[0xff; 64], &f.1.mem[0..64]);
228 assert_eq!(&[0x00; 64], &f.1 .0[64..128]); 226 assert_eq!(&[0x00; 64], &f.1.mem[64..128]);
229 }
230
231 pub struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize>([u8; SIZE]);
232
233 impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> {
234 pub const fn new() -> Self {
235 Self([0xff; SIZE])
236 }
237 }
238
239 impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType
240 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
241 {
242 type Error = core::convert::Infallible;
243 }
244
245 impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ReadNorFlash
246 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
247 {
248 const READ_SIZE: usize = 1;
249
250 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
251 let len = bytes.len();
252 bytes.copy_from_slice(&self.0[offset as usize..offset as usize + len]);
253 Ok(())
254 }
255
256 fn capacity(&self) -> usize {
257 SIZE
258 }
259 }
260
261 impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> NorFlash
262 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
263 {
264 const WRITE_SIZE: usize = WRITE_SIZE;
265 const ERASE_SIZE: usize = ERASE_SIZE;
266
267 fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
268 let from = from as usize;
269 let to = to as usize;
270 assert_eq!(0, from % ERASE_SIZE);
271 assert_eq!(0, to % ERASE_SIZE);
272 self.0[from..to].fill(0xff);
273 Ok(())
274 }
275
276 fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
277 let offset = offset as usize;
278 assert_eq!(0, bytes.len() % WRITE_SIZE);
279 assert_eq!(0, offset % WRITE_SIZE);
280 assert!(offset + bytes.len() <= SIZE);
281
282 self.0[offset..offset + bytes.len()].copy_from_slice(bytes);
283 Ok(())
284 }
285 } 227 }
286} 228}
diff --git a/embassy-embedded-hal/src/flash/mem_flash.rs b/embassy-embedded-hal/src/flash/mem_flash.rs
new file mode 100644
index 000000000..4e10627df
--- /dev/null
+++ b/embassy-embedded-hal/src/flash/mem_flash.rs
@@ -0,0 +1,127 @@
1use alloc::vec::Vec;
2
3use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
4use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
5
6extern crate alloc;
7
8pub(crate) struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> {
9 pub mem: [u8; SIZE],
10 pub writes: Vec<(u32, usize)>,
11 pub erases: Vec<(u32, u32)>,
12}
13
14impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> {
15 #[allow(unused)]
16 pub const fn new(fill: u8) -> Self {
17 Self {
18 mem: [fill; SIZE],
19 writes: Vec::new(),
20 erases: Vec::new(),
21 }
22 }
23
24 fn read(&mut self, offset: u32, bytes: &mut [u8]) {
25 let len = bytes.len();
26 bytes.copy_from_slice(&self.mem[offset as usize..offset as usize + len]);
27 }
28
29 fn write(&mut self, offset: u32, bytes: &[u8]) {
30 self.writes.push((offset, bytes.len()));
31 let offset = offset as usize;
32 assert_eq!(0, bytes.len() % WRITE_SIZE);
33 assert_eq!(0, offset % WRITE_SIZE);
34 assert!(offset + bytes.len() <= SIZE);
35
36 self.mem[offset..offset + bytes.len()].copy_from_slice(bytes);
37 }
38
39 fn erase(&mut self, from: u32, to: u32) {
40 self.erases.push((from, to));
41 let from = from as usize;
42 let to = to as usize;
43 assert_eq!(0, from % ERASE_SIZE);
44 assert_eq!(0, to % ERASE_SIZE);
45 self.mem[from..to].fill(0xff);
46 }
47}
48
49impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> Default
50 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
51{
52 fn default() -> Self {
53 Self::new(0xff)
54 }
55}
56
57impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType
58 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
59{
60 type Error = core::convert::Infallible;
61}
62
63impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ReadNorFlash
64 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
65{
66 const READ_SIZE: usize = 1;
67
68 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
69 self.read(offset, bytes);
70 Ok(())
71 }
72
73 fn capacity(&self) -> usize {
74 SIZE
75 }
76}
77
78impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> NorFlash
79 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
80{
81 const WRITE_SIZE: usize = WRITE_SIZE;
82 const ERASE_SIZE: usize = ERASE_SIZE;
83
84 fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
85 self.write(offset, bytes);
86 Ok(())
87 }
88
89 fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
90 self.erase(from, to);
91 Ok(())
92 }
93}
94
95#[cfg(feature = "nightly")]
96impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncReadNorFlash
97 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
98{
99 const READ_SIZE: usize = 1;
100
101 async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
102 self.read(offset, bytes);
103 Ok(())
104 }
105
106 fn capacity(&self) -> usize {
107 SIZE
108 }
109}
110
111#[cfg(feature = "nightly")]
112impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncNorFlash
113 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
114{
115 const WRITE_SIZE: usize = WRITE_SIZE;
116 const ERASE_SIZE: usize = ERASE_SIZE;
117
118 async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
119 self.write(offset, bytes);
120 Ok(())
121 }
122
123 async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
124 self.erase(from, to);
125 Ok(())
126 }
127}
diff --git a/embassy-embedded-hal/src/flash/mod.rs b/embassy-embedded-hal/src/flash/mod.rs
new file mode 100644
index 000000000..c80dd6aac
--- /dev/null
+++ b/embassy-embedded-hal/src/flash/mod.rs
@@ -0,0 +1,9 @@
1//! Utilities related to flash.
2
3mod concat_flash;
4#[cfg(test)]
5pub(crate) mod mem_flash;
6mod partition;
7
8pub use concat_flash::ConcatFlash;
9pub use partition::Partition;
diff --git a/embassy-embedded-hal/src/flash/partition.rs b/embassy-embedded-hal/src/flash/partition.rs
new file mode 100644
index 000000000..084425e95
--- /dev/null
+++ b/embassy-embedded-hal/src/flash/partition.rs
@@ -0,0 +1,150 @@
1use embassy_sync::blocking_mutex::raw::RawMutex;
2use embassy_sync::mutex::Mutex;
3use embedded_storage::nor_flash::{ErrorType, NorFlashError, NorFlashErrorKind};
4#[cfg(feature = "nightly")]
5use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash};
6
7/// A logical partition of an underlying shared flash
8///
9/// A partition holds an offset and a size of the flash,
10/// and is restricted to operate with that range.
11/// There is no guarantee that muliple partitions on the same flash
12/// operate on mutually exclusive ranges - such a separation is up to
13/// the user to guarantee.
14pub struct Partition<'a, M: RawMutex, T> {
15 flash: &'a Mutex<M, T>,
16 offset: u32,
17 size: u32,
18}
19
20#[derive(Debug)]
21#[cfg_attr(feature = "defmt", derive(defmt::Format))]
22pub enum Error<T> {
23 Partition,
24 OutOfBounds,
25 Flash(T),
26}
27
28impl<'a, M: RawMutex, T> Partition<'a, M, T> {
29 /// Create a new partition
30 pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self {
31 Self { flash, offset, size }
32 }
33}
34
35impl<T: NorFlashError> NorFlashError for Error<T> {
36 fn kind(&self) -> NorFlashErrorKind {
37 match self {
38 Error::Partition => NorFlashErrorKind::Other,
39 Error::OutOfBounds => NorFlashErrorKind::OutOfBounds,
40 Error::Flash(f) => f.kind(),
41 }
42 }
43}
44
45impl<M: RawMutex, T: ErrorType> ErrorType for Partition<'_, M, T> {
46 type Error = Error<T::Error>;
47}
48
49#[cfg(feature = "nightly")]
50impl<M: RawMutex, T: ReadNorFlash> ReadNorFlash for Partition<'_, M, T> {
51 const READ_SIZE: usize = T::READ_SIZE;
52
53 async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
54 if self.offset % T::READ_SIZE as u32 != 0 || self.size % T::READ_SIZE as u32 != 0 {
55 return Err(Error::Partition);
56 }
57 if offset + bytes.len() as u32 > self.size {
58 return Err(Error::OutOfBounds);
59 }
60
61 let mut flash = self.flash.lock().await;
62 flash.read(self.offset + offset, bytes).await.map_err(Error::Flash)
63 }
64
65 fn capacity(&self) -> usize {
66 self.size as usize
67 }
68}
69
70#[cfg(feature = "nightly")]
71impl<M: RawMutex, T: NorFlash> NorFlash for Partition<'_, M, T> {
72 const WRITE_SIZE: usize = T::WRITE_SIZE;
73 const ERASE_SIZE: usize = T::ERASE_SIZE;
74
75 async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
76 if self.offset % T::WRITE_SIZE as u32 != 0 || self.size % T::WRITE_SIZE as u32 != 0 {
77 return Err(Error::Partition);
78 }
79 if offset + bytes.len() as u32 > self.size {
80 return Err(Error::OutOfBounds);
81 }
82
83 let mut flash = self.flash.lock().await;
84 flash.write(self.offset + offset, bytes).await.map_err(Error::Flash)
85 }
86
87 async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
88 if self.offset % T::ERASE_SIZE as u32 != 0 || self.size % T::ERASE_SIZE as u32 != 0 {
89 return Err(Error::Partition);
90 }
91 if to > self.size {
92 return Err(Error::OutOfBounds);
93 }
94
95 let mut flash = self.flash.lock().await;
96 flash
97 .erase(self.offset + from, self.offset + to)
98 .await
99 .map_err(Error::Flash)
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use embassy_sync::blocking_mutex::raw::NoopRawMutex;
106
107 use super::*;
108 use crate::flash::mem_flash::MemFlash;
109
110 #[futures_test::test]
111 async fn can_read() {
112 let mut flash = MemFlash::<1024, 128, 4>::default();
113 flash.mem[12..20].fill(0xAA);
114
115 let flash = Mutex::<NoopRawMutex, _>::new(flash);
116 let mut partition = Partition::new(&flash, 8, 12);
117
118 let mut read_buf = [0; 8];
119 partition.read(4, &mut read_buf).await.unwrap();
120
121 assert!(read_buf.iter().position(|&x| x != 0xAA).is_none());
122 }
123
124 #[futures_test::test]
125 async fn can_write() {
126 let flash = MemFlash::<1024, 128, 4>::default();
127
128 let flash = Mutex::<NoopRawMutex, _>::new(flash);
129 let mut partition = Partition::new(&flash, 8, 12);
130
131 let write_buf = [0xAA; 8];
132 partition.write(4, &write_buf).await.unwrap();
133
134 let flash = flash.try_lock().unwrap();
135 assert!(flash.mem[12..20].iter().position(|&x| x != 0xAA).is_none());
136 }
137
138 #[futures_test::test]
139 async fn can_erase() {
140 let flash = MemFlash::<1024, 128, 4>::new(0x00);
141
142 let flash = Mutex::<NoopRawMutex, _>::new(flash);
143 let mut partition = Partition::new(&flash, 128, 256);
144
145 partition.erase(0, 128).await.unwrap();
146
147 let flash = flash.try_lock().unwrap();
148 assert!(flash.mem[128..256].iter().position(|&x| x != 0xFF).is_none());
149 }
150}