1475: Add YieldingAsync adapter r=Dirbaio a=rmja This PR calls `yield_now()` for long blocking `NorFlash` read and erase operations. The motivation for this change is to allow for other tasks on the same executor to get something done between these long running operations, for example a task that feeds a watchdog. This will allow the watchdog to have a timer relative to e.g. one sector erase, instead of all sector erase. 1478: stm32: Minor fixes in flash regions for F4 dual bank layout r=Dirbaio a=rmja This PR has the following fixes: * Ensure that `FlashRegion` instances can only be created within the embassy-stm32 crate. * Remove `Drop` trait for `AltFlashLayout`, as it is hard to use, as one cannot take the individual regions out from the struct. Instead of going back to single bank mode on `Drop`, we instead transition to single bank mode when calling `Flash::into_regions()`. * Add missing `otp_region` to the dual bank layout and implement `NorFlash` for the alternate regions. 1482: Add ConcatFlash utility r=Dirbaio a=rmja This PR adds a `ConcatFlash` utility that can be used to concatenate two `NorFlash` flashes. This is especially useful when concatenating multiple flash regions with unequal erase size. Co-authored-by: Rasmus Melchior Jacobsen <rmja@laesoe.org>
This commit is contained in:
@ -14,11 +14,14 @@ target = "x86_64-unknown-linux-gnu"
|
||||
[features]
|
||||
std = []
|
||||
# Enable nightly-only features
|
||||
nightly = ["embedded-hal-async", "embedded-storage-async"]
|
||||
nightly = ["embassy-futures", "embedded-hal-async", "embedded-storage-async"]
|
||||
|
||||
[dependencies]
|
||||
embassy-futures = { version = "0.1.0", path = "../embassy-futures", optional = true }
|
||||
embassy-sync = { version = "0.2.0", path = "../embassy-sync" }
|
||||
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
|
||||
embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [
|
||||
"unproven",
|
||||
] }
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" }
|
||||
embedded-hal-async = { version = "=0.2.0-alpha.1", optional = true }
|
||||
embedded-storage = "0.3.0"
|
||||
@ -26,3 +29,6 @@ embedded-storage-async = { version = "0.4.0", optional = true }
|
||||
nb = "1.0.0"
|
||||
|
||||
defmt = { version = "0.3", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
futures-test = "0.3.17"
|
||||
|
@ -1,5 +1,3 @@
|
||||
//! Adapters between embedded-hal traits.
|
||||
|
||||
use embedded_hal_02::{blocking, serial};
|
||||
|
||||
/// Wrapper that implements async traits using blocking implementations.
|
7
embassy-embedded-hal/src/adapter/mod.rs
Normal file
7
embassy-embedded-hal/src/adapter/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
//! Adapters between embedded-hal traits.
|
||||
|
||||
mod blocking_async;
|
||||
mod yielding_async;
|
||||
|
||||
pub use blocking_async::BlockingAsync;
|
||||
pub use yielding_async::YieldingAsync;
|
232
embassy-embedded-hal/src/adapter/yielding_async.rs
Normal file
232
embassy-embedded-hal/src/adapter/yielding_async.rs
Normal file
@ -0,0 +1,232 @@
|
||||
use embassy_futures::yield_now;
|
||||
|
||||
/// Wrapper that yields for each operation to the wrapped instance
|
||||
///
|
||||
/// This can be used in combination with BlockingAsync<T> to enforce yields
|
||||
/// between long running blocking operations.
|
||||
pub struct YieldingAsync<T> {
|
||||
wrapped: T,
|
||||
}
|
||||
|
||||
impl<T> YieldingAsync<T> {
|
||||
/// Create a new instance of a wrapper that yields after each operation.
|
||||
pub fn new(wrapped: T) -> Self {
|
||||
Self { wrapped }
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// I2C implementations
|
||||
//
|
||||
impl<T> embedded_hal_1::i2c::ErrorType for YieldingAsync<T>
|
||||
where
|
||||
T: embedded_hal_1::i2c::ErrorType,
|
||||
{
|
||||
type Error = T::Error;
|
||||
}
|
||||
|
||||
impl<T> embedded_hal_async::i2c::I2c for YieldingAsync<T>
|
||||
where
|
||||
T: embedded_hal_async::i2c::I2c,
|
||||
{
|
||||
async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.wrapped.read(address, read).await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
|
||||
self.wrapped.write(address, write).await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.wrapped.write_read(address, write, read).await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn transaction(
|
||||
&mut self,
|
||||
address: u8,
|
||||
operations: &mut [embedded_hal_1::i2c::Operation<'_>],
|
||||
) -> Result<(), Self::Error> {
|
||||
self.wrapped.transaction(address, operations).await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// SPI implementations
|
||||
//
|
||||
|
||||
impl<T> embedded_hal_async::spi::ErrorType for YieldingAsync<T>
|
||||
where
|
||||
T: embedded_hal_async::spi::ErrorType,
|
||||
{
|
||||
type Error = T::Error;
|
||||
}
|
||||
|
||||
impl<T> embedded_hal_async::spi::SpiBus<u8> for YieldingAsync<T>
|
||||
where
|
||||
T: embedded_hal_async::spi::SpiBus,
|
||||
{
|
||||
async fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Result<(), Self::Error> {
|
||||
self.wrapped.transfer(read, write).await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn transfer_in_place<'a>(&'a mut self, words: &'a mut [u8]) -> Result<(), Self::Error> {
|
||||
self.wrapped.transfer_in_place(words).await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> embedded_hal_async::spi::SpiBusFlush for YieldingAsync<T>
|
||||
where
|
||||
T: embedded_hal_async::spi::SpiBusFlush,
|
||||
{
|
||||
async fn flush(&mut self) -> Result<(), Self::Error> {
|
||||
self.wrapped.flush().await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> embedded_hal_async::spi::SpiBusWrite<u8> for YieldingAsync<T>
|
||||
where
|
||||
T: embedded_hal_async::spi::SpiBusWrite<u8>,
|
||||
{
|
||||
async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> {
|
||||
self.wrapped.write(data).await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> embedded_hal_async::spi::SpiBusRead<u8> for YieldingAsync<T>
|
||||
where
|
||||
T: embedded_hal_async::spi::SpiBusRead<u8>,
|
||||
{
|
||||
async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.wrapped.read(data).await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// NOR flash implementations
|
||||
///
|
||||
impl<T: embedded_storage::nor_flash::ErrorType> embedded_storage::nor_flash::ErrorType for YieldingAsync<T> {
|
||||
type Error = T::Error;
|
||||
}
|
||||
|
||||
impl<T: embedded_storage_async::nor_flash::ReadNorFlash> embedded_storage_async::nor_flash::ReadNorFlash
|
||||
for YieldingAsync<T>
|
||||
{
|
||||
const READ_SIZE: usize = T::READ_SIZE;
|
||||
|
||||
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.wrapped.read(offset, bytes).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
self.wrapped.capacity()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: embedded_storage_async::nor_flash::NorFlash> embedded_storage_async::nor_flash::NorFlash for YieldingAsync<T> {
|
||||
const WRITE_SIZE: usize = T::WRITE_SIZE;
|
||||
const ERASE_SIZE: usize = T::ERASE_SIZE;
|
||||
|
||||
async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
||||
self.wrapped.write(offset, bytes).await?;
|
||||
yield_now().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
||||
// Yield between each actual erase
|
||||
for from in (from..to).step_by(T::ERASE_SIZE) {
|
||||
let to = core::cmp::min(from + T::ERASE_SIZE as u32, to);
|
||||
self.wrapped.erase(from, to).await?;
|
||||
yield_now().await;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use embedded_storage_async::nor_flash::NorFlash;
|
||||
|
||||
use super::*;
|
||||
|
||||
extern crate std;
|
||||
|
||||
#[derive(Default)]
|
||||
struct FakeFlash(Vec<(u32, u32)>);
|
||||
|
||||
impl embedded_storage::nor_flash::ErrorType for FakeFlash {
|
||||
type Error = std::convert::Infallible;
|
||||
}
|
||||
|
||||
impl embedded_storage_async::nor_flash::ReadNorFlash for FakeFlash {
|
||||
const READ_SIZE: usize = 1;
|
||||
|
||||
async fn read(&mut self, _offset: u32, _bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_storage_async::nor_flash::NorFlash for FakeFlash {
|
||||
const WRITE_SIZE: usize = 4;
|
||||
const ERASE_SIZE: usize = 128;
|
||||
|
||||
async fn write(&mut self, _offset: u32, _bytes: &[u8]) -> Result<(), Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
||||
self.0.push((from, to));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[futures_test::test]
|
||||
async fn can_erase() {
|
||||
let fake = FakeFlash::default();
|
||||
let mut yielding = YieldingAsync::new(fake);
|
||||
|
||||
yielding.erase(0, 256).await.unwrap();
|
||||
|
||||
let fake = yielding.wrapped;
|
||||
assert_eq!(2, fake.0.len());
|
||||
assert_eq!((0, 128), fake.0[0]);
|
||||
assert_eq!((128, 256), fake.0[1]);
|
||||
}
|
||||
|
||||
#[futures_test::test]
|
||||
async fn can_erase_wrong_erase_size() {
|
||||
let fake = FakeFlash::default();
|
||||
let mut yielding = YieldingAsync::new(fake);
|
||||
|
||||
yielding.erase(0, 257).await.unwrap();
|
||||
|
||||
let fake = yielding.wrapped;
|
||||
assert_eq!(3, fake.0.len());
|
||||
assert_eq!((0, 128), fake.0[0]);
|
||||
assert_eq!((128, 256), fake.0[1]);
|
||||
assert_eq!((256, 257), fake.0[2]);
|
||||
}
|
||||
}
|
286
embassy-embedded-hal/src/flash.rs
Normal file
286
embassy-embedded-hal/src/flash.rs
Normal file
@ -0,0 +1,286 @@
|
||||
//! Utilities related to flash.
|
||||
|
||||
use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, ReadNorFlash};
|
||||
#[cfg(feature = "nightly")]
|
||||
use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
|
||||
|
||||
/// Convenience helper for concatenating two consecutive flashes into one.
|
||||
/// This is especially useful if used with "flash regions", where one may
|
||||
/// want to concatenate multiple regions into one larger region.
|
||||
pub struct ConcatFlash<First, Second>(First, Second);
|
||||
|
||||
impl<First, Second> ConcatFlash<First, Second> {
|
||||
/// Create a new flash that concatenates two consecutive flashes.
|
||||
pub fn new(first: First, second: Second) -> Self {
|
||||
Self(first, second)
|
||||
}
|
||||
}
|
||||
|
||||
const fn get_read_size(first_read_size: usize, second_read_size: usize) -> usize {
|
||||
if first_read_size != second_read_size {
|
||||
panic!("The read size for the concatenated flashes must be the same");
|
||||
}
|
||||
first_read_size
|
||||
}
|
||||
|
||||
const fn get_write_size(first_write_size: usize, second_write_size: usize) -> usize {
|
||||
if first_write_size != second_write_size {
|
||||
panic!("The write size for the concatenated flashes must be the same");
|
||||
}
|
||||
first_write_size
|
||||
}
|
||||
|
||||
const fn get_max_erase_size(first_erase_size: usize, second_erase_size: usize) -> usize {
|
||||
let max_erase_size = if first_erase_size > second_erase_size {
|
||||
first_erase_size
|
||||
} else {
|
||||
second_erase_size
|
||||
};
|
||||
if max_erase_size % first_erase_size != 0 || max_erase_size % second_erase_size != 0 {
|
||||
panic!("The erase sizes for the concatenated flashes must have have a gcd equal to the max erase size");
|
||||
}
|
||||
max_erase_size
|
||||
}
|
||||
|
||||
impl<First, Second, E> ErrorType for ConcatFlash<First, Second>
|
||||
where
|
||||
First: ErrorType<Error = E>,
|
||||
Second: ErrorType<Error = E>,
|
||||
E: NorFlashError,
|
||||
{
|
||||
type Error = E;
|
||||
}
|
||||
|
||||
impl<First, Second, E> ReadNorFlash for ConcatFlash<First, Second>
|
||||
where
|
||||
First: ReadNorFlash<Error = E>,
|
||||
Second: ReadNorFlash<Error = E>,
|
||||
E: NorFlashError,
|
||||
{
|
||||
const READ_SIZE: usize = get_read_size(First::READ_SIZE, Second::READ_SIZE);
|
||||
|
||||
fn read(&mut self, mut offset: u32, mut bytes: &mut [u8]) -> Result<(), E> {
|
||||
if offset < self.0.capacity() as u32 {
|
||||
let len = core::cmp::min(self.0.capacity() - offset as usize, bytes.len());
|
||||
self.0.read(offset, &mut bytes[..len])?;
|
||||
offset += len as u32;
|
||||
bytes = &mut bytes[len..];
|
||||
}
|
||||
|
||||
if !bytes.is_empty() {
|
||||
self.1.read(offset - self.0.capacity() as u32, bytes)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
self.0.capacity() + self.1.capacity()
|
||||
}
|
||||
}
|
||||
|
||||
impl<First, Second, E> NorFlash for ConcatFlash<First, Second>
|
||||
where
|
||||
First: NorFlash<Error = E>,
|
||||
Second: NorFlash<Error = E>,
|
||||
E: NorFlashError,
|
||||
{
|
||||
const WRITE_SIZE: usize = get_write_size(First::WRITE_SIZE, Second::WRITE_SIZE);
|
||||
const ERASE_SIZE: usize = get_max_erase_size(First::ERASE_SIZE, Second::ERASE_SIZE);
|
||||
|
||||
fn write(&mut self, mut offset: u32, mut bytes: &[u8]) -> Result<(), E> {
|
||||
if offset < self.0.capacity() as u32 {
|
||||
let len = core::cmp::min(self.0.capacity() - offset as usize, bytes.len());
|
||||
self.0.write(offset, &bytes[..len])?;
|
||||
offset += len as u32;
|
||||
bytes = &bytes[len..];
|
||||
}
|
||||
|
||||
if !bytes.is_empty() {
|
||||
self.1.write(offset - self.0.capacity() as u32, bytes)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn erase(&mut self, mut from: u32, to: u32) -> Result<(), E> {
|
||||
if from < self.0.capacity() as u32 {
|
||||
let to = core::cmp::min(self.0.capacity() as u32, to);
|
||||
self.0.erase(from, to)?;
|
||||
from = self.0.capacity() as u32;
|
||||
}
|
||||
|
||||
if from < to {
|
||||
self.1
|
||||
.erase(from - self.0.capacity() as u32, to - self.0.capacity() as u32)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl<First, Second, E> AsyncReadNorFlash for ConcatFlash<First, Second>
|
||||
where
|
||||
First: AsyncReadNorFlash<Error = E>,
|
||||
Second: AsyncReadNorFlash<Error = E>,
|
||||
E: NorFlashError,
|
||||
{
|
||||
const READ_SIZE: usize = get_read_size(First::READ_SIZE, Second::READ_SIZE);
|
||||
|
||||
async fn read(&mut self, mut offset: u32, mut bytes: &mut [u8]) -> Result<(), E> {
|
||||
if offset < self.0.capacity() as u32 {
|
||||
let len = core::cmp::min(self.0.capacity() - offset as usize, bytes.len());
|
||||
self.0.read(offset, &mut bytes[..len]).await?;
|
||||
offset += len as u32;
|
||||
bytes = &mut bytes[len..];
|
||||
}
|
||||
|
||||
if !bytes.is_empty() {
|
||||
self.1.read(offset - self.0.capacity() as u32, bytes).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
self.0.capacity() + self.1.capacity()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl<First, Second, E> AsyncNorFlash for ConcatFlash<First, Second>
|
||||
where
|
||||
First: AsyncNorFlash<Error = E>,
|
||||
Second: AsyncNorFlash<Error = E>,
|
||||
E: NorFlashError,
|
||||
{
|
||||
const WRITE_SIZE: usize = get_write_size(First::WRITE_SIZE, Second::WRITE_SIZE);
|
||||
const ERASE_SIZE: usize = get_max_erase_size(First::ERASE_SIZE, Second::ERASE_SIZE);
|
||||
|
||||
async fn write(&mut self, mut offset: u32, mut bytes: &[u8]) -> Result<(), E> {
|
||||
if offset < self.0.capacity() as u32 {
|
||||
let len = core::cmp::min(self.0.capacity() - offset as usize, bytes.len());
|
||||
self.0.write(offset, &bytes[..len]).await?;
|
||||
offset += len as u32;
|
||||
bytes = &bytes[len..];
|
||||
}
|
||||
|
||||
if !bytes.is_empty() {
|
||||
self.1.write(offset - self.0.capacity() as u32, bytes).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn erase(&mut self, mut from: u32, to: u32) -> Result<(), E> {
|
||||
if from < self.0.capacity() as u32 {
|
||||
let to = core::cmp::min(self.0.capacity() as u32, to);
|
||||
self.0.erase(from, to).await?;
|
||||
from = self.0.capacity() as u32;
|
||||
}
|
||||
|
||||
if from < to {
|
||||
self.1
|
||||
.erase(from - self.0.capacity() as u32, to - self.0.capacity() as u32)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn can_write_and_read_across_flashes() {
|
||||
let first = MemFlash::<64, 16, 4>::new();
|
||||
let second = MemFlash::<64, 64, 4>::new();
|
||||
let mut f = ConcatFlash::new(first, second);
|
||||
|
||||
f.write(60, &[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]).unwrap();
|
||||
|
||||
assert_eq!(&[0x11, 0x22, 0x33, 0x44], &f.0 .0[60..]);
|
||||
assert_eq!(&[0x55, 0x66, 0x77, 0x88], &f.1 .0[0..4]);
|
||||
|
||||
let mut read_buf = [0; 8];
|
||||
f.read(60, &mut read_buf).unwrap();
|
||||
|
||||
assert_eq!(&[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88], &read_buf);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_erase_across_flashes() {
|
||||
let mut first = MemFlash::<128, 16, 4>::new();
|
||||
let mut second = MemFlash::<128, 64, 4>::new();
|
||||
first.0.fill(0x00);
|
||||
second.0.fill(0x00);
|
||||
|
||||
let mut f = ConcatFlash::new(first, second);
|
||||
|
||||
f.erase(64, 192).unwrap();
|
||||
|
||||
assert_eq!(&[0x00; 64], &f.0 .0[0..64]);
|
||||
assert_eq!(&[0xff; 64], &f.0 .0[64..128]);
|
||||
assert_eq!(&[0xff; 64], &f.1 .0[0..64]);
|
||||
assert_eq!(&[0x00; 64], &f.1 .0[64..128]);
|
||||
}
|
||||
|
||||
pub struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize>([u8; SIZE]);
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE> {
|
||||
pub const fn new() -> Self {
|
||||
Self([0xff; SIZE])
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
type Error = core::convert::Infallible;
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ReadNorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const READ_SIZE: usize = 1;
|
||||
|
||||
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
let len = bytes.len();
|
||||
bytes.copy_from_slice(&self.0[offset as usize..offset as usize + len]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
SIZE
|
||||
}
|
||||
}
|
||||
|
||||
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> NorFlash
|
||||
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
|
||||
{
|
||||
const WRITE_SIZE: usize = WRITE_SIZE;
|
||||
const ERASE_SIZE: usize = ERASE_SIZE;
|
||||
|
||||
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
||||
let from = from as usize;
|
||||
let to = to as usize;
|
||||
assert_eq!(0, from % ERASE_SIZE);
|
||||
assert_eq!(0, to % ERASE_SIZE);
|
||||
self.0[from..to].fill(0xff);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
||||
let offset = offset as usize;
|
||||
assert_eq!(0, bytes.len() % WRITE_SIZE);
|
||||
assert_eq!(0, offset % WRITE_SIZE);
|
||||
assert!(offset + bytes.len() <= SIZE);
|
||||
|
||||
self.0[offset..offset + bytes.len()].copy_from_slice(bytes);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,8 @@
|
||||
#[cfg(feature = "nightly")]
|
||||
pub mod adapter;
|
||||
|
||||
pub mod flash;
|
||||
|
||||
pub mod shared_bus;
|
||||
|
||||
/// Set the configuration of a peripheral driver.
|
||||
|
Reference in New Issue
Block a user