2023-05-25 23:51:10 +02:00
|
|
|
use core::marker::PhantomData;
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
use atomic_polyfill::{fence, Ordering};
|
|
|
|
use embassy_hal_common::drop::OnDrop;
|
2023-05-25 23:51:10 +02:00
|
|
|
use embassy_hal_common::into_ref;
|
2023-05-24 17:24:28 +02:00
|
|
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
|
|
|
use embassy_sync::mutex::Mutex;
|
2023-05-24 12:17:12 +02:00
|
|
|
|
|
|
|
use super::{
|
2023-05-25 21:46:26 +02:00
|
|
|
blocking_read, ensure_sector_aligned, family, get_sector, Async, Error, Flash, FlashLayout, FLASH_BASE, FLASH_SIZE,
|
2023-05-26 00:31:41 +02:00
|
|
|
WRITE_SIZE,
|
2023-05-24 12:17:12 +02:00
|
|
|
};
|
2023-06-08 16:08:40 +02:00
|
|
|
use crate::interrupt::InterruptExt;
|
2023-05-25 23:51:10 +02:00
|
|
|
use crate::peripherals::FLASH;
|
|
|
|
use crate::{interrupt, Peripheral};
|
2023-05-24 12:17:12 +02:00
|
|
|
|
2023-05-24 17:24:28 +02:00
|
|
|
pub(super) static REGION_ACCESS: Mutex<CriticalSectionRawMutex, ()> = Mutex::new(());
|
|
|
|
|
2023-05-25 21:40:54 +02:00
|
|
|
impl<'d> Flash<'d, Async> {
|
2023-05-25 23:51:10 +02:00
|
|
|
pub fn new(
|
|
|
|
p: impl Peripheral<P = FLASH> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<crate::interrupt::typelevel::FLASH, InterruptHandler> + 'd,
|
2023-05-25 23:51:10 +02:00
|
|
|
) -> Self {
|
|
|
|
into_ref!(p);
|
|
|
|
|
2023-06-08 16:08:40 +02:00
|
|
|
crate::interrupt::FLASH.unpend();
|
|
|
|
unsafe { crate::interrupt::FLASH.enable() };
|
2023-05-25 23:51:10 +02:00
|
|
|
|
|
|
|
Self {
|
|
|
|
inner: p,
|
|
|
|
_mode: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 17:24:28 +02:00
|
|
|
pub fn into_regions(self) -> FlashLayout<'d, Async> {
|
2023-05-26 15:23:36 +02:00
|
|
|
assert!(family::is_default_layout());
|
2023-05-24 17:24:28 +02:00
|
|
|
FlashLayout::new(self.inner)
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
|
|
|
unsafe { erase_sectored(FLASH_BASE as u32, from, to).await }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 23:51:10 +02:00
|
|
|
/// Interrupt handler
|
|
|
|
pub struct InterruptHandler;
|
|
|
|
|
2023-06-08 16:08:40 +02:00
|
|
|
impl interrupt::typelevel::Handler<crate::interrupt::typelevel::FLASH> for InterruptHandler {
|
2023-05-25 23:51:10 +02:00
|
|
|
unsafe fn on_interrupt() {
|
|
|
|
family::on_interrupt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 00:31:41 +02:00
|
|
|
#[cfg(feature = "nightly")]
|
2023-05-25 21:40:54 +02:00
|
|
|
impl embedded_storage_async::nor_flash::ReadNorFlash for Flash<'_, Async> {
|
2023-05-26 00:31:41 +02:00
|
|
|
const READ_SIZE: usize = super::READ_SIZE;
|
2023-05-24 17:24:28 +02:00
|
|
|
|
|
|
|
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
self.read(offset, bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
FLASH_SIZE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 00:31:41 +02:00
|
|
|
#[cfg(feature = "nightly")]
|
2023-05-25 21:40:54 +02:00
|
|
|
impl embedded_storage_async::nor_flash::NorFlash for Flash<'_, Async> {
|
2023-05-24 12:17:12 +02:00
|
|
|
const WRITE_SIZE: usize = WRITE_SIZE;
|
2023-05-26 00:31:41 +02:00
|
|
|
const ERASE_SIZE: usize = super::MAX_ERASE_SIZE;
|
2023-05-24 12:17:12 +02:00
|
|
|
|
|
|
|
async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
self.write(offset, bytes).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
|
|
self.erase(from, to).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) async unsafe fn write_chunked(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
|
|
|
if offset + bytes.len() as u32 > size {
|
|
|
|
return Err(Error::Size);
|
|
|
|
}
|
|
|
|
if offset % WRITE_SIZE as u32 != 0 || bytes.len() % WRITE_SIZE != 0 {
|
|
|
|
return Err(Error::Unaligned);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut address = base + offset;
|
|
|
|
trace!("Writing {} bytes at 0x{:x}", bytes.len(), address);
|
|
|
|
|
|
|
|
for chunk in bytes.chunks(WRITE_SIZE) {
|
|
|
|
family::clear_all_err();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
family::unlock();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
family::enable_write();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
let _on_drop = OnDrop::new(|| {
|
|
|
|
family::disable_write();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
family::lock();
|
|
|
|
});
|
|
|
|
|
|
|
|
family::write(address, chunk.try_into().unwrap()).await?;
|
|
|
|
address += WRITE_SIZE as u32;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) async unsafe fn erase_sectored(base: u32, from: u32, to: u32) -> Result<(), Error> {
|
|
|
|
let start_address = base + from;
|
|
|
|
let end_address = base + to;
|
|
|
|
let regions = family::get_flash_regions();
|
|
|
|
|
|
|
|
ensure_sector_aligned(start_address, end_address, regions)?;
|
|
|
|
|
|
|
|
trace!("Erasing from 0x{:x} to 0x{:x}", start_address, end_address);
|
|
|
|
|
|
|
|
let mut address = start_address;
|
|
|
|
while address < end_address {
|
|
|
|
let sector = get_sector(address, regions);
|
|
|
|
trace!("Erasing sector: {:?}", sector);
|
|
|
|
|
|
|
|
family::clear_all_err();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
family::unlock();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
let _on_drop = OnDrop::new(|| family::lock());
|
|
|
|
|
|
|
|
family::erase_sector(§or).await?;
|
|
|
|
address += sector.size;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach_flash_region! {
|
|
|
|
($type_name:ident, $write_size:literal, $erase_size:literal) => {
|
2023-05-24 17:24:28 +02:00
|
|
|
impl crate::_generated::flash_regions::$type_name<'_, Async> {
|
2023-05-25 21:40:54 +02:00
|
|
|
pub async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
2023-05-25 21:46:26 +02:00
|
|
|
blocking_read(self.0.base, self.0.size, offset, bytes)
|
2023-05-24 17:24:28 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
|
|
|
let _guard = REGION_ACCESS.lock().await;
|
|
|
|
unsafe { erase_sectored(self.0.base, from, to).await }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 00:31:41 +02:00
|
|
|
#[cfg(feature = "nightly")]
|
2023-05-24 17:24:28 +02:00
|
|
|
impl embedded_storage_async::nor_flash::ReadNorFlash for crate::_generated::flash_regions::$type_name<'_, Async> {
|
2023-05-26 00:31:41 +02:00
|
|
|
const READ_SIZE: usize = super::READ_SIZE;
|
2023-05-24 12:17:12 +02:00
|
|
|
|
|
|
|
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
2023-05-25 21:40:54 +02:00
|
|
|
self.read(offset, bytes).await
|
2023-05-24 12:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
self.0.size as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 00:31:41 +02:00
|
|
|
#[cfg(feature = "nightly")]
|
2023-05-24 17:24:28 +02:00
|
|
|
impl embedded_storage_async::nor_flash::NorFlash for crate::_generated::flash_regions::$type_name<'_, Async> {
|
2023-05-24 12:17:12 +02:00
|
|
|
const WRITE_SIZE: usize = $write_size;
|
|
|
|
const ERASE_SIZE: usize = $erase_size;
|
|
|
|
|
|
|
|
async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
self.write(offset, bytes).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
|
|
self.erase(from, to).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|