-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework NorFlash & Storage traits #12
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3eb1428
Rework NorFlash & Storage traits, and add RmwNorFlashStorage wrapper …
MathiasKoch 5943283
Change implementation to use associated consts for read, write and er…
MathiasKoch 363bce3
Make RmwNorFlashStorage::new take a mutable merge_buffer, that must b…
MathiasKoch 931eb63
Add new function to MultiwriteNorFlash, and bound on MultiwriteNorFla…
MathiasKoch ac8492b
Change from address to offset argument naming, as these are offsets f…
MathiasKoch 4f4ab10
Update nor_flash.rs
MathiasKoch d69248a
Update src/lib.rs
MathiasKoch 72560ac
Update Cargo.toml
MathiasKoch 2cbb985
Address review comments around nor_flash
MathiasKoch e02bda5
Merge branch 'storage-rework' of github.com:rust-embedded-community/e…
MathiasKoch 55f8505
Change away from iterators
MathiasKoch 0ca3bed
Extract RmwNorFlash impl to separate PR
MathiasKoch 6a67ff2
Remove unused heapless dependency, and fix indentation
MathiasKoch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,169 +2,45 @@ | |
//! | ||
//! Storage traits to allow on and off board storage devices to read and write | ||
//! data. | ||
//! | ||
//! Implementation based on `Cuervo`s great work in | ||
//! https://www.ecorax.net/as-above-so-below-1/ and | ||
//! https://www.ecorax.net/as-above-so-below-2/ | ||
|
||
#![no_std] | ||
#![deny(missing_docs)] | ||
#![deny(unsafe_code)] | ||
|
||
use core::ops::{Add, Sub}; | ||
use heapless::{consts::*, Vec}; | ||
use nb; | ||
|
||
/// Currently contains [`OverlapIterator`] | ||
pub mod iter; | ||
|
||
/// An address denotes the read/write address of a single word. | ||
#[derive(Default, Copy, Clone, Debug, PartialOrd, PartialEq, Eq, Ord)] | ||
pub struct Address(pub u32); | ||
|
||
impl Add<usize> for Address { | ||
type Output = Self; | ||
|
||
fn add(self, rhs: usize) -> Self::Output { | ||
Address(self.0 + rhs as u32) | ||
} | ||
} | ||
|
||
impl Add<isize> for Address { | ||
type Output = Self; | ||
|
||
fn add(self, rhs: isize) -> Self::Output { | ||
Address((self.0 as isize + rhs) as u32) | ||
} | ||
} | ||
impl Sub<usize> for Address { | ||
type Output = Self; | ||
|
||
fn sub(self, rhs: usize) -> Self::Output { | ||
Address(self.0 - rhs as u32) | ||
} | ||
} | ||
|
||
impl Sub<isize> for Address { | ||
type Output = Self; | ||
|
||
fn sub(self, rhs: isize) -> Self::Output { | ||
Address((self.0 as isize - rhs) as u32) | ||
} | ||
} | ||
|
||
impl Sub<Address> for Address { | ||
type Output = Self; | ||
|
||
fn sub(self, rhs: Address) -> Self::Output { | ||
Address(self.0 - rhs.0) | ||
} | ||
} | ||
/// Technology specific traits for NOR Flashes | ||
pub mod nor_flash; | ||
|
||
/// A region denotes a contiguous piece of memory between two addresses. | ||
pub trait Region { | ||
/// Check if `address` is contained in the region of `Self` | ||
fn contains(&self, address: Address) -> bool; | ||
fn contains(&self, address: u32) -> bool; | ||
} | ||
|
||
/// Transparent storage trait | ||
pub trait ReadWriteStorage { | ||
/// Transparent read only storage trait | ||
pub trait ReadStorage { | ||
/// An enumeration of storage errors | ||
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: Address, bytes: &mut [u8]) -> nb::Result<(), Self::Error>; | ||
|
||
/// Write a slice of data to the storage peripheral, starting the write | ||
/// operation at the given address. | ||
fn try_write(&mut self, address: Address, bytes: &[u8]) -> nb::Result<(), Self::Error>; | ||
|
||
/// The range of possible addresses within the peripheral. | ||
/// operation at the given address offset, and reading `bytes.len()` bytes. | ||
/// | ||
/// (start_addr, end_addr) | ||
fn range(&self) -> (Address, Address); | ||
/// This should throw an error in case `bytes.len()` will be larger than | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you fix the tab usage clippy warning here? |
||
/// `self.capacity() - offset`. | ||
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>; | ||
|
||
/// Erase the given storage range, clearing all data within `[from..to]`. | ||
fn try_erase(&mut self, from: Address, to: Address) -> nb::Result<(), Self::Error>; | ||
/// The capacity of the storage peripheral in bytes. | ||
fn capacity(&self) -> usize; | ||
} | ||
|
||
/// NOR flash region trait. | ||
pub trait NorFlashRegion { | ||
/// The range of possible addresses within the region. | ||
/// | ||
/// (start_addr, end_addr) | ||
fn range(&self) -> (Address, Address); | ||
/// Maximum number of bytes that can be written at once. | ||
fn page_size(&self) -> usize; | ||
/// List of avalable erase sizes in this region. | ||
/// Should be sorted in ascending order. | ||
/// Currently limited to 5 sizes, but could be increased if necessary. | ||
fn erase_sizes(&self) -> Vec<usize, U5>; | ||
} | ||
|
||
/// Blanket implementation for all types implementing [`NorFlashRegion`] | ||
impl<T: NorFlashRegion> Region for T { | ||
fn contains(&self, address: Address) -> bool { | ||
let (start, end) = self.range(); | ||
address.0 >= start.0 && address.0 < end.0 | ||
} | ||
} | ||
|
||
/// NOR flash storage trait | ||
pub trait NorFlash { | ||
/// An enumeration of storage errors | ||
type Error; | ||
/// Region type | ||
type Region: NorFlashRegion; | ||
|
||
/// 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: Address, bytes: &mut [u8]) -> nb::Result<(), Self::Error>; | ||
|
||
/// 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. | ||
/// | ||
/// Since this is done on a NOR flash all bytes are anded with the current | ||
/// content in the flash. This means no 0s can to turned into 1s this way. | ||
fn try_write(&mut self, address: Address, bytes: &[u8]) -> nb::Result<(), Self::Error>; | ||
|
||
/// Erase the given storage range, clearing all data within `[from..to]`. | ||
/// The given range will contain all 1s afterwards. | ||
/// | ||
/// This should return an error if the range is not aligned to a proper | ||
/// erase resolution | ||
fn try_erase(&mut self, from: Address, to: Address) -> nb::Result<(), Self::Error>; | ||
|
||
/// Get all distinct memory reagions. These must not overlap, but can be disjoint. | ||
/// Most chips will return a single region, but some chips have regions with | ||
/// different erase sizes. | ||
/// Currently limited to 4 regions, but could be increased if necessary | ||
fn regions(&self) -> Vec<Self::Region, U4>; | ||
} | ||
|
||
/// Marker trait for NOR flashes with uniform erase and page sizes across the whole | ||
/// address range | ||
pub trait UniformNorFlash {} | ||
|
||
/// Blanket implementation for all types implementing [`NorFlash`] and [`UniformNorFlash`] | ||
impl<T: NorFlash + UniformNorFlash> NorFlashRegion for T { | ||
/// The range of possible addresses within the peripheral. | ||
/// operation at the given address offset (between 0 and `self.capacity()`). | ||
/// | ||
/// (start_addr, end_addr) | ||
fn range(&self) -> (Address, Address) { | ||
self.regions()[0].range() | ||
} | ||
/// Maximum number of bytes that can be written at once. | ||
fn page_size(&self) -> usize { | ||
self.regions()[0].page_size() | ||
} | ||
/// List of avalable erase sizes in this region. | ||
/// Should be sorted in ascending order. | ||
/// Currently limited to 5 sizes, but could be increased if necessary. | ||
fn erase_sizes(&self) -> Vec<usize, U5> { | ||
self.regions()[0].erase_sizes() | ||
} | ||
/// **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. | ||
fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/// Read only NOR flash trait. | ||
pub trait ReadNorFlash { | ||
/// An enumeration of storage errors | ||
type Error; | ||
|
||
/// The minumum number of bytes the storage peripheral can read | ||
const READ_SIZE: usize; | ||
|
||
/// 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 | ||
/// the peripheral end address. | ||
fn try_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>; | ||
|
||
/// The capacity of the peripheral in bytes. | ||
fn capacity(&self) -> usize; | ||
} | ||
|
||
/// NOR flash trait. | ||
pub trait NorFlash: ReadNorFlash { | ||
/// The minumum number of bytes the storage peripheral can write | ||
const WRITE_SIZE: usize; | ||
|
||
/// The minumum number of bytes the storage peripheral can erase | ||
const ERASE_SIZE: usize; | ||
|
||
/// Erase the given storage range, clearing all data within `[from..to]`. | ||
/// The given range will contain all 1s afterwards. | ||
/// | ||
/// This should return an error if the range is not aligned to a proper | ||
/// erase resolution | ||
/// If power is lost during erase, contents of the page are undefined. | ||
/// `from` and `to` must both be multiples of `ERASE_SIZE` and `from` <= `to`. | ||
fn try_erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error>; | ||
|
||
/// If power is lost during write, the contents of the written words are undefined, | ||
/// but the rest of the page is guaranteed to be unchanged. | ||
/// It is not allowed to write to the same word twice. | ||
/// `offset` and `bytes.len()` must both be multiples of `WRITE_SIZE`. | ||
fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>; | ||
} | ||
|
||
/// Marker trait for NorFlash relaxing the restrictions on `write`. | ||
/// | ||
/// Writes to the same word twice are now allowed. The result is the logical AND of the | ||
/// previous data and the written data. That is, it is only possible to change 1 bits to 0 bits. | ||
/// | ||
/// If power is lost during write: | ||
/// - Bits that were 1 on flash and are written to 1 are guaranteed to stay as 1 | ||
/// - Bits that were 1 on flash and are written to 0 are undefined | ||
/// - Bits that were 0 on flash are guaranteed to stay as 0 | ||
/// - Rest of the bits in the page are guaranteed to be unchanged | ||
pub trait MultiwriteNorFlash: NorFlash {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we bump heapless? The latest version (0.7) requires 1.51.0, though, due to const-generics.