Skip to content

Commit 2cbb985

Browse files
committed
Address review comments around nor_flash
1 parent ac8492b commit 2cbb985

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/nor_flash.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,14 @@ pub trait NorFlash: ReadNorFlash {
3232
///
3333
/// This should return an error if the range is not aligned to a proper
3434
/// erase resolution
35-
/// Erases page at addr, sets it all to 0xFF
3635
/// If power is lost during erase, contents of the page are undefined.
37-
/// `from` and `to` must both be multiples of `erase_size()` and `from` <= `to`.
36+
/// `from` and `to` must both be multiples of `ERASE_SIZE` and `from` <= `to`.
3837
fn try_erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error>;
3938

40-
/// Writes data to addr, bitwise ANDing if there's already data written at that location,
41-
/// If power is lost during write, the contents of the written words are undefined.
42-
/// The rest of the page is guaranteed to be unchanged.
39+
/// If power is lost during write, the contents of the written words are undefined,
40+
/// but the rest of the page is guaranteed to be unchanged.
4341
/// It is not allowed to write to the same word twice.
44-
/// `offset` and `bytes.len()` must both be multiples of `write_size()` and properly aligned.
42+
/// `offset` and `bytes.len()` must both be multiples of `WRITE_SIZE`.
4543
fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>;
4644
}
4745

@@ -131,7 +129,7 @@ where
131129
{
132130
fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
133131
// Perform read/modify/write operations on the byte slice.
134-
let last_page = (self.storage.capacity() / S::ERASE_SIZE) - 1;
132+
let last_page = self.storage.capacity() / S::ERASE_SIZE;
135133

136134
// `data` is the part of `bytes` contained within `page`,
137135
// and `addr` in the address offset of `page` + any offset into the page as requested by `address`
@@ -141,16 +139,18 @@ where
141139
{
142140
let offset_into_page = addr.saturating_sub(page.start) as usize;
143141

144-
self.storage.try_read(page.start, self.merge_buffer)?;
142+
self.storage
143+
.try_read(page.start, &mut self.merge_buffer[..S::ERASE_SIZE])?;
145144

146145
// If we cannot write multiple times to the same page, we will have to erase it
147146
self.storage.try_erase(page.start, page.end())?;
148-
self.merge_buffer
147+
self.merge_buffer[..S::ERASE_SIZE]
149148
.iter_mut()
150149
.skip(offset_into_page)
151150
.zip(data)
152151
.for_each(|(byte, input)| *byte = *input);
153-
self.storage.try_write(page.start, self.merge_buffer)?;
152+
self.storage
153+
.try_write(page.start, &self.merge_buffer[..S::ERASE_SIZE])?;
154154
}
155155
Ok(())
156156
}
@@ -204,7 +204,7 @@ where
204204
{
205205
fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
206206
// Perform read/modify/write operations on the byte slice.
207-
let last_page = (self.storage.capacity() / S::ERASE_SIZE) - 1;
207+
let last_page = self.storage.capacity() / S::ERASE_SIZE;
208208

209209
// `data` is the part of `bytes` contained within `page`,
210210
// and `addr` in the address offset of `page` + any offset into the page as requested by `address`
@@ -214,24 +214,25 @@ where
214214
{
215215
let offset_into_page = addr.saturating_sub(page.start) as usize;
216216

217-
self.storage.try_read(page.start, self.merge_buffer)?;
217+
self.storage
218+
.try_read(page.start, &mut self.merge_buffer[..S::ERASE_SIZE])?;
218219

219-
let rhs = &self.merge_buffer[offset_into_page..];
220-
let is_subset =
221-
data.len() < rhs.len() && data.iter().zip(rhs.iter()).all(|(a, b)| (*a | *b) == *b);
220+
let rhs = &self.merge_buffer[offset_into_page..S::ERASE_SIZE];
221+
let is_subset = data.iter().zip(rhs.iter()).all(|(a, b)| *a & *b == *a);
222222

223223
// Check if we can write the data block directly, under the limitations imposed by NorFlash:
224224
// - We can only change 1's to 0's
225225
if is_subset {
226226
self.storage.try_write(addr, data)?;
227227
} else {
228228
self.storage.try_erase(page.start, page.end())?;
229-
self.merge_buffer
229+
self.merge_buffer[..S::ERASE_SIZE]
230230
.iter_mut()
231231
.skip(offset_into_page)
232232
.zip(data)
233233
.for_each(|(byte, input)| *byte = *input);
234-
self.storage.try_write(page.start, self.merge_buffer)?;
234+
self.storage
235+
.try_write(page.start, &self.merge_buffer[..S::ERASE_SIZE])?;
235236
}
236237
}
237238
Ok(())

0 commit comments

Comments
 (0)