diff options
| author | Rasmus Melchior Jacobsen <[email protected]> | 2023-05-30 13:40:04 +0200 |
|---|---|---|
| committer | Rasmus Melchior Jacobsen <[email protected]> | 2023-05-30 13:40:04 +0200 |
| commit | 1cd87f0028c8a0f9b87a09016a22179fb05ced33 (patch) | |
| tree | aec9504d323cbbe1dd6f247a9bd2fd4a0838f1d0 /embassy-boot/boot/src | |
| parent | c5ec453ec1e87c2f5c5047bf357ae99298aa6d12 (diff) | |
Cleanup MemFlash
Diffstat (limited to 'embassy-boot/boot/src')
| -rw-r--r-- | embassy-boot/boot/src/mem_flash.rs | 106 |
1 files changed, 56 insertions, 50 deletions
diff --git a/embassy-boot/boot/src/mem_flash.rs b/embassy-boot/boot/src/mem_flash.rs index 3ba84a92c..2728e9720 100644 --- a/embassy-boot/boot/src/mem_flash.rs +++ b/embassy-boot/boot/src/mem_flash.rs | |||
| @@ -34,21 +34,61 @@ impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> MemFla | |||
| 34 | } | 34 | } |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | pub fn program(&mut self, offset: u32, bytes: &[u8]) -> Result<(), MemFlashError> { | 37 | fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), MemFlashError> { |
| 38 | let len = bytes.len(); | ||
| 39 | bytes.copy_from_slice(&self.mem[offset as usize..offset as usize + len]); | ||
| 40 | Ok(()) | ||
| 41 | } | ||
| 42 | |||
| 43 | fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), MemFlashError> { | ||
| 38 | let offset = offset as usize; | 44 | let offset = offset as usize; |
| 39 | assert!(bytes.len() % WRITE_SIZE == 0); | 45 | assert!(bytes.len() % WRITE_SIZE == 0); |
| 40 | assert!(offset % WRITE_SIZE == 0); | 46 | assert!(offset % WRITE_SIZE == 0); |
| 41 | assert!(offset + bytes.len() <= SIZE); | 47 | assert!(offset + bytes.len() <= SIZE); |
| 42 | 48 | ||
| 43 | self.mem[offset..offset + bytes.len()].copy_from_slice(bytes); | 49 | if let Some(pending_successes) = self.pending_write_successes { |
| 50 | if pending_successes > 0 { | ||
| 51 | self.pending_write_successes = Some(pending_successes - 1); | ||
| 52 | } else { | ||
| 53 | return Err(MemFlashError); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | for ((offset, mem_byte), new_byte) in self | ||
| 58 | .mem | ||
| 59 | .iter_mut() | ||
| 60 | .enumerate() | ||
| 61 | .skip(offset) | ||
| 62 | .take(bytes.len()) | ||
| 63 | .zip(bytes) | ||
| 64 | { | ||
| 65 | assert_eq!(0xFF, *mem_byte, "Offset {} is not erased", offset); | ||
| 66 | *mem_byte = *new_byte; | ||
| 67 | } | ||
| 44 | 68 | ||
| 45 | Ok(()) | 69 | Ok(()) |
| 46 | } | 70 | } |
| 47 | 71 | ||
| 48 | pub fn assert_eq(&self, offset: u32, expectation: &[u8]) { | 72 | fn erase(&mut self, from: u32, to: u32) -> Result<(), MemFlashError> { |
| 49 | for i in 0..expectation.len() { | 73 | let from = from as usize; |
| 50 | assert_eq!(self.mem[offset as usize + i], expectation[i], "Index {}", i); | 74 | let to = to as usize; |
| 75 | assert!(from % ERASE_SIZE == 0); | ||
| 76 | assert!(to % ERASE_SIZE == 0, "To: {}, erase size: {}", to, ERASE_SIZE); | ||
| 77 | for i in from..to { | ||
| 78 | self.mem[i] = 0xFF; | ||
| 51 | } | 79 | } |
| 80 | Ok(()) | ||
| 81 | } | ||
| 82 | |||
| 83 | pub fn program(&mut self, offset: u32, bytes: &[u8]) -> Result<(), MemFlashError> { | ||
| 84 | let offset = offset as usize; | ||
| 85 | assert!(bytes.len() % WRITE_SIZE == 0); | ||
| 86 | assert!(offset % WRITE_SIZE == 0); | ||
| 87 | assert!(offset + bytes.len() <= SIZE); | ||
| 88 | |||
| 89 | self.mem[offset..offset + bytes.len()].copy_from_slice(bytes); | ||
| 90 | |||
| 91 | Ok(()) | ||
| 52 | } | 92 | } |
| 53 | } | 93 | } |
| 54 | 94 | ||
| @@ -78,9 +118,7 @@ impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ReadNo | |||
| 78 | const READ_SIZE: usize = 1; | 118 | const READ_SIZE: usize = 1; |
| 79 | 119 | ||
| 80 | fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { | 120 | fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { |
| 81 | let len = bytes.len(); | 121 | self.read(offset, bytes) |
| 82 | bytes.copy_from_slice(&self.mem[offset as usize..offset as usize + len]); | ||
| 83 | Ok(()) | ||
| 84 | } | 122 | } |
| 85 | 123 | ||
| 86 | fn capacity(&self) -> usize { | 124 | fn capacity(&self) -> usize { |
| @@ -94,44 +132,12 @@ impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> NorFla | |||
| 94 | const WRITE_SIZE: usize = WRITE_SIZE; | 132 | const WRITE_SIZE: usize = WRITE_SIZE; |
| 95 | const ERASE_SIZE: usize = ERASE_SIZE; | 133 | const ERASE_SIZE: usize = ERASE_SIZE; |
| 96 | 134 | ||
| 97 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 98 | let from = from as usize; | ||
| 99 | let to = to as usize; | ||
| 100 | assert!(from % ERASE_SIZE == 0); | ||
| 101 | assert!(to % ERASE_SIZE == 0, "To: {}, erase size: {}", to, ERASE_SIZE); | ||
| 102 | for i in from..to { | ||
| 103 | self.mem[i] = 0xFF; | ||
| 104 | } | ||
| 105 | Ok(()) | ||
| 106 | } | ||
| 107 | |||
| 108 | fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { | 135 | fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { |
| 109 | let offset = offset as usize; | 136 | self.write(offset, bytes) |
| 110 | assert!(bytes.len() % WRITE_SIZE == 0); | 137 | } |
| 111 | assert!(offset % WRITE_SIZE == 0); | ||
| 112 | assert!(offset + bytes.len() <= SIZE); | ||
| 113 | |||
| 114 | if let Some(pending_successes) = self.pending_write_successes { | ||
| 115 | if pending_successes > 0 { | ||
| 116 | self.pending_write_successes = Some(pending_successes - 1); | ||
| 117 | } else { | ||
| 118 | return Err(MemFlashError); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | for ((offset, mem_byte), new_byte) in self | ||
| 123 | .mem | ||
| 124 | .iter_mut() | ||
| 125 | .enumerate() | ||
| 126 | .skip(offset) | ||
| 127 | .take(bytes.len()) | ||
| 128 | .zip(bytes) | ||
| 129 | { | ||
| 130 | assert_eq!(0xFF, *mem_byte, "Offset {} is not erased", offset); | ||
| 131 | *mem_byte = *new_byte; | ||
| 132 | } | ||
| 133 | 138 | ||
| 134 | Ok(()) | 139 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { |
| 140 | self.erase(from, to) | ||
| 135 | } | 141 | } |
| 136 | } | 142 | } |
| 137 | 143 | ||
| @@ -142,11 +148,11 @@ impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncR | |||
| 142 | const READ_SIZE: usize = 1; | 148 | const READ_SIZE: usize = 1; |
| 143 | 149 | ||
| 144 | async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { | 150 | async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { |
| 145 | <Self as ReadNorFlash>::read(self, offset, bytes) | 151 | self.read(offset, bytes) |
| 146 | } | 152 | } |
| 147 | 153 | ||
| 148 | fn capacity(&self) -> usize { | 154 | fn capacity(&self) -> usize { |
| 149 | <Self as ReadNorFlash>::capacity(self) | 155 | SIZE |
| 150 | } | 156 | } |
| 151 | } | 157 | } |
| 152 | 158 | ||
| @@ -157,11 +163,11 @@ impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> AsyncN | |||
| 157 | const WRITE_SIZE: usize = WRITE_SIZE; | 163 | const WRITE_SIZE: usize = WRITE_SIZE; |
| 158 | const ERASE_SIZE: usize = ERASE_SIZE; | 164 | const ERASE_SIZE: usize = ERASE_SIZE; |
| 159 | 165 | ||
| 160 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | 166 | async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { |
| 161 | <Self as NorFlash>::erase(self, from, to) | 167 | self.write(offset, bytes) |
| 162 | } | 168 | } |
| 163 | 169 | ||
| 164 | async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { | 170 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { |
| 165 | <Self as NorFlash>::write(self, offset, bytes) | 171 | self.erase(from, to) |
| 166 | } | 172 | } |
| 167 | } | 173 | } |
