Skip to content

Commit

Permalink
Change from address to offset argument naming, as these are offsets f…
Browse files Browse the repository at this point in the history
…rom peripheral base address
  • Loading branch information
MathiasKoch committed Apr 8, 2021
1 parent 931eb63 commit ac8492b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ pub trait ReadStorage {
type Error;

/// Read a slice of data from the storage peripheral, starting the read
/// operation at the given address, and reading until end address
/// (`self.range().1`) or buffer length, whichever comes first.
fn try_read(&mut self, address: u32, bytes: &mut [u8]) -> Result<(), Self::Error>;
/// 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()`.
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>;

/// The capacity of the storage peripheral in bytes.
fn capacity(&self) -> usize;
Expand All @@ -35,13 +37,10 @@ pub trait ReadStorage {
/// Transparent read/write storage trait
pub trait Storage: ReadStorage {
/// Write a slice of data to the storage peripheral, starting the write
/// operation at the given address.
/// operation at the given address offset (between 0 and `self.capacity()`).
///
/// **NOTE:**
/// This function will automatically erase any pages necessary to write the given data,
/// and might as such do RMW operations at an undesirable performance impact.
///
/// CONSIDERATIONS:
/// - Should the address here be normalized (always start from zero?)
fn try_write(&mut self, address: u32, bytes: &[u8]) -> Result<(), Self::Error>;
fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>;
}
24 changes: 12 additions & 12 deletions src/nor_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub trait ReadNorFlash {
const READ_SIZE: usize;

/// Read a slice of data from the storage peripheral, starting the read
/// operation at the given address, and reading `bytes.len()` bytes.
/// operation at the given address offset, and reading `bytes.len()` bytes.
///
/// This should throw an error in case `bytes.len()` will be larger than
/// the peripheral end address.
fn try_read(&mut self, address: u32, bytes: &mut [u8]) -> Result<(), Self::Error>;
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>;

/// The capacity of the peripheral in bytes.
fn capacity(&self) -> usize;
Expand Down Expand Up @@ -41,8 +41,8 @@ pub trait NorFlash: ReadNorFlash {
/// If power is lost during write, the contents of the written words are undefined.
/// The rest of the page is guaranteed to be unchanged.
/// It is not allowed to write to the same word twice.
/// `address` and `bytes.len()` must both be multiples of `write_size()` and properly aligned.
fn try_write(&mut self, address: u32, bytes: &[u8]) -> Result<(), Self::Error>;
/// `offset` and `bytes.len()` must both be multiples of `write_size()` and properly aligned.
fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>;
}

/// Marker trait for NorFlash relaxing the restrictions on `write`.
Expand Down Expand Up @@ -115,9 +115,9 @@ where
{
type Error = S::Error;

fn try_read(&mut self, address: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
// Nothing special to be done for reads
self.storage.try_read(address, bytes)
self.storage.try_read(offset, bytes)
}

fn capacity(&self) -> usize {
Expand All @@ -129,15 +129,15 @@ impl<'a, S> Storage for RmwNorFlashStorage<'a, S>
where
S: NorFlash,
{
fn try_write(&mut self, address: u32, bytes: &[u8]) -> Result<(), Self::Error> {
fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
// Perform read/modify/write operations on the byte slice.
let last_page = (self.storage.capacity() / S::ERASE_SIZE) - 1;

// `data` is the part of `bytes` contained within `page`,
// and `addr` in the address offset of `page` + any offset into the page as requested by `address`
for (data, page, addr) in (0..last_page as u32)
.map(move |i| Page::new(i, S::ERASE_SIZE))
.overlaps(bytes, address)
.overlaps(bytes, offset)
{
let offset_into_page = addr.saturating_sub(page.start) as usize;

Expand Down Expand Up @@ -188,9 +188,9 @@ where
{
type Error = S::Error;

fn try_read(&mut self, address: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
// Nothing special to be done for reads
self.storage.try_read(address, bytes)
self.storage.try_read(offset, bytes)
}

fn capacity(&self) -> usize {
Expand All @@ -202,15 +202,15 @@ impl<'a, S> Storage for RmwMultiwriteNorFlashStorage<'a, S>
where
S: MultiwriteNorFlash,
{
fn try_write(&mut self, address: u32, bytes: &[u8]) -> Result<(), Self::Error> {
fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
// Perform read/modify/write operations on the byte slice.
let last_page = (self.storage.capacity() / S::ERASE_SIZE) - 1;

// `data` is the part of `bytes` contained within `page`,
// and `addr` in the address offset of `page` + any offset into the page as requested by `address`
for (data, page, addr) in (0..last_page as u32)
.map(move |i| Page::new(i, S::ERASE_SIZE))
.overlaps(bytes, address)
.overlaps(bytes, offset)
{
let offset_into_page = addr.saturating_sub(page.start) as usize;

Expand Down

0 comments on commit ac8492b

Please sign in to comment.