2023-03-29 15:45:18 +02:00
|
|
|
use embassy_hal_common::drop::OnDrop;
|
|
|
|
use embassy_hal_common::{into_ref, PeripheralRef};
|
|
|
|
|
2023-03-30 08:32:36 +02:00
|
|
|
use super::{family, Error, FlashLayout, FlashRegion, FlashSector, FLASH_BASE, FLASH_SIZE, WRITE_SIZE};
|
|
|
|
use crate::flash::FlashBank;
|
2023-03-29 15:45:18 +02:00
|
|
|
use crate::Peripheral;
|
|
|
|
|
|
|
|
pub struct Flash<'d> {
|
|
|
|
inner: PeripheralRef<'d, crate::peripherals::FLASH>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d> Flash<'d> {
|
|
|
|
pub fn new(p: impl Peripheral<P = crate::peripherals::FLASH> + 'd) -> Self {
|
|
|
|
into_ref!(p);
|
|
|
|
Self { inner: p }
|
|
|
|
}
|
|
|
|
|
2023-03-30 04:24:41 +02:00
|
|
|
pub fn into_regions(self) -> FlashLayout<'d> {
|
|
|
|
FlashLayout::new(self.release())
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
blocking_read(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes)
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 17:26:32 +02:00
|
|
|
pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
|
|
|
unsafe { blocking_write(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes) }
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
unsafe { blocking_erase(FLASH_BASE as u32, from, to) }
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
2023-03-30 04:24:41 +02:00
|
|
|
|
|
|
|
pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> {
|
|
|
|
let mut flash = self;
|
|
|
|
unsafe { flash.inner.clone_unchecked() }
|
|
|
|
}
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Flash<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { family::lock() };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 06:01:56 +02:00
|
|
|
impl Drop for FlashLayout<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { family::lock() };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 17:26:32 +02:00
|
|
|
fn blocking_read(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-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-04-01 17:26:32 +02:00
|
|
|
unsafe fn blocking_write(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
|
|
|
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-03-30 15:13:44 +02:00
|
|
|
critical_section::with(|_| {
|
|
|
|
family::clear_all_err();
|
|
|
|
family::unlock();
|
|
|
|
family::begin_write();
|
|
|
|
let _ = OnDrop::new(|| {
|
|
|
|
family::end_write();
|
|
|
|
family::lock();
|
|
|
|
});
|
|
|
|
family::blocking_write(address, chunk.try_into().unwrap())
|
|
|
|
})?;
|
|
|
|
address += WRITE_SIZE as u32;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-01 17:26:32 +02:00
|
|
|
unsafe fn blocking_erase(base: u32, from: u32, to: u32) -> 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();
|
|
|
|
|
|
|
|
// Test if the address range is aligned at sector base addresses
|
|
|
|
let mut address = start_address;
|
|
|
|
while address < end_address {
|
|
|
|
let sector = get_sector(address, regions);
|
|
|
|
if sector.start != address {
|
|
|
|
return Err(Error::Unaligned);
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
2023-03-30 15:13:44 +02:00
|
|
|
address += sector.size;
|
|
|
|
}
|
|
|
|
if address != end_address {
|
|
|
|
return Err(Error::Unaligned);
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
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);
|
|
|
|
trace!("Erasing sector: {}", sector);
|
|
|
|
|
|
|
|
critical_section::with(|_| {
|
|
|
|
family::clear_all_err();
|
|
|
|
family::unlock();
|
|
|
|
let _ = OnDrop::new(|| {
|
|
|
|
family::lock();
|
|
|
|
});
|
|
|
|
family::blocking_erase_sector(§or)
|
|
|
|
})?;
|
|
|
|
address += sector.size;
|
|
|
|
}
|
|
|
|
Ok(())
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
|
2023-03-30 08:32:36 +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");
|
|
|
|
}
|
|
|
|
|
2023-03-30 04:24:41 +02:00
|
|
|
impl FlashRegion {
|
|
|
|
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
blocking_read(self.base, self.size, offset, bytes)
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
unsafe { blocking_write(self.base, self.size, offset, bytes) }
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
|
2023-03-30 15:13:44 +02:00
|
|
|
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
unsafe { blocking_erase(self.base, from, to) }
|
2023-03-30 04:24:41 +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-03-30 04:24:41 +02:00
|
|
|
impl crate::_generated::flash_regions::$type_name {
|
|
|
|
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
blocking_read(self.0.base, self.0.size, offset, bytes)
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
unsafe { blocking_write(self.0.base, self.0.size, offset, bytes) }
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
2023-04-01 17:26:32 +02:00
|
|
|
unsafe { blocking_erase(self.0.base, from, to) }
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 09:05: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-03-30 09:05:13 +02:00
|
|
|
impl embedded_storage::nor_flash::ReadNorFlash for crate::_generated::flash_regions::$type_name {
|
2023-03-29 15:45:18 +02:00
|
|
|
const READ_SIZE: usize = 1;
|
|
|
|
|
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
2023-03-30 15:13:44 +02:00
|
|
|
self.blocking_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 09:05:13 +02:00
|
|
|
impl embedded_storage::nor_flash::NorFlash for crate::_generated::flash_regions::$type_name {
|
2023-03-30 04:24:41 +02:00
|
|
|
const WRITE_SIZE: usize = $write_size;
|
|
|
|
const ERASE_SIZE: usize = $erase_size;
|
2023-03-29 15:45:18 +02:00
|
|
|
|
|
|
|
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
2023-03-30 15:13:44 +02:00
|
|
|
self.blocking_write(offset, bytes)
|
2023-03-30 04:24:41 +02:00
|
|
|
}
|
2023-03-29 15:45:18 +02:00
|
|
|
|
2023-03-30 04:24:41 +02:00
|
|
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
2023-03-30 15:13:44 +02:00
|
|
|
self.blocking_erase(from, to)
|
2023-03-29 15:45:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|