Expose flash region settings as an array
This commit is contained in:
parent
d6ce1c4325
commit
6806bb9692
@ -3,7 +3,7 @@ use std::fmt::Write as _;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
|
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::{Ident, TokenStream};
|
||||||
use quote::{format_ident, quote};
|
use quote::{format_ident, quote};
|
||||||
use stm32_metapac::metadata::{MemoryRegionKind, METADATA};
|
use stm32_metapac::metadata::{MemoryRegionKind, METADATA};
|
||||||
|
|
||||||
@ -106,12 +106,15 @@ fn main() {
|
|||||||
// ========
|
// ========
|
||||||
// Generate FLASH regions
|
// Generate FLASH regions
|
||||||
let mut flash_regions = TokenStream::new();
|
let mut flash_regions = TokenStream::new();
|
||||||
let flash_memory_regions = METADATA
|
let flash_memory_regions: Vec<_> = METADATA
|
||||||
.memory
|
.memory
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|x| x.kind == MemoryRegionKind::Flash && x.settings.is_some());
|
.filter(|x| x.kind == MemoryRegionKind::Flash && x.settings.is_some())
|
||||||
for region in flash_memory_regions.clone() {
|
.collect();
|
||||||
let region_name = format_ident!("{}", get_flash_region_name(region.name));
|
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 base = region.address as usize;
|
||||||
let size = region.size as usize;
|
let size = region.size as usize;
|
||||||
let settings = region.settings.as_ref().unwrap();
|
let settings = region.settings.as_ref().unwrap();
|
||||||
@ -121,21 +124,26 @@ fn main() {
|
|||||||
|
|
||||||
flash_regions.extend(quote! {
|
flash_regions.extend(quote! {
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub struct #region_name(());
|
pub struct #region_type(());
|
||||||
});
|
});
|
||||||
|
|
||||||
flash_regions.extend(quote! {
|
flash_regions.extend(quote! {
|
||||||
impl crate::flash::FlashRegion for #region_name {
|
pub const #settings_name: crate::flash::FlashRegionSettings = crate::flash::FlashRegionSettings {
|
||||||
const BASE: usize = #base;
|
base: #base,
|
||||||
const SIZE: usize = #size;
|
size: #size,
|
||||||
const ERASE_SIZE: usize = #erase_size;
|
erase_size: #erase_size,
|
||||||
const WRITE_SIZE: usize = #write_size;
|
write_size: #write_size,
|
||||||
const ERASE_VALUE: u8 = #erase_value;
|
erase_value: #erase_value,
|
||||||
|
};
|
||||||
|
|
||||||
|
impl crate::flash::FlashRegion for #region_type {
|
||||||
|
const SETTINGS: crate::flash::FlashRegionSettings = #settings_name;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let (fields, inits): (Vec<TokenStream>, Vec<TokenStream>) = flash_memory_regions
|
let (fields, (inits, settings)): (Vec<TokenStream>, (Vec<TokenStream>, Vec<Ident>)) = flash_memory_regions
|
||||||
|
.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());
|
||||||
@ -146,11 +154,13 @@ fn main() {
|
|||||||
let init = quote! {
|
let init = quote! {
|
||||||
#field_name: #field_type(())
|
#field_name: #field_type(())
|
||||||
};
|
};
|
||||||
|
let settings_name = format_ident!("{}_SETTINGS", region_name);
|
||||||
|
|
||||||
(field, init)
|
(field, (init, settings_name))
|
||||||
})
|
})
|
||||||
.unzip();
|
.unzip();
|
||||||
|
|
||||||
|
let regions_len = flash_memory_regions.len();
|
||||||
flash_regions.extend(quote! {
|
flash_regions.extend(quote! {
|
||||||
pub struct FlashRegions {
|
pub struct FlashRegions {
|
||||||
#(#fields),*
|
#(#fields),*
|
||||||
@ -163,6 +173,10 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const FLASH_REGIONS: [&crate::flash::FlashRegionSettings; #regions_len] = [
|
||||||
|
#(&#settings),*
|
||||||
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
g.extend(quote! { pub mod flash_regions { #flash_regions } });
|
g.extend(quote! { pub mod flash_regions { #flash_regions } });
|
||||||
|
@ -19,6 +19,14 @@ pub struct Flash<'d> {
|
|||||||
_inner: PeripheralRef<'d, FLASH>,
|
_inner: PeripheralRef<'d, FLASH>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct FlashRegionSettings {
|
||||||
|
pub base: usize,
|
||||||
|
pub size: usize,
|
||||||
|
pub erase_size: usize,
|
||||||
|
pub write_size: usize,
|
||||||
|
pub erase_value: u8,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct FlashSector {
|
pub struct FlashSector {
|
||||||
pub index: u8,
|
pub index: u8,
|
||||||
@ -122,32 +130,28 @@ impl Drop for FlashRegions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait FlashRegion {
|
pub trait FlashRegion {
|
||||||
const BASE: usize;
|
const SETTINGS: FlashRegionSettings;
|
||||||
const SIZE: usize;
|
|
||||||
const ERASE_SIZE: usize;
|
|
||||||
const WRITE_SIZE: usize;
|
|
||||||
const ERASE_VALUE: u8;
|
|
||||||
|
|
||||||
fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
|
||||||
if offset as usize + bytes.len() > Self::SIZE {
|
if offset as usize + bytes.len() > Self::SETTINGS.size {
|
||||||
return Err(Error::Size);
|
return Err(Error::Size);
|
||||||
}
|
}
|
||||||
|
|
||||||
let first_address = Self::BASE as u32 + offset;
|
let first_address = Self::SETTINGS.base as u32 + offset;
|
||||||
let flash_data = unsafe { core::slice::from_raw_parts(first_address as *const u8, bytes.len()) };
|
let flash_data = unsafe { core::slice::from_raw_parts(first_address as *const u8, bytes.len()) };
|
||||||
bytes.copy_from_slice(flash_data);
|
bytes.copy_from_slice(flash_data);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn blocking_write(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
|
fn blocking_write(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
|
||||||
if offset as usize + buf.len() > Self::SIZE {
|
if offset as usize + buf.len() > Self::SETTINGS.size {
|
||||||
return Err(Error::Size);
|
return Err(Error::Size);
|
||||||
}
|
}
|
||||||
if offset as usize % Self::WRITE_SIZE != 0 || buf.len() as usize % Self::WRITE_SIZE != 0 {
|
if offset as usize % Self::SETTINGS.write_size != 0 || buf.len() as usize % Self::SETTINGS.write_size != 0 {
|
||||||
return Err(Error::Unaligned);
|
return Err(Error::Unaligned);
|
||||||
}
|
}
|
||||||
|
|
||||||
let start_address = Self::BASE as u32 + offset;
|
let start_address = Self::SETTINGS.base as u32 + offset;
|
||||||
trace!("Writing {} bytes from 0x{:x}", buf.len(), start_address);
|
trace!("Writing {} bytes from 0x{:x}", buf.len(), start_address);
|
||||||
|
|
||||||
// Protect agains simultaneous write/erase to multiple regions.
|
// Protect agains simultaneous write/erase to multiple regions.
|
||||||
@ -163,15 +167,15 @@ pub trait FlashRegion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
|
||||||
if to < from || to as usize > Self::SIZE {
|
if to < from || to as usize > Self::SETTINGS.size {
|
||||||
return Err(Error::Size);
|
return Err(Error::Size);
|
||||||
}
|
}
|
||||||
if (from as usize % Self::ERASE_SIZE) != 0 || (to as usize % Self::ERASE_SIZE) != 0 {
|
if (from as usize % Self::SETTINGS.erase_size) != 0 || (to as usize % Self::SETTINGS.erase_size) != 0 {
|
||||||
return Err(Error::Unaligned);
|
return Err(Error::Unaligned);
|
||||||
}
|
}
|
||||||
|
|
||||||
let start_address = Self::BASE as u32 + from;
|
let start_address = Self::SETTINGS.base as u32 + from;
|
||||||
let end_address = Self::BASE as u32 + to;
|
let end_address = Self::SETTINGS.base as u32 + to;
|
||||||
trace!("Erasing from 0x{:x} to 0x{:x}", start_address, end_address);
|
trace!("Erasing from 0x{:x} to 0x{:x}", start_address, end_address);
|
||||||
|
|
||||||
// Protect agains simultaneous write/erase to multiple regions.
|
// Protect agains simultaneous write/erase to multiple regions.
|
||||||
@ -224,20 +228,20 @@ foreach_flash_region! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ReadNorFlash for crate::_generated::flash_regions::$name {
|
impl ReadNorFlash for crate::_generated::flash_regions::$name {
|
||||||
const READ_SIZE: usize = <crate::_generated::flash_regions::$name as FlashRegion>::WRITE_SIZE;
|
const READ_SIZE: usize = <crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.write_size;
|
||||||
|
|
||||||
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
||||||
self.blocking_read(offset, bytes)
|
self.blocking_read(offset, bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn capacity(&self) -> usize {
|
fn capacity(&self) -> usize {
|
||||||
<crate::_generated::flash_regions::$name as FlashRegion>::SIZE
|
<crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NorFlash for crate::_generated::flash_regions::$name {
|
impl NorFlash for crate::_generated::flash_regions::$name {
|
||||||
const WRITE_SIZE: usize = <crate::_generated::flash_regions::$name as FlashRegion>::WRITE_SIZE;
|
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>::ERASE_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> {
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
||||||
self.blocking_erase(from, to)
|
self.blocking_erase(from, to)
|
||||||
|
Loading…
Reference in New Issue
Block a user