diff options
| author | Andreas Lindahl Flåten (ALF) <[email protected]> | 2025-11-05 16:47:09 +0100 |
|---|---|---|
| committer | Andreas Lindahl Flåten (ALF) <[email protected]> | 2025-11-13 11:26:06 +0100 |
| commit | dccf185e1489c0055fcacdea59ce7837cc4d076d (patch) | |
| tree | 4117813ecca3209236023032a0a2c2735fbedd76 /embassy-stm32/src/flash/c.rs | |
| parent | a6a38cdd22cb2f47f3cc869f447d7cdb80e27ec6 (diff) | |
Add c.rs flash for the stm32c0 family
This is basically a copy of the `g.rs` file, with multi bank support
removed (c0 is single bank only).
Diffstat (limited to 'embassy-stm32/src/flash/c.rs')
| -rw-r--r-- | embassy-stm32/src/flash/c.rs | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/embassy-stm32/src/flash/c.rs b/embassy-stm32/src/flash/c.rs new file mode 100644 index 000000000..af3d07ac6 --- /dev/null +++ b/embassy-stm32/src/flash/c.rs | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | use core::ptr::write_volatile; | ||
| 2 | use core::sync::atomic::{Ordering, fence}; | ||
| 3 | |||
| 4 | use cortex_m::interrupt; | ||
| 5 | |||
| 6 | use super::{FlashSector, WRITE_SIZE}; | ||
| 7 | use crate::flash::Error; | ||
| 8 | use crate::pac; | ||
| 9 | |||
| 10 | pub(crate) unsafe fn lock() { | ||
| 11 | pac::FLASH.cr().modify(|w| w.set_lock(true)); | ||
| 12 | } | ||
| 13 | pub(crate) unsafe fn unlock() { | ||
| 14 | // Wait, while the memory interface is busy. | ||
| 15 | wait_busy(); | ||
| 16 | |||
| 17 | // Unlock flash | ||
| 18 | if pac::FLASH.cr().read().lock() { | ||
| 19 | pac::FLASH.keyr().write_value(0x4567_0123); | ||
| 20 | pac::FLASH.keyr().write_value(0xCDEF_89AB); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | pub(crate) unsafe fn enable_blocking_write() { | ||
| 25 | assert_eq!(0, WRITE_SIZE % 4); | ||
| 26 | pac::FLASH.cr().write(|w| w.set_pg(true)); | ||
| 27 | } | ||
| 28 | |||
| 29 | pub(crate) unsafe fn disable_blocking_write() { | ||
| 30 | pac::FLASH.cr().write(|w| w.set_pg(false)); | ||
| 31 | } | ||
| 32 | |||
| 33 | pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> { | ||
| 34 | let mut address = start_address; | ||
| 35 | for val in buf.chunks(4) { | ||
| 36 | write_volatile(address as *mut u32, u32::from_le_bytes(unwrap!(val.try_into()))); | ||
| 37 | address += val.len() as u32; | ||
| 38 | |||
| 39 | // prevents parallelism errors | ||
| 40 | fence(Ordering::SeqCst); | ||
| 41 | } | ||
| 42 | |||
| 43 | wait_ready_blocking() | ||
| 44 | } | ||
| 45 | |||
| 46 | pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> { | ||
| 47 | let idx = (sector.start - super::FLASH_BASE as u32) / super::BANK1_REGION.erase_size as u32; | ||
| 48 | |||
| 49 | #[cfg(feature = "defmt")] | ||
| 50 | defmt::trace!("STM32C0 Erase: addr=0x{:08x}, idx={}, erase_size={}", sector.start, idx, super::BANK1_REGION.erase_size); | ||
| 51 | |||
| 52 | |||
| 53 | wait_busy(); | ||
| 54 | clear_all_err(); | ||
| 55 | |||
| 56 | // Explicitly unlock before erase | ||
| 57 | unlock(); | ||
| 58 | |||
| 59 | interrupt::free(|_| { | ||
| 60 | #[cfg(feature = "defmt")] | ||
| 61 | { | ||
| 62 | let cr_before = pac::FLASH.cr().read(); | ||
| 63 | defmt::trace!("FLASH_CR before: 0x{:08x}", cr_before.0); | ||
| 64 | } | ||
| 65 | |||
| 66 | pac::FLASH.cr().modify(|w| { | ||
| 67 | w.set_per(true); | ||
| 68 | w.set_pnb(idx as u8); | ||
| 69 | w.set_strt(true); | ||
| 70 | }); | ||
| 71 | |||
| 72 | #[cfg(feature = "defmt")] | ||
| 73 | { | ||
| 74 | let cr_after = pac::FLASH.cr().read(); | ||
| 75 | defmt::trace!("FLASH_CR after: 0x{:08x}, PER={}, PNB={}, STRT={}", | ||
| 76 | cr_after.0, cr_after.per(), cr_after.pnb(), cr_after.strt()); | ||
| 77 | } | ||
| 78 | }); | ||
| 79 | |||
| 80 | let ret: Result<(), Error> = wait_ready_blocking(); | ||
| 81 | |||
| 82 | // Clear erase bit | ||
| 83 | pac::FLASH.cr().modify(|w| w.set_per(false)); | ||
| 84 | |||
| 85 | // Explicitly lock after erase | ||
| 86 | lock(); | ||
| 87 | |||
| 88 | // Extra wait to ensure operation completes | ||
| 89 | wait_busy(); | ||
| 90 | |||
| 91 | ret | ||
| 92 | } | ||
| 93 | |||
| 94 | pub(crate) unsafe fn wait_ready_blocking() -> Result<(), Error> { | ||
| 95 | while pac::FLASH.sr().read().bsy() {} | ||
| 96 | |||
| 97 | let sr = pac::FLASH.sr().read(); | ||
| 98 | |||
| 99 | if sr.progerr() { | ||
| 100 | return Err(Error::Prog); | ||
| 101 | } | ||
| 102 | |||
| 103 | if sr.wrperr() { | ||
| 104 | return Err(Error::Protected); | ||
| 105 | } | ||
| 106 | |||
| 107 | if sr.pgaerr() { | ||
| 108 | return Err(Error::Unaligned); | ||
| 109 | } | ||
| 110 | |||
| 111 | Ok(()) | ||
| 112 | } | ||
| 113 | |||
| 114 | pub(crate) unsafe fn clear_all_err() { | ||
| 115 | // read and write back the same value. | ||
| 116 | // This clears all "write 1 to clear" bits. | ||
| 117 | pac::FLASH.sr().modify(|_| {}); | ||
| 118 | } | ||
| 119 | |||
| 120 | fn wait_busy() { | ||
| 121 | while pac::FLASH.sr().read().bsy() {} | ||
| 122 | } | ||
