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