2023-05-26 21:40:12 +02:00
|
|
|
use embassy_sync::blocking_mutex::raw::RawMutex;
|
|
|
|
use embassy_sync::mutex::Mutex;
|
|
|
|
use embedded_storage::nor_flash::{ErrorType, NorFlashError, NorFlashErrorKind};
|
|
|
|
use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash};
|
|
|
|
|
|
|
|
/// A logical partition of an underlying shared flash
|
|
|
|
///
|
|
|
|
/// A partition holds an offset and a size of the flash,
|
|
|
|
/// and is restricted to operate with that range.
|
|
|
|
/// There is no guarantee that muliple partitions on the same flash
|
|
|
|
/// operate on mutually exclusive ranges - such a separation is up to
|
|
|
|
/// the user to guarantee.
|
2023-05-26 22:07:23 +02:00
|
|
|
pub struct Partition<'a, M: RawMutex, T: NorFlash> {
|
2023-05-26 21:40:12 +02:00
|
|
|
flash: &'a Mutex<M, T>,
|
|
|
|
offset: u32,
|
|
|
|
size: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Error<T> {
|
|
|
|
OutOfBounds,
|
|
|
|
Flash(T),
|
|
|
|
}
|
|
|
|
|
2023-05-26 22:07:23 +02:00
|
|
|
impl<'a, M: RawMutex, T: NorFlash> Partition<'a, M, T> {
|
2023-05-26 21:40:12 +02:00
|
|
|
/// Create a new partition
|
|
|
|
pub const fn new(flash: &'a Mutex<M, T>, offset: u32, size: u32) -> Self {
|
2023-05-26 22:07:23 +02:00
|
|
|
if offset % T::READ_SIZE as u32 != 0 || offset % T::WRITE_SIZE as u32 != 0 || offset % T::ERASE_SIZE as u32 != 0
|
|
|
|
{
|
|
|
|
panic!("Partition offset must be a multiple of read, write and erase size");
|
|
|
|
}
|
|
|
|
if size % T::READ_SIZE as u32 != 0 || size % T::WRITE_SIZE as u32 != 0 || size % T::ERASE_SIZE as u32 != 0 {
|
|
|
|
panic!("Partition size must be a multiple of read, write and erase size");
|
|
|
|
}
|
2023-05-26 21:40:12 +02:00
|
|
|
Self { flash, offset, size }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: NorFlashError> NorFlashError for Error<T> {
|
|
|
|
fn kind(&self) -> NorFlashErrorKind {
|
|
|
|
match self {
|
|
|
|
Error::OutOfBounds => NorFlashErrorKind::OutOfBounds,
|
|
|
|
Error::Flash(f) => f.kind(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 22:07:23 +02:00
|
|
|
impl<M: RawMutex, T: NorFlash> ErrorType for Partition<'_, M, T> {
|
2023-05-26 21:40:12 +02:00
|
|
|
type Error = Error<T::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "nightly")]
|
2023-05-26 22:07:23 +02:00
|
|
|
impl<M: RawMutex, T: NorFlash> ReadNorFlash for Partition<'_, M, T> {
|
2023-05-26 21:40:12 +02:00
|
|
|
const READ_SIZE: usize = T::READ_SIZE;
|
|
|
|
|
|
|
|
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
if offset + bytes.len() as u32 > self.size {
|
|
|
|
return Err(Error::OutOfBounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut flash = self.flash.lock().await;
|
|
|
|
flash.read(self.offset + offset, bytes).await.map_err(Error::Flash)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
self.size as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
impl<M: RawMutex, T: NorFlash> NorFlash for Partition<'_, M, 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> {
|
|
|
|
if offset + bytes.len() as u32 > self.size {
|
|
|
|
return Err(Error::OutOfBounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut flash = self.flash.lock().await;
|
|
|
|
flash.write(self.offset + offset, bytes).await.map_err(Error::Flash)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
|
|
if to > self.size {
|
|
|
|
return Err(Error::OutOfBounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut flash = self.flash.lock().await;
|
|
|
|
flash
|
|
|
|
.erase(self.offset + from, self.offset + to)
|
|
|
|
.await
|
|
|
|
.map_err(Error::Flash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::flash::mem_flash::MemFlash;
|
|
|
|
|
|
|
|
#[futures_test::test]
|
|
|
|
async fn can_read() {
|
|
|
|
let mut flash = MemFlash::<1024, 128, 4>::default();
|
2023-05-26 22:07:23 +02:00
|
|
|
flash.mem[132..132 + 8].fill(0xAA);
|
2023-05-26 21:40:12 +02:00
|
|
|
|
|
|
|
let flash = Mutex::<NoopRawMutex, _>::new(flash);
|
2023-05-26 22:07:23 +02:00
|
|
|
let mut partition = Partition::new(&flash, 128, 256);
|
2023-05-26 21:40:12 +02:00
|
|
|
|
|
|
|
let mut read_buf = [0; 8];
|
|
|
|
partition.read(4, &mut read_buf).await.unwrap();
|
|
|
|
|
|
|
|
assert!(read_buf.iter().position(|&x| x != 0xAA).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[futures_test::test]
|
|
|
|
async fn can_write() {
|
|
|
|
let flash = MemFlash::<1024, 128, 4>::default();
|
|
|
|
|
|
|
|
let flash = Mutex::<NoopRawMutex, _>::new(flash);
|
2023-05-26 22:07:23 +02:00
|
|
|
let mut partition = Partition::new(&flash, 128, 256);
|
2023-05-26 21:40:12 +02:00
|
|
|
|
|
|
|
let write_buf = [0xAA; 8];
|
|
|
|
partition.write(4, &write_buf).await.unwrap();
|
|
|
|
|
|
|
|
let flash = flash.try_lock().unwrap();
|
2023-05-26 22:07:23 +02:00
|
|
|
assert!(flash.mem[132..132 + 8].iter().position(|&x| x != 0xAA).is_none());
|
2023-05-26 21:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[futures_test::test]
|
|
|
|
async fn can_erase() {
|
|
|
|
let flash = MemFlash::<1024, 128, 4>::new(0x00);
|
|
|
|
|
|
|
|
let flash = Mutex::<NoopRawMutex, _>::new(flash);
|
|
|
|
let mut partition = Partition::new(&flash, 128, 256);
|
|
|
|
|
|
|
|
partition.erase(0, 128).await.unwrap();
|
|
|
|
|
|
|
|
let flash = flash.try_lock().unwrap();
|
|
|
|
assert!(flash.mem[128..256].iter().position(|&x| x != 0xFF).is_none());
|
|
|
|
}
|
|
|
|
}
|