diff options
| author | Dion Dokter <[email protected]> | 2024-04-14 00:45:53 +0200 |
|---|---|---|
| committer | Dion Dokter <[email protected]> | 2024-04-14 00:45:53 +0200 |
| commit | ca84be80bcbfb4459212fa02e75cfafc85d6df51 (patch) | |
| tree | d45541d1305b621e3e63eead1e7ffc1c58a7b39e /embassy-stm32/src/flash | |
| parent | 0a785585bcc268ca1eb5341b1bcc54113cf96298 (diff) | |
Add wdt and flash
Diffstat (limited to 'embassy-stm32/src/flash')
| -rw-r--r-- | embassy-stm32/src/flash/mod.rs | 3 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/u0.rs | 96 |
2 files changed, 98 insertions, 1 deletions
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs index 9d7861816..8c6ca2471 100644 --- a/embassy-stm32/src/flash/mod.rs +++ b/embassy-stm32/src/flash/mod.rs | |||
| @@ -101,10 +101,11 @@ pub enum FlashBank { | |||
| 101 | #[cfg_attr(flash_h7ab, path = "h7.rs")] | 101 | #[cfg_attr(flash_h7ab, path = "h7.rs")] |
| 102 | #[cfg_attr(flash_u5, path = "u5.rs")] | 102 | #[cfg_attr(flash_u5, path = "u5.rs")] |
| 103 | #[cfg_attr(flash_h50, path = "h50.rs")] | 103 | #[cfg_attr(flash_h50, path = "h50.rs")] |
| 104 | #[cfg_attr(flash_u0, path = "u0.rs")] | ||
| 104 | #[cfg_attr( | 105 | #[cfg_attr( |
| 105 | not(any( | 106 | not(any( |
| 106 | flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f0, flash_f1, flash_f3, flash_f4, flash_f7, flash_g0, | 107 | flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f0, flash_f1, flash_f3, flash_f4, flash_f7, flash_g0, |
| 107 | flash_g4, flash_h7, flash_h7ab, flash_u5, flash_h50 | 108 | flash_g4, flash_h7, flash_h7ab, flash_u5, flash_h50, flash_u0 |
| 108 | )), | 109 | )), |
| 109 | path = "other.rs" | 110 | path = "other.rs" |
| 110 | )] | 111 | )] |
diff --git a/embassy-stm32/src/flash/u0.rs b/embassy-stm32/src/flash/u0.rs new file mode 100644 index 000000000..dfc5a2f76 --- /dev/null +++ b/embassy-stm32/src/flash/u0.rs | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | use core::ptr::write_volatile; | ||
| 2 | use core::sync::atomic::{fence, Ordering}; | ||
| 3 | |||
| 4 | use cortex_m::interrupt; | ||
| 5 | |||
| 6 | use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | ||
| 7 | use crate::flash::Error; | ||
| 8 | use crate::pac; | ||
| 9 | |||
| 10 | pub(crate) const fn is_default_layout() -> bool { | ||
| 11 | true | ||
| 12 | } | ||
| 13 | |||
| 14 | pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] { | ||
| 15 | &FLASH_REGIONS | ||
| 16 | } | ||
| 17 | |||
| 18 | pub(crate) unsafe fn lock() { | ||
| 19 | pac::FLASH.cr().modify(|w| w.set_lock(true)); | ||
| 20 | } | ||
| 21 | pub(crate) unsafe fn unlock() { | ||
| 22 | // Wait, while the memory interface is busy. | ||
| 23 | while pac::FLASH.sr().read().bsy1() {} | ||
| 24 | |||
| 25 | // Unlock flash | ||
| 26 | if pac::FLASH.cr().read().lock() { | ||
| 27 | pac::FLASH.keyr().write(|w| w.set_key(0x4567_0123)); | ||
| 28 | pac::FLASH.keyr().write(|w| w.set_key(0xCDEF_89AB)); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | pub(crate) unsafe fn enable_blocking_write() { | ||
| 33 | assert_eq!(0, WRITE_SIZE % 4); | ||
| 34 | pac::FLASH.cr().write(|w| w.set_pg(true)); | ||
| 35 | } | ||
| 36 | |||
| 37 | pub(crate) unsafe fn disable_blocking_write() { | ||
| 38 | pac::FLASH.cr().write(|w| w.set_pg(false)); | ||
| 39 | } | ||
| 40 | |||
| 41 | pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> { | ||
| 42 | let mut address = start_address; | ||
| 43 | for val in buf.chunks(4) { | ||
| 44 | write_volatile(address as *mut u32, u32::from_le_bytes(val.try_into().unwrap())); | ||
| 45 | address += val.len() as u32; | ||
| 46 | |||
| 47 | // prevents parallelism errors | ||
| 48 | fence(Ordering::SeqCst); | ||
| 49 | } | ||
| 50 | |||
| 51 | wait_ready_blocking() | ||
| 52 | } | ||
| 53 | |||
| 54 | pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> { | ||
| 55 | let idx = (sector.start - super::FLASH_BASE as u32) / super::BANK1_REGION.erase_size as u32; | ||
| 56 | while pac::FLASH.sr().read().bsy1() {} | ||
| 57 | clear_all_err(); | ||
| 58 | |||
| 59 | interrupt::free(|_| { | ||
| 60 | pac::FLASH.cr().modify(|w| { | ||
| 61 | w.set_per(true); | ||
| 62 | w.set_pnb(idx as u8); | ||
| 63 | w.set_strt(true); | ||
| 64 | }); | ||
| 65 | }); | ||
| 66 | |||
| 67 | let ret: Result<(), Error> = wait_ready_blocking(); | ||
| 68 | pac::FLASH.cr().modify(|w| w.set_per(false)); | ||
| 69 | ret | ||
| 70 | } | ||
| 71 | |||
| 72 | pub(crate) unsafe fn wait_ready_blocking() -> Result<(), Error> { | ||
| 73 | while pac::FLASH.sr().read().bsy1() {} | ||
| 74 | |||
| 75 | let sr = pac::FLASH.sr().read(); | ||
| 76 | |||
| 77 | if sr.progerr() { | ||
| 78 | return Err(Error::Prog); | ||
| 79 | } | ||
| 80 | |||
| 81 | if sr.wrperr() { | ||
| 82 | return Err(Error::Protected); | ||
| 83 | } | ||
| 84 | |||
| 85 | if sr.pgaerr() { | ||
| 86 | return Err(Error::Unaligned); | ||
| 87 | } | ||
| 88 | |||
| 89 | Ok(()) | ||
| 90 | } | ||
| 91 | |||
| 92 | pub(crate) unsafe fn clear_all_err() { | ||
| 93 | // read and write back the same value. | ||
| 94 | // This clears all "write 1 to clear" bits. | ||
| 95 | pac::FLASH.sr().modify(|_| {}); | ||
| 96 | } | ||
