diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs index c239d9673..6534e1b8e 100644 --- a/embassy-stm32/src/flash/common.rs +++ b/embassy-stm32/src/flash/common.rs @@ -122,6 +122,12 @@ impl Drop for Flash<'_> { } } +impl Drop for FlashLayout<'_> { + fn drop(&mut self) { + unsafe { family::lock() }; + } +} + static REGION_LOCK: Mutex = Mutex::new(()); fn take_lock_spin() -> MutexGuard<'static, CriticalSectionRawMutex, ()> { diff --git a/embassy-stm32/src/flash/f3.rs b/embassy-stm32/src/flash/f3.rs index 7b339ccc5..5ac1d8fdf 100644 --- a/embassy-stm32/src/flash/f3.rs +++ b/embassy-stm32/src/flash/f3.rs @@ -3,12 +3,10 @@ use core::ptr::write_volatile; 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::pac; -const ERASE_SIZE: usize = BANK1::SETTINGS.erase_size; - pub(crate) unsafe fn lock() { 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); }); - 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| { w.set_strt(true); @@ -103,11 +101,11 @@ unsafe fn blocking_wait_ready() -> Result<(), Error> { } 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; FlashSector { index: index as u8, - start: BANK1::SETTINGS.base as u32 + index * sector_size, + start: BANK1_REGION.base + index * sector_size, size: sector_size, } } diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs index 257aae2d0..52ef7ad50 100644 --- a/embassy-stm32/src/flash/f4.rs +++ b/embassy-stm32/src/flash/f4.rs @@ -84,9 +84,6 @@ mod alt_regions { pub use alt_regions::AltFlashLayout; fn is_dual_bank() -> bool { - // let asd: super::Bank1Region1; - // let sad = &super::BANK_1_REGION_1; - match FLASH_SIZE / 1024 { // 1 MB devices depend on configuration 1024 => { diff --git a/embassy-stm32/src/flash/h7.rs b/embassy-stm32/src/flash/h7.rs index 82dd4b28b..360ad77db 100644 --- a/embassy-stm32/src/flash/h7.rs +++ b/embassy-stm32/src/flash/h7.rs @@ -3,15 +3,12 @@ use core::ptr::write_volatile; 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::pac; -const ERASE_SIZE: usize = BANK1::SETTINGS.erase_size; -const SECOND_BANK_OFFSET: usize = 0x0010_0000; - const fn is_dual_bank() -> bool { - FLASH_SIZE / 2 > ERASE_SIZE + FLASH_REGIONS.len() == 2 } pub(crate) unsafe fn lock() { @@ -24,7 +21,6 @@ pub(crate) unsafe fn lock() { 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(0xCDEF_89AB)); - 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(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> { // 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) } else { 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 { - let sector_size = BANK1::SETTINGS.erase_size as u32; + let sector_size = BANK1_REGION.erase_size; let index = address / sector_size; FlashSector { index: index as u8, - start: BANK1::SETTINGS.base as u32 + index * sector_size, + start: BANK1_REGION.base + index * sector_size, size: sector_size, } } diff --git a/embassy-stm32/src/flash/l.rs b/embassy-stm32/src/flash/l.rs index edcf2b2f0..f4b11011f 100644 --- a/embassy-stm32/src/flash/l.rs +++ b/embassy-stm32/src/flash/l.rs @@ -2,7 +2,7 @@ use core::ptr::write_volatile; 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::pac; @@ -73,7 +73,7 @@ pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), E #[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)] 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 { - let sector_size = BANK1::SETTINGS.erase_size as u32; + let sector_size = BANK1_REGION.erase_size; let index = address / sector_size; FlashSector { index: index as u8, - start: BANK1::SETTINGS.base as u32 + index * sector_size, + start: BANK1_REGION.base + index * sector_size, size: sector_size, } } diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs index 7dc714715..110b81496 100644 --- a/embassy-stm32/src/flash/mod.rs +++ b/embassy-stm32/src/flash/mod.rs @@ -24,9 +24,9 @@ pub struct FlashSector { pub size: u32, } -impl Drop for FlashLayout<'_> { - fn drop(&mut self) { - unsafe { family::lock() }; +impl FlashRegion { + pub const fn end(&self) -> u32 { + self.base + self.size } } @@ -35,40 +35,14 @@ impl Drop for FlashLayout<'_> { #[cfg_attr(flash_f4, path = "f4.rs")] #[cfg_attr(flash_f7, path = "f7.rs")] #[cfg_attr(flash_h7, path = "h7.rs")] +#[cfg_attr( + not(any( + flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f3, flash_f4, flash_f7, flash_h7 + )), + path = "other.rs" +)] mod family; -#[cfg(not(any( - flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f3, flash_f4, flash_f7, flash_h7 -)))] -mod family { - 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!(); - } -} - #[allow(unused_imports)] pub use family::*; diff --git a/embassy-stm32/src/flash/other.rs b/embassy-stm32/src/flash/other.rs new file mode 100644 index 000000000..fd3551224 --- /dev/null +++ b/embassy-stm32/src/flash/other.rs @@ -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!(); +} diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 75ff2d754..3f2d078f8 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -43,7 +43,6 @@ pub mod i2c; #[cfg(crc)] pub mod crc; -#[cfg(flash)] pub mod flash; pub mod pwm; #[cfg(quadspi)]