use embedded_storage::nor_flash::{NorFlashError, NorFlashErrorKind};
use crate::error::Error;
pub struct Partition<F> {
inner: F,
base: u32,
len: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PartitionError<E> {
OutOfBounds,
NotAligned,
Device(E),
}
impl<E: NorFlashError> NorFlashError for PartitionError<E> {
fn kind(&self) -> NorFlashErrorKind {
match self {
Self::OutOfBounds => NorFlashErrorKind::OutOfBounds,
Self::NotAligned => NorFlashErrorKind::NotAligned,
Self::Device(e) => e.kind(),
}
}
}
impl<F> Partition<F> {
pub const fn base(&self) -> u32 {
self.base
}
pub const fn len(&self) -> u32 {
self.len
}
pub const fn is_empty(&self) -> bool {
self.len == 0
}
pub const fn end(&self) -> u32 {
self.base + self.len
}
pub const fn contains(&self, addr: u32) -> bool {
addr >= self.base && addr < self.end()
}
pub const fn inner(&self) -> &F {
&self.inner
}
pub const fn inner_mut(&mut self) -> &mut F {
&mut self.inner
}
pub fn into_inner(self) -> F {
self.inner
}
fn translate<E>(
&self,
align: usize,
offset: u32,
length: usize,
) -> Result<u32, PartitionError<E>> {
let end = (offset as u64)
.checked_add(length as u64)
.ok_or(PartitionError::OutOfBounds)?;
if end > self.len as u64 {
return Err(PartitionError::OutOfBounds);
}
if align > 1 && (!(offset as usize).is_multiple_of(align) || !length.is_multiple_of(align))
{
return Err(PartitionError::NotAligned);
}
Ok(self.base + offset)
}
fn translate_erase<E>(
&self,
erase_size: usize,
from: u32,
to: u32,
) -> Result<(u32, u32), PartitionError<E>> {
if from > to || to > self.len {
return Err(PartitionError::OutOfBounds);
}
let addr = self.translate(erase_size, from, (to - from) as usize)?;
Ok((addr, addr + (to - from)))
}
fn validate(
base: u32,
len: u32,
capacity: usize,
erase_size: usize,
write_size: usize,
read_size: usize,
) -> Result<(), Error> {
if len == 0 || erase_size == 0 || write_size == 0 || read_size == 0 {
return Err(Error::InvalidConfig);
}
let aligned = |v: u32| {
(v as usize).is_multiple_of(erase_size)
&& (v as usize).is_multiple_of(write_size)
&& (v as usize).is_multiple_of(read_size)
};
if !aligned(base) || !aligned(len) {
return Err(Error::InvalidConfig);
}
let end = (base as u64)
.checked_add(len as u64)
.ok_or(Error::Overflow)?;
if end > capacity as u64 {
return Err(Error::InvalidConfig);
}
Ok(())
}
}
impl<F> core::fmt::Debug for Partition<F> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Partition")
.field("base", &self.base)
.field("len", &self.len)
.finish_non_exhaustive()
}
}
#[cfg(feature = "defmt")]
impl<F> defmt::Format for Partition<F> {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"Partition {{ base: {=u32}, len: {=u32} }}",
self.base,
self.len
)
}
}
impl<F: crate::spool::Removable> crate::spool::Removable for Partition<F> {
fn is_present(&self) -> bool {
self.inner.is_present()
}
}
mod blocking_impl {
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
use super::{Partition, PartitionError};
use crate::error::Error;
impl<F: NorFlash> Partition<F> {
pub fn new(inner: F, base: u32, len: u32) -> Result<Self, Error> {
Self::validate(
base,
len,
inner.capacity(),
F::ERASE_SIZE,
F::WRITE_SIZE,
F::READ_SIZE,
)?;
Ok(Self { inner, base, len })
}
}
impl<F: ErrorType> ErrorType for Partition<F> {
type Error = PartitionError<F::Error>;
}
impl<F: ReadNorFlash> ReadNorFlash for Partition<F> {
const READ_SIZE: usize = F::READ_SIZE;
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
let addr = self.translate(F::READ_SIZE, offset, bytes.len())?;
self.inner.read(addr, bytes).map_err(PartitionError::Device)
}
fn capacity(&self) -> usize {
self.len as usize
}
}
impl<F: NorFlash> NorFlash for Partition<F> {
const WRITE_SIZE: usize = F::WRITE_SIZE;
const ERASE_SIZE: usize = F::ERASE_SIZE;
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
let (from, to) = self.translate_erase(F::ERASE_SIZE, from, to)?;
self.inner.erase(from, to).map_err(PartitionError::Device)
}
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
let addr = self.translate(F::WRITE_SIZE, offset, bytes.len())?;
self.inner
.write(addr, bytes)
.map_err(PartitionError::Device)
}
}
}
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
mod async_impl {
use embedded_storage_async::nor_flash::{NorFlash, ReadNorFlash};
use super::{Partition, PartitionError};
use crate::error::Error;
impl<F: NorFlash> Partition<F> {
pub fn new_async(inner: F, base: u32, len: u32) -> Result<Self, Error> {
Self::validate(
base,
len,
inner.capacity(),
F::ERASE_SIZE,
F::WRITE_SIZE,
F::READ_SIZE,
)?;
Ok(Self { inner, base, len })
}
}
impl<F: ReadNorFlash> ReadNorFlash for Partition<F> {
const READ_SIZE: usize = F::READ_SIZE;
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
let addr = self.translate(F::READ_SIZE, offset, bytes.len())?;
self.inner
.read(addr, bytes)
.await
.map_err(PartitionError::Device)
}
fn capacity(&self) -> usize {
self.len as usize
}
}
impl<F: NorFlash> NorFlash for Partition<F> {
const WRITE_SIZE: usize = F::WRITE_SIZE;
const ERASE_SIZE: usize = F::ERASE_SIZE;
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
let (from, to) = self.translate_erase(F::ERASE_SIZE, from, to)?;
self.inner
.erase(from, to)
.await
.map_err(PartitionError::Device)
}
async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
let addr = self.translate(F::WRITE_SIZE, offset, bytes.len())?;
self.inner
.write(addr, bytes)
.await
.map_err(PartitionError::Device)
}
}
}