diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-07-31 10:35:28 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-31 10:35:28 +0000 |
| commit | 958cace36dec6d0473c85a4129c871be9e563f2a (patch) | |
| tree | 5d57d2e837558c74153eb3d60e135a4b12caa9fd /embassy-stm32 | |
| parent | 2568c714c80267503554bcf090a2dbcd80b41e08 (diff) | |
| parent | 0ddabf04234ca8ad877d6ae13cc690567d6f4832 (diff) | |
Merge pull request #1724 from bguruprasath5/stm32g0-flash-support
Added STM32G0 Flash Support
Diffstat (limited to 'embassy-stm32')
| -rw-r--r-- | embassy-stm32/src/flash/g0.rs | 115 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/mod.rs | 3 |
2 files changed, 117 insertions, 1 deletions
diff --git a/embassy-stm32/src/flash/g0.rs b/embassy-stm32/src/flash/g0.rs new file mode 100644 index 000000000..60ff1abf1 --- /dev/null +++ b/embassy-stm32/src/flash/g0.rs | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | use core::convert::TryInto; | ||
| 2 | use core::ptr::write_volatile; | ||
| 3 | use core::sync::atomic::{fence, Ordering}; | ||
| 4 | |||
| 5 | use cortex_m::interrupt; | ||
| 6 | |||
| 7 | use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | ||
| 8 | use crate::flash::Error; | ||
| 9 | use crate::pac; | ||
| 10 | |||
| 11 | pub const fn is_default_layout() -> bool { | ||
| 12 | true | ||
| 13 | } | ||
| 14 | |||
| 15 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { | ||
| 16 | &FLASH_REGIONS | ||
| 17 | } | ||
| 18 | |||
| 19 | pub(crate) unsafe fn lock() { | ||
| 20 | pac::FLASH.cr().modify(|w| w.set_lock(true)); | ||
| 21 | } | ||
| 22 | pub(crate) unsafe fn unlock() { | ||
| 23 | // Wait, while the memory interface is busy. | ||
| 24 | while pac::FLASH.sr().read().bsy() {} | ||
| 25 | |||
| 26 | // Unlock flash | ||
| 27 | pac::FLASH.keyr().write(|w| w.set_keyr(0x4567_0123)); | ||
| 28 | pac::FLASH.keyr().write(|w| w.set_keyr(0xCDEF_89AB)); | ||
| 29 | } | ||
| 30 | |||
| 31 | pub(crate) unsafe fn enable_blocking_write() { | ||
| 32 | assert_eq!(0, WRITE_SIZE % 4); | ||
| 33 | pac::FLASH.cr().write(|w| w.set_pg(true)); | ||
| 34 | } | ||
| 35 | |||
| 36 | pub(crate) unsafe fn disable_blocking_write() { | ||
| 37 | pac::FLASH.cr().write(|w| w.set_pg(false)); | ||
| 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 | wait_ready_blocking() | ||
| 51 | } | ||
| 52 | |||
| 53 | pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> { | ||
| 54 | let idx = (sector.start - super::FLASH_BASE as u32) / super::BANK1_REGION.erase_size as u32; | ||
| 55 | while pac::FLASH.sr().read().bsy() {} | ||
| 56 | clear_all_err(); | ||
| 57 | |||
| 58 | interrupt::free(|_| { | ||
| 59 | pac::FLASH.cr().modify(|w| { | ||
| 60 | w.set_per(true); | ||
| 61 | w.set_pnb(idx as u8); | ||
| 62 | w.set_strt(true); | ||
| 63 | }); | ||
| 64 | }); | ||
| 65 | |||
| 66 | let ret: Result<(), Error> = wait_ready_blocking(); | ||
| 67 | pac::FLASH.cr().modify(|w| w.set_per(false)); | ||
| 68 | ret | ||
| 69 | } | ||
| 70 | |||
| 71 | pub(crate) unsafe fn wait_ready_blocking() -> Result<(), Error> { | ||
| 72 | while pac::FLASH.sr().read().bsy() {} | ||
| 73 | |||
| 74 | let sr = pac::FLASH.sr().read(); | ||
| 75 | |||
| 76 | if sr.progerr() { | ||
| 77 | return Err(Error::Prog); | ||
| 78 | } | ||
| 79 | |||
| 80 | if sr.wrperr() { | ||
| 81 | return Err(Error::Protected); | ||
| 82 | } | ||
| 83 | |||
| 84 | if sr.pgaerr() { | ||
| 85 | return Err(Error::Unaligned); | ||
| 86 | } | ||
| 87 | |||
| 88 | Ok(()) | ||
| 89 | } | ||
| 90 | |||
| 91 | pub(crate) unsafe fn clear_all_err() { | ||
| 92 | pac::FLASH.sr().modify(|w| { | ||
| 93 | if w.progerr() { | ||
| 94 | w.set_progerr(true); | ||
| 95 | } | ||
| 96 | if w.pgserr() { | ||
| 97 | w.set_pgserr(true); | ||
| 98 | } | ||
| 99 | if w.rderr() { | ||
| 100 | w.set_rderr(true); | ||
| 101 | } | ||
| 102 | if w.optverr() { | ||
| 103 | w.set_optverr(true); | ||
| 104 | } | ||
| 105 | if w.sizerr() { | ||
| 106 | w.set_sizerr(true); | ||
| 107 | } | ||
| 108 | if w.pgaerr() { | ||
| 109 | w.set_pgaerr(true); | ||
| 110 | } | ||
| 111 | if w.wrperr() { | ||
| 112 | w.set_wrperr(true); | ||
| 113 | } | ||
| 114 | }); | ||
| 115 | } | ||
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs index f44ef2c16..4308037f2 100644 --- a/embassy-stm32/src/flash/mod.rs +++ b/embassy-stm32/src/flash/mod.rs | |||
| @@ -63,10 +63,11 @@ impl FlashRegion { | |||
| 63 | #[cfg_attr(flash_f3, path = "f3.rs")] | 63 | #[cfg_attr(flash_f3, path = "f3.rs")] |
| 64 | #[cfg_attr(flash_f4, path = "f4.rs")] | 64 | #[cfg_attr(flash_f4, path = "f4.rs")] |
| 65 | #[cfg_attr(flash_f7, path = "f7.rs")] | 65 | #[cfg_attr(flash_f7, path = "f7.rs")] |
| 66 | #[cfg_attr(flash_g0, path = "g0.rs")] | ||
| 66 | #[cfg_attr(flash_h7, path = "h7.rs")] | 67 | #[cfg_attr(flash_h7, path = "h7.rs")] |
| 67 | #[cfg_attr( | 68 | #[cfg_attr( |
| 68 | not(any( | 69 | not(any( |
| 69 | flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f0, flash_f3, flash_f4, flash_f7, flash_h7 | 70 | flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f0, flash_f3, flash_f4, flash_f7, flash_g0, flash_h7 |
| 70 | )), | 71 | )), |
| 71 | path = "other.rs" | 72 | path = "other.rs" |
| 72 | )] | 73 | )] |
