Skip to content

Commit 139776e

Browse files
committed
Add NOR flash trait
1 parent 02f86de commit 139776e

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/lib.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![deny(unsafe_code)]
1313

1414
use core::ops::{Add, Sub};
15+
use heapless::{consts::*, Vec};
1516
use nb;
1617

1718
/// Currently contains [`OverlapIterator`]
@@ -66,8 +67,8 @@ pub trait Region {
6667
fn contains(&self, address: Address) -> bool;
6768
}
6869

69-
/// Storage trait
70-
pub trait ReadWrite {
70+
/// Transparent storage trait
71+
pub trait ReadWriteStorage {
7172
/// An enumeration of storage errors
7273
type Error;
7374

@@ -86,8 +87,57 @@ pub trait ReadWrite {
8687
fn range(&self) -> (Address, Address);
8788

8889
/// Erase the given storage range, clearing all data within `[from..to]`.
90+
fn try_erase(&mut self, from: Address, to: Address) -> nb::Result<(), Self::Error>;
91+
}
92+
93+
/// NOR flash region trait.
94+
pub trait NorFlashRegion {
95+
/// The range of possible addresses within the region.
96+
///
97+
/// (start_addr, end_addr)
98+
fn range(&self) -> (Address, Address);
99+
/// Maximum number of bytes that can be written at once.
100+
fn page_size(&self) -> usize;
101+
/// List of avalable erase sizes in this region.
102+
/// Should be sorted in ascending order.
103+
/// Currently limited to 5 sizes, but could be increased if necessary.
104+
fn erase_sizes(&self) -> Vec<usize, U5>;
105+
}
106+
107+
/// NOR flash storage trait
108+
pub trait NorFlash {
109+
/// An enumeration of storage errors
110+
type Error;
111+
/// Region type
112+
type Region: NorFlashRegion;
113+
114+
/// Read a slice of data from the storage peripheral, starting the read
115+
/// operation at the given address, and reading until end address
116+
/// (`self.range().1`) or buffer length, whichever comes first.
117+
fn try_read(&mut self, address: Address, bytes: &mut [u8]) -> nb::Result<(), Self::Error>;
118+
119+
/// Write a slice of data to the storage peripheral, starting the write
120+
/// operation at the given address.
121+
///
122+
/// Since this is done on a NOR flash all bytes are anded with the current
123+
/// content in the flash. This means no 0s can to turned into 1s this way.
124+
fn try_write(&mut self, address: Address, bytes: &[u8]) -> nb::Result<(), Self::Error>;
125+
126+
/// Erase the given storage range, clearing all data within `[from..to]`.
127+
/// The given range will contain all 1s afterwards.
89128
///
90129
/// This should return an error if the range is not aligned to a proper
91130
/// erase resolution
92131
fn try_erase(&mut self, from: Address, to: Address) -> nb::Result<(), Self::Error>;
132+
133+
/// The range of possible addresses within the peripheral.
134+
///
135+
/// (start_addr, end_addr)
136+
fn range(&self) -> (Address, Address);
137+
138+
/// Get all distinct memory reagions. These must not overlap, but can be disjoint.
139+
/// Most chips will return a single region, but some chips have regions with
140+
/// different erase sizes.
141+
/// Currently limited to 4 regions, but could be increased if necessary
142+
fn regions(&self) -> Vec<Self::Region, U4>;
93143
}

0 commit comments

Comments
 (0)