Let Flash<Async/Blocking> be a thing
This commit is contained in:
@ -10,25 +10,22 @@ use super::{
|
||||
|
||||
pub(super) static REGION_ACCESS: Mutex<CriticalSectionRawMutex, ()> = Mutex::new(());
|
||||
|
||||
impl<'d> Flash<'d> {
|
||||
impl<'d> Flash<'d, Async> {
|
||||
pub fn into_regions(self) -> FlashLayout<'d, Async> {
|
||||
assert!(!self.blocking_only);
|
||||
family::set_default_layout();
|
||||
FlashLayout::new(self.inner)
|
||||
}
|
||||
|
||||
pub async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
||||
assert!(!self.blocking_only);
|
||||
unsafe { write_chunked(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes).await }
|
||||
}
|
||||
|
||||
pub async fn erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
||||
assert!(!self.blocking_only);
|
||||
unsafe { erase_sectored(FLASH_BASE as u32, from, to).await }
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_storage_async::nor_flash::ReadNorFlash for Flash<'_> {
|
||||
impl embedded_storage_async::nor_flash::ReadNorFlash for Flash<'_, Async> {
|
||||
const READ_SIZE: usize = READ_SIZE;
|
||||
|
||||
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
@ -40,7 +37,7 @@ impl embedded_storage_async::nor_flash::ReadNorFlash for Flash<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_storage_async::nor_flash::NorFlash for Flash<'_> {
|
||||
impl embedded_storage_async::nor_flash::NorFlash for Flash<'_, Async> {
|
||||
const WRITE_SIZE: usize = WRITE_SIZE;
|
||||
const ERASE_SIZE: usize = MAX_ERASE_SIZE;
|
||||
|
||||
@ -114,7 +111,7 @@ pub(super) async unsafe fn erase_sectored(base: u32, from: u32, to: u32) -> Resu
|
||||
foreach_flash_region! {
|
||||
($type_name:ident, $write_size:literal, $erase_size:literal) => {
|
||||
impl crate::_generated::flash_regions::$type_name<'_, Async> {
|
||||
pub fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
||||
pub async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
||||
read_blocking(self.0.base, self.0.size, offset, bytes)
|
||||
}
|
||||
|
||||
@ -133,7 +130,7 @@ foreach_flash_region! {
|
||||
const READ_SIZE: usize = READ_SIZE;
|
||||
|
||||
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.read(offset, bytes)
|
||||
self.read(offset, bytes).await
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
|
@ -1,3 +1,5 @@
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use atomic_polyfill::{fence, Ordering};
|
||||
use embassy_cortex_m::interrupt::{Interrupt, InterruptExt};
|
||||
use embassy_hal_common::drop::OnDrop;
|
||||
@ -5,19 +7,18 @@ use embassy_hal_common::{into_ref, PeripheralRef};
|
||||
use stm32_metapac::FLASH_BASE;
|
||||
|
||||
use super::{
|
||||
family, Blocking, Error, FlashBank, FlashLayout, FlashRegion, FlashSector, FLASH_SIZE, MAX_ERASE_SIZE, READ_SIZE,
|
||||
WRITE_SIZE,
|
||||
family, Async, Blocking, Error, FlashBank, FlashLayout, FlashRegion, FlashSector, FLASH_SIZE, MAX_ERASE_SIZE,
|
||||
READ_SIZE, WRITE_SIZE,
|
||||
};
|
||||
use crate::peripherals::FLASH;
|
||||
use crate::{interrupt, Peripheral};
|
||||
|
||||
pub struct Flash<'d> {
|
||||
pub struct Flash<'d, MODE = Async> {
|
||||
pub(crate) inner: PeripheralRef<'d, FLASH>,
|
||||
#[cfg(all(feature = "nightly", flash_f4))]
|
||||
pub(crate) blocking_only: bool,
|
||||
_mode: PhantomData<MODE>,
|
||||
}
|
||||
|
||||
impl<'d> Flash<'d> {
|
||||
impl<'d> Flash<'d, Async> {
|
||||
pub fn new(
|
||||
p: impl Peripheral<P = FLASH> + 'd,
|
||||
_irq: impl interrupt::Binding<crate::interrupt::FLASH, InterruptHandler> + 'd,
|
||||
@ -30,21 +31,23 @@ impl<'d> Flash<'d> {
|
||||
|
||||
Self {
|
||||
inner: p,
|
||||
#[cfg(all(feature = "nightly", flash_f4))]
|
||||
blocking_only: false,
|
||||
_mode: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_blocking_only(p: impl Peripheral<P = FLASH> + 'd) -> Self {
|
||||
impl<'d> Flash<'d, Blocking> {
|
||||
pub fn new_blocking(p: impl Peripheral<P = FLASH> + 'd) -> Self {
|
||||
into_ref!(p);
|
||||
|
||||
Self {
|
||||
inner: p,
|
||||
#[cfg(all(feature = "nightly", flash_f4))]
|
||||
blocking_only: true,
|
||||
_mode: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, MODE> Flash<'d, MODE> {
|
||||
pub fn into_blocking_regions(self) -> FlashLayout<'d, Blocking> {
|
||||
family::set_default_layout();
|
||||
FlashLayout::new(self.inner)
|
||||
@ -222,11 +225,11 @@ pub(super) fn ensure_sector_aligned(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl embedded_storage::nor_flash::ErrorType for Flash<'_> {
|
||||
impl<MODE> embedded_storage::nor_flash::ErrorType for Flash<'_, MODE> {
|
||||
type Error = Error;
|
||||
}
|
||||
|
||||
impl embedded_storage::nor_flash::ReadNorFlash for Flash<'_> {
|
||||
impl<MODE> embedded_storage::nor_flash::ReadNorFlash for Flash<'_, MODE> {
|
||||
const READ_SIZE: usize = READ_SIZE;
|
||||
|
||||
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
@ -238,7 +241,7 @@ impl embedded_storage::nor_flash::ReadNorFlash for Flash<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_storage::nor_flash::NorFlash for Flash<'_> {
|
||||
impl<MODE> embedded_storage::nor_flash::NorFlash for Flash<'_, MODE> {
|
||||
const WRITE_SIZE: usize = WRITE_SIZE;
|
||||
const ERASE_SIZE: usize = MAX_ERASE_SIZE;
|
||||
|
||||
@ -253,11 +256,13 @@ impl embedded_storage::nor_flash::NorFlash for Flash<'_> {
|
||||
|
||||
foreach_flash_region! {
|
||||
($type_name:ident, $write_size:literal, $erase_size:literal) => {
|
||||
impl<'d> crate::_generated::flash_regions::$type_name<'d, Blocking> {
|
||||
impl<MODE> crate::_generated::flash_regions::$type_name<'_, MODE> {
|
||||
pub fn read_blocking(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
||||
read_blocking(self.0.base, self.0.size, offset, bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::_generated::flash_regions::$type_name<'_, Blocking> {
|
||||
pub fn write_blocking(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
||||
unsafe { write_blocking(self.0.base, self.0.size, offset, bytes, write_chunk_with_critical_section) }
|
||||
}
|
||||
@ -271,7 +276,7 @@ foreach_flash_region! {
|
||||
type Error = Error;
|
||||
}
|
||||
|
||||
impl embedded_storage::nor_flash::ReadNorFlash for crate::_generated::flash_regions::$type_name<'_, Blocking> {
|
||||
impl<MODE> embedded_storage::nor_flash::ReadNorFlash for crate::_generated::flash_regions::$type_name<'_, MODE> {
|
||||
const READ_SIZE: usize = READ_SIZE;
|
||||
|
||||
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
|
@ -107,10 +107,16 @@ mod alt_regions {
|
||||
|
||||
macro_rules! foreach_altflash_region {
|
||||
($type_name:ident, $region:ident) => {
|
||||
impl<MODE> $type_name<'_, MODE> {
|
||||
pub fn read_blocking(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
||||
crate::flash::common::read_blocking(self.0.base, self.0.size, offset, bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl $type_name<'_, Async> {
|
||||
pub async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
||||
crate::flash::common::read_blocking(self.0.base, self.0.size, offset, bytes)
|
||||
self.read_blocking(offset, bytes)
|
||||
}
|
||||
|
||||
pub async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
||||
@ -124,10 +130,22 @@ mod alt_regions {
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_storage::nor_flash::ErrorType for $type_name<'_, Async> {
|
||||
impl<MODE> embedded_storage::nor_flash::ErrorType for $type_name<'_, MODE> {
|
||||
type Error = Error;
|
||||
}
|
||||
|
||||
impl<MODE> embedded_storage::nor_flash::ReadNorFlash for $type_name<'_, MODE> {
|
||||
const READ_SIZE: usize = crate::flash::READ_SIZE;
|
||||
|
||||
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.read_blocking(offset, bytes)
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
self.0.size as usize
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
impl embedded_storage_async::nor_flash::ReadNorFlash for $type_name<'_, Async> {
|
||||
const READ_SIZE: usize = crate::flash::READ_SIZE;
|
||||
|
Reference in New Issue
Block a user