Skip to content

Commit e02bda5

Browse files
committed
Merge branch 'storage-rework' of github.com:rust-embedded-community/embedded-storage into storage-rework
2 parents 2cbb985 + 72560ac commit e02bda5

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ keywords = ["storage"]
1515
categories = ["embedded", "hardware-support", "no-std"]
1616

1717
[dependencies]
18-
heapless = "^0.5"
18+
heapless = "^0.5"

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub trait ReadStorage {
2727
/// operation at the given address offset, and reading `bytes.len()` bytes.
2828
///
2929
/// This should throw an error in case `bytes.len()` will be larger than
30-
/// `self.capacity()`.
30+
/// `self.capacity() - offset`.
3131
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>;
3232

3333
/// The capacity of the storage peripheral in bytes.

src/nor_flash.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub trait ReadNorFlash {
1111
/// Read a slice of data from the storage peripheral, starting the read
1212
/// operation at the given address offset, and reading `bytes.len()` bytes.
1313
///
14-
/// This should throw an error in case `bytes.len()` will be larger than
14+
/// This should throw an error in case `bytes.len()` will be larger than
1515
/// the peripheral end address.
1616
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>;
1717

@@ -223,7 +223,19 @@ where
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 {
226-
self.storage.try_write(addr, data)?;
226+
// Use `merge_buffer` as allocation for padding `data` to `WRITE_SIZE`
227+
let offset = addr as usize % S::WRITE_SIZE;
228+
self.merge_buffer[..S::WRITE_SIZE]
229+
.iter_mut()
230+
.for_each(|c| *c = 0u8);
231+
self.merge_buffer[..S::WRITE_SIZE]
232+
.iter_mut()
233+
.skip(offset)
234+
.zip(data)
235+
.for_each(|(a, b)| *a = *b);
236+
let aligned_addr = addr - offset as u32;
237+
self.storage
238+
.try_write(aligned_addr, &self.merge_buffer[..S::WRITE_SIZE])?;
227239
} else {
228240
self.storage.try_erase(page.start, page.end())?;
229241
self.merge_buffer[..S::ERASE_SIZE]

0 commit comments

Comments
 (0)