aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-05-26 22:31:30 +0200
committerGitHub <[email protected]>2023-05-26 22:31:30 +0200
commitef8695cecb8cd4124cf5045de7ccc0ac80c6ef4a (patch)
treee58b509c86fc7eae837bfa2e0bc6a58808321856
parenta8b426d0fe86e6fc3d1813765946cc82e774c3d3 (diff)
parente495473fc341e1403a004901e0d0a575f33350e0 (diff)
Merge pull request #1494 from rmja/flash-partition
Create flash partition for shared flash access
-rw-r--r--embassy-embedded-hal/Cargo.toml1
-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.rs128
-rw-r--r--embassy-embedded-hal/src/flash/mod.rs11
-rw-r--r--embassy-embedded-hal/src/flash/partition.rs145
6 files changed, 306 insertions, 127 deletions
diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml
index ad2f14568..35c70bb63 100644
--- a/embassy-embedded-hal/Cargo.toml
+++ b/embassy-embedded-hal/Cargo.toml
@@ -31,4 +31,5 @@ nb = "1.0.0"
31defmt = { version = "0.3", optional = true } 31defmt = { version = "0.3", optional = true }
32 32
33[dev-dependencies] 33[dev-dependencies]
34critical-section = { version = "1.1.1", features = ["std"] }
34futures-test = "0.3.17" 35futures-test = "0.3.17"
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..afb0d1a15
--- /dev/null
+++ b/embassy-embedded-hal/src/flash/mem_flash.rs
@@ -0,0 +1,128 @@
1use alloc::vec::Vec;
2
3use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
4#[cfg(feature = "nightly")]
5use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
6
7extern crate alloc;
8
9pub(crate) struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> {
10 pub mem: [u8; SIZE],
11 pub writes: Vec<(u32, usize)>,
12 pub erases: Vec<(u32, u32)>,
13}
14
15impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> {
16 #[allow(unused)]
17 pub const fn new(fill: u8) -> Self {
18 Self {
19 mem: [fill; SIZE],
20 writes: Vec::new(),
21 erases: Vec::new(),
22 }
23 }
24
25 fn read(&mut self, offset: u32, bytes: &mut [u8]) {
26 let len = bytes.len();
27 bytes.copy_from_slice(&self.mem[offset as usize..offset as usize + len]);
28 }
29
30 fn write(&mut self, offset: u32, bytes: &[u8]) {
31 self.writes.push((offset, bytes.len()));
32 let offset = offset as usize;
33 assert_eq!(0, bytes.len() % WRITE_SIZE);
34 assert_eq!(0, offset % WRITE_SIZE);
35 assert!(offset + bytes.len() <= SIZE);
36
37 self.mem[offset..offset + bytes.len()].copy_from_slice(bytes);
38 }
39
40 fn erase(&mut self, from: u32, to: u32) {
41 self.erases.push((from, to));
42 let from = from as usize;
43 let to = to as usize;
44 assert_eq!(0, from % ERASE_SIZE);
45 assert_eq!(0, to % ERASE_SIZE);
46 self.mem[from..to].fill(0xff);
47 }
48}
49
50impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> Default
51 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
52{
53 fn default() -> Self {
54 Self::new(0xff)
55 }
56}
57
58impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType
59 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
60{
61 type Error = core::convert::Infallible;
62}
63
64impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ReadNorFlash
65 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
66{
67 const READ_SIZE: usize = 1;
68
69 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
70 self.read(offset, bytes);
71 Ok(())
72 }
73
74 fn capacity(&self) -> usize {
75 SIZE
76 }
77}
78
79impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> NorFlash
80 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
81{
82 const WRITE_SIZE: usize = WRITE_SIZE;
83 const ERASE_SIZE: usize = ERASE_SIZE;
84
85 fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
86 self.write(offset, bytes);
87 Ok(())
88 }
89
90 fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
91 self.erase(from, to);
92 Ok(())
93 }
94}
95
96#[cfg(feature = "nightly")]
97impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncReadNorFlash
98 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
99{
100 const READ_SIZE: usize = 1;
101
102 async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
103 self.read(offset, bytes);
104 Ok(())
105 }
106
107 fn capacity(&self) -> usize {
108 SIZE
109 }
110}
111
112#[cfg(feature = "nightly")]
113impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncNorFlash
114 for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
115{
116 const WRITE_SIZE: usize = WRITE_SIZE;
117 const ERASE_SIZE: usize = ERASE_SIZE;
118
119 async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
120 self.write(offset, bytes);
121 Ok(())
122 }
123
124 async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
125 self.erase(from, to);
126 Ok(())
127 }
128}
diff --git a/embassy-embedded-hal/src/flash/mod.rs b/embassy-embedded-hal/src/flash/mod.rs
new file mode 100644
index 000000000..0210b198d
--- /dev/null
+++ b/embassy-embedded-hal/src/flash/mod.rs
@@ -0,0 +1,11 @@
1//! Utilities related to flash.
2
3mod concat_flash;
4#[cfg(test)]
5pub(crate) mod mem_flash;
6#[cfg(feature = "nightly")]
7mod partition;
8
9pub use concat_flash::ConcatFlash;
10#[cfg(feature = "nightly")]
11pub 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..66d93c0ea
--- /dev/null
+++ b/embassy-embedded-hal/src/flash/partition.rs
@@ -0,0 +1,145 @@
1use embassy_sync::blocking_mutex::raw::RawMutex;
2use embassy_sync::mutex::Mutex;
3use embedded_storage::nor_flash::{ErrorType, NorFlashError, NorFlashErrorKind};
4use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash};
5
6/// A logical partition of an underlying shared flash
7///
8/// A partition holds an offset and a size of the flash,
9/// and is restricted to operate with that range.
10/// There is no guarantee that muliple partitions on the same flash
11/// operate on mutually exclusive ranges - such a separation is up to
12/// the user to guarantee.
13pub struct Partition<'a, M: RawMutex, T: NorFlash> {
14 flash: &'a Mutex<M, T>,
15 offset: u32,
16 size: u32,
17}
18
19#[derive(Debug)]
20#[cfg_attr(feature = "defmt", derive(defmt::Format))]
21pub enum Error<T> {
22 OutOfBounds,
23 Flash(T),
24}
25
26impl<'a, M: RawMutex, T: NorFlash> Partition<'a, M, T> {
27 /// Create a new partition
28 pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self {
29 if offset % T::READ_SIZE as u32 != 0 || offset % T::WRITE_SIZE as u32 != 0 || offset % T::ERASE_SIZE as u32 != 0
30 {
31 panic!("Partition offset must be a multiple of read, write and erase size");
32 }
33 if size % T::READ_SIZE as u32 != 0 || size % T::WRITE_SIZE as u32 != 0 || size % T::ERASE_SIZE as u32 != 0 {
34 panic!("Partition size must be a multiple of read, write and erase size");
35 }
36 Self { flash, offset, size }
37 }
38}
39
40impl<T: NorFlashError> NorFlashError for Error<T> {
41 fn kind(&self) -> NorFlashErrorKind {
42 match self {
43 Error::OutOfBounds => NorFlashErrorKind::OutOfBounds,
44 Error::Flash(f) => f.kind(),
45 }
46 }
47}
48
49impl<M: RawMutex, T: NorFlash> ErrorType for Partition<'_, M, T> {
50 type Error = Error<T::Error>;
51}
52
53#[cfg(feature = "nightly")]
54impl<M: RawMutex, T: NorFlash> ReadNorFlash for Partition<'_, M, T> {
55 const READ_SIZE: usize = T::READ_SIZE;
56
57 async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
58 if offset + bytes.len() as u32 > self.size {
59 return Err(Error::OutOfBounds);
60 }
61
62 let mut flash = self.flash.lock().await;
63 flash.read(self.offset + offset, bytes).await.map_err(Error::Flash)
64 }
65
66 fn capacity(&self) -> usize {
67 self.size as usize
68 }
69}
70
71#[cfg(feature = "nightly")]
72impl<M: RawMutex, T: NorFlash> NorFlash for Partition<'_, M, T> {
73 const WRITE_SIZE: usize = T::WRITE_SIZE;
74 const ERASE_SIZE: usize = T::ERASE_SIZE;
75
76 async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
77 if offset + bytes.len() as u32 > self.size {
78 return Err(Error::OutOfBounds);
79 }
80
81 let mut flash = self.flash.lock().await;
82 flash.write(self.offset + offset, bytes).await.map_err(Error::Flash)
83 }
84
85 async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
86 if to > self.size {
87 return Err(Error::OutOfBounds);
88 }
89
90 let mut flash = self.flash.lock().await;
91 flash
92 .erase(self.offset + from, self.offset + to)
93 .await
94 .map_err(Error::Flash)
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use embassy_sync::blocking_mutex::raw::NoopRawMutex;
101
102 use super::*;
103 use crate::flash::mem_flash::MemFlash;
104
105 #[futures_test::test]
106 async fn can_read() {
107 let mut flash = MemFlash::<1024, 128, 4>::default();
108 flash.mem[132..132 + 8].fill(0xAA);
109
110 let flash = Mutex::<NoopRawMutex, _>::new(flash);
111 let mut partition = Partition::new(&flash, 128, 256);
112
113 let mut read_buf = [0; 8];
114 partition.read(4, &mut read_buf).await.unwrap();
115
116 assert!(read_buf.iter().position(|&x| x != 0xAA).is_none());
117 }
118
119 #[futures_test::test]
120 async fn can_write() {
121 let flash = MemFlash::<1024, 128, 4>::default();
122
123 let flash = Mutex::<NoopRawMutex, _>::new(flash);
124 let mut partition = Partition::new(&flash, 128, 256);
125
126 let write_buf = [0xAA; 8];
127 partition.write(4, &write_buf).await.unwrap();
128
129 let flash = flash.try_lock().unwrap();
130 assert!(flash.mem[132..132 + 8].iter().position(|&x| x != 0xAA).is_none());
131 }
132
133 #[futures_test::test]
134 async fn can_erase() {
135 let flash = MemFlash::<1024, 128, 4>::new(0x00);
136
137 let flash = Mutex::<NoopRawMutex, _>::new(flash);
138 let mut partition = Partition::new(&flash, 128, 256);
139
140 partition.erase(0, 128).await.unwrap();
141
142 let flash = flash.try_lock().unwrap();
143 assert!(flash.mem[128..256].iter().position(|&x| x != 0xFF).is_none());
144 }
145}