diff options
Diffstat (limited to 'embassy-embedded-hal/src/flash/partition/blocking.rs')
| -rw-r--r-- | embassy-embedded-hal/src/flash/partition/blocking.rs | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/embassy-embedded-hal/src/flash/partition/blocking.rs b/embassy-embedded-hal/src/flash/partition/blocking.rs index 1a4c80f92..dc52e292a 100644 --- a/embassy-embedded-hal/src/flash/partition/blocking.rs +++ b/embassy-embedded-hal/src/flash/partition/blocking.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | use core::cell::RefCell; | ||
| 2 | |||
| 1 | use embassy_sync::blocking_mutex::raw::RawMutex; | 3 | use embassy_sync::blocking_mutex::raw::RawMutex; |
| 2 | use embassy_sync::blocking_mutex::Mutex; | 4 | use embassy_sync::blocking_mutex::Mutex; |
| 3 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | 5 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; |
| @@ -12,14 +14,14 @@ use super::Error; | |||
| 12 | /// operate on mutually exclusive ranges - such a separation is up to | 14 | /// operate on mutually exclusive ranges - such a separation is up to |
| 13 | /// the user to guarantee. | 15 | /// the user to guarantee. |
| 14 | pub struct BlockingPartition<'a, M: RawMutex, T: NorFlash> { | 16 | pub struct BlockingPartition<'a, M: RawMutex, T: NorFlash> { |
| 15 | flash: &'a Mutex<M, T>, | 17 | flash: &'a Mutex<M, RefCell<T>>, |
| 16 | offset: u32, | 18 | offset: u32, |
| 17 | size: u32, | 19 | size: u32, |
| 18 | } | 20 | } |
| 19 | 21 | ||
| 20 | impl<'a, M: RawMutex, T: NorFlash> BlockingPartition<'a, M, T> { | 22 | impl<'a, M: RawMutex, T: NorFlash> BlockingPartition<'a, M, T> { |
| 21 | /// Create a new partition | 23 | /// Create a new partition |
| 22 | pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self { | 24 | pub const fn new(flash: &'a Mutex<M, RefCell<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 | 25 | if offset % T::READ_SIZE as u32 != 0 || offset % T::WRITE_SIZE as u32 != 0 || offset % T::ERASE_SIZE as u32 != 0 |
| 24 | { | 26 | { |
| 25 | panic!("Partition offset must be a multiple of read, write and erase size"); | 27 | panic!("Partition offset must be a multiple of read, write and erase size"); |
| @@ -43,8 +45,12 @@ impl<M: RawMutex, T: NorFlash> ReadNorFlash for BlockingPartition<'_, M, T> { | |||
| 43 | return Err(Error::OutOfBounds); | 45 | return Err(Error::OutOfBounds); |
| 44 | } | 46 | } |
| 45 | 47 | ||
| 46 | self.flash | 48 | self.flash.lock(|flash| { |
| 47 | .lock(|flash| flash.read(self.offset + offset, bytes).map_err(Error::Flash)) | 49 | flash |
| 50 | .borrow_mut() | ||
| 51 | .read(self.offset + offset, bytes) | ||
| 52 | .map_err(Error::Flash) | ||
| 53 | }) | ||
| 48 | } | 54 | } |
| 49 | 55 | ||
| 50 | fn capacity(&self) -> usize { | 56 | fn capacity(&self) -> usize { |
| @@ -61,8 +67,12 @@ impl<M: RawMutex, T: NorFlash> NorFlash for BlockingPartition<'_, M, T> { | |||
| 61 | return Err(Error::OutOfBounds); | 67 | return Err(Error::OutOfBounds); |
| 62 | } | 68 | } |
| 63 | 69 | ||
| 64 | self.flash | 70 | self.flash.lock(|flash| { |
| 65 | .lock(|flash| flash.write(self.offset + offset, bytes).map_err(Error::Flash)) | 71 | flash |
| 72 | .borrow_mut() | ||
| 73 | .write(self.offset + offset, bytes) | ||
| 74 | .map_err(Error::Flash) | ||
| 75 | }) | ||
| 66 | } | 76 | } |
| 67 | 77 | ||
| 68 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | 78 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { |
| @@ -70,8 +80,12 @@ impl<M: RawMutex, T: NorFlash> NorFlash for BlockingPartition<'_, M, T> { | |||
| 70 | return Err(Error::OutOfBounds); | 80 | return Err(Error::OutOfBounds); |
| 71 | } | 81 | } |
| 72 | 82 | ||
| 73 | self.flash | 83 | self.flash.lock(|flash| { |
| 74 | .lock(|flash| flash.erase(self.offset + from, self.offset + to).map_err(Error::Flash)) | 84 | flash |
| 85 | .borrow_mut() | ||
| 86 | .erase(self.offset + from, self.offset + to) | ||
| 87 | .map_err(Error::Flash) | ||
| 88 | }) | ||
| 75 | } | 89 | } |
| 76 | } | 90 | } |
| 77 | 91 | ||
| @@ -87,7 +101,7 @@ mod tests { | |||
| 87 | let mut flash = MemFlash::<1024, 128, 4>::default(); | 101 | let mut flash = MemFlash::<1024, 128, 4>::default(); |
| 88 | flash.mem[132..132 + 8].fill(0xAA); | 102 | flash.mem[132..132 + 8].fill(0xAA); |
| 89 | 103 | ||
| 90 | let flash = Mutex::<NoopRawMutex, _>::new(flash); | 104 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(flash)); |
| 91 | let mut partition = BlockingPartition::new(&flash, 128, 256); | 105 | let mut partition = BlockingPartition::new(&flash, 128, 256); |
| 92 | 106 | ||
| 93 | let mut read_buf = [0; 8]; | 107 | let mut read_buf = [0; 8]; |
| @@ -100,13 +114,13 @@ mod tests { | |||
| 100 | fn can_write() { | 114 | fn can_write() { |
| 101 | let flash = MemFlash::<1024, 128, 4>::default(); | 115 | let flash = MemFlash::<1024, 128, 4>::default(); |
| 102 | 116 | ||
| 103 | let flash = Mutex::<NoopRawMutex, _>::new(flash); | 117 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(flash)); |
| 104 | let mut partition = BlockingPartition::new(&flash, 128, 256); | 118 | let mut partition = BlockingPartition::new(&flash, 128, 256); |
| 105 | 119 | ||
| 106 | let write_buf = [0xAA; 8]; | 120 | let write_buf = [0xAA; 8]; |
| 107 | partition.write(4, &write_buf).unwrap(); | 121 | partition.write(4, &write_buf).unwrap(); |
| 108 | 122 | ||
| 109 | let flash = flash.into_inner(); | 123 | let flash = flash.into_inner().take(); |
| 110 | assert!(flash.mem[132..132 + 8].iter().position(|&x| x != 0xAA).is_none()); | 124 | assert!(flash.mem[132..132 + 8].iter().position(|&x| x != 0xAA).is_none()); |
| 111 | } | 125 | } |
| 112 | 126 | ||
| @@ -114,12 +128,12 @@ mod tests { | |||
| 114 | fn can_erase() { | 128 | fn can_erase() { |
| 115 | let flash = MemFlash::<1024, 128, 4>::new(0x00); | 129 | let flash = MemFlash::<1024, 128, 4>::new(0x00); |
| 116 | 130 | ||
| 117 | let flash = Mutex::<NoopRawMutex, _>::new(flash); | 131 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(flash)); |
| 118 | let mut partition = BlockingPartition::new(&flash, 128, 256); | 132 | let mut partition = BlockingPartition::new(&flash, 128, 256); |
| 119 | 133 | ||
| 120 | partition.erase(0, 128).unwrap(); | 134 | partition.erase(0, 128).unwrap(); |
| 121 | 135 | ||
| 122 | let flash = flash.into_inner(); | 136 | let flash = flash.into_inner().take(); |
| 123 | assert!(flash.mem[128..256].iter().position(|&x| x != 0xFF).is_none()); | 137 | assert!(flash.mem[128..256].iter().position(|&x| x != 0xFF).is_none()); |
| 124 | } | 138 | } |
| 125 | } | 139 | } |
