| 1 | use embassy_sync::blocking_mutex::raw::RawMutex; |
| 2 | use embassy_sync::mutex::Mutex; |
| 3 | use embedded_storage::nor_flash::ErrorType; |
| 4 | use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash}; |
| 5 | |
| 6 | use super::Error; |
| 7 | |
| 8 | /// A logical partition of an underlying shared flash |
| 9 | /// |
| 10 | /// A partition holds an offset and a size of the flash, |
| 11 | /// and is restricted to operate with that range. |
| 12 | /// There is no guarantee that muliple partitions on the same flash |
| 13 | /// operate on mutually exclusive ranges - such a separation is up to |
| 14 | /// the user to guarantee. |
| 15 | pub struct Partition<'a, M: RawMutex, T: NorFlash> { |
| 16 | flash: &'a Mutex<M, T>, |
| 17 | offset: u32, |
| 18 | size: u32, |
| 19 | } |
| 20 | |
| 21 | impl<'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 | |
| 31 | impl<'a, M: RawMutex, T: NorFlash> Partition<'a, M, T> { |
| 32 | /// Create a new partition |
| 33 | 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 | /// Get the partition offset within the flash |
| 45 | pub const fn offset(&self) -> u32 { |
| 46 | self.offset |
| 47 | } |
| 48 | |
| 49 | /// Get the partition size |
| 50 | pub const fn size(&self) -> u32 { |
| 51 | self.size |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl<M: RawMutex, T: NorFlash> ErrorType for Partition<'_, M, T> { |
| 56 | type Error = Error<T::Error>; |
| 57 | } |
| 58 | |
| 59 | impl<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: MutexGuard<'_, M, T> = self.flash.lock().await; |
| 68 | flash.read(self.offset + offset, bytes).await.map_err(op:Error::Flash) |
| 69 | } |
| 70 | |
| 71 | fn capacity(&self) -> usize { |
| 72 | self.size as usize |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl<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)] |
| 103 | mod 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 | } |
| 150 | |