aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/flash
diff options
context:
space:
mode:
authorAndreas Lindahl Flåten (ALF) <[email protected]>2025-11-05 16:47:09 +0100
committerAndreas Lindahl Flåten (ALF) <[email protected]>2025-11-13 11:26:06 +0100
commitdccf185e1489c0055fcacdea59ce7837cc4d076d (patch)
tree4117813ecca3209236023032a0a2c2735fbedd76 /embassy-stm32/src/flash
parenta6a38cdd22cb2f47f3cc869f447d7cdb80e27ec6 (diff)
Add c.rs flash for the stm32c0 family
This is basically a copy of the `g.rs` file, with multi bank support removed (c0 is single bank only).
Diffstat (limited to 'embassy-stm32/src/flash')
-rw-r--r--embassy-stm32/src/flash/c.rs122
-rw-r--r--embassy-stm32/src/flash/common.rs2
-rw-r--r--embassy-stm32/src/flash/mod.rs3
3 files changed, 125 insertions, 2 deletions
diff --git a/embassy-stm32/src/flash/c.rs b/embassy-stm32/src/flash/c.rs
new file mode 100644
index 000000000..af3d07ac6
--- /dev/null
+++ b/embassy-stm32/src/flash/c.rs
@@ -0,0 +1,122 @@
1use core::ptr::write_volatile;
2use core::sync::atomic::{Ordering, fence};
3
4use cortex_m::interrupt;
5
6use super::{FlashSector, WRITE_SIZE};
7use crate::flash::Error;
8use crate::pac;
9
10pub(crate) unsafe fn lock() {
11 pac::FLASH.cr().modify(|w| w.set_lock(true));
12}
13pub(crate) unsafe fn unlock() {
14 // Wait, while the memory interface is busy.
15 wait_busy();
16
17 // Unlock flash
18 if pac::FLASH.cr().read().lock() {
19 pac::FLASH.keyr().write_value(0x4567_0123);
20 pac::FLASH.keyr().write_value(0xCDEF_89AB);
21 }
22}
23
24pub(crate) unsafe fn enable_blocking_write() {
25 assert_eq!(0, WRITE_SIZE % 4);
26 pac::FLASH.cr().write(|w| w.set_pg(true));
27}
28
29pub(crate) unsafe fn disable_blocking_write() {
30 pac::FLASH.cr().write(|w| w.set_pg(false));
31}
32
33pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> {
34 let mut address = start_address;
35 for val in buf.chunks(4) {
36 write_volatile(address as *mut u32, u32::from_le_bytes(unwrap!(val.try_into())));
37 address += val.len() as u32;
38
39 // prevents parallelism errors
40 fence(Ordering::SeqCst);
41 }
42
43 wait_ready_blocking()
44}
45
46pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
47 let idx = (sector.start - super::FLASH_BASE as u32) / super::BANK1_REGION.erase_size as u32;
48
49 #[cfg(feature = "defmt")]
50 defmt::trace!("STM32C0 Erase: addr=0x{:08x}, idx={}, erase_size={}", sector.start, idx, super::BANK1_REGION.erase_size);
51
52
53 wait_busy();
54 clear_all_err();
55
56 // Explicitly unlock before erase
57 unlock();
58
59 interrupt::free(|_| {
60 #[cfg(feature = "defmt")]
61 {
62 let cr_before = pac::FLASH.cr().read();
63 defmt::trace!("FLASH_CR before: 0x{:08x}", cr_before.0);
64 }
65
66 pac::FLASH.cr().modify(|w| {
67 w.set_per(true);
68 w.set_pnb(idx as u8);
69 w.set_strt(true);
70 });
71
72 #[cfg(feature = "defmt")]
73 {
74 let cr_after = pac::FLASH.cr().read();
75 defmt::trace!("FLASH_CR after: 0x{:08x}, PER={}, PNB={}, STRT={}",
76 cr_after.0, cr_after.per(), cr_after.pnb(), cr_after.strt());
77 }
78 });
79
80 let ret: Result<(), Error> = wait_ready_blocking();
81
82 // Clear erase bit
83 pac::FLASH.cr().modify(|w| w.set_per(false));
84
85 // Explicitly lock after erase
86 lock();
87
88 // Extra wait to ensure operation completes
89 wait_busy();
90
91 ret
92}
93
94pub(crate) unsafe fn wait_ready_blocking() -> Result<(), Error> {
95 while pac::FLASH.sr().read().bsy() {}
96
97 let sr = pac::FLASH.sr().read();
98
99 if sr.progerr() {
100 return Err(Error::Prog);
101 }
102
103 if sr.wrperr() {
104 return Err(Error::Protected);
105 }
106
107 if sr.pgaerr() {
108 return Err(Error::Unaligned);
109 }
110
111 Ok(())
112}
113
114pub(crate) unsafe fn clear_all_err() {
115 // read and write back the same value.
116 // This clears all "write 1 to clear" bits.
117 pac::FLASH.sr().modify(|_| {});
118}
119
120fn wait_busy() {
121 while pac::FLASH.sr().read().bsy() {}
122}
diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs
index b595938a6..508bb2548 100644
--- a/embassy-stm32/src/flash/common.rs
+++ b/embassy-stm32/src/flash/common.rs
@@ -102,7 +102,7 @@ pub(super) unsafe fn blocking_write(
102 } 102 }
103 103
104 let mut address = base + offset; 104 let mut address = base + offset;
105 trace!("Writing {} bytes at 0x{:x}", bytes.len(), address); 105 trace!("Writing {} bytes at 0x{:x} (base=0x{:x}, offset=0x{:x})", bytes.len(), address, base, offset);
106 106
107 for chunk in bytes.chunks(WRITE_SIZE) { 107 for chunk in bytes.chunks(WRITE_SIZE) {
108 write_chunk(address, chunk)?; 108 write_chunk(address, chunk)?;
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs
index 3e74d857a..39cd9b3a9 100644
--- a/embassy-stm32/src/flash/mod.rs
+++ b/embassy-stm32/src/flash/mod.rs
@@ -99,6 +99,7 @@ compile_error!("The 'eeprom' cfg is enabled for a non-L0/L1 chip family. This is
99#[cfg_attr(flash_f4, path = "f4.rs")] 99#[cfg_attr(flash_f4, path = "f4.rs")]
100#[cfg_attr(flash_f7, path = "f7.rs")] 100#[cfg_attr(flash_f7, path = "f7.rs")]
101#[cfg_attr(any(flash_g0x0, flash_g0x1, flash_g4c2, flash_g4c3, flash_g4c4), path = "g.rs")] 101#[cfg_attr(any(flash_g0x0, flash_g0x1, flash_g4c2, flash_g4c3, flash_g4c4), path = "g.rs")]
102#[cfg_attr(flash_c0, path = "c.rs")]
102#[cfg_attr(flash_h7, path = "h7.rs")] 103#[cfg_attr(flash_h7, path = "h7.rs")]
103#[cfg_attr(flash_h7ab, path = "h7.rs")] 104#[cfg_attr(flash_h7ab, path = "h7.rs")]
104#[cfg_attr(any(flash_u5, flash_wba), path = "u5.rs")] 105#[cfg_attr(any(flash_u5, flash_wba), path = "u5.rs")]
@@ -108,7 +109,7 @@ compile_error!("The 'eeprom' cfg is enabled for a non-L0/L1 chip family. This is
108#[cfg_attr( 109#[cfg_attr(
109 not(any( 110 not(any(
110 flash_l0, flash_l1, flash_l4, flash_l5, flash_wl, flash_wb, flash_f0, flash_f1, flash_f2, flash_f3, flash_f4, 111 flash_l0, flash_l1, flash_l4, flash_l5, flash_wl, flash_wb, flash_f0, flash_f1, flash_f2, flash_f3, flash_f4,
111 flash_f7, flash_g0x0, flash_g0x1, flash_g4c2, flash_g4c3, flash_g4c4, flash_h7, flash_h7ab, flash_u5, 112 flash_f7, flash_g0x0, flash_g0x1, flash_g4c2, flash_g4c3, flash_g4c4, flash_c0, flash_h7, flash_h7ab, flash_u5,
112 flash_wba, flash_h50, flash_u0, flash_h5, 113 flash_wba, flash_h50, flash_u0, flash_h5,
113 )), 114 )),
114 path = "other.rs" 115 path = "other.rs"