2022-05-06 09:21:29 +02:00
|
|
|
use core::convert::TryInto;
|
|
|
|
use core::ptr::write_volatile;
|
2023-07-12 18:30:43 +02:00
|
|
|
use core::sync::atomic::{fence, Ordering};
|
2023-03-25 16:04:45 +01:00
|
|
|
|
2023-03-30 08:32:36 +02:00
|
|
|
use super::{FlashRegion, FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE};
|
2022-05-06 09:21:29 +02:00
|
|
|
use crate::flash::Error;
|
|
|
|
use crate::pac;
|
|
|
|
|
2023-05-26 15:23:36 +02:00
|
|
|
pub const fn is_default_layout() -> bool {
|
|
|
|
true
|
|
|
|
}
|
2023-05-23 22:50:41 +02:00
|
|
|
|
2022-05-06 09:21:29 +02:00
|
|
|
const fn is_dual_bank() -> bool {
|
2023-03-30 06:01:56 +02:00
|
|
|
FLASH_REGIONS.len() == 2
|
2022-05-06 09:21:29 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 15:47:45 +02:00
|
|
|
pub fn get_flash_regions() -> &'static [&'static FlashRegion] {
|
2023-03-30 08:32:36 +02:00
|
|
|
&FLASH_REGIONS
|
|
|
|
}
|
|
|
|
|
2022-05-06 09:21:29 +02:00
|
|
|
pub(crate) unsafe fn lock() {
|
|
|
|
pac::FLASH.bank(0).cr().modify(|w| w.set_lock(true));
|
|
|
|
if is_dual_bank() {
|
|
|
|
pac::FLASH.bank(1).cr().modify(|w| w.set_lock(true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) unsafe fn unlock() {
|
|
|
|
pac::FLASH.bank(0).keyr().write(|w| w.set_keyr(0x4567_0123));
|
|
|
|
pac::FLASH.bank(0).keyr().write(|w| w.set_keyr(0xCDEF_89AB));
|
|
|
|
if is_dual_bank() {
|
|
|
|
pac::FLASH.bank(1).keyr().write(|w| w.set_keyr(0x4567_0123));
|
|
|
|
pac::FLASH.bank(1).keyr().write(|w| w.set_keyr(0xCDEF_89AB));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub(crate) unsafe fn enable_blocking_write() {
|
2023-03-25 16:04:45 +01:00
|
|
|
assert_eq!(0, WRITE_SIZE % 4);
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:17:12 +02:00
|
|
|
pub(crate) unsafe fn disable_blocking_write() {}
|
2023-03-25 16:04:45 +01:00
|
|
|
|
2023-05-25 21:46:26 +02:00
|
|
|
pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> {
|
2023-03-25 16:04:45 +01:00
|
|
|
// We cannot have the write setup sequence in begin_write as it depends on the address
|
2023-03-30 06:01:56 +02:00
|
|
|
let bank = if start_address < BANK1_REGION.end() {
|
2022-05-06 09:21:29 +02:00
|
|
|
pac::FLASH.bank(0)
|
|
|
|
} else {
|
|
|
|
pac::FLASH.bank(1)
|
|
|
|
};
|
|
|
|
bank.cr().write(|w| {
|
|
|
|
w.set_pg(true);
|
|
|
|
w.set_psize(2); // 32 bits at once
|
|
|
|
});
|
2022-10-18 22:42:02 +02:00
|
|
|
cortex_m::asm::isb();
|
|
|
|
cortex_m::asm::dsb();
|
2023-03-25 16:04:45 +01:00
|
|
|
fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
let mut res = None;
|
|
|
|
let mut address = start_address;
|
|
|
|
for val in buf.chunks(4) {
|
|
|
|
write_volatile(address as *mut u32, u32::from_le_bytes(val.try_into().unwrap()));
|
|
|
|
address += val.len() as u32;
|
|
|
|
|
2023-05-25 21:46:26 +02:00
|
|
|
res = Some(blocking_wait_ready(bank));
|
2023-03-25 16:04:45 +01:00
|
|
|
bank.sr().modify(|w| {
|
|
|
|
if w.eop() {
|
|
|
|
w.set_eop(true);
|
2022-05-06 09:21:29 +02:00
|
|
|
}
|
2023-03-25 16:04:45 +01:00
|
|
|
});
|
|
|
|
if res.unwrap().is_err() {
|
|
|
|
break;
|
2022-05-06 09:21:29 +02:00
|
|
|
}
|
2023-03-25 16:04:45 +01:00
|
|
|
}
|
2022-05-06 09:21:29 +02:00
|
|
|
|
|
|
|
bank.cr().write(|w| w.set_pg(false));
|
|
|
|
|
2022-10-18 22:42:02 +02:00
|
|
|
cortex_m::asm::isb();
|
|
|
|
cortex_m::asm::dsb();
|
2023-03-25 16:04:45 +01:00
|
|
|
fence(Ordering::SeqCst);
|
2022-10-18 22:42:02 +02:00
|
|
|
|
2023-03-25 16:04:45 +01:00
|
|
|
res.unwrap()
|
2022-05-06 09:21:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-25 21:46:26 +02:00
|
|
|
pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
|
2023-03-30 08:32:36 +02:00
|
|
|
let bank = pac::FLASH.bank(sector.bank as usize);
|
2022-05-06 09:21:29 +02:00
|
|
|
bank.cr().modify(|w| {
|
|
|
|
w.set_ser(true);
|
2023-03-30 08:32:36 +02:00
|
|
|
w.set_snb(sector.index_in_bank)
|
2022-05-06 09:21:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
bank.cr().modify(|w| {
|
|
|
|
w.set_start(true);
|
|
|
|
});
|
|
|
|
|
2023-05-25 21:46:26 +02:00
|
|
|
let ret: Result<(), Error> = blocking_wait_ready(bank);
|
2022-05-06 09:21:29 +02:00
|
|
|
bank.cr().modify(|w| w.set_ser(false));
|
|
|
|
bank_clear_all_err(bank);
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) unsafe fn clear_all_err() {
|
|
|
|
bank_clear_all_err(pac::FLASH.bank(0));
|
|
|
|
bank_clear_all_err(pac::FLASH.bank(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn bank_clear_all_err(bank: pac::flash::Bank) {
|
2023-07-31 12:48:52 +02:00
|
|
|
// read and write back the same value.
|
|
|
|
// This clears all "write 0 to clear" bits.
|
|
|
|
bank.sr().modify(|_| {});
|
2022-05-06 09:21:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-25 21:46:26 +02:00
|
|
|
unsafe fn blocking_wait_ready(bank: pac::flash::Bank) -> Result<(), Error> {
|
2022-05-06 09:21:29 +02:00
|
|
|
loop {
|
|
|
|
let sr = bank.sr().read();
|
|
|
|
|
|
|
|
if !sr.bsy() && !sr.qw() {
|
|
|
|
if sr.wrperr() {
|
|
|
|
return Err(Error::Protected);
|
|
|
|
}
|
|
|
|
if sr.pgserr() {
|
|
|
|
error!("pgserr");
|
|
|
|
return Err(Error::Seq);
|
|
|
|
}
|
|
|
|
if sr.incerr() {
|
|
|
|
// writing to a different address when programming 256 bit word was not finished
|
|
|
|
error!("incerr");
|
|
|
|
return Err(Error::Seq);
|
|
|
|
}
|
|
|
|
if sr.operr() {
|
|
|
|
return Err(Error::Prog);
|
|
|
|
}
|
|
|
|
if sr.sneccerr1() {
|
|
|
|
// single ECC error
|
|
|
|
return Err(Error::Prog);
|
|
|
|
}
|
|
|
|
if sr.dbeccerr() {
|
|
|
|
// double ECC error
|
|
|
|
return Err(Error::Prog);
|
|
|
|
}
|
|
|
|
if sr.rdperr() {
|
|
|
|
return Err(Error::Protected);
|
|
|
|
}
|
|
|
|
if sr.rdserr() {
|
|
|
|
return Err(Error::Protected);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|