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