Merge pull request #2032 from andresv/stm32-check-flash-lock-before-unlocking

stm32 flash: check lock bit before unlocking
This commit is contained in:
Dario Nieuwenhuis 2023-10-10 10:12:55 +00:00 committed by GitHub
commit eff73d6dfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 20 deletions

View File

@ -19,9 +19,11 @@ pub(crate) unsafe fn lock() {
} }
pub(crate) unsafe fn unlock() { pub(crate) unsafe fn unlock() {
if pac::FLASH.cr().read().lock() {
pac::FLASH.keyr().write(|w| w.set_fkeyr(0x4567_0123)); pac::FLASH.keyr().write(|w| w.set_fkeyr(0x4567_0123));
pac::FLASH.keyr().write(|w| w.set_fkeyr(0xCDEF_89AB)); pac::FLASH.keyr().write(|w| w.set_fkeyr(0xCDEF_89AB));
} }
}
pub(crate) unsafe fn enable_blocking_write() { pub(crate) unsafe fn enable_blocking_write() {
assert_eq!(0, WRITE_SIZE % 2); assert_eq!(0, WRITE_SIZE % 2);

View File

@ -19,9 +19,11 @@ pub(crate) unsafe fn lock() {
} }
pub(crate) unsafe fn unlock() { pub(crate) unsafe fn unlock() {
if pac::FLASH.cr().read().lock() {
pac::FLASH.keyr().write(|w| w.set_fkeyr(0x4567_0123)); pac::FLASH.keyr().write(|w| w.set_fkeyr(0x4567_0123));
pac::FLASH.keyr().write(|w| w.set_fkeyr(0xCDEF_89AB)); pac::FLASH.keyr().write(|w| w.set_fkeyr(0xCDEF_89AB));
} }
}
pub(crate) unsafe fn enable_blocking_write() { pub(crate) unsafe fn enable_blocking_write() {
assert_eq!(0, WRITE_SIZE % 2); assert_eq!(0, WRITE_SIZE % 2);

View File

@ -228,9 +228,11 @@ pub(crate) unsafe fn lock() {
} }
pub(crate) unsafe fn unlock() { pub(crate) unsafe fn unlock() {
if pac::FLASH.cr().read().lock() {
pac::FLASH.keyr().write(|w| w.set_key(0x45670123)); pac::FLASH.keyr().write(|w| w.set_key(0x45670123));
pac::FLASH.keyr().write(|w| w.set_key(0xCDEF89AB)); pac::FLASH.keyr().write(|w| w.set_key(0xCDEF89AB));
} }
}
pub(crate) unsafe fn enable_write() { pub(crate) unsafe fn enable_write() {
assert_eq!(0, WRITE_SIZE % 4); assert_eq!(0, WRITE_SIZE % 4);

View File

@ -19,9 +19,11 @@ pub(crate) unsafe fn lock() {
} }
pub(crate) unsafe fn unlock() { pub(crate) unsafe fn unlock() {
if pac::FLASH.cr().read().lock() {
pac::FLASH.keyr().write(|w| w.set_key(0x4567_0123)); 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(0xCDEF_89AB));
} }
}
pub(crate) unsafe fn enable_blocking_write() { pub(crate) unsafe fn enable_blocking_write() {
assert_eq!(0, WRITE_SIZE % 4); assert_eq!(0, WRITE_SIZE % 4);

View File

@ -24,9 +24,11 @@ pub(crate) unsafe fn unlock() {
while pac::FLASH.sr().read().bsy() {} while pac::FLASH.sr().read().bsy() {}
// Unlock flash // Unlock flash
if pac::FLASH.cr().read().lock() {
pac::FLASH.keyr().write(|w| w.set_keyr(0x4567_0123)); pac::FLASH.keyr().write(|w| w.set_keyr(0x4567_0123));
pac::FLASH.keyr().write(|w| w.set_keyr(0xCDEF_89AB)); pac::FLASH.keyr().write(|w| w.set_keyr(0xCDEF_89AB));
} }
}
pub(crate) unsafe fn enable_blocking_write() { pub(crate) unsafe fn enable_blocking_write() {
assert_eq!(0, WRITE_SIZE % 4); assert_eq!(0, WRITE_SIZE % 4);

View File

@ -26,13 +26,17 @@ pub(crate) unsafe fn lock() {
} }
pub(crate) unsafe fn unlock() { pub(crate) unsafe fn unlock() {
if pac::FLASH.bank(0).cr().read().lock() {
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() {
if pac::FLASH.bank(1).cr().read().lock() {
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));
} }
} }
}
pub(crate) unsafe fn enable_blocking_write() { pub(crate) unsafe fn enable_blocking_write() {
assert_eq!(0, WRITE_SIZE % 4); assert_eq!(0, WRITE_SIZE % 4);

View File

@ -28,19 +28,25 @@ pub(crate) unsafe fn lock() {
pub(crate) unsafe fn unlock() { pub(crate) unsafe fn unlock() {
#[cfg(any(flash_wl, flash_wb, flash_l4))] #[cfg(any(flash_wl, flash_wb, flash_l4))]
{ {
if pac::FLASH.cr().read().lock() {
pac::FLASH.keyr().write(|w| w.set_keyr(0x4567_0123)); pac::FLASH.keyr().write(|w| w.set_keyr(0x4567_0123));
pac::FLASH.keyr().write(|w| w.set_keyr(0xCDEF_89AB)); pac::FLASH.keyr().write(|w| w.set_keyr(0xCDEF_89AB));
} }
}
#[cfg(any(flash_l0, flash_l1))] #[cfg(any(flash_l0, flash_l1))]
{ {
if pac::FLASH.pecr().read().pelock() {
pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x89ABCDEF)); pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x89ABCDEF));
pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x02030405)); pac::FLASH.pekeyr().write(|w| w.set_pekeyr(0x02030405));
}
if pac::FLASH.pecr().read().prglock() {
pac::FLASH.prgkeyr().write(|w| w.set_prgkeyr(0x8C9DAEBF)); pac::FLASH.prgkeyr().write(|w| w.set_prgkeyr(0x8C9DAEBF));
pac::FLASH.prgkeyr().write(|w| w.set_prgkeyr(0x13141516)); pac::FLASH.prgkeyr().write(|w| w.set_prgkeyr(0x13141516));
} }
} }
}
pub(crate) unsafe fn enable_blocking_write() { pub(crate) unsafe fn enable_blocking_write() {
assert_eq!(0, WRITE_SIZE % 4); assert_eq!(0, WRITE_SIZE % 4);