diff --git a/embassy-stm32/src/flash/asynch.rs b/embassy-stm32/src/flash/asynch.rs index a0d994956..3c3ece99a 100644 --- a/embassy-stm32/src/flash/asynch.rs +++ b/embassy-stm32/src/flash/asynch.rs @@ -34,7 +34,7 @@ impl<'d> Flash<'d, Async> { } pub fn into_regions(self) -> FlashLayout<'d, Async> { - family::set_default_layout(); + assert!(family::is_default_layout()); FlashLayout::new(self.inner) } diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs index cc0e2572b..6c1912900 100644 --- a/embassy-stm32/src/flash/common.rs +++ b/embassy-stm32/src/flash/common.rs @@ -30,7 +30,7 @@ impl<'d> Flash<'d, Blocking> { impl<'d, MODE> Flash<'d, MODE> { pub fn into_blocking_regions(self) -> FlashLayout<'d, Blocking> { - family::set_default_layout(); + assert!(family::is_default_layout()); FlashLayout::new(self.inner) } diff --git a/embassy-stm32/src/flash/f0.rs b/embassy-stm32/src/flash/f0.rs index e9916d14b..02bd4cc1f 100644 --- a/embassy-stm32/src/flash/f0.rs +++ b/embassy-stm32/src/flash/f0.rs @@ -7,7 +7,9 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; -pub const fn set_default_layout() {} +pub const fn is_default_layout() -> bool { + true +} pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS diff --git a/embassy-stm32/src/flash/f3.rs b/embassy-stm32/src/flash/f3.rs index 4e65f5580..b093a7837 100644 --- a/embassy-stm32/src/flash/f3.rs +++ b/embassy-stm32/src/flash/f3.rs @@ -7,7 +7,9 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; -pub const fn set_default_layout() {} +pub const fn is_default_layout() -> bool { + true +} pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs index 25d96ca1e..5e1fc696f 100644 --- a/embassy-stm32/src/flash/f4.rs +++ b/embassy-stm32/src/flash/f4.rs @@ -69,7 +69,7 @@ mod alt_regions { impl<'d> Flash<'d> { pub fn into_alt_regions(self) -> AltFlashLayout<'d, Async> { - super::set_alt_layout(); + assert!(!super::is_default_layout()); // SAFETY: We never expose the cloned peripheral references, and their instance is not public. // Also, all async flash region operations are protected with a mutex. @@ -86,7 +86,7 @@ mod alt_regions { } pub fn into_alt_blocking_regions(self) -> AltFlashLayout<'d, Blocking> { - super::set_alt_layout(); + assert!(!super::is_default_layout()); // SAFETY: We never expose the cloned peripheral references, and their instance is not public. // Also, all blocking flash region operations are protected with a cs. @@ -191,38 +191,21 @@ impl FlashSector { } #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] -pub fn set_default_layout() { - unsafe { - pac::FLASH.optkeyr().write(|w| w.set_optkey(0x08192A3B)); - pac::FLASH.optkeyr().write(|w| w.set_optkey(0x4C5D6E7F)); - pac::FLASH.optcr().modify(|r| { - r.set_db1m(false); - r.set_optlock(true) - }); - }; +pub(crate) fn is_default_layout() -> bool { + unsafe { !pac::FLASH.optcr().read().db1m() } } #[cfg(not(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479)))] -pub const fn set_default_layout() {} - -#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] -fn set_alt_layout() { - unsafe { - pac::FLASH.optkeyr().write(|w| w.set_optkey(0x08192A3B)); - pac::FLASH.optkeyr().write(|w| w.set_optkey(0x4C5D6E7F)); - pac::FLASH.optcr().modify(|r| { - r.set_db1m(true); - r.set_optlock(true) - }); - }; +pub(crate) const fn is_default_layout() -> bool { + true } #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] pub fn get_flash_regions() -> &'static [&'static FlashRegion] { - if unsafe { pac::FLASH.optcr().read().db1m() } { - &ALT_FLASH_REGIONS - } else { + if is_default_layout() { &FLASH_REGIONS + } else { + &ALT_FLASH_REGIONS } } diff --git a/embassy-stm32/src/flash/f7.rs b/embassy-stm32/src/flash/f7.rs index e6267e178..1a6d6bb03 100644 --- a/embassy-stm32/src/flash/f7.rs +++ b/embassy-stm32/src/flash/f7.rs @@ -6,7 +6,9 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; -pub const fn set_default_layout() {} +pub const fn is_default_layout() -> bool { + true +} pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS diff --git a/embassy-stm32/src/flash/h7.rs b/embassy-stm32/src/flash/h7.rs index d09ebc0dd..9baf059ee 100644 --- a/embassy-stm32/src/flash/h7.rs +++ b/embassy-stm32/src/flash/h7.rs @@ -7,7 +7,9 @@ use super::{FlashRegion, FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; -pub const fn set_default_layout() {} +pub const fn is_default_layout() -> bool { + true +} const fn is_dual_bank() -> bool { FLASH_REGIONS.len() == 2 diff --git a/embassy-stm32/src/flash/l.rs b/embassy-stm32/src/flash/l.rs index c4bbd5477..deefd05ed 100644 --- a/embassy-stm32/src/flash/l.rs +++ b/embassy-stm32/src/flash/l.rs @@ -6,7 +6,9 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; -pub const fn set_default_layout() {} +pub const fn is_default_layout() -> bool { + true +} pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS diff --git a/embassy-stm32/src/flash/other.rs b/embassy-stm32/src/flash/other.rs index ccdcfeb7a..a7e8d1d57 100644 --- a/embassy-stm32/src/flash/other.rs +++ b/embassy-stm32/src/flash/other.rs @@ -2,7 +2,9 @@ use super::{Error, FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; -pub const fn set_default_layout() {} +pub const fn is_default_layout() -> bool { + true +} pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS