diff options
Diffstat (limited to 'embassy-embedded-hal/src/flash/partition/blocking.rs')
| -rw-r--r-- | embassy-embedded-hal/src/flash/partition/blocking.rs | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/flash/partition/blocking.rs b/embassy-embedded-hal/src/flash/partition/blocking.rs new file mode 100644 index 000000000..1a4c80f92 --- /dev/null +++ b/embassy-embedded-hal/src/flash/partition/blocking.rs | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | use embassy_sync::blocking_mutex::raw::RawMutex; | ||
| 2 | use embassy_sync::blocking_mutex::Mutex; | ||
| 3 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | ||
| 4 | |||
| 5 | use super::Error; | ||
| 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. | ||
| 14 | pub struct BlockingPartition<'a, M: RawMutex, T: NorFlash> { | ||
| 15 | flash: &'a Mutex<M, T>, | ||
| 16 | offset: u32, | ||
| 17 | size: u32, | ||
| 18 | } | ||
| 19 | |||
| 20 | impl<'a, M: RawMutex, T: NorFlash> BlockingPartition<'a, M, T> { | ||
| 21 | /// Create a new partition | ||
| 22 | pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self { | ||
| 23 | if offset % T::READ_SIZE as u32 != 0 || offset % T::WRITE_SIZE as u32 != 0 || offset % T::ERASE_SIZE as u32 != 0 | ||
| 24 | { | ||
| 25 | panic!("Partition offset must be a multiple of read, write and erase size"); | ||
| 26 | } | ||
| 27 | if size % T::READ_SIZE as u32 != 0 || size % T::WRITE_SIZE as u32 != 0 || size % T::ERASE_SIZE as u32 != 0 { | ||
| 28 | panic!("Partition size must be a multiple of read, write and erase size"); | ||
| 29 | } | ||
| 30 | Self { flash, offset, size } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | impl<M: RawMutex, T: NorFlash> ErrorType for BlockingPartition<'_, M, T> { | ||
| 35 | type Error = Error<T::Error>; | ||
| 36 | } | ||
| 37 | |||
| 38 | impl<M: RawMutex, T: NorFlash> ReadNorFlash for BlockingPartition<'_, M, T> { | ||
| 39 | const READ_SIZE: usize = T::READ_SIZE; | ||
| 40 | |||
| 41 | fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { | ||
| 42 | if offset + bytes.len() as u32 > self.size { | ||
| 43 | return Err(Error::OutOfBounds); | ||
| 44 | } | ||
| 45 | |||
| 46 | self.flash | ||
| 47 | .lock(|flash| flash.read(self.offset + offset, bytes).map_err(Error::Flash)) | ||
| 48 | } | ||
| 49 | |||
| 50 | fn capacity(&self) -> usize { | ||
| 51 | self.size as usize | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | impl<M: RawMutex, T: NorFlash> NorFlash for BlockingPartition<'_, M, T> { | ||
| 56 | const WRITE_SIZE: usize = T::WRITE_SIZE; | ||
| 57 | const ERASE_SIZE: usize = T::ERASE_SIZE; | ||
| 58 | |||
| 59 | fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 60 | if offset + bytes.len() as u32 > self.size { | ||
| 61 | return Err(Error::OutOfBounds); | ||
| 62 | } | ||
| 63 | |||
| 64 | self.flash | ||
| 65 | .lock(|flash| flash.write(self.offset + offset, bytes).map_err(Error::Flash)) | ||
| 66 | } | ||
| 67 | |||
| 68 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 69 | if to > self.size { | ||
| 70 | return Err(Error::OutOfBounds); | ||
| 71 | } | ||
| 72 | |||
| 73 | self.flash | ||
| 74 | .lock(|flash| flash.erase(self.offset + from, self.offset + to).map_err(Error::Flash)) | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | #[cfg(test)] | ||
| 79 | mod tests { | ||
| 80 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | ||
| 81 | |||
| 82 | use super::*; | ||
| 83 | use crate::flash::mem_flash::MemFlash; | ||
| 84 | |||
| 85 | #[test] | ||
| 86 | fn can_read() { | ||
| 87 | let mut flash = MemFlash::<1024, 128, 4>::default(); | ||
| 88 | flash.mem[132..132 + 8].fill(0xAA); | ||
| 89 | |||
| 90 | let flash = Mutex::<NoopRawMutex, _>::new(flash); | ||
| 91 | let mut partition = BlockingPartition::new(&flash, 128, 256); | ||
| 92 | |||
| 93 | let mut read_buf = [0; 8]; | ||
| 94 | partition.read(4, &mut read_buf).unwrap(); | ||
| 95 | |||
| 96 | assert!(read_buf.iter().position(|&x| x != 0xAA).is_none()); | ||
| 97 | } | ||
| 98 | |||
| 99 | #[test] | ||
| 100 | fn can_write() { | ||
| 101 | let flash = MemFlash::<1024, 128, 4>::default(); | ||
| 102 | |||
| 103 | let flash = Mutex::<NoopRawMutex, _>::new(flash); | ||
| 104 | let mut partition = BlockingPartition::new(&flash, 128, 256); | ||
| 105 | |||
| 106 | let write_buf = [0xAA; 8]; | ||
| 107 | partition.write(4, &write_buf).unwrap(); | ||
| 108 | |||
| 109 | let flash = flash.into_inner(); | ||
| 110 | assert!(flash.mem[132..132 + 8].iter().position(|&x| x != 0xAA).is_none()); | ||
| 111 | } | ||
| 112 | |||
| 113 | #[test] | ||
| 114 | fn can_erase() { | ||
| 115 | let flash = MemFlash::<1024, 128, 4>::new(0x00); | ||
| 116 | |||
| 117 | let flash = Mutex::<NoopRawMutex, _>::new(flash); | ||
| 118 | let mut partition = BlockingPartition::new(&flash, 128, 256); | ||
| 119 | |||
| 120 | partition.erase(0, 128).unwrap(); | ||
| 121 | |||
| 122 | let flash = flash.into_inner(); | ||
| 123 | assert!(flash.mem[128..256].iter().position(|&x| x != 0xFF).is_none()); | ||
| 124 | } | ||
| 125 | } | ||
