Remove FlashRegion trait and rename Settings to FlashRegion

This commit is contained in:
Rasmus Melchior Jacobsen 2023-03-30 04:24:41 +02:00
parent ef1890e911
commit def576ac46
3 changed files with 103 additions and 64 deletions

View file

@ -112,64 +112,58 @@ fn main() {
.filter(|x| x.kind == MemoryRegionKind::Flash && x.settings.is_some())
.collect();
for region in flash_memory_regions.iter() {
let region_name = get_flash_region_name(region.name);
let region_type = format_ident!("{}", region_name);
let settings_name = format_ident!("{}_SETTINGS", region_name);
let base = region.address as usize;
let size = region.size as usize;
let region_name = format_ident!("{}", get_flash_region_name(region.name));
let base = region.address;
let size = region.size;
let settings = region.settings.as_ref().unwrap();
let erase_size = settings.erase_size as usize;
let write_size = settings.write_size as usize;
let erase_size = settings.erase_size;
let write_size = settings.write_size;
let erase_value = settings.erase_value;
flash_regions.extend(quote! {
#[allow(non_camel_case_types)]
pub struct #region_type(());
});
flash_regions.extend(quote! {
pub const #settings_name: crate::flash::FlashRegionSettings = crate::flash::FlashRegionSettings {
pub const #region_name: crate::flash::FlashRegion = crate::flash::FlashRegion {
base: #base,
size: #size,
erase_size: #erase_size,
write_size: #write_size,
erase_value: #erase_value,
};
});
impl crate::flash::FlashRegion for #region_type {
const SETTINGS: crate::flash::FlashRegionSettings = #settings_name;
}
let region_type = format_ident!("{}", get_flash_region_type_name(region.name));
flash_regions.extend(quote! {
pub struct #region_type(pub(crate) &'static crate::flash::FlashRegion);
});
}
let (fields, (inits, settings)): (Vec<TokenStream>, (Vec<TokenStream>, Vec<Ident>)) = flash_memory_regions
let (fields, (inits, region_names)): (Vec<TokenStream>, (Vec<TokenStream>, Vec<Ident>)) = flash_memory_regions
.iter()
.map(|f| {
let region_name = get_flash_region_name(f.name);
let field_name = format_ident!("{}", region_name.to_lowercase());
let field_type = format_ident!("{}", region_name);
let field_type = format_ident!("{}", get_flash_region_type_name(f.name));
let field = quote! {
pub #field_name: #field_type
};
let region_name = format_ident!("{}", region_name);
let init = quote! {
#field_name: #field_type(())
#field_name: #field_type(&#region_name)
};
let settings_name = format_ident!("{}_SETTINGS", region_name);
(field, (init, settings_name))
(field, (init, region_name))
})
.unzip();
let regions_len = flash_memory_regions.len();
flash_regions.extend(quote! {
#[cfg(flash)]
pub struct FlashRegions<'d> {
pub struct FlashLayout<'d> {
_inner: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>,
#(#fields),*
}
#[cfg(flash)]
impl<'d> FlashRegions<'d> {
impl<'d> FlashLayout<'d> {
pub(crate) const fn new(p: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>) -> Self {
Self {
_inner: p,
@ -178,8 +172,8 @@ fn main() {
}
}
pub const FLASH_REGIONS: [&crate::flash::FlashRegionSettings; #regions_len] = [
#(&#settings),*
pub const FLASH_REGIONS: [&crate::flash::FlashRegion; #regions_len] = [
#(&#region_names),*
];
});
@ -651,8 +645,11 @@ fn main() {
.iter()
.filter(|m| m.kind == MemoryRegionKind::Flash && m.settings.is_some())
{
let settings = m.settings.as_ref().unwrap();
let mut row = Vec::new();
row.push(get_flash_region_name(m.name));
row.push(get_flash_region_type_name(m.name));
row.push(settings.write_size.to_string());
row.push(settings.erase_size.to_string());
flash_regions_table.push(row);
}
@ -906,6 +903,10 @@ macro_rules! {} {{
.unwrap();
}
fn get_flash_region_type_name(name: &str) -> String {
name.replace("BANK_", "Bank").replace("REGION_", "Region")
}
fn get_flash_region_name(name: &str) -> String {
name.replace("BANK_", "BANK").replace("REGION_", "REGION")
}

View file

@ -2,7 +2,6 @@ use embassy_hal_common::drop::OnDrop;
use embassy_hal_common::{into_ref, PeripheralRef};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::mutex::{Mutex, MutexGuard};
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
use super::{family, Error, FlashRegion};
pub use crate::_generated::flash_regions::*;
@ -19,10 +18,8 @@ impl<'d> Flash<'d> {
Self { inner: p }
}
pub fn into_regions(self) -> FlashRegions<'d> {
let mut flash = self;
let p = unsafe { flash.inner.clone_unchecked() };
FlashRegions::new(p)
pub fn into_regions(self) -> FlashLayout<'d> {
FlashLayout::new(self.release())
}
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
@ -114,6 +111,11 @@ impl<'d> Flash<'d> {
}
Ok(())
}
pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> {
let mut flash = self;
unsafe { flash.inner.clone_unchecked() }
}
}
impl Drop for Flash<'_> {
@ -132,45 +134,85 @@ fn take_lock_spin() -> MutexGuard<'static, CriticalSectionRawMutex, ()> {
}
}
impl FlashRegion {
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
unsafe { self.blocking_read_inner(offset, bytes) }
}
pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
unsafe { self.blocking_write_inner(offset, bytes) }
}
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
unsafe { self.blocking_erase_inner(from, to) }
}
unsafe fn blocking_read_inner(&self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
Flash::blocking_read_inner(self.base + offset, bytes)
}
unsafe fn blocking_write_inner(&self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
let start_address = self.base + offset;
// Protect agains simultaneous write/erase to multiple regions.
let _guard = take_lock_spin();
Flash::blocking_write_inner(start_address, bytes)
}
unsafe fn blocking_erase_inner(&self, from: u32, to: u32) -> Result<(), Error> {
let start_address = self.base + from;
let end_address = self.base + to;
// Protect agains simultaneous write/erase to multiple regions.
let _guard = take_lock_spin();
Flash::blocking_erase_inner(start_address, end_address)
}
}
foreach_flash_region! {
($name:ident) => {
impl ErrorType for crate::_generated::flash_regions::$name {
($type_name:ident, $write_size:ident, $erase_size:ident) => {
impl crate::_generated::flash_regions::$type_name {
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
unsafe { self.0.blocking_read_inner(offset, bytes) }
}
pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
unsafe { self.0.blocking_write_inner(offset, bytes) }
}
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
unsafe { self.0.blocking_erase_inner(from, to) }
}
}
impl ErrorType for crate::_generated::flash_regions::$type_name {
type Error = Error;
}
impl ReadNorFlash for crate::_generated::flash_regions::$name {
impl ReadNorFlash for crate::_generated::flash_regions::$type_name {
const READ_SIZE: usize = 1;
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
Flash::blocking_read_inner(Self::SETTINGS.base as u32 + offset, bytes)
unsafe { self.0.blocking_read_inner(offset, bytes) }
}
fn capacity(&self) -> usize {
<crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.size
self.0.size as usize
}
}
impl NorFlash for crate::_generated::flash_regions::$name {
const WRITE_SIZE: usize = <crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.write_size;
const ERASE_SIZE: usize = <crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.erase_size;
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
let start_address = Self::SETTINGS.base as u32 + from;
let end_address = Self::SETTINGS.base as u32 + to;
// Protect agains simultaneous write/erase to multiple regions.
let _guard = take_lock_spin();
unsafe { Flash::blocking_erase_inner(start_address, end_address) }
}
impl NorFlash for crate::_generated::flash_regions::$type_name {
const WRITE_SIZE: usize = $write_size;
const ERASE_SIZE: usize = $erase_size;
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
let start_address = Self::SETTINGS.base as u32 + offset;
unsafe { self.0.blocking_write_inner(offset, bytes) }
}
// Protect agains simultaneous write/erase to multiple regions.
let _guard = take_lock_spin();
unsafe { Flash::blocking_write_inner(start_address, bytes) }
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
unsafe { self.0.blocking_erase_inner(from, to) }
}
}
};

View file

@ -45,15 +45,11 @@ mod common;
#[cfg(flash)]
pub use common::*;
pub trait FlashRegion {
const SETTINGS: FlashRegionSettings;
}
pub struct FlashRegionSettings {
pub base: usize,
pub size: usize,
pub erase_size: usize,
pub write_size: usize,
pub struct FlashRegion {
pub base: u32,
pub size: u32,
pub erase_size: u32,
pub write_size: u32,
pub erase_value: u8,
}
@ -64,7 +60,7 @@ pub struct FlashSector {
pub size: u32,
}
impl Drop for FlashRegions<'_> {
impl Drop for FlashLayout<'_> {
fn drop(&mut self) {
unsafe { family::lock() };
}