aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/flash/partition.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-embedded-hal/src/flash/partition.rs')
-rw-r--r--embassy-embedded-hal/src/flash/partition.rs35
1 files changed, 15 insertions, 20 deletions
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 @@
1use embassy_sync::blocking_mutex::raw::RawMutex; 1use embassy_sync::blocking_mutex::raw::RawMutex;
2use embassy_sync::mutex::Mutex; 2use embassy_sync::mutex::Mutex;
3use embedded_storage::nor_flash::{ErrorType, NorFlashError, NorFlashErrorKind}; 3use embedded_storage::nor_flash::{ErrorType, NorFlashError, NorFlashErrorKind};
4#[cfg(feature = "nightly")]
5use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash}; 4use 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.
14pub struct Partition<'a, M: RawMutex, T> { 13pub 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))]
22pub enum Error<T> { 21pub enum Error<T> {
23 Partition,
24 OutOfBounds, 22 OutOfBounds,
25 Flash(T), 23 Flash(T),
26} 24}
27 25
28impl<'a, M: RawMutex, T> Partition<'a, M, T> { 26impl<'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> {
35impl<T: NorFlashError> NorFlashError for Error<T> { 40impl<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
45impl<M: RawMutex, T: ErrorType> ErrorType for Partition<'_, M, T> { 49impl<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")]
50impl<M: RawMutex, T: ReadNorFlash> ReadNorFlash for Partition<'_, M, T> { 54impl<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]