aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2023-05-14 21:39:55 +0000
committerGitHub <[email protected]>2023-05-14 21:39:55 +0000
commitae4827587ccfb9ee8c65eb2d56439e3df3c052d0 (patch)
tree8300ad0877229ebfc7d5ab6c6e4be8ac6fac4d3b
parentcdd326284a4a8bd45e59d18822a6d7269030f1c0 (diff)
parentec7a4fd9cc6284fffbce3c74a82d2942713d4cd4 (diff)
Merge #1454
1454: stm32f0 flash implementation r=Dirbaio a=jp99 i've copied and modified the f3 implementation and it seems to be working. Co-authored-by: Jaap Prickartz <[email protected]>
-rw-r--r--embassy-stm32/src/flash/f0.rs105
-rw-r--r--embassy-stm32/src/flash/mod.rs3
2 files changed, 107 insertions, 1 deletions
diff --git a/embassy-stm32/src/flash/f0.rs b/embassy-stm32/src/flash/f0.rs
new file mode 100644
index 000000000..033afecd5
--- /dev/null
+++ b/embassy-stm32/src/flash/f0.rs
@@ -0,0 +1,105 @@
1use core::convert::TryInto;
2use core::ptr::write_volatile;
3
4use atomic_polyfill::{fence, Ordering};
5
6use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
7use crate::flash::Error;
8use crate::pac;
9
10pub const fn get_flash_regions() -> &'static [&'static FlashRegion] {
11 &FLASH_REGIONS
12}
13
14pub(crate) unsafe fn lock() {
15 pac::FLASH.cr().modify(|w| w.set_lock(true));
16}
17
18pub(crate) unsafe fn unlock() {
19 pac::FLASH.keyr().write(|w| w.set_fkeyr(0x4567_0123));
20 pac::FLASH.keyr().write(|w| w.set_fkeyr(0xCDEF_89AB));
21}
22
23pub(crate) unsafe fn begin_write() {
24 assert_eq!(0, WRITE_SIZE % 2);
25
26 pac::FLASH.cr().write(|w| w.set_pg(true));
27}
28
29pub(crate) unsafe fn end_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 chunk in buf.chunks(2) {
36 write_volatile(address as *mut u16, u16::from_le_bytes(chunk.try_into().unwrap()));
37 address += chunk.len() as u32;
38
39 // prevents parallelism errors
40 fence(Ordering::SeqCst);
41 }
42
43 blocking_wait_ready()
44}
45
46pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
47 pac::FLASH.cr().modify(|w| {
48 w.set_per(true);
49 });
50
51 pac::FLASH.ar().write(|w| w.set_far(sector.start));
52
53 pac::FLASH.cr().modify(|w| {
54 w.set_strt(true);
55 });
56
57 let mut ret: Result<(), Error> = blocking_wait_ready();
58
59 if !pac::FLASH.sr().read().eop() {
60 trace!("FLASH: EOP not set");
61 ret = Err(Error::Prog);
62 } else {
63 pac::FLASH.sr().write(|w| w.set_eop(true));
64 }
65
66 pac::FLASH.cr().modify(|w| w.set_per(false));
67
68 clear_all_err();
69 if ret.is_err() {
70 return ret;
71 }
72 Ok(())
73}
74
75pub(crate) unsafe fn clear_all_err() {
76 pac::FLASH.sr().modify(|w| {
77 if w.pgerr() {
78 w.set_pgerr(true);
79 }
80 if w.wrprt() {
81 w.set_wrprt(true)
82 };
83 if w.eop() {
84 w.set_eop(true);
85 }
86 });
87}
88
89unsafe fn blocking_wait_ready() -> Result<(), Error> {
90 loop {
91 let sr = pac::FLASH.sr().read();
92
93 if !sr.bsy() {
94 if sr.wrprt() {
95 return Err(Error::Protected);
96 }
97
98 if sr.pgerr() {
99 return Err(Error::Seq);
100 }
101
102 return Ok(());
103 }
104 }
105}
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs
index 7d5596b1f..f6efa7753 100644
--- a/embassy-stm32/src/flash/mod.rs
+++ b/embassy-stm32/src/flash/mod.rs
@@ -49,13 +49,14 @@ impl FlashRegion {
49} 49}
50 50
51#[cfg_attr(any(flash_l0, flash_l1, flash_l4, flash_wl, flash_wb), path = "l.rs")] 51#[cfg_attr(any(flash_l0, flash_l1, flash_l4, flash_wl, flash_wb), path = "l.rs")]
52#[cfg_attr(flash_f0, path = "f0.rs")]
52#[cfg_attr(flash_f3, path = "f3.rs")] 53#[cfg_attr(flash_f3, path = "f3.rs")]
53#[cfg_attr(flash_f4, path = "f4.rs")] 54#[cfg_attr(flash_f4, path = "f4.rs")]
54#[cfg_attr(flash_f7, path = "f7.rs")] 55#[cfg_attr(flash_f7, path = "f7.rs")]
55#[cfg_attr(flash_h7, path = "h7.rs")] 56#[cfg_attr(flash_h7, path = "h7.rs")]
56#[cfg_attr( 57#[cfg_attr(
57 not(any( 58 not(any(
58 flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f3, flash_f4, flash_f7, flash_h7 59 flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f0, flash_f3, flash_f4, flash_f7, flash_h7
59 )), 60 )),
60 path = "other.rs" 61 path = "other.rs"
61)] 62)]