Align families

This commit is contained in:
Rasmus Melchior Jacobsen 2023-03-30 06:01:56 +02:00
parent 91d8afd371
commit e3c4e00be0
8 changed files with 55 additions and 58 deletions

View File

@ -122,6 +122,12 @@ impl Drop for Flash<'_> {
} }
} }
impl Drop for FlashLayout<'_> {
fn drop(&mut self) {
unsafe { family::lock() };
}
}
static REGION_LOCK: Mutex<CriticalSectionRawMutex, ()> = Mutex::new(()); static REGION_LOCK: Mutex<CriticalSectionRawMutex, ()> = Mutex::new(());
fn take_lock_spin() -> MutexGuard<'static, CriticalSectionRawMutex, ()> { fn take_lock_spin() -> MutexGuard<'static, CriticalSectionRawMutex, ()> {

View File

@ -3,12 +3,10 @@ use core::ptr::write_volatile;
use atomic_polyfill::{fence, Ordering}; use atomic_polyfill::{fence, Ordering};
use super::{FlashRegion, FlashSector, BANK1, WRITE_SIZE}; use super::{FlashSector, BANK1_REGION, WRITE_SIZE};
use crate::flash::Error; use crate::flash::Error;
use crate::pac; use crate::pac;
const ERASE_SIZE: usize = BANK1::SETTINGS.erase_size;
pub(crate) unsafe fn lock() { pub(crate) unsafe fn lock() {
pac::FLASH.cr().modify(|w| w.set_lock(true)); pac::FLASH.cr().modify(|w| w.set_lock(true));
} }
@ -46,7 +44,7 @@ pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), E
w.set_per(true); w.set_per(true);
}); });
pac::FLASH.ar().write(|w| w.set_far(sector.first)); pac::FLASH.ar().write(|w| w.set_far(sector.start));
pac::FLASH.cr().modify(|w| { pac::FLASH.cr().modify(|w| {
w.set_strt(true); w.set_strt(true);
@ -103,11 +101,11 @@ unsafe fn blocking_wait_ready() -> Result<(), Error> {
} }
pub(crate) fn get_sector(address: u32) -> FlashSector { pub(crate) fn get_sector(address: u32) -> FlashSector {
let sector_size = BANK1::SETTINGS.erase_size as u32; let sector_size = BANK1_REGION.erase_size;
let index = address / sector_size; let index = address / sector_size;
FlashSector { FlashSector {
index: index as u8, index: index as u8,
start: BANK1::SETTINGS.base as u32 + index * sector_size, start: BANK1_REGION.base + index * sector_size,
size: sector_size, size: sector_size,
} }
} }

View File

@ -84,9 +84,6 @@ mod alt_regions {
pub use alt_regions::AltFlashLayout; pub use alt_regions::AltFlashLayout;
fn is_dual_bank() -> bool { fn is_dual_bank() -> bool {
// let asd: super::Bank1Region1;
// let sad = &super::BANK_1_REGION_1;
match FLASH_SIZE / 1024 { match FLASH_SIZE / 1024 {
// 1 MB devices depend on configuration // 1 MB devices depend on configuration
1024 => { 1024 => {

View File

@ -3,15 +3,12 @@ use core::ptr::write_volatile;
use atomic_polyfill::{fence, Ordering}; use atomic_polyfill::{fence, Ordering};
use super::{FlashRegion, FlashSector, BANK1, FLASH_SIZE, WRITE_SIZE}; use super::{FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE};
use crate::flash::Error; use crate::flash::Error;
use crate::pac; use crate::pac;
const ERASE_SIZE: usize = BANK1::SETTINGS.erase_size;
const SECOND_BANK_OFFSET: usize = 0x0010_0000;
const fn is_dual_bank() -> bool { const fn is_dual_bank() -> bool {
FLASH_SIZE / 2 > ERASE_SIZE FLASH_REGIONS.len() == 2
} }
pub(crate) unsafe fn lock() { pub(crate) unsafe fn lock() {
@ -24,7 +21,6 @@ pub(crate) unsafe fn lock() {
pub(crate) unsafe fn unlock() { pub(crate) unsafe fn unlock() {
pac::FLASH.bank(0).keyr().write(|w| w.set_keyr(0x4567_0123)); pac::FLASH.bank(0).keyr().write(|w| w.set_keyr(0x4567_0123));
pac::FLASH.bank(0).keyr().write(|w| w.set_keyr(0xCDEF_89AB)); pac::FLASH.bank(0).keyr().write(|w| w.set_keyr(0xCDEF_89AB));
if is_dual_bank() { if is_dual_bank() {
pac::FLASH.bank(1).keyr().write(|w| w.set_keyr(0x4567_0123)); pac::FLASH.bank(1).keyr().write(|w| w.set_keyr(0x4567_0123));
pac::FLASH.bank(1).keyr().write(|w| w.set_keyr(0xCDEF_89AB)); pac::FLASH.bank(1).keyr().write(|w| w.set_keyr(0xCDEF_89AB));
@ -39,7 +35,7 @@ pub(crate) unsafe fn end_write() {}
pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> { pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> {
// We cannot have the write setup sequence in begin_write as it depends on the address // We cannot have the write setup sequence in begin_write as it depends on the address
let bank = if !is_dual_bank() || (start_address - super::FLASH_BASE as u32) < SECOND_BANK_OFFSET as u32 { let bank = if start_address < BANK1_REGION.end() {
pac::FLASH.bank(0) pac::FLASH.bank(0)
} else { } else {
pac::FLASH.bank(1) pac::FLASH.bank(1)
@ -181,11 +177,11 @@ unsafe fn blocking_wait_ready(bank: pac::flash::Bank) -> Result<(), Error> {
} }
pub(crate) fn get_sector(address: u32) -> FlashSector { pub(crate) fn get_sector(address: u32) -> FlashSector {
let sector_size = BANK1::SETTINGS.erase_size as u32; let sector_size = BANK1_REGION.erase_size;
let index = address / sector_size; let index = address / sector_size;
FlashSector { FlashSector {
index: index as u8, index: index as u8,
start: BANK1::SETTINGS.base as u32 + index * sector_size, start: BANK1_REGION.base + index * sector_size,
size: sector_size, size: sector_size,
} }
} }

View File

@ -2,7 +2,7 @@ use core::ptr::write_volatile;
use atomic_polyfill::{fence, Ordering}; use atomic_polyfill::{fence, Ordering};
use super::{FlashRegion, FlashSector, BANK1, WRITE_SIZE}; use super::{FlashSector, BANK1_REGION, WRITE_SIZE};
use crate::flash::Error; use crate::flash::Error;
use crate::pac; use crate::pac;
@ -73,7 +73,7 @@ pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), E
#[cfg(any(flash_wl, flash_wb, flash_l4))] #[cfg(any(flash_wl, flash_wb, flash_l4))]
{ {
let idx = (sector.start - super::FLASH_BASE as u32) / BANK1::SETTINGS.erase_size as u32; let idx = (sector.start - super::FLASH_BASE as u32) / BANK1_REGION.erase_size as u32;
#[cfg(flash_l4)] #[cfg(flash_l4)]
let (idx, bank) = if idx > 255 { (idx - 256, true) } else { (idx, false) }; let (idx, bank) = if idx > 255 { (idx - 256, true) } else { (idx, false) };
@ -182,11 +182,11 @@ unsafe fn blocking_wait_ready() -> Result<(), Error> {
} }
pub(crate) fn get_sector(address: u32) -> FlashSector { pub(crate) fn get_sector(address: u32) -> FlashSector {
let sector_size = BANK1::SETTINGS.erase_size as u32; let sector_size = BANK1_REGION.erase_size;
let index = address / sector_size; let index = address / sector_size;
FlashSector { FlashSector {
index: index as u8, index: index as u8,
start: BANK1::SETTINGS.base as u32 + index * sector_size, start: BANK1_REGION.base + index * sector_size,
size: sector_size, size: sector_size,
} }
} }

View File

@ -24,9 +24,9 @@ pub struct FlashSector {
pub size: u32, pub size: u32,
} }
impl Drop for FlashLayout<'_> { impl FlashRegion {
fn drop(&mut self) { pub const fn end(&self) -> u32 {
unsafe { family::lock() }; self.base + self.size
} }
} }
@ -35,39 +35,13 @@ impl Drop for FlashLayout<'_> {
#[cfg_attr(flash_f4, path = "f4.rs")] #[cfg_attr(flash_f4, path = "f4.rs")]
#[cfg_attr(flash_f7, path = "f7.rs")] #[cfg_attr(flash_f7, path = "f7.rs")]
#[cfg_attr(flash_h7, path = "h7.rs")] #[cfg_attr(flash_h7, path = "h7.rs")]
mod family; #[cfg_attr(
not(any(
#[cfg(not(any(
flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f3, flash_f4, flash_f7, flash_h7 flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f3, flash_f4, flash_f7, flash_h7
)))] )),
mod family { path = "other.rs"
use super::{Error, FlashSector, WRITE_SIZE}; )]
mod family;
pub(crate) unsafe fn lock() {
unimplemented!();
}
pub(crate) unsafe fn unlock() {
unimplemented!();
}
pub(crate) unsafe fn begin_write() {
unimplemented!();
}
pub(crate) unsafe fn end_write() {
unimplemented!();
}
pub(crate) unsafe fn blocking_write(_start_address: u32, _buf: &[u8; WRITE_SIZE]) -> Result<(), Error> {
unimplemented!();
}
pub(crate) unsafe fn blocking_erase_sector(_sector: &FlashSector) -> Result<(), Error> {
unimplemented!();
}
pub(crate) unsafe fn clear_all_err() {
unimplemented!();
}
pub(crate) fn get_sector(_address: u32) -> FlashSector {
unimplemented!();
}
}
#[allow(unused_imports)] #[allow(unused_imports)]
pub use family::*; pub use family::*;

View File

@ -0,0 +1,27 @@
#[allow(unused)]
use super::{Error, FlashSector, WRITE_SIZE};
pub(crate) unsafe fn lock() {
unimplemented!();
}
pub(crate) unsafe fn unlock() {
unimplemented!();
}
pub(crate) unsafe fn begin_write() {
unimplemented!();
}
pub(crate) unsafe fn end_write() {
unimplemented!();
}
pub(crate) unsafe fn blocking_write(_start_address: u32, _buf: &[u8; WRITE_SIZE]) -> Result<(), Error> {
unimplemented!();
}
pub(crate) unsafe fn blocking_erase_sector(_sector: &FlashSector) -> Result<(), Error> {
unimplemented!();
}
pub(crate) unsafe fn clear_all_err() {
unimplemented!();
}
pub(crate) fn get_sector(_address: u32) -> FlashSector {
unimplemented!();
}

View File

@ -43,7 +43,6 @@ pub mod i2c;
#[cfg(crc)] #[cfg(crc)]
pub mod crc; pub mod crc;
#[cfg(flash)]
pub mod flash; pub mod flash;
pub mod pwm; pub mod pwm;
#[cfg(quadspi)] #[cfg(quadspi)]