diff options
| author | Zach <[email protected]> | 2024-02-17 12:04:53 -0600 |
|---|---|---|
| committer | Zach <[email protected]> | 2024-02-17 12:04:53 -0600 |
| commit | dd9f0d9d9e73feffce371ab7fda714b09b268715 (patch) | |
| tree | 586fb16591606e0fe80f175fee3d68fc27c2cd25 /embassy-stm32/src/flash | |
| parent | 377e58e408f830f79171a470ba602b7d8bc525e4 (diff) | |
support u5 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/u5.rs | 105 |
2 files changed, 107 insertions, 1 deletions
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs index 47232f4a4..4f43a7a48 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(any(flash_g0, flash_g4), path = "g.rs")] | 101 | #[cfg_attr(any(flash_g0, flash_g4), path = "g.rs")] |
| 102 | #[cfg_attr(flash_h7, path = "h7.rs")] | 102 | #[cfg_attr(flash_h7, path = "h7.rs")] |
| 103 | #[cfg_attr(flash_h7ab, path = "h7.rs")] | 103 | #[cfg_attr(flash_h7ab, path = "h7.rs")] |
| 104 | #[cfg_attr(flash_u5, path = "u5.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 | 108 | flash_g4, flash_h7, flash_h7ab, flash_u5 |
| 108 | )), | 109 | )), |
| 109 | path = "other.rs" | 110 | path = "other.rs" |
| 110 | )] | 111 | )] |
diff --git a/embassy-stm32/src/flash/u5.rs b/embassy-stm32/src/flash/u5.rs new file mode 100644 index 000000000..3787082f9 --- /dev/null +++ b/embassy-stm32/src/flash/u5.rs | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | use core::convert::TryInto; | ||
| 2 | use core::ptr::write_volatile; | ||
| 3 | use core::sync::atomic::{fence, Ordering}; | ||
| 4 | |||
| 5 | use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | ||
| 6 | use crate::flash::Error; | ||
| 7 | use crate::pac; | ||
| 8 | |||
| 9 | pub(crate) const fn is_default_layout() -> bool { | ||
| 10 | true | ||
| 11 | } | ||
| 12 | |||
| 13 | pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] { | ||
| 14 | &FLASH_REGIONS | ||
| 15 | } | ||
| 16 | |||
| 17 | pub(crate) unsafe fn lock() { | ||
| 18 | pac::FLASH.seccr().modify(|w| w.set_lock(true)); | ||
| 19 | } | ||
| 20 | |||
| 21 | pub(crate) unsafe fn unlock() { | ||
| 22 | if pac::FLASH.seccr().read().lock() { | ||
| 23 | pac::FLASH.seckeyr().write_value(0x4567_0123); | ||
| 24 | pac::FLASH.seckeyr().write_value(0xCDEF_89AB); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | pub(crate) unsafe fn enable_blocking_write() { | ||
| 29 | assert_eq!(0, WRITE_SIZE % 4); | ||
| 30 | |||
| 31 | pac::FLASH.seccr().write(|w| { | ||
| 32 | w.set_pg(pac::flash::vals::SeccrPg::B_0X1); | ||
| 33 | }); | ||
| 34 | } | ||
| 35 | |||
| 36 | pub(crate) unsafe fn disable_blocking_write() { | ||
| 37 | pac::FLASH.seccr().write(|w| w.set_pg(pac::flash::vals::SeccrPg::B_0X0)); | ||
| 38 | } | ||
| 39 | |||
| 40 | pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> { | ||
| 41 | let mut address = start_address; | ||
| 42 | for val in buf.chunks(4) { | ||
| 43 | write_volatile(address as *mut u32, u32::from_le_bytes(val.try_into().unwrap())); | ||
| 44 | address += val.len() as u32; | ||
| 45 | |||
| 46 | // prevents parallelism errors | ||
| 47 | fence(Ordering::SeqCst); | ||
| 48 | } | ||
| 49 | |||
| 50 | blocking_wait_ready() | ||
| 51 | } | ||
| 52 | |||
| 53 | pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> { | ||
| 54 | pac::FLASH.seccr().modify(|w| { | ||
| 55 | w.set_per(pac::flash::vals::SeccrPer::B_0X1); | ||
| 56 | w.set_pnb(sector.index_in_bank) | ||
| 57 | }); | ||
| 58 | |||
| 59 | pac::FLASH.seccr().modify(|w| { | ||
| 60 | w.set_strt(true); | ||
| 61 | }); | ||
| 62 | |||
| 63 | let ret: Result<(), Error> = blocking_wait_ready(); | ||
| 64 | pac::FLASH | ||
| 65 | .seccr() | ||
| 66 | .modify(|w| w.set_per(pac::flash::vals::SeccrPer::B_0X0)); | ||
| 67 | clear_all_err(); | ||
| 68 | ret | ||
| 69 | } | ||
| 70 | |||
| 71 | pub(crate) unsafe fn clear_all_err() { | ||
| 72 | // read and write back the same value. | ||
| 73 | // This clears all "write 1 to clear" bits. | ||
| 74 | pac::FLASH.secsr().modify(|_| {}); | ||
| 75 | } | ||
| 76 | |||
| 77 | unsafe fn blocking_wait_ready() -> Result<(), Error> { | ||
| 78 | loop { | ||
| 79 | let sr = pac::FLASH.secsr().read(); | ||
| 80 | |||
| 81 | if !sr.bsy() { | ||
| 82 | if sr.pgserr() { | ||
| 83 | return Err(Error::Seq); | ||
| 84 | } | ||
| 85 | |||
| 86 | if sr.sizerr() { | ||
| 87 | return Err(Error::Size); | ||
| 88 | } | ||
| 89 | |||
| 90 | if sr.pgaerr() { | ||
| 91 | return Err(Error::Unaligned); | ||
| 92 | } | ||
| 93 | |||
| 94 | if sr.wrperr() { | ||
| 95 | return Err(Error::Protected); | ||
| 96 | } | ||
| 97 | |||
| 98 | if sr.progerr() { | ||
| 99 | return Err(Error::Prog); | ||
| 100 | } | ||
| 101 | |||
| 102 | return Ok(()); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
