From cd8198037fa37db9af7e9dde1e00122df9ed15a3 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Thu, 25 May 2023 13:08:40 +0200 Subject: [PATCH] Actually transition to dual bank mode - key was required --- embassy-stm32/src/flash/f4.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs index 3d696223c..50ab446bd 100644 --- a/embassy-stm32/src/flash/f4.rs +++ b/embassy-stm32/src/flash/f4.rs @@ -71,7 +71,7 @@ mod alt_regions { impl<'d> Flash<'d> { pub fn into_alt_regions(self) -> AltFlashLayout<'d, Async> { - unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(true)) }; + super::set_alt_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. @@ -88,7 +88,7 @@ mod alt_regions { } pub fn into_alt_blocking_regions(self) -> AltFlashLayout<'d, Blocking> { - unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(true)) }; + super::set_alt_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. @@ -171,12 +171,31 @@ static WAKER: AtomicWaker = AtomicWaker::new(); #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] pub fn set_default_layout() { - unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(false)) }; + 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) + }); + }; } #[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) + }); + }; +} + #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] pub fn get_flash_regions() -> &'static [&'static FlashRegion] { if unsafe { pac::FLASH.optcr().read().db1m() } { @@ -207,8 +226,8 @@ pub(crate) unsafe fn lock() { } pub(crate) unsafe fn unlock() { - pac::FLASH.keyr().write(|w| w.set_key(0x4567_0123)); - pac::FLASH.keyr().write(|w| w.set_key(0xCDEF_89AB)); + pac::FLASH.keyr().write(|w| w.set_key(0x45670123)); + pac::FLASH.keyr().write(|w| w.set_key(0xCDEF89AB)); } #[cfg(feature = "nightly")]