diff options
| -rw-r--r-- | embassy-embedded-hal/src/flash/mod.rs | 5 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/flash/partition/asynch.rs (renamed from embassy-embedded-hal/src/flash/partition.rs) | 22 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/flash/partition/blocking.rs | 139 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/flash/partition/mod.rs | 30 |
4 files changed, 173 insertions, 23 deletions
diff --git a/embassy-embedded-hal/src/flash/mod.rs b/embassy-embedded-hal/src/flash/mod.rs index 0210b198d..7e4ef290f 100644 --- a/embassy-embedded-hal/src/flash/mod.rs +++ b/embassy-embedded-hal/src/flash/mod.rs | |||
| @@ -3,9 +3,6 @@ | |||
| 3 | mod concat_flash; | 3 | mod concat_flash; |
| 4 | #[cfg(test)] | 4 | #[cfg(test)] |
| 5 | pub(crate) mod mem_flash; | 5 | pub(crate) mod mem_flash; |
| 6 | #[cfg(feature = "nightly")] | 6 | pub mod partition; |
| 7 | mod partition; | ||
| 8 | 7 | ||
| 9 | pub use concat_flash::ConcatFlash; | 8 | pub use concat_flash::ConcatFlash; |
| 10 | #[cfg(feature = "nightly")] | ||
| 11 | pub use partition::Partition; | ||
diff --git a/embassy-embedded-hal/src/flash/partition.rs b/embassy-embedded-hal/src/flash/partition/asynch.rs index 66d93c0ea..141e0d9fc 100644 --- a/embassy-embedded-hal/src/flash/partition.rs +++ b/embassy-embedded-hal/src/flash/partition/asynch.rs | |||
| @@ -1,8 +1,10 @@ | |||
| 1 | use embassy_sync::blocking_mutex::raw::RawMutex; | 1 | use embassy_sync::blocking_mutex::raw::RawMutex; |
| 2 | use embassy_sync::mutex::Mutex; | 2 | use embassy_sync::mutex::Mutex; |
| 3 | use embedded_storage::nor_flash::{ErrorType, NorFlashError, NorFlashErrorKind}; | 3 | use embedded_storage::nor_flash::ErrorType; |
| 4 | use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash}; | 4 | use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash}; |
| 5 | 5 | ||
| 6 | use super::Error; | ||
| 7 | |||
| 6 | /// A logical partition of an underlying shared flash | 8 | /// A logical partition of an underlying shared flash |
| 7 | /// | 9 | /// |
| 8 | /// A partition holds an offset and a size of the flash, | 10 | /// A partition holds an offset and a size of the flash, |
| @@ -16,13 +18,6 @@ pub struct Partition<'a, M: RawMutex, T: NorFlash> { | |||
| 16 | size: u32, | 18 | size: u32, |
| 17 | } | 19 | } |
| 18 | 20 | ||
| 19 | #[derive(Debug)] | ||
| 20 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 21 | pub enum Error<T> { | ||
| 22 | OutOfBounds, | ||
| 23 | Flash(T), | ||
| 24 | } | ||
| 25 | |||
| 26 | impl<'a, M: RawMutex, T: NorFlash> Partition<'a, M, T> { | 21 | impl<'a, M: RawMutex, T: NorFlash> Partition<'a, M, T> { |
| 27 | /// Create a new partition | 22 | /// Create a new partition |
| 28 | pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self { | 23 | pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self { |
| @@ -37,20 +32,10 @@ impl<'a, M: RawMutex, T: NorFlash> Partition<'a, M, T> { | |||
| 37 | } | 32 | } |
| 38 | } | 33 | } |
| 39 | 34 | ||
| 40 | impl<T: NorFlashError> NorFlashError for Error<T> { | ||
| 41 | fn kind(&self) -> NorFlashErrorKind { | ||
| 42 | match self { | ||
| 43 | Error::OutOfBounds => NorFlashErrorKind::OutOfBounds, | ||
| 44 | Error::Flash(f) => f.kind(), | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | impl<M: RawMutex, T: NorFlash> ErrorType for Partition<'_, M, T> { | 35 | impl<M: RawMutex, T: NorFlash> ErrorType for Partition<'_, M, T> { |
| 50 | type Error = Error<T::Error>; | 36 | type Error = Error<T::Error>; |
| 51 | } | 37 | } |
| 52 | 38 | ||
| 53 | #[cfg(feature = "nightly")] | ||
| 54 | impl<M: RawMutex, T: NorFlash> ReadNorFlash for Partition<'_, M, T> { | 39 | impl<M: RawMutex, T: NorFlash> ReadNorFlash for Partition<'_, M, T> { |
| 55 | const READ_SIZE: usize = T::READ_SIZE; | 40 | const READ_SIZE: usize = T::READ_SIZE; |
| 56 | 41 | ||
| @@ -68,7 +53,6 @@ impl<M: RawMutex, T: NorFlash> ReadNorFlash for Partition<'_, M, T> { | |||
| 68 | } | 53 | } |
| 69 | } | 54 | } |
| 70 | 55 | ||
| 71 | #[cfg(feature = "nightly")] | ||
| 72 | impl<M: RawMutex, T: NorFlash> NorFlash for Partition<'_, M, T> { | 56 | impl<M: RawMutex, T: NorFlash> NorFlash for Partition<'_, M, T> { |
| 73 | const WRITE_SIZE: usize = T::WRITE_SIZE; | 57 | const WRITE_SIZE: usize = T::WRITE_SIZE; |
| 74 | const ERASE_SIZE: usize = T::ERASE_SIZE; | 58 | const ERASE_SIZE: usize = T::ERASE_SIZE; |
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..dc52e292a --- /dev/null +++ b/embassy-embedded-hal/src/flash/partition/blocking.rs | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | use core::cell::RefCell; | ||
| 2 | |||
| 3 | use embassy_sync::blocking_mutex::raw::RawMutex; | ||
| 4 | use embassy_sync::blocking_mutex::Mutex; | ||
| 5 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | ||
| 6 | |||
| 7 | use super::Error; | ||
| 8 | |||
| 9 | /// A logical partition of an underlying shared flash | ||
| 10 | /// | ||
| 11 | /// A partition holds an offset and a size of the flash, | ||
| 12 | /// and is restricted to operate with that range. | ||
| 13 | /// There is no guarantee that muliple partitions on the same flash | ||
| 14 | /// operate on mutually exclusive ranges - such a separation is up to | ||
| 15 | /// the user to guarantee. | ||
| 16 | pub struct BlockingPartition<'a, M: RawMutex, T: NorFlash> { | ||
| 17 | flash: &'a Mutex<M, RefCell<T>>, | ||
| 18 | offset: u32, | ||
| 19 | size: u32, | ||
| 20 | } | ||
| 21 | |||
| 22 | impl<'a, M: RawMutex, T: NorFlash> BlockingPartition<'a, M, T> { | ||
| 23 | /// Create a new partition | ||
| 24 | pub const fn new(flash: &'a Mutex<M, RefCell<T>>, offset: u32, size: u32) -> Self { | ||
| 25 | if offset % T::READ_SIZE as u32 != 0 || offset % T::WRITE_SIZE as u32 != 0 || offset % T::ERASE_SIZE as u32 != 0 | ||
| 26 | { | ||
| 27 | panic!("Partition offset must be a multiple of read, write and erase size"); | ||
| 28 | } | ||
| 29 | if size % T::READ_SIZE as u32 != 0 || size % T::WRITE_SIZE as u32 != 0 || size % T::ERASE_SIZE as u32 != 0 { | ||
| 30 | panic!("Partition size must be a multiple of read, write and erase size"); | ||
| 31 | } | ||
| 32 | Self { flash, offset, size } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | impl<M: RawMutex, T: NorFlash> ErrorType for BlockingPartition<'_, M, T> { | ||
| 37 | type Error = Error<T::Error>; | ||
| 38 | } | ||
| 39 | |||
| 40 | impl<M: RawMutex, T: NorFlash> ReadNorFlash for BlockingPartition<'_, M, T> { | ||
| 41 | const READ_SIZE: usize = T::READ_SIZE; | ||
| 42 | |||
| 43 | fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { | ||
| 44 | if offset + bytes.len() as u32 > self.size { | ||
| 45 | return Err(Error::OutOfBounds); | ||
| 46 | } | ||
| 47 | |||
| 48 | self.flash.lock(|flash| { | ||
| 49 | flash | ||
| 50 | .borrow_mut() | ||
| 51 | .read(self.offset + offset, bytes) | ||
| 52 | .map_err(Error::Flash) | ||
| 53 | }) | ||
| 54 | } | ||
| 55 | |||
| 56 | fn capacity(&self) -> usize { | ||
| 57 | self.size as usize | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | impl<M: RawMutex, T: NorFlash> NorFlash for BlockingPartition<'_, M, T> { | ||
| 62 | const WRITE_SIZE: usize = T::WRITE_SIZE; | ||
| 63 | const ERASE_SIZE: usize = T::ERASE_SIZE; | ||
| 64 | |||
| 65 | fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 66 | if offset + bytes.len() as u32 > self.size { | ||
| 67 | return Err(Error::OutOfBounds); | ||
| 68 | } | ||
| 69 | |||
| 70 | self.flash.lock(|flash| { | ||
| 71 | flash | ||
| 72 | .borrow_mut() | ||
| 73 | .write(self.offset + offset, bytes) | ||
| 74 | .map_err(Error::Flash) | ||
| 75 | }) | ||
| 76 | } | ||
| 77 | |||
| 78 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 79 | if to > self.size { | ||
| 80 | return Err(Error::OutOfBounds); | ||
| 81 | } | ||
| 82 | |||
| 83 | self.flash.lock(|flash| { | ||
| 84 | flash | ||
| 85 | .borrow_mut() | ||
| 86 | .erase(self.offset + from, self.offset + to) | ||
| 87 | .map_err(Error::Flash) | ||
| 88 | }) | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | #[cfg(test)] | ||
| 93 | mod tests { | ||
| 94 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | ||
| 95 | |||
| 96 | use super::*; | ||
| 97 | use crate::flash::mem_flash::MemFlash; | ||
| 98 | |||
| 99 | #[test] | ||
| 100 | fn can_read() { | ||
| 101 | let mut flash = MemFlash::<1024, 128, 4>::default(); | ||
| 102 | flash.mem[132..132 + 8].fill(0xAA); | ||
| 103 | |||
| 104 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(flash)); | ||
| 105 | let mut partition = BlockingPartition::new(&flash, 128, 256); | ||
| 106 | |||
| 107 | let mut read_buf = [0; 8]; | ||
| 108 | partition.read(4, &mut read_buf).unwrap(); | ||
| 109 | |||
| 110 | assert!(read_buf.iter().position(|&x| x != 0xAA).is_none()); | ||
| 111 | } | ||
| 112 | |||
| 113 | #[test] | ||
| 114 | fn can_write() { | ||
| 115 | let flash = MemFlash::<1024, 128, 4>::default(); | ||
| 116 | |||
| 117 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(flash)); | ||
| 118 | let mut partition = BlockingPartition::new(&flash, 128, 256); | ||
| 119 | |||
| 120 | let write_buf = [0xAA; 8]; | ||
| 121 | partition.write(4, &write_buf).unwrap(); | ||
| 122 | |||
| 123 | let flash = flash.into_inner().take(); | ||
| 124 | assert!(flash.mem[132..132 + 8].iter().position(|&x| x != 0xAA).is_none()); | ||
| 125 | } | ||
| 126 | |||
| 127 | #[test] | ||
| 128 | fn can_erase() { | ||
| 129 | let flash = MemFlash::<1024, 128, 4>::new(0x00); | ||
| 130 | |||
| 131 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(flash)); | ||
| 132 | let mut partition = BlockingPartition::new(&flash, 128, 256); | ||
| 133 | |||
| 134 | partition.erase(0, 128).unwrap(); | ||
| 135 | |||
| 136 | let flash = flash.into_inner().take(); | ||
| 137 | assert!(flash.mem[128..256].iter().position(|&x| x != 0xFF).is_none()); | ||
| 138 | } | ||
| 139 | } | ||
diff --git a/embassy-embedded-hal/src/flash/partition/mod.rs b/embassy-embedded-hal/src/flash/partition/mod.rs new file mode 100644 index 000000000..a12e49ce1 --- /dev/null +++ b/embassy-embedded-hal/src/flash/partition/mod.rs | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | //! Flash Partition utilities | ||
| 2 | |||
| 3 | use embedded_storage::nor_flash::{NorFlashError, NorFlashErrorKind}; | ||
| 4 | |||
| 5 | #[cfg(feature = "nightly")] | ||
| 6 | mod asynch; | ||
| 7 | mod blocking; | ||
| 8 | |||
| 9 | #[cfg(feature = "nightly")] | ||
| 10 | pub use asynch::Partition; | ||
| 11 | pub use blocking::BlockingPartition; | ||
| 12 | |||
| 13 | /// Partition error | ||
| 14 | #[derive(Debug)] | ||
| 15 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 16 | pub enum Error<T> { | ||
| 17 | /// The requested flash area is outside the partition | ||
| 18 | OutOfBounds, | ||
| 19 | /// Underlying flash error | ||
| 20 | Flash(T), | ||
| 21 | } | ||
| 22 | |||
| 23 | impl<T: NorFlashError> NorFlashError for Error<T> { | ||
| 24 | fn kind(&self) -> NorFlashErrorKind { | ||
| 25 | match self { | ||
| 26 | Error::OutOfBounds => NorFlashErrorKind::OutOfBounds, | ||
| 27 | Error::Flash(f) => f.kind(), | ||
| 28 | } | ||
| 29 | } | ||
| 30 | } | ||
