diff options
| -rw-r--r-- | embassy-embedded-hal/src/flash/mem_flash.rs | 1 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/flash/mod.rs | 2 | ||||
| -rw-r--r-- | embassy-embedded-hal/src/flash/partition.rs | 35 |
3 files changed, 18 insertions, 20 deletions
diff --git a/embassy-embedded-hal/src/flash/mem_flash.rs b/embassy-embedded-hal/src/flash/mem_flash.rs index 4e10627df..afb0d1a15 100644 --- a/embassy-embedded-hal/src/flash/mem_flash.rs +++ b/embassy-embedded-hal/src/flash/mem_flash.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | use alloc::vec::Vec; | 1 | use alloc::vec::Vec; |
| 2 | 2 | ||
| 3 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | 3 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; |
| 4 | #[cfg(feature = "nightly")] | ||
| 4 | use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash}; | 5 | use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash}; |
| 5 | 6 | ||
| 6 | extern crate alloc; | 7 | extern crate alloc; |
diff --git a/embassy-embedded-hal/src/flash/mod.rs b/embassy-embedded-hal/src/flash/mod.rs index c80dd6aac..0210b198d 100644 --- a/embassy-embedded-hal/src/flash/mod.rs +++ b/embassy-embedded-hal/src/flash/mod.rs | |||
| @@ -3,7 +3,9 @@ | |||
| 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 | mod partition; | 7 | mod partition; |
| 7 | 8 | ||
| 8 | pub use concat_flash::ConcatFlash; | 9 | pub use concat_flash::ConcatFlash; |
| 10 | #[cfg(feature = "nightly")] | ||
| 9 | pub use partition::Partition; | 11 | pub use partition::Partition; |
diff --git a/embassy-embedded-hal/src/flash/partition.rs b/embassy-embedded-hal/src/flash/partition.rs index 084425e95..66d93c0ea 100644 --- a/embassy-embedded-hal/src/flash/partition.rs +++ b/embassy-embedded-hal/src/flash/partition.rs | |||
| @@ -1,7 +1,6 @@ | |||
| 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, NorFlashError, NorFlashErrorKind}; |
| 4 | #[cfg(feature = "nightly")] | ||
| 5 | use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash}; | 4 | use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash}; |
| 6 | 5 | ||
| 7 | /// A logical partition of an underlying shared flash | 6 | /// A logical partition of an underlying shared flash |
| @@ -11,7 +10,7 @@ use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash}; | |||
| 11 | /// There is no guarantee that muliple partitions on the same flash | 10 | /// There is no guarantee that muliple partitions on the same flash |
| 12 | /// operate on mutually exclusive ranges - such a separation is up to | 11 | /// operate on mutually exclusive ranges - such a separation is up to |
| 13 | /// the user to guarantee. | 12 | /// the user to guarantee. |
| 14 | pub struct Partition<'a, M: RawMutex, T> { | 13 | pub struct Partition<'a, M: RawMutex, T: NorFlash> { |
| 15 | flash: &'a Mutex<M, T>, | 14 | flash: &'a Mutex<M, T>, |
| 16 | offset: u32, | 15 | offset: u32, |
| 17 | size: u32, | 16 | size: u32, |
| @@ -20,14 +19,20 @@ pub struct Partition<'a, M: RawMutex, T> { | |||
| 20 | #[derive(Debug)] | 19 | #[derive(Debug)] |
| 21 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 20 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 22 | pub enum Error<T> { | 21 | pub enum Error<T> { |
| 23 | Partition, | ||
| 24 | OutOfBounds, | 22 | OutOfBounds, |
| 25 | Flash(T), | 23 | Flash(T), |
| 26 | } | 24 | } |
| 27 | 25 | ||
| 28 | impl<'a, M: RawMutex, T> Partition<'a, M, T> { | 26 | impl<'a, M: RawMutex, T: NorFlash> Partition<'a, M, T> { |
| 29 | /// Create a new partition | 27 | /// Create a new partition |
| 30 | pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self { | 28 | pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self { |
| 29 | if offset % T::READ_SIZE as u32 != 0 || offset % T::WRITE_SIZE as u32 != 0 || offset % T::ERASE_SIZE as u32 != 0 | ||
| 30 | { | ||
| 31 | panic!("Partition offset must be a multiple of read, write and erase size"); | ||
| 32 | } | ||
| 33 | if size % T::READ_SIZE as u32 != 0 || size % T::WRITE_SIZE as u32 != 0 || size % T::ERASE_SIZE as u32 != 0 { | ||
| 34 | panic!("Partition size must be a multiple of read, write and erase size"); | ||
| 35 | } | ||
| 31 | Self { flash, offset, size } | 36 | Self { flash, offset, size } |
| 32 | } | 37 | } |
| 33 | } | 38 | } |
| @@ -35,25 +40,21 @@ impl<'a, M: RawMutex, T> Partition<'a, M, T> { | |||
| 35 | impl<T: NorFlashError> NorFlashError for Error<T> { | 40 | impl<T: NorFlashError> NorFlashError for Error<T> { |
| 36 | fn kind(&self) -> NorFlashErrorKind { | 41 | fn kind(&self) -> NorFlashErrorKind { |
| 37 | match self { | 42 | match self { |
| 38 | Error::Partition => NorFlashErrorKind::Other, | ||
| 39 | Error::OutOfBounds => NorFlashErrorKind::OutOfBounds, | 43 | Error::OutOfBounds => NorFlashErrorKind::OutOfBounds, |
| 40 | Error::Flash(f) => f.kind(), | 44 | Error::Flash(f) => f.kind(), |
| 41 | } | 45 | } |
| 42 | } | 46 | } |
| 43 | } | 47 | } |
| 44 | 48 | ||
| 45 | impl<M: RawMutex, T: ErrorType> ErrorType for Partition<'_, M, T> { | 49 | impl<M: RawMutex, T: NorFlash> ErrorType for Partition<'_, M, T> { |
| 46 | type Error = Error<T::Error>; | 50 | type Error = Error<T::Error>; |
| 47 | } | 51 | } |
| 48 | 52 | ||
| 49 | #[cfg(feature = "nightly")] | 53 | #[cfg(feature = "nightly")] |
| 50 | impl<M: RawMutex, T: ReadNorFlash> ReadNorFlash for Partition<'_, M, T> { | 54 | impl<M: RawMutex, T: NorFlash> ReadNorFlash for Partition<'_, M, T> { |
| 51 | const READ_SIZE: usize = T::READ_SIZE; | 55 | const READ_SIZE: usize = T::READ_SIZE; |
| 52 | 56 | ||
| 53 | async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { | 57 | async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { |
| 54 | if self.offset % T::READ_SIZE as u32 != 0 || self.size % T::READ_SIZE as u32 != 0 { | ||
| 55 | return Err(Error::Partition); | ||
| 56 | } | ||
| 57 | if offset + bytes.len() as u32 > self.size { | 58 | if offset + bytes.len() as u32 > self.size { |
| 58 | return Err(Error::OutOfBounds); | 59 | return Err(Error::OutOfBounds); |
| 59 | } | 60 | } |
| @@ -73,9 +74,6 @@ impl<M: RawMutex, T: NorFlash> NorFlash for Partition<'_, M, T> { | |||
| 73 | const ERASE_SIZE: usize = T::ERASE_SIZE; | 74 | const ERASE_SIZE: usize = T::ERASE_SIZE; |
| 74 | 75 | ||
| 75 | async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { | 76 | async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { |
| 76 | if self.offset % T::WRITE_SIZE as u32 != 0 || self.size % T::WRITE_SIZE as u32 != 0 { | ||
| 77 | return Err(Error::Partition); | ||
| 78 | } | ||
| 79 | if offset + bytes.len() as u32 > self.size { | 77 | if offset + bytes.len() as u32 > self.size { |
| 80 | return Err(Error::OutOfBounds); | 78 | return Err(Error::OutOfBounds); |
| 81 | } | 79 | } |
| @@ -85,9 +83,6 @@ impl<M: RawMutex, T: NorFlash> NorFlash for Partition<'_, M, T> { | |||
| 85 | } | 83 | } |
| 86 | 84 | ||
| 87 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | 85 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { |
| 88 | if self.offset % T::ERASE_SIZE as u32 != 0 || self.size % T::ERASE_SIZE as u32 != 0 { | ||
| 89 | return Err(Error::Partition); | ||
| 90 | } | ||
| 91 | if to > self.size { | 86 | if to > self.size { |
| 92 | return Err(Error::OutOfBounds); | 87 | return Err(Error::OutOfBounds); |
| 93 | } | 88 | } |
| @@ -110,10 +105,10 @@ mod tests { | |||
| 110 | #[futures_test::test] | 105 | #[futures_test::test] |
| 111 | async fn can_read() { | 106 | async fn can_read() { |
| 112 | let mut flash = MemFlash::<1024, 128, 4>::default(); | 107 | let mut flash = MemFlash::<1024, 128, 4>::default(); |
| 113 | flash.mem[12..20].fill(0xAA); | 108 | flash.mem[132..132 + 8].fill(0xAA); |
| 114 | 109 | ||
| 115 | let flash = Mutex::<NoopRawMutex, _>::new(flash); | 110 | let flash = Mutex::<NoopRawMutex, _>::new(flash); |
| 116 | let mut partition = Partition::new(&flash, 8, 12); | 111 | let mut partition = Partition::new(&flash, 128, 256); |
| 117 | 112 | ||
| 118 | let mut read_buf = [0; 8]; | 113 | let mut read_buf = [0; 8]; |
| 119 | partition.read(4, &mut read_buf).await.unwrap(); | 114 | partition.read(4, &mut read_buf).await.unwrap(); |
| @@ -126,13 +121,13 @@ mod tests { | |||
| 126 | let flash = MemFlash::<1024, 128, 4>::default(); | 121 | let flash = MemFlash::<1024, 128, 4>::default(); |
| 127 | 122 | ||
| 128 | let flash = Mutex::<NoopRawMutex, _>::new(flash); | 123 | let flash = Mutex::<NoopRawMutex, _>::new(flash); |
| 129 | let mut partition = Partition::new(&flash, 8, 12); | 124 | let mut partition = Partition::new(&flash, 128, 256); |
| 130 | 125 | ||
| 131 | let write_buf = [0xAA; 8]; | 126 | let write_buf = [0xAA; 8]; |
| 132 | partition.write(4, &write_buf).await.unwrap(); | 127 | partition.write(4, &write_buf).await.unwrap(); |
| 133 | 128 | ||
| 134 | let flash = flash.try_lock().unwrap(); | 129 | let flash = flash.try_lock().unwrap(); |
| 135 | assert!(flash.mem[12..20].iter().position(|&x| x != 0xAA).is_none()); | 130 | assert!(flash.mem[132..132 + 8].iter().position(|&x| x != 0xAA).is_none()); |
| 136 | } | 131 | } |
| 137 | 132 | ||
| 138 | #[futures_test::test] | 133 | #[futures_test::test] |
