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::{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
102#[cfg(test)]
103mod tests {
104 use embassy_sync::blocking_mutex::raw::NoopRawMutex;
105
106 use super::*;
107 use crate::flash::mem_flash::MemFlash;
108
109 #[futures_test::test]
110 async fn can_read() {
111 let mut flash = MemFlash::<1024, 128, 4>::default();
112 flash.mem[132..132 + 8].fill(0xAA);
113
114 let flash = Mutex::<NoopRawMutex, _>::new(flash);
115 let mut partition = Partition::new(&flash, 128, 256);
116
117 let mut read_buf = [0; 8];
118 partition.read(4, &mut read_buf).await.unwrap();
119
120 assert!(read_buf.iter().position(|&x| x != 0xAA).is_none());
121 }
122
123 #[futures_test::test]
124 async fn can_write() {
125 let flash = MemFlash::<1024, 128, 4>::default();
126
127 let flash = Mutex::<NoopRawMutex, _>::new(flash);
128 let mut partition = Partition::new(&flash, 128, 256);
129
130 let write_buf = [0xAA; 8];
131 partition.write(4, &write_buf).await.unwrap();
132
133 let flash = flash.try_lock().unwrap();
134 assert!(flash.mem[132..132 + 8].iter().position(|&x| x != 0xAA).is_none());
135 }
136
137 #[futures_test::test]
138 async fn can_erase() {
139 let flash = MemFlash::<1024, 128, 4>::new(0x00);
140
141 let flash = Mutex::<NoopRawMutex, _>::new(flash);
142 let mut partition = Partition::new(&flash, 128, 256);
143
144 partition.erase(0, 128).await.unwrap();
145
146 let flash = flash.try_lock().unwrap();
147 assert!(flash.mem[128..256].iter().position(|&x| x != 0xFF).is_none());
148 }
149}