embassy/embassy-traits/src/flash.rs

60 lines
1.7 KiB
Rust
Raw Normal View History

2020-09-22 18:03:43 +02:00
use core::future::Future;
2021-02-28 22:05:37 +01:00
use core::pin::Pin;
2020-09-22 18:03:43 +02:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2021-01-09 00:51:07 +01:00
#[non_exhaustive]
2020-09-22 18:03:43 +02:00
pub enum Error {
Failed,
AddressMisaligned,
BufferMisaligned,
}
pub trait Flash {
2021-03-21 20:54:09 +01:00
type ReadFuture<'a>: Future<Output = Result<(), Error>>
where
Self: 'a;
type WriteFuture<'a>: Future<Output = Result<(), Error>>
where
Self: 'a;
type ErasePageFuture<'a>: Future<Output = Result<(), Error>>
where
Self: 'a;
2020-09-22 18:03:43 +02:00
/// Reads data from the flash device.
///
/// address must be a multiple of self.read_size().
/// buf.len() must be a multiple of self.read_size().
2021-02-28 22:05:37 +01:00
fn read<'a>(self: Pin<&'a mut Self>, address: usize, buf: &'a mut [u8])
-> Self::ReadFuture<'a>;
2020-09-22 18:03:43 +02:00
/// Writes data to the flash device.
///
/// address must be a multiple of self.write_size().
/// buf.len() must be a multiple of self.write_size().
2021-02-28 22:05:37 +01:00
fn write<'a>(self: Pin<&'a mut Self>, address: usize, buf: &'a [u8]) -> Self::WriteFuture<'a>;
2020-09-22 18:03:43 +02:00
/// Erases a single page from the flash device.
///
/// address must be a multiple of self.erase_size().
2021-02-28 22:05:37 +01:00
fn erase<'a>(self: Pin<&'a mut Self>, address: usize) -> Self::ErasePageFuture<'a>;
2020-09-22 18:03:43 +02:00
/// Returns the total size, in bytes.
/// This is not guaranteed to be a power of 2.
fn size(&self) -> usize;
/// Returns the read size in bytes.
/// This is guaranteed to be a power of 2.
fn read_size(&self) -> usize;
/// Returns the write size in bytes.
/// This is guaranteed to be a power of 2.
fn write_size(&self) -> usize;
/// Returns the erase size in bytes.
/// This is guaranteed to be a power of 2.
fn erase_size(&self) -> usize;
}