Let Flash<Async/Blocking> be a thing

This commit is contained in:
Rasmus Melchior Jacobsen 2023-05-25 21:40:54 +02:00
parent 18d14dff48
commit 860b519f99
19 changed files with 63 additions and 43 deletions

View File

@ -10,25 +10,22 @@ use super::{
pub(super) static REGION_ACCESS: Mutex<CriticalSectionRawMutex, ()> = Mutex::new(()); 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> { pub fn into_regions(self) -> FlashLayout<'d, Async> {
assert!(!self.blocking_only);
family::set_default_layout(); family::set_default_layout();
FlashLayout::new(self.inner) FlashLayout::new(self.inner)
} }
pub async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { 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 } 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> { 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 } 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; const READ_SIZE: usize = READ_SIZE;
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { 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 WRITE_SIZE: usize = WRITE_SIZE;
const ERASE_SIZE: usize = MAX_ERASE_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! { foreach_flash_region! {
($type_name:ident, $write_size:literal, $erase_size:literal) => { ($type_name:ident, $write_size:literal, $erase_size:literal) => {
impl crate::_generated::flash_regions::$type_name<'_, Async> { 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) read_blocking(self.0.base, self.0.size, offset, bytes)
} }
@ -133,7 +130,7 @@ foreach_flash_region! {
const READ_SIZE: usize = READ_SIZE; const READ_SIZE: usize = READ_SIZE;
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { 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 { fn capacity(&self) -> usize {

View File

@ -1,3 +1,5 @@
use core::marker::PhantomData;
use atomic_polyfill::{fence, Ordering}; use atomic_polyfill::{fence, Ordering};
use embassy_cortex_m::interrupt::{Interrupt, InterruptExt}; use embassy_cortex_m::interrupt::{Interrupt, InterruptExt};
use embassy_hal_common::drop::OnDrop; use embassy_hal_common::drop::OnDrop;
@ -5,19 +7,18 @@ use embassy_hal_common::{into_ref, PeripheralRef};
use stm32_metapac::FLASH_BASE; use stm32_metapac::FLASH_BASE;
use super::{ use super::{
family, Blocking, Error, FlashBank, FlashLayout, FlashRegion, FlashSector, FLASH_SIZE, MAX_ERASE_SIZE, READ_SIZE, family, Async, Blocking, Error, FlashBank, FlashLayout, FlashRegion, FlashSector, FLASH_SIZE, MAX_ERASE_SIZE,
WRITE_SIZE, READ_SIZE, WRITE_SIZE,
}; };
use crate::peripherals::FLASH; use crate::peripherals::FLASH;
use crate::{interrupt, Peripheral}; use crate::{interrupt, Peripheral};
pub struct Flash<'d> { pub struct Flash<'d, MODE = Async> {
pub(crate) inner: PeripheralRef<'d, FLASH>, pub(crate) inner: PeripheralRef<'d, FLASH>,
#[cfg(all(feature = "nightly", flash_f4))] _mode: PhantomData<MODE>,
pub(crate) blocking_only: bool,
} }
impl<'d> Flash<'d> { impl<'d> Flash<'d, Async> {
pub fn new( pub fn new(
p: impl Peripheral<P = FLASH> + 'd, p: impl Peripheral<P = FLASH> + 'd,
_irq: impl interrupt::Binding<crate::interrupt::FLASH, InterruptHandler> + 'd, _irq: impl interrupt::Binding<crate::interrupt::FLASH, InterruptHandler> + 'd,
@ -30,21 +31,23 @@ impl<'d> Flash<'d> {
Self { Self {
inner: p, inner: p,
#[cfg(all(feature = "nightly", flash_f4))] _mode: PhantomData,
blocking_only: false,
} }
} }
}
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); into_ref!(p);
Self { Self {
inner: p, inner: p,
#[cfg(all(feature = "nightly", flash_f4))] _mode: PhantomData,
blocking_only: true,
} }
} }
}
impl<'d, MODE> Flash<'d, MODE> {
pub fn into_blocking_regions(self) -> FlashLayout<'d, Blocking> { pub fn into_blocking_regions(self) -> FlashLayout<'d, Blocking> {
family::set_default_layout(); family::set_default_layout();
FlashLayout::new(self.inner) FlashLayout::new(self.inner)
@ -222,11 +225,11 @@ pub(super) fn ensure_sector_aligned(
Ok(()) Ok(())
} }
impl embedded_storage::nor_flash::ErrorType for Flash<'_> { impl<MODE> embedded_storage::nor_flash::ErrorType for Flash<'_, MODE> {
type Error = Error; 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; const READ_SIZE: usize = READ_SIZE;
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { 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 WRITE_SIZE: usize = WRITE_SIZE;
const ERASE_SIZE: usize = MAX_ERASE_SIZE; const ERASE_SIZE: usize = MAX_ERASE_SIZE;
@ -253,11 +256,13 @@ impl embedded_storage::nor_flash::NorFlash for Flash<'_> {
foreach_flash_region! { foreach_flash_region! {
($type_name:ident, $write_size:literal, $erase_size:literal) => { ($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> { pub fn read_blocking(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
read_blocking(self.0.base, self.0.size, offset, bytes) 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> { 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) } 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; 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; const READ_SIZE: usize = READ_SIZE;
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {

View File

@ -107,10 +107,16 @@ mod alt_regions {
macro_rules! foreach_altflash_region { macro_rules! foreach_altflash_region {
($type_name:ident, $region:ident) => { ($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")] #[cfg(feature = "nightly")]
impl $type_name<'_, Async> { impl $type_name<'_, Async> {
pub async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { 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> { 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; 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")] #[cfg(feature = "nightly")]
impl embedded_storage_async::nor_flash::ReadNorFlash for $type_name<'_, Async> { impl embedded_storage_async::nor_flash::ReadNorFlash for $type_name<'_, Async> {
const READ_SIZE: usize = crate::flash::READ_SIZE; const READ_SIZE: usize = crate::flash::READ_SIZE;

View File

@ -26,7 +26,7 @@ async fn main(_s: Spawner) {
let mut watchdog = Watchdog::new(p.WATCHDOG); let mut watchdog = Watchdog::new(p.WATCHDOG);
watchdog.start(Duration::from_secs(8)); watchdog.start(Duration::from_secs(8));
let mut flash: Flash<_, FLASH_SIZE> = Flash::new_blocking_only(p.FLASH); let mut flash: Flash<_, FLASH_SIZE> = Flash::new_blocking(p.FLASH);
let mut updater = FirmwareUpdater::default(); let mut updater = FirmwareUpdater::default();

View File

@ -17,7 +17,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::new_blocking_only(p.FLASH); let flash = Flash::new_blocking(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PC13, Pull::Up); let button = Input::new(p.PC13, Pull::Up);

View File

@ -16,7 +16,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let mut flash = Flash::new_blocking_only(p.FLASH); let mut flash = Flash::new_blocking(p.FLASH);
let button = Input::new(p.PC13, Pull::Down); let button = Input::new(p.PC13, Pull::Down);
let mut button = ExtiInput::new(button, p.EXTI13); let mut button = ExtiInput::new(button, p.EXTI13);

View File

@ -16,7 +16,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let mut flash = Flash::new_blocking_only(p.FLASH); let mut flash = Flash::new_blocking(p.FLASH);
let button = Input::new(p.PC13, Pull::Down); let button = Input::new(p.PC13, Pull::Down);
let mut button = ExtiInput::new(button, p.EXTI13); let mut button = ExtiInput::new(button, p.EXTI13);

View File

@ -18,7 +18,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::new_blocking_only(p.FLASH); let flash = Flash::new_blocking(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PB2, Pull::Up); let button = Input::new(p.PB2, Pull::Up);

View File

@ -18,7 +18,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::new_blocking_only(p.FLASH); let flash = Flash::new_blocking(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PB2, Pull::Up); let button = Input::new(p.PB2, Pull::Up);

View File

@ -17,7 +17,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::new_blocking_only(p.FLASH); let flash = Flash::new_blocking(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PC13, Pull::Up); let button = Input::new(p.PC13, Pull::Up);

View File

@ -17,7 +17,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::new_blocking_only(p.FLASH); let flash = Flash::new_blocking(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PA0, Pull::Up); let button = Input::new(p.PA0, Pull::Up);

View File

@ -20,7 +20,7 @@ fn main() -> ! {
*/ */
let mut bl: BootLoader<2048> = BootLoader::default(); let mut bl: BootLoader<2048> = BootLoader::default();
let layout = Flash::new_blocking_only(p.FLASH).into_blocking_regions(); let layout = Flash::new_blocking(p.FLASH).into_blocking_regions();
let mut flash = BootFlash::new(layout.bank1_region); let mut flash = BootFlash::new(layout.bank1_region);
let start = bl.prepare(&mut SingleFlashConfig::new(&mut flash)); let start = bl.prepare(&mut SingleFlashConfig::new(&mut flash));
core::mem::drop(flash); core::mem::drop(flash);

View File

@ -14,7 +14,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x26000; const ADDR: u32 = 0x26000;
let mut f = Flash::new_blocking_only(p.FLASH).into_blocking_regions().bank1_region; let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank1_region;
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];

View File

@ -14,7 +14,7 @@ async fn main(_spawner: Spawner) {
// Once can also call `into_regions()` to get access to NorFlash implementations // Once can also call `into_regions()` to get access to NorFlash implementations
// for each of the unique characteristics. // for each of the unique characteristics.
let mut f = Flash::new_blocking_only(p.FLASH); let mut f = Flash::new_blocking(p.FLASH);
// Sector 5 // Sector 5
test_flash(&mut f, 128 * 1024, 128 * 1024); test_flash(&mut f, 128 * 1024, 128 * 1024);

View File

@ -4,7 +4,7 @@
use defmt::{info, unwrap}; use defmt::{info, unwrap};
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::{flash::Flash, interrupt}; use embassy_stm32::flash::Flash;
use embassy_time::{Duration, Timer}; use embassy_time::{Duration, Timer};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
@ -18,7 +18,7 @@ async fn main(_spawner: Spawner) {
// wait a bit before accessing the flash // wait a bit before accessing the flash
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
let mut f = Flash::new(p.FLASH, interrupt::take!(FLASH)).into_blocking_regions().bank1_region3; let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank1_region3;
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 32]; let mut buf = [0u8; 32];

View File

@ -18,7 +18,7 @@ async fn main(_spawner: Spawner) {
// wait a bit before accessing the flash // wait a bit before accessing the flash
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
let mut f = Flash::new_blocking_only(p.FLASH).into_blocking_regions().bank2_region; let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank2_region;
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 32]; let mut buf = [0u8; 32];

View File

@ -14,7 +14,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x26000; const ADDR: u32 = 0x26000;
let mut f = Flash::new_blocking_only(p.FLASH).into_blocking_regions().bank1_region; let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank1_region;
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];

View File

@ -14,7 +14,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x26000; const ADDR: u32 = 0x26000;
let mut f = Flash::new_blocking_only(p.FLASH).into_blocking_regions().bank1_region; let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank1_region;
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];

View File

@ -14,7 +14,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x36000; const ADDR: u32 = 0x36000;
let mut f = Flash::new_blocking_only(p.FLASH).into_blocking_regions().bank1_region; let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank1_region;
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];