2023-04-01 18:10:20 +02:00
|
|
|
use atomic_polyfill::{fence, Ordering};
|
2023-05-24 12:17:12 +02:00
|
|
|
use embassy_cortex_m::interrupt::InterruptExt;
|
|
|
|
use embassy_futures::block_on;
|
2023-03-29 15:45:18 +02:00
|
|
|
use embassy_hal_common::drop::OnDrop;
|
|
|
|
use embassy_hal_common::{into_ref, PeripheralRef};
|
2023-05-24 12:17:12 +02:00
|
|
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
|
|
|
use embassy_sync::mutex::Mutex;
|
|
|
|
use stm32_metapac::FLASH_BASE;
|
|
|
|
|
|
|
|
use super::{
|
2023-05-24 13:11:03 +02:00
|
|
|
family, Error, FlashBank, FlashLayout, FlashRegion, FlashSector, FLASH_SIZE, MAX_ERASE_SIZE, READ_SIZE, WRITE_SIZE,
|
2023-05-24 12:17:12 +02:00
|
|
|
};
|
|
|
|
use crate::peripherals::FLASH;
|
2023-03-29 15:45:18 +02:00
|
|
|
use crate::Peripheral;
|
|
|
|
|
|
|
|
pub struct Flash<'d> {
|
2023-05-24 12:17:12 +02:00
|
|
|
pub(crate) inner: PeripheralRef<'d, FLASH>,
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub(crate) static REGION_ACCESS: Mutex<CriticalSectionRawMutex, ()> = Mutex::new(());
|
|
|
|
|
2023-03-29 15:45:18 +02:00
|
|
|
impl<'d> Flash<'d> {
|
2023-05-24 12:17:12 +02:00
|
|
|
pub fn new(p: impl Peripheral<P = FLASH> + 'd, irq: impl Peripheral<P = crate::interrupt::FLASH> + 'd) -> Self {
|
|
|
|
into_ref!(p, irq);
|
|
|
|
|
|
|
|
irq.set_handler(family::on_interrupt);
|
|
|
|
irq.unpend();
|
|
|
|
irq.enable();
|
|
|
|
|
2023-03-29 15:45:18 +02:00
|
|
|
Self { inner: p }
|
|
|
|
}
|
|
|
|
|
2023-03-30 04:24:41 +02:00
|
|
|
pub fn into_regions(self) -> FlashLayout<'d> {
|
2023-05-23 22:50:41 +02:00
|
|
|
family::set_default_layout();
|
2023-05-24 12:17:12 +02:00
|
|
|
FlashLayout::new(self.inner)
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
|
|
|
read_blocking(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes)
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub fn write_blocking(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
|
|
|
unsafe { write_chunked_blocking(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes) }
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub fn erase_blocking(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
|
|
|
unsafe { erase_sectored_blocking(FLASH_BASE as u32, from, to) }
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub(super) fn read_blocking(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
if offset + bytes.len() as u32 > size {
|
2023-03-30 15:13:44 +02:00
|
|
|
return Err(Error::Size);
|
|
|
|
}
|
|
|
|
|
2023-04-01 17:26:32 +02:00
|
|
|
let start_address = base + offset;
|
2023-03-30 15:13:44 +02:00
|
|
|
let flash_data = unsafe { core::slice::from_raw_parts(start_address as *const u8, bytes.len()) };
|
|
|
|
bytes.copy_from_slice(flash_data);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub(super) unsafe fn write_chunked_blocking(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
if offset + bytes.len() as u32 > size {
|
2023-03-30 15:13:44 +02:00
|
|
|
return Err(Error::Size);
|
|
|
|
}
|
2023-04-01 17:26:32 +02:00
|
|
|
if offset % WRITE_SIZE as u32 != 0 || bytes.len() % WRITE_SIZE != 0 {
|
2023-03-30 15:13:44 +02:00
|
|
|
return Err(Error::Unaligned);
|
|
|
|
}
|
|
|
|
|
2023-04-01 17:26:32 +02:00
|
|
|
let mut address = base + offset;
|
|
|
|
trace!("Writing {} bytes at 0x{:x}", bytes.len(), address);
|
2023-03-29 15:45:18 +02:00
|
|
|
|
2023-04-01 17:26:32 +02:00
|
|
|
for chunk in bytes.chunks(WRITE_SIZE) {
|
2023-05-24 12:17:12 +02:00
|
|
|
family::clear_all_err();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
family::unlock();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
family::enable_blocking_write();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
let _on_drop = OnDrop::new(|| {
|
|
|
|
family::disable_blocking_write();
|
2023-04-01 18:10:20 +02:00
|
|
|
fence(Ordering::SeqCst);
|
2023-05-24 12:17:12 +02:00
|
|
|
family::lock();
|
|
|
|
});
|
2023-04-01 18:10:20 +02:00
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
family::write_blocking(address, chunk.try_into().unwrap())?;
|
2023-03-30 15:13:44 +02:00
|
|
|
address += WRITE_SIZE as u32;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub(super) unsafe fn erase_sectored_blocking(base: u32, from: u32, to: u32) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
let start_address = base + from;
|
|
|
|
let end_address = base + to;
|
2023-03-30 15:13:44 +02:00
|
|
|
let regions = family::get_flash_regions();
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
ensure_sector_aligned(start_address, end_address, regions)?;
|
2023-03-30 15:13:44 +02:00
|
|
|
|
|
|
|
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);
|
2023-04-03 08:02:43 +02:00
|
|
|
trace!("Erasing sector: {:?}", sector);
|
2023-03-30 15:13:44 +02:00
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
family::clear_all_err();
|
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
family::unlock();
|
|
|
|
fence(Ordering::SeqCst);
|
2023-04-01 18:10:20 +02:00
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
let _on_drop = OnDrop::new(|| family::lock());
|
2023-04-01 18:10:20 +02:00
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
family::erase_sector_blocking(§or)?;
|
2023-03-30 15:13:44 +02:00
|
|
|
address += sector.size;
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:59 +02:00
|
|
|
pub(crate) fn get_sector(address: u32, regions: &[&FlashRegion]) -> FlashSector {
|
|
|
|
let mut current_bank = FlashBank::Bank1;
|
|
|
|
let mut bank_offset = 0;
|
|
|
|
for region in regions {
|
|
|
|
if region.bank != current_bank {
|
|
|
|
current_bank = region.bank;
|
|
|
|
bank_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if address < region.end() {
|
|
|
|
let index_in_region = (address - region.base) / region.erase_size;
|
|
|
|
return FlashSector {
|
|
|
|
bank: region.bank,
|
|
|
|
index_in_bank: bank_offset + index_in_region as u8,
|
|
|
|
start: region.base + index_in_region * region.erase_size,
|
|
|
|
size: region.erase_size,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bank_offset += region.sectors();
|
|
|
|
}
|
|
|
|
|
|
|
|
panic!("Flash sector not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn ensure_sector_aligned(
|
|
|
|
start_address: u32,
|
|
|
|
end_address: u32,
|
|
|
|
regions: &[&FlashRegion],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mut address = start_address;
|
|
|
|
while address < end_address {
|
|
|
|
let sector = get_sector(address, regions);
|
|
|
|
if sector.start != address {
|
|
|
|
return Err(Error::Unaligned);
|
|
|
|
}
|
|
|
|
address += sector.size;
|
|
|
|
}
|
|
|
|
if address != end_address {
|
|
|
|
return Err(Error::Unaligned);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
impl embedded_storage::nor_flash::ErrorType for Flash<'_> {
|
|
|
|
type Error = Error;
|
|
|
|
}
|
2023-03-30 08:32:36 +02:00
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
impl embedded_storage::nor_flash::ReadNorFlash for Flash<'_> {
|
|
|
|
const READ_SIZE: usize = READ_SIZE;
|
2023-03-30 08:32:36 +02:00
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
self.read(offset, bytes)
|
2023-03-30 08:32:36 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
FLASH_SIZE
|
|
|
|
}
|
2023-03-30 08:32:36 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
impl embedded_storage::nor_flash::NorFlash for Flash<'_> {
|
|
|
|
const WRITE_SIZE: usize = WRITE_SIZE;
|
|
|
|
const ERASE_SIZE: usize = MAX_ERASE_SIZE;
|
|
|
|
|
|
|
|
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
self.write_blocking(offset, bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
|
|
self.erase_blocking(from, to)
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
2023-05-24 12:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
impl embedded_storage_async::nor_flash::ReadNorFlash for Flash<'_> {
|
|
|
|
const READ_SIZE: usize = READ_SIZE;
|
2023-03-30 04:24:41 +02:00
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
self.read(offset, bytes)
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
FLASH_SIZE
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub struct BlockingFlashRegion<'d, const WRITE_SIZE: u32, const ERASE_SIZE: u32>(
|
|
|
|
&'static FlashRegion,
|
|
|
|
PeripheralRef<'d, FLASH>,
|
|
|
|
);
|
|
|
|
|
|
|
|
impl<const WRITE_SIZE: u32, const ERASE_SIZE: u32> BlockingFlashRegion<'_, WRITE_SIZE, ERASE_SIZE> {
|
|
|
|
pub fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
|
|
|
read_blocking(self.0.base, self.0.size, offset, bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
|
|
|
let _guard = block_on(REGION_ACCESS.lock());
|
|
|
|
unsafe { write_chunked_blocking(self.0.base, self.0.size, offset, bytes) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
|
|
|
let _guard = block_on(REGION_ACCESS.lock());
|
|
|
|
unsafe { erase_sectored_blocking(self.0.base, from, to) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const WRITE_SIZE: u32, const ERASE_SIZE: u32> embedded_storage::nor_flash::ErrorType
|
|
|
|
for BlockingFlashRegion<'_, WRITE_SIZE, ERASE_SIZE>
|
|
|
|
{
|
2023-04-18 13:48:37 +02:00
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
impl<const WRITE_SIZE: u32, const ERASE_SIZE: u32> embedded_storage::nor_flash::ReadNorFlash
|
|
|
|
for BlockingFlashRegion<'_, WRITE_SIZE, ERASE_SIZE>
|
|
|
|
{
|
|
|
|
const READ_SIZE: usize = READ_SIZE;
|
2023-04-18 13:48:37 +02:00
|
|
|
|
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
2023-05-24 12:17:12 +02:00
|
|
|
self.read(offset, bytes)
|
2023-04-18 13:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn capacity(&self) -> usize {
|
2023-05-24 12:17:12 +02:00
|
|
|
self.0.size as usize
|
2023-04-18 13:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
impl<const WRITE_SIZE: u32, const ERASE_SIZE: u32> embedded_storage::nor_flash::NorFlash
|
|
|
|
for BlockingFlashRegion<'_, WRITE_SIZE, ERASE_SIZE>
|
|
|
|
{
|
|
|
|
const WRITE_SIZE: usize = WRITE_SIZE as usize;
|
|
|
|
const ERASE_SIZE: usize = ERASE_SIZE as usize;
|
2023-04-18 13:48:37 +02:00
|
|
|
|
|
|
|
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
2023-05-24 12:17:12 +02:00
|
|
|
self.write(offset, bytes)
|
2023-04-18 13:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
2023-05-24 12:17:12 +02:00
|
|
|
self.erase(from, to)
|
2023-04-18 13:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 15:45:18 +02:00
|
|
|
foreach_flash_region! {
|
2023-03-30 09:05:13 +02:00
|
|
|
($type_name:ident, $write_size:literal, $erase_size:literal) => {
|
2023-05-24 12:17:12 +02:00
|
|
|
paste::paste! {
|
|
|
|
pub type [<Blocking $type_name>]<'d> = BlockingFlashRegion<'d, $write_size, $erase_size>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> crate::_generated::flash_regions::$type_name<'d> {
|
|
|
|
/// Make this flash region work in a blocking context.
|
|
|
|
///
|
|
|
|
/// SAFETY
|
|
|
|
///
|
|
|
|
/// This function is unsafe as incorect usage of parallel blocking operations
|
|
|
|
/// on multiple regions may cause a deadlock because each region requires mutual access to the flash.
|
|
|
|
pub unsafe fn into_blocking(self) -> BlockingFlashRegion<'d, $write_size, $erase_size> {
|
|
|
|
BlockingFlashRegion(self.0, self.1)
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
|
|
|
read_blocking(self.0.base, self.0.size, offset, bytes)
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub fn try_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
|
|
|
let _guard = REGION_ACCESS.try_lock().map_err(|_| Error::TryLockError)?;
|
|
|
|
unsafe { write_chunked_blocking(self.0.base, self.0.size, offset, bytes) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
|
|
|
let _guard = REGION_ACCESS.try_lock().map_err(|_| Error::TryLockError)?;
|
|
|
|
unsafe { erase_sectored_blocking(self.0.base, from, to) }
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 10:27:13 +02:00
|
|
|
impl embedded_storage::nor_flash::ErrorType for crate::_generated::flash_regions::$type_name<'_> {
|
2023-03-29 15:45:18 +02:00
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
2023-04-05 10:27:13 +02:00
|
|
|
impl embedded_storage::nor_flash::ReadNorFlash for crate::_generated::flash_regions::$type_name<'_> {
|
2023-05-24 12:17:12 +02:00
|
|
|
const READ_SIZE: usize = READ_SIZE;
|
2023-03-29 15:45:18 +02:00
|
|
|
|
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
2023-05-24 12:17:12 +02:00
|
|
|
self.read(offset, bytes)
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn capacity(&self) -> usize {
|
2023-03-30 04:24:41 +02:00
|
|
|
self.0.size as usize
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|