Ensure that flash locking is defered to after write

This commit is contained in:
Rasmus Melchior Jacobsen 2023-04-01 18:10:20 +02:00
parent e11eebfa57
commit dd88775871

View File

@ -1,3 +1,4 @@
use atomic_polyfill::{fence, Ordering};
use embassy_hal_common::drop::OnDrop; use embassy_hal_common::drop::OnDrop;
use embassy_hal_common::{into_ref, PeripheralRef}; 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) { for chunk in bytes.chunks(WRITE_SIZE) {
critical_section::with(|_| { critical_section::with(|_| {
family::clear_all_err(); family::clear_all_err();
fence(Ordering::SeqCst);
family::unlock(); family::unlock();
fence(Ordering::SeqCst);
family::begin_write(); family::begin_write();
let _ = OnDrop::new(|| { fence(Ordering::SeqCst);
let _on_drop = OnDrop::new(|| {
family::end_write(); family::end_write();
fence(Ordering::SeqCst);
family::lock(); family::lock();
}); });
family::blocking_write(address, chunk.try_into().unwrap()) family::blocking_write(address, chunk.try_into().unwrap())
})?; })?;
address += WRITE_SIZE as u32; address += WRITE_SIZE as u32;
@ -114,10 +121,14 @@ unsafe fn blocking_erase(base: u32, from: u32, to: u32) -> Result<(), Error> {
critical_section::with(|_| { critical_section::with(|_| {
family::clear_all_err(); family::clear_all_err();
fence(Ordering::SeqCst);
family::unlock(); family::unlock();
let _ = OnDrop::new(|| { fence(Ordering::SeqCst);
let _on_drop = OnDrop::new(|| {
family::lock(); family::lock();
}); });
family::blocking_erase_sector(&sector) family::blocking_erase_sector(&sector)
})?; })?;
address += sector.size; address += sector.size;