Skip to content

Commit

Permalink
Merge branch 'storage-rework' of github.com:rust-embedded-community/e…
Browse files Browse the repository at this point in the history
…mbedded-storage into storage-rework
  • Loading branch information
MathiasKoch committed May 7, 2021
2 parents 2cbb985 + 72560ac commit e02bda5
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -15,4 +15,4 @@ keywords = ["storage"]
categories = ["embedded", "hardware-support", "no-std"]

[dependencies]
heapless = "^0.5"
heapless = "^0.5"
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ pub trait ReadStorage {
/// operation at the given address offset, and reading `bytes.len()` bytes.
///
/// This should throw an error in case `bytes.len()` will be larger than
/// `self.capacity()`.
/// `self.capacity() - offset`.
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>;

/// The capacity of the storage peripheral in bytes.
16 changes: 14 additions & 2 deletions src/nor_flash.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ pub trait ReadNorFlash {
/// Read a slice of data from the storage peripheral, starting the read
/// operation at the given address offset, and reading `bytes.len()` bytes.
///
/// This should throw an error in case `bytes.len()` will be larger than
/// This should throw an error in case `bytes.len()` will be larger than
/// the peripheral end address.
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>;

@@ -223,7 +223,19 @@ where
// Check if we can write the data block directly, under the limitations imposed by NorFlash:
// - We can only change 1's to 0's
if is_subset {
self.storage.try_write(addr, data)?;
// Use `merge_buffer` as allocation for padding `data` to `WRITE_SIZE`
let offset = addr as usize % S::WRITE_SIZE;
self.merge_buffer[..S::WRITE_SIZE]
.iter_mut()
.for_each(|c| *c = 0u8);
self.merge_buffer[..S::WRITE_SIZE]
.iter_mut()
.skip(offset)
.zip(data)
.for_each(|(a, b)| *a = *b);
let aligned_addr = addr - offset as u32;
self.storage
.try_write(aligned_addr, &self.merge_buffer[..S::WRITE_SIZE])?;
} else {
self.storage.try_erase(page.start, page.end())?;
self.merge_buffer[..S::ERASE_SIZE]

0 comments on commit e02bda5

Please sign in to comment.