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()) .filter(|x| x.kind == MemoryRegionKind::Flash && x.settings.is_some())
.collect(); .collect();
for region in flash_memory_regions.iter() { for region in flash_memory_regions.iter() {
let region_name = get_flash_region_name(region.name); let region_name = format_ident!("{}", get_flash_region_name(region.name));
let region_type = format_ident!("{}", region_name); let base = region.address;
let settings_name = format_ident!("{}_SETTINGS", region_name); let size = region.size;
let base = region.address as usize;
let size = region.size as usize;
let settings = region.settings.as_ref().unwrap(); let settings = region.settings.as_ref().unwrap();
let erase_size = settings.erase_size as usize; let erase_size = settings.erase_size;
let write_size = settings.write_size as usize; let write_size = settings.write_size;
let erase_value = settings.erase_value; let erase_value = settings.erase_value;
flash_regions.extend(quote! { flash_regions.extend(quote! {
#[allow(non_camel_case_types)] pub const #region_name: crate::flash::FlashRegion = crate::flash::FlashRegion {
pub struct #region_type(());
});
flash_regions.extend(quote! {
pub const #settings_name: crate::flash::FlashRegionSettings = crate::flash::FlashRegionSettings {
base: #base, base: #base,
size: #size, size: #size,
erase_size: #erase_size, erase_size: #erase_size,
write_size: #write_size, write_size: #write_size,
erase_value: #erase_value, erase_value: #erase_value,
}; };
});
impl crate::flash::FlashRegion for #region_type { let region_type = format_ident!("{}", get_flash_region_type_name(region.name));
const SETTINGS: crate::flash::FlashRegionSettings = #settings_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() .iter()
.map(|f| { .map(|f| {
let region_name = get_flash_region_name(f.name); let region_name = get_flash_region_name(f.name);
let field_name = format_ident!("{}", region_name.to_lowercase()); 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! { let field = quote! {
pub #field_name: #field_type pub #field_name: #field_type
}; };
let region_name = format_ident!("{}", region_name);
let init = quote! { 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(); .unzip();
let regions_len = flash_memory_regions.len(); let regions_len = flash_memory_regions.len();
flash_regions.extend(quote! { flash_regions.extend(quote! {
#[cfg(flash)] #[cfg(flash)]
pub struct FlashRegions<'d> { pub struct FlashLayout<'d> {
_inner: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>, _inner: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>,
#(#fields),* #(#fields),*
} }
#[cfg(flash)] #[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 { pub(crate) const fn new(p: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>) -> Self {
Self { Self {
_inner: p, _inner: p,
@ -178,8 +172,8 @@ fn main() {
} }
} }
pub const FLASH_REGIONS: [&crate::flash::FlashRegionSettings; #regions_len] = [ pub const FLASH_REGIONS: [&crate::flash::FlashRegion; #regions_len] = [
#(&#settings),* #(&#region_names),*
]; ];
}); });
@ -651,8 +645,11 @@ fn main() {
.iter() .iter()
.filter(|m| m.kind == MemoryRegionKind::Flash && m.settings.is_some()) .filter(|m| m.kind == MemoryRegionKind::Flash && m.settings.is_some())
{ {
let settings = m.settings.as_ref().unwrap();
let mut row = Vec::new(); 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); flash_regions_table.push(row);
} }
@ -906,6 +903,10 @@ macro_rules! {} {{
.unwrap(); .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 { fn get_flash_region_name(name: &str) -> String {
name.replace("BANK_", "BANK").replace("REGION_", "REGION") 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_hal_common::{into_ref, PeripheralRef};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::mutex::{Mutex, MutexGuard}; use embassy_sync::mutex::{Mutex, MutexGuard};
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
use super::{family, Error, FlashRegion}; use super::{family, Error, FlashRegion};
pub use crate::_generated::flash_regions::*; pub use crate::_generated::flash_regions::*;
@ -19,10 +18,8 @@ impl<'d> Flash<'d> {
Self { inner: p } Self { inner: p }
} }
pub fn into_regions(self) -> FlashRegions<'d> { pub fn into_regions(self) -> FlashLayout<'d> {
let mut flash = self; FlashLayout::new(self.release())
let p = unsafe { flash.inner.clone_unchecked() };
FlashRegions::new(p)
} }
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
@ -114,6 +111,11 @@ impl<'d> Flash<'d> {
} }
Ok(()) Ok(())
} }
pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> {
let mut flash = self;
unsafe { flash.inner.clone_unchecked() }
}
} }
impl Drop for Flash<'_> { 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! { foreach_flash_region! {
($name:ident) => { ($type_name:ident, $write_size:ident, $erase_size:ident) => {
impl ErrorType for crate::_generated::flash_regions::$name { 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; 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; const READ_SIZE: usize = 1;
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { 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 { 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 { impl NorFlash for crate::_generated::flash_regions::$type_name {
const WRITE_SIZE: usize = <crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.write_size; const WRITE_SIZE: usize = $write_size;
const ERASE_SIZE: usize = <crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.erase_size; const ERASE_SIZE: usize = $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) }
}
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { 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. fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
let _guard = take_lock_spin(); unsafe { self.0.blocking_erase_inner(from, to) }
unsafe { Flash::blocking_write_inner(start_address, bytes) }
} }
} }
}; };

View file

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