aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/flash
diff options
context:
space:
mode:
authorRasmus Melchior Jacobsen <[email protected]>2023-05-27 17:02:54 +0200
committerRasmus Melchior Jacobsen <[email protected]>2023-05-27 17:02:54 +0200
commit85ce44f78e6d269675085ce6aa54c6353ed13ec7 (patch)
tree4c4813b3bda9c06fad945931f117101b91c04bba /embassy-embedded-hal/src/flash
parent6d8f409018f9fabd23e80e07a20b357989c4d841 (diff)
Use RefCell in blocking mutex
Diffstat (limited to 'embassy-embedded-hal/src/flash')
-rw-r--r--embassy-embedded-hal/src/flash/partition/blocking.rs40
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 @@
1use core::cell::RefCell;
2
1use embassy_sync::blocking_mutex::raw::RawMutex; 3use embassy_sync::blocking_mutex::raw::RawMutex;
2use embassy_sync::blocking_mutex::Mutex; 4use embassy_sync::blocking_mutex::Mutex;
3use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; 5use 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.
14pub struct BlockingPartition<'a, M: RawMutex, T: NorFlash> { 16pub 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
20impl<'a, M: RawMutex, T: NorFlash> BlockingPartition<'a, M, T> { 22impl<'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}