From dd88775871d2946df6e7e06bf2b536e94973dbaa Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Sat, 1 Apr 2023 18:10:20 +0200 Subject: [PATCH] Ensure that flash locking is defered to after write --- embassy-stm32/src/flash/common.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs index 287c54f5..41c28c56 100644 --- a/embassy-stm32/src/flash/common.rs +++ b/embassy-stm32/src/flash/common.rs @@ -1,3 +1,4 @@ +use atomic_polyfill::{fence, Ordering}; use embassy_hal_common::drop::OnDrop; use embassy_hal_common::{into_ref, PeripheralRef}; @@ -74,12 +75,18 @@ unsafe fn blocking_write(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Res for chunk in bytes.chunks(WRITE_SIZE) { critical_section::with(|_| { family::clear_all_err(); + fence(Ordering::SeqCst); family::unlock(); + fence(Ordering::SeqCst); family::begin_write(); - let _ = OnDrop::new(|| { + fence(Ordering::SeqCst); + + let _on_drop = OnDrop::new(|| { family::end_write(); + fence(Ordering::SeqCst); family::lock(); }); + family::blocking_write(address, chunk.try_into().unwrap()) })?; address += WRITE_SIZE as u32; @@ -114,10 +121,14 @@ unsafe fn blocking_erase(base: u32, from: u32, to: u32) -> Result<(), Error> { critical_section::with(|_| { family::clear_all_err(); + fence(Ordering::SeqCst); family::unlock(); - let _ = OnDrop::new(|| { + fence(Ordering::SeqCst); + + let _on_drop = OnDrop::new(|| { family::lock(); }); + family::blocking_erase_sector(§or) })?; address += sector.size;