embassy/embassy-stm32/src/flash/common.rs

305 lines
9.0 KiB
Rust
Raw Normal View History

2023-05-25 21:40:54 +02:00
use core::marker::PhantomData;
use atomic_polyfill::{fence, Ordering};
2023-05-25 13:42:42 +02:00
use embassy_cortex_m::interrupt::{Interrupt, InterruptExt};
use embassy_hal_common::drop::OnDrop;
use embassy_hal_common::{into_ref, PeripheralRef};
use stm32_metapac::FLASH_BASE;
use super::{
2023-05-25 21:40:54 +02:00
family, Async, Blocking, Error, FlashBank, FlashLayout, FlashRegion, FlashSector, FLASH_SIZE, MAX_ERASE_SIZE,
READ_SIZE, WRITE_SIZE,
};
use crate::peripherals::FLASH;
2023-05-25 13:42:42 +02:00
use crate::{interrupt, Peripheral};
2023-05-25 21:40:54 +02:00
pub struct Flash<'d, MODE = Async> {
pub(crate) inner: PeripheralRef<'d, FLASH>,
2023-05-25 21:40:54 +02:00
_mode: PhantomData<MODE>,
}
2023-05-25 21:40:54 +02:00
impl<'d> Flash<'d, Async> {
2023-05-25 13:42:42 +02:00
pub fn new(
p: impl Peripheral<P = FLASH> + 'd,
_irq: impl interrupt::Binding<crate::interrupt::FLASH, InterruptHandler> + 'd,
) -> Self {
into_ref!(p);
2023-05-25 13:42:42 +02:00
let flash_irq = unsafe { crate::interrupt::FLASH::steal() };
flash_irq.unpend();
flash_irq.enable();
2023-05-25 13:42:42 +02:00
Self {
inner: p,
2023-05-25 21:40:54 +02:00
_mode: PhantomData,
2023-05-25 13:42:42 +02:00
}
}
2023-05-25 21:40:54 +02:00
}
2023-05-25 13:42:42 +02:00
2023-05-25 21:40:54 +02:00
impl<'d> Flash<'d, Blocking> {
pub fn new_blocking(p: impl Peripheral<P = FLASH> + 'd) -> Self {
2023-05-25 13:42:42 +02:00
into_ref!(p);
Self {
inner: p,
2023-05-25 21:40:54 +02:00
_mode: PhantomData,
2023-05-25 13:42:42 +02:00
}
}
2023-05-25 21:40:54 +02:00
}
2023-05-25 21:40:54 +02:00
impl<'d, MODE> Flash<'d, MODE> {
pub fn into_blocking_regions(self) -> FlashLayout<'d, Blocking> {
2023-05-23 22:50:41 +02:00
family::set_default_layout();
FlashLayout::new(self.inner)
}
pub fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
read_blocking(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes)
}
pub fn write_blocking(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
unsafe {
write_blocking(
FLASH_BASE as u32,
FLASH_SIZE as u32,
offset,
bytes,
write_chunk_unlocked,
)
}
}
pub fn erase_blocking(&mut self, from: u32, to: u32) -> Result<(), Error> {
unsafe { erase_blocking(FLASH_BASE as u32, from, to, erase_sector_unlocked) }
}
}
2023-05-25 13:42:42 +02:00
/// Interrupt handler
pub struct InterruptHandler;
impl interrupt::Handler<crate::interrupt::FLASH> for InterruptHandler {
unsafe fn on_interrupt() {
family::on_interrupt();
}
}
pub(super) fn read_blocking(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
if offset + bytes.len() as u32 > size {
2023-03-30 15:13:44 +02:00
return Err(Error::Size);
}
2023-05-25 20:55:12 +02:00
#[cfg(flash_f4)]
family::assert_not_corrupted_read();
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(())
}
pub(super) unsafe fn write_blocking(
base: u32,
size: u32,
offset: u32,
bytes: &[u8],
write_chunk: unsafe fn(u32, &[u8]) -> Result<(), Error>,
) -> Result<(), Error> {
if offset + bytes.len() as u32 > size {
2023-03-30 15:13:44 +02:00
return Err(Error::Size);
}
if offset % WRITE_SIZE as u32 != 0 || bytes.len() % WRITE_SIZE != 0 {
2023-03-30 15:13:44 +02:00
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) {
write_chunk(address, chunk)?;
2023-03-30 15:13:44 +02:00
address += WRITE_SIZE as u32;
}
Ok(())
}
pub(super) unsafe fn write_chunk_unlocked(address: u32, chunk: &[u8]) -> Result<(), Error> {
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();
fence(Ordering::SeqCst);
family::lock();
});
family::write_blocking(address, chunk.try_into().unwrap())
}
pub(super) unsafe fn write_chunk_with_critical_section(address: u32, chunk: &[u8]) -> Result<(), Error> {
critical_section::with(|_| write_chunk_unlocked(address, chunk))
}
pub(super) unsafe fn erase_blocking(
base: u32,
from: u32,
to: u32,
erase_sector: unsafe fn(&FlashSector) -> Result<(), Error>,
) -> Result<(), Error> {
let start_address = base + from;
let end_address = base + to;
2023-03-30 15:13:44 +02:00
let regions = family::get_flash_regions();
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);
erase_sector(&sector)?;
2023-03-30 15:13:44 +02:00
address += sector.size;
}
Ok(())
}
pub(super) unsafe fn erase_sector_unlocked(sector: &FlashSector) -> Result<(), Error> {
family::clear_all_err();
fence(Ordering::SeqCst);
family::unlock();
fence(Ordering::SeqCst);
let _on_drop = OnDrop::new(|| family::lock());
family::erase_sector_blocking(&sector)
}
pub(super) unsafe fn erase_sector_with_critical_section(sector: &FlashSector) -> Result<(), Error> {
critical_section::with(|_| erase_sector_unlocked(sector))
}
pub(super) 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(super) 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-25 21:40:54 +02:00
impl<MODE> embedded_storage::nor_flash::ErrorType for Flash<'_, MODE> {
type Error = Error;
}
2023-05-25 21:40:54 +02:00
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> {
self.read(offset, bytes)
}
fn capacity(&self) -> usize {
FLASH_SIZE
}
}
2023-05-25 21:40:54 +02:00
impl<MODE> embedded_storage::nor_flash::NorFlash for Flash<'_, MODE> {
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)
}
}
foreach_flash_region! {
($type_name:ident, $write_size:literal, $erase_size:literal) => {
2023-05-25 21:40:54 +02:00
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)
}
2023-05-25 21:40:54 +02:00
}
2023-05-25 21:40:54 +02:00
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) }
}
pub fn erase_blocking(&mut self, from: u32, to: u32) -> Result<(), Error> {
unsafe { erase_blocking(self.0.base, from, to, erase_sector_with_critical_section) }
}
}
impl<MODE> embedded_storage::nor_flash::ErrorType for crate::_generated::flash_regions::$type_name<'_, MODE> {
type Error = Error;
}
2023-05-25 21:40:54 +02:00
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> {
self.read_blocking(offset, bytes)
}
fn capacity(&self) -> usize {
self.0.size as usize
}
}
impl embedded_storage::nor_flash::NorFlash for crate::_generated::flash_regions::$type_name<'_, Blocking> {
const WRITE_SIZE: usize = $write_size;
const ERASE_SIZE: usize = $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)
}
}
};
}