embassy_embedded_hal/flash/partition/
asynch.rs1use embassy_sync::blocking_mutex::raw::RawMutex;
2use embassy_sync::mutex::Mutex;
3use embedded_storage::nor_flash::ErrorType;
4use embedded_storage_async::nor_flash::{MultiwriteNorFlash, NorFlash, ReadNorFlash};
5
6use super::Error;
7
8pub struct Partition<'a, M: RawMutex, T: NorFlash> {
16 flash: &'a Mutex<M, T>,
17 offset: u32,
18 size: u32,
19}
20
21impl<'a, M: RawMutex, T: NorFlash> Clone for Partition<'a, M, T> {
22 fn clone(&self) -> Self {
23 Self {
24 flash: self.flash,
25 offset: self.offset,
26 size: self.size,
27 }
28 }
29}
30
31impl<'a, M: RawMutex, T: NorFlash> Partition<'a, M, T> {
32 pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self {
34 if offset % T::READ_SIZE as u32 != 0 || offset % T::WRITE_SIZE as u32 != 0 || offset % T::ERASE_SIZE as u32 != 0
35 {
36 panic!("Partition offset must be a multiple of read, write and erase size");
37 }
38 if size % T::READ_SIZE as u32 != 0 || size % T::WRITE_SIZE as u32 != 0 || size % T::ERASE_SIZE as u32 != 0 {
39 panic!("Partition size must be a multiple of read, write and erase size");
40 }
41 Self { flash, offset, size }
42 }
43
44 pub const fn offset(&self) -> u32 {
46 self.offset
47 }
48
49 pub const fn size(&self) -> u32 {
51 self.size
52 }
53}
54
55impl<M: RawMutex, T: NorFlash> ErrorType for Partition<'_, M, T> {
56 type Error = Error<T::Error>;
57}
58
59impl<M: RawMutex, T: NorFlash> ReadNorFlash for Partition<'_, M, T> {
60 const READ_SIZE: usize = T::READ_SIZE;
61
62 async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
63 if offset + bytes.len() as u32 > self.size {
64 return Err(Error::OutOfBounds);
65 }
66
67 let mut flash = self.flash.lock().await;
68 flash.read(self.offset + offset, bytes).await.map_err(Error::Flash)
69 }
70
71 fn capacity(&self) -> usize {
72 self.size as usize
73 }
74}
75
76impl<M: RawMutex, T: NorFlash> NorFlash for Partition<'_, M, T> {
77 const WRITE_SIZE: usize = T::WRITE_SIZE;
78 const ERASE_SIZE: usize = T::ERASE_SIZE;
79
80 async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
81 if offset + bytes.len() as u32 > self.size {
82 return Err(Error::OutOfBounds);
83 }
84
85 let mut flash = self.flash.lock().await;
86 flash.write(self.offset + offset, bytes).await.map_err(Error::Flash)
87 }
88
89 async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
90 if to > self.size {
91 return Err(Error::OutOfBounds);
92 }
93
94 let mut flash = self.flash.lock().await;
95 flash
96 .erase(self.offset + from, self.offset + to)
97 .await
98 .map_err(Error::Flash)
99 }
100}
101
102impl<M: RawMutex, T: MultiwriteNorFlash> MultiwriteNorFlash for Partition<'_, M, T> {}
103
104#[cfg(test)]
105mod tests {
106 use embassy_sync::blocking_mutex::raw::NoopRawMutex;
107
108 use super::*;
109 use crate::flash::mem_flash::MemFlash;
110
111 #[futures_test::test]
112 async fn can_read() {
113 let mut flash = MemFlash::<1024, 128, 4>::default();
114 flash.mem[132..132 + 8].fill(0xAA);
115
116 let flash = Mutex::<NoopRawMutex, _>::new(flash);
117 let mut partition = Partition::new(&flash, 128, 256);
118
119 let mut read_buf = [0; 8];
120 partition.read(4, &mut read_buf).await.unwrap();
121
122 assert!(read_buf.iter().position(|&x| x != 0xAA).is_none());
123 }
124
125 #[futures_test::test]
126 async fn can_write() {
127 let flash = MemFlash::<1024, 128, 4>::default();
128
129 let flash = Mutex::<NoopRawMutex, _>::new(flash);
130 let mut partition = Partition::new(&flash, 128, 256);
131
132 let write_buf = [0xAA; 8];
133 partition.write(4, &write_buf).await.unwrap();
134
135 let flash = flash.try_lock().unwrap();
136 assert!(flash.mem[132..132 + 8].iter().position(|&x| x != 0xAA).is_none());
137 }
138
139 #[futures_test::test]
140 async fn can_erase() {
141 let flash = MemFlash::<1024, 128, 4>::new(0x00);
142
143 let flash = Mutex::<NoopRawMutex, _>::new(flash);
144 let mut partition = Partition::new(&flash, 128, 256);
145
146 partition.erase(0, 128).await.unwrap();
147
148 let flash = flash.try_lock().unwrap();
149 assert!(flash.mem[128..256].iter().position(|&x| x != 0xFF).is_none());
150 }
151}