diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-02-18 17:55:35 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-18 17:55:35 +0000 |
| commit | 034e47abacd22d0bd1a0c8187477abf364a60819 (patch) | |
| tree | bd0c091096b3d189e4ec8a91bf546e7138edcab5 | |
| parent | c7e3eca98c11f4624b0c5204da4a2c425c191745 (diff) | |
| parent | dd9f0d9d9e73feffce371ab7fda714b09b268715 (diff) | |
Merge pull request #2591 from exzachlyvv/zvv/u5-flash
support u5 flash
| -rw-r--r-- | embassy-stm32/src/flash/mod.rs | 3 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/u5.rs | 105 | ||||
| -rw-r--r-- | examples/stm32u5/src/bin/flash.rs | 55 |
3 files changed, 162 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 | } | ||
diff --git a/examples/stm32u5/src/bin/flash.rs b/examples/stm32u5/src/bin/flash.rs new file mode 100644 index 000000000..e4fd6bb9c --- /dev/null +++ b/examples/stm32u5/src/bin/flash.rs | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::{info, unwrap}; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::flash::Flash; | ||
| 7 | use embassy_time::Timer; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[embassy_executor::main] | ||
| 11 | async fn main(_spawner: Spawner) { | ||
| 12 | let p = embassy_stm32::init(Default::default()); | ||
| 13 | info!("Hello Flash!"); | ||
| 14 | |||
| 15 | const ADDR: u32 = 0x8_0000; // This is the offset into the third region, the absolute address is 4x32K + 128K + 0x8_0000. | ||
| 16 | |||
| 17 | // wait a bit before accessing the flash | ||
| 18 | Timer::after_millis(300).await; | ||
| 19 | |||
| 20 | let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank1_region; | ||
| 21 | |||
| 22 | info!("Reading..."); | ||
| 23 | let mut buf = [0u8; 32]; | ||
| 24 | unwrap!(f.blocking_read(ADDR, &mut buf)); | ||
| 25 | info!("Read: {=[u8]:x}", buf); | ||
| 26 | |||
| 27 | info!("Erasing..."); | ||
| 28 | unwrap!(f.blocking_erase(ADDR, ADDR + 256 * 1024)); | ||
| 29 | |||
| 30 | info!("Reading..."); | ||
| 31 | let mut buf = [0u8; 32]; | ||
| 32 | unwrap!(f.blocking_read(ADDR, &mut buf)); | ||
| 33 | info!("Read after erase: {=[u8]:x}", buf); | ||
| 34 | |||
| 35 | info!("Writing..."); | ||
| 36 | unwrap!(f.blocking_write( | ||
| 37 | ADDR, | ||
| 38 | &[ | ||
| 39 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, | ||
| 40 | 30, 31, 32 | ||
| 41 | ] | ||
| 42 | )); | ||
| 43 | |||
| 44 | info!("Reading..."); | ||
| 45 | let mut buf = [0u8; 32]; | ||
| 46 | unwrap!(f.blocking_read(ADDR, &mut buf)); | ||
| 47 | info!("Read: {=[u8]:x}", buf); | ||
| 48 | assert_eq!( | ||
| 49 | &buf[..], | ||
| 50 | &[ | ||
| 51 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, | ||
| 52 | 30, 31, 32 | ||
| 53 | ] | ||
| 54 | ); | ||
| 55 | } | ||
