aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-07-31 10:35:28 +0000
committerGitHub <[email protected]>2023-07-31 10:35:28 +0000
commit958cace36dec6d0473c85a4129c871be9e563f2a (patch)
tree5d57d2e837558c74153eb3d60e135a4b12caa9fd
parent2568c714c80267503554bcf090a2dbcd80b41e08 (diff)
parent0ddabf04234ca8ad877d6ae13cc690567d6f4832 (diff)
Merge pull request #1724 from bguruprasath5/stm32g0-flash-support
Added STM32G0 Flash Support
-rw-r--r--embassy-stm32/src/flash/g0.rs115
-rw-r--r--embassy-stm32/src/flash/mod.rs3
-rw-r--r--examples/stm32g0/src/bin/flash.rs44
3 files changed, 161 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 @@
1use core::convert::TryInto;
2use core::ptr::write_volatile;
3use core::sync::atomic::{fence, Ordering};
4
5use cortex_m::interrupt;
6
7use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
8use crate::flash::Error;
9use crate::pac;
10
11pub const fn is_default_layout() -> bool {
12 true
13}
14
15pub const fn get_flash_regions() -> &'static [&'static FlashRegion] {
16 &FLASH_REGIONS
17}
18
19pub(crate) unsafe fn lock() {
20 pac::FLASH.cr().modify(|w| w.set_lock(true));
21}
22pub(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
31pub(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
36pub(crate) unsafe fn disable_blocking_write() {
37 pac::FLASH.cr().write(|w| w.set_pg(false));
38}
39
40pub(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
53pub(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
71pub(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
91pub(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)]
diff --git a/examples/stm32g0/src/bin/flash.rs b/examples/stm32g0/src/bin/flash.rs
new file mode 100644
index 000000000..ed9f2e843
--- /dev/null
+++ b/examples/stm32g0/src/bin/flash.rs
@@ -0,0 +1,44 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_stm32::flash::Flash;
8use {defmt_rtt as _, panic_probe as _};
9
10#[embassy_executor::main]
11async fn main(_spawner: Spawner) {
12 let p = embassy_stm32::init(Default::default());
13 info!("Hello World!");
14
15 let addr: u32 = 0x8000;
16
17 let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank1_region;
18
19 info!("Reading...");
20 let mut buf = [0u8; 32];
21 unwrap!(f.blocking_read(addr, &mut buf));
22 info!("Read: {=[u8]:x}", buf);
23 info!("Erasing...");
24 unwrap!(f.blocking_erase(addr, addr + 2 * 1024));
25
26 info!("Reading...");
27 let mut buf = [0u8; 32];
28 unwrap!(f.blocking_read(addr, &mut buf));
29 info!("Read after erase: {=[u8]:x}", buf);
30
31 info!("Writing...");
32 unwrap!(f.blocking_write(
33 addr,
34 &[
35 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,
36 30, 31, 32
37 ]
38 ));
39
40 info!("Reading...");
41 let mut buf = [0u8; 32];
42 unwrap!(f.blocking_read(addr, &mut buf));
43 info!("Read: {=[u8]:x}", buf);
44}