Let FlashLayout and FlashRegion depends on a Blocking/Async mode generic

This commit is contained in:
Rasmus Melchior Jacobsen
2023-05-24 17:24:28 +02:00
parent 6df6239704
commit 44b6494ab7
6 changed files with 179 additions and 206 deletions

View File

@ -1,12 +1,21 @@
use atomic_polyfill::{fence, Ordering};
use embassy_hal_common::drop::OnDrop;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::mutex::Mutex;
use super::{
ensure_sector_aligned, family, get_sector, Error, Flash, FLASH_BASE, FLASH_SIZE, MAX_ERASE_SIZE, READ_SIZE,
REGION_ACCESS, WRITE_SIZE,
ensure_sector_aligned, family, get_sector, read_blocking, Async, Error, Flash, FlashLayout, FLASH_BASE, FLASH_SIZE,
MAX_ERASE_SIZE, READ_SIZE, WRITE_SIZE,
};
pub(super) static REGION_ACCESS: Mutex<CriticalSectionRawMutex, ()> = Mutex::new(());
impl<'d> Flash<'d> {
pub fn into_regions(self) -> FlashLayout<'d, Async> {
family::set_default_layout();
FlashLayout::new(self.inner)
}
pub async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
unsafe { write_chunked(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes).await }
}
@ -16,6 +25,18 @@ impl<'d> Flash<'d> {
}
}
impl embedded_storage_async::nor_flash::ReadNorFlash for Flash<'_> {
const READ_SIZE: usize = READ_SIZE;
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
self.read(offset, bytes)
}
fn capacity(&self) -> usize {
FLASH_SIZE
}
}
impl embedded_storage_async::nor_flash::NorFlash for Flash<'_> {
const WRITE_SIZE: usize = WRITE_SIZE;
const ERASE_SIZE: usize = MAX_ERASE_SIZE;
@ -89,7 +110,11 @@ 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<'_> {
impl crate::_generated::flash_regions::$type_name<'_, Async> {
pub fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
read_blocking(self.0.base, self.0.size, offset, bytes)
}
pub async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
let _guard = REGION_ACCESS.lock().await;
unsafe { write_chunked(self.0.base, self.0.size, offset, bytes).await }
@ -101,7 +126,7 @@ foreach_flash_region! {
}
}
impl embedded_storage_async::nor_flash::ReadNorFlash for crate::_generated::flash_regions::$type_name<'_> {
impl embedded_storage_async::nor_flash::ReadNorFlash for crate::_generated::flash_regions::$type_name<'_, Async> {
const READ_SIZE: usize = READ_SIZE;
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
@ -113,7 +138,7 @@ foreach_flash_region! {
}
}
impl embedded_storage_async::nor_flash::NorFlash for crate::_generated::flash_regions::$type_name<'_> {
impl embedded_storage_async::nor_flash::NorFlash for crate::_generated::flash_regions::$type_name<'_, Async> {
const WRITE_SIZE: usize = $write_size;
const ERASE_SIZE: usize = $erase_size;