Remove the Flash trait

This commit is contained in:
Rasmus Melchior Jacobsen 2023-04-04 21:18:41 +02:00
parent 25577e0eaf
commit 6c93309df4
4 changed files with 32 additions and 57 deletions

View File

@ -30,22 +30,18 @@ where
}
}
/// Extension of the embedded-storage flash type information with block size and erase value.
pub trait Flash: NorFlash {
/// The erase value of the flash. Typically the default of 0xFF is used, but some flashes use a different value.
const ERASE_VALUE: u8 = 0xFF;
}
/// Trait defining the flash handles used for active and DFU partition.
/// The ACTIVE and DFU erase sizes must be equal. If this is not the case, then consider adding an adapter for the
/// smallest flash to increase its erase size such that they match. See e.g. [`crate::large_erase::LargeErase`].
pub trait FlashConfig {
/// The erase value of the state flash. Typically the default of 0xFF is used, but some flashes use a different value.
const STATE_ERASE_VALUE: u8 = 0xFF;
/// Flash type used for the state partition.
type STATE: Flash;
type STATE: NorFlash;
/// Flash type used for the active partition.
type ACTIVE: Flash;
type ACTIVE: NorFlash;
/// Flash type used for the dfu partition.
type DFU: Flash;
type DFU: NorFlash;
/// Return flash instance used to write/read to/from active partition.
fn active(&mut self) -> &mut Self::ACTIVE;
@ -208,7 +204,7 @@ impl BootLoader {
let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE];
// Invalidate progress
state_word.fill(!P::STATE::ERASE_VALUE);
state_word.fill(!P::STATE_ERASE_VALUE);
self.state
.write_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?;
@ -237,7 +233,7 @@ impl BootLoader {
self.state
.read_blocking(state_flash, P::STATE::WRITE_SIZE as u32, state_word)?;
if state_word.iter().any(|&b| b != P::STATE::ERASE_VALUE) {
if state_word.iter().any(|&b| b != P::STATE_ERASE_VALUE) {
// Progress is invalid
return Ok(max_index);
}
@ -249,7 +245,7 @@ impl BootLoader {
state_word,
)?;
if state_word.iter().any(|&b| b == P::STATE::ERASE_VALUE) {
if state_word.iter().any(|&b| b == P::STATE_ERASE_VALUE) {
return Ok(index);
}
}
@ -263,7 +259,7 @@ impl BootLoader {
aligned_buf: &mut [u8],
) -> Result<(), BootError> {
let state_word = &mut aligned_buf[..P::STATE::WRITE_SIZE];
state_word.fill(!P::STATE::ERASE_VALUE);
state_word.fill(!P::STATE_ERASE_VALUE);
self.state
.write_blocking(p.state(), (2 + index) as u32 * P::STATE::WRITE_SIZE as u32, state_word)?;
Ok(())
@ -386,16 +382,16 @@ fn assert_partitions(active: Partition, dfu: Partition, state: Partition, page_s
}
/// A flash wrapper implementing the Flash and embedded_storage traits.
pub struct BootFlash<F, const ERASE_VALUE: u8 = 0xFF>
pub struct BootFlash<F>
where
F: NorFlash + ReadNorFlash,
F: NorFlash,
{
flash: F,
}
impl<F, const ERASE_VALUE: u8> BootFlash<F, ERASE_VALUE>
impl<F> BootFlash<F>
where
F: NorFlash + ReadNorFlash,
F: NorFlash,
{
/// Create a new instance of a bootable flash
pub fn new(flash: F) -> Self {
@ -403,23 +399,16 @@ where
}
}
impl<F, const ERASE_VALUE: u8> Flash for BootFlash<F, ERASE_VALUE>
impl<F> ErrorType for BootFlash<F>
where
F: NorFlash + ReadNorFlash,
{
const ERASE_VALUE: u8 = ERASE_VALUE;
}
impl<F, const ERASE_VALUE: u8> ErrorType for BootFlash<F, ERASE_VALUE>
where
F: ReadNorFlash + NorFlash,
F: NorFlash,
{
type Error = F::Error;
}
impl<F, const ERASE_VALUE: u8> NorFlash for BootFlash<F, ERASE_VALUE>
impl<F> NorFlash for BootFlash<F>
where
F: ReadNorFlash + NorFlash,
F: NorFlash,
{
const WRITE_SIZE: usize = F::WRITE_SIZE;
const ERASE_SIZE: usize = F::ERASE_SIZE;
@ -433,9 +422,9 @@ where
}
}
impl<F, const ERASE_VALUE: u8> ReadNorFlash for BootFlash<F, ERASE_VALUE>
impl<F> ReadNorFlash for BootFlash<F>
where
F: ReadNorFlash + NorFlash,
F: NorFlash,
{
const READ_SIZE: usize = F::READ_SIZE;
@ -451,14 +440,14 @@ where
/// Convenience provider that uses a single flash for all partitions.
pub struct SingleFlashConfig<'a, F>
where
F: Flash,
F: NorFlash,
{
flash: &'a mut F,
}
impl<'a, F> SingleFlashConfig<'a, F>
where
F: Flash,
F: NorFlash,
{
/// Create a provider for a single flash.
pub fn new(flash: &'a mut F) -> Self {
@ -468,7 +457,7 @@ where
impl<'a, F> FlashConfig for SingleFlashConfig<'a, F>
where
F: Flash,
F: NorFlash,
{
type STATE = F;
type ACTIVE = F;
@ -488,9 +477,9 @@ where
/// Convenience flash provider that uses separate flash instances for each partition.
pub struct MultiFlashConfig<'a, ACTIVE, STATE, DFU>
where
ACTIVE: Flash,
STATE: Flash,
DFU: Flash,
ACTIVE: NorFlash,
STATE: NorFlash,
DFU: NorFlash,
{
active: &'a mut ACTIVE,
state: &'a mut STATE,
@ -499,9 +488,9 @@ where
impl<'a, ACTIVE, STATE, DFU> MultiFlashConfig<'a, ACTIVE, STATE, DFU>
where
ACTIVE: Flash,
STATE: Flash,
DFU: Flash,
ACTIVE: NorFlash,
STATE: NorFlash,
DFU: NorFlash,
{
/// Create a new flash provider with separate configuration for all three partitions.
pub fn new(active: &'a mut ACTIVE, state: &'a mut STATE, dfu: &'a mut DFU) -> Self {
@ -511,9 +500,9 @@ where
impl<'a, ACTIVE, STATE, DFU> FlashConfig for MultiFlashConfig<'a, ACTIVE, STATE, DFU>
where
ACTIVE: Flash,
STATE: Flash,
DFU: Flash,
ACTIVE: NorFlash,
STATE: NorFlash,
DFU: NorFlash,
{
type STATE = STATE;
type ACTIVE = ACTIVE;

View File

@ -3,8 +3,6 @@
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
use crate::Flash;
pub struct LargeErase<F, const ERASE_SIZE: usize>(pub F);
impl<F, const ERASE_SIZE: usize> LargeErase<F, ERASE_SIZE> {
@ -13,10 +11,6 @@ impl<F, const ERASE_SIZE: usize> LargeErase<F, ERASE_SIZE> {
}
}
impl<F: Flash, const ERASE_SIZE: usize> Flash for LargeErase<F, ERASE_SIZE> {
const ERASE_VALUE: u8 = F::ERASE_VALUE;
}
impl<F: ErrorType, const ERASE_SIZE: usize> ErrorType for LargeErase<F, ERASE_SIZE> {
type Error = F::Error;
}

View File

@ -11,7 +11,7 @@ mod large_erase;
mod mem_flash;
mod partition;
pub use boot_loader::{BootError, BootFlash, BootLoader, Flash, FlashConfig, MultiFlashConfig, SingleFlashConfig};
pub use boot_loader::{BootError, BootFlash, BootLoader, FlashConfig, MultiFlashConfig, SingleFlashConfig};
pub use firmware_updater::{FirmwareUpdater, FirmwareUpdaterError};
pub use partition::Partition;

View File

@ -5,8 +5,6 @@ use core::ops::{Bound, Range, RangeBounds};
use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash};
use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
use crate::Flash;
pub struct MemFlash<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> {
pub mem: [u8; SIZE],
pub pending_write_successes: Option<usize>,
@ -44,12 +42,6 @@ impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> Defaul
}
}
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> Flash
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
{
const ERASE_VALUE: u8 = 0xFF;
}
impl<const SIZE: usize, const ERASE_SIZE: usize, const WRITE_SIZE: usize> ErrorType
for MemFlash<SIZE, ERASE_SIZE, WRITE_SIZE>
{