aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTorin Cooper-Bennun <[email protected]>2024-02-19 15:49:43 +0000
committerTorin Cooper-Bennun <[email protected]>2024-02-19 16:05:50 +0000
commit67230dc4443d82aac14b590ba873fa647e5fc548 (patch)
tree1ac1e34da8530ae31c177eb54dced454f56fd03f
parent69bfcaad42e560b3d52c0e03a761dcedf34cc09f (diff)
flash: h50: first pass at implementation
-rw-r--r--embassy-stm32/src/flash/h50.rs124
-rw-r--r--embassy-stm32/src/flash/mod.rs3
2 files changed, 126 insertions, 1 deletions
diff --git a/embassy-stm32/src/flash/h50.rs b/embassy-stm32/src/flash/h50.rs
new file mode 100644
index 000000000..db05bef5d
--- /dev/null
+++ b/embassy-stm32/src/flash/h50.rs
@@ -0,0 +1,124 @@
1/// STM32H50 series flash impl. See RM0492
2use core::{
3 ptr::write_volatile,
4 sync::atomic::{fence, Ordering},
5};
6
7use cortex_m::interrupt;
8use pac::flash::regs::Nssr;
9use pac::flash::vals::Bksel;
10
11use super::{Error, FlashBank, FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
12use crate::pac;
13
14pub(crate) const fn is_default_layout() -> bool {
15 true
16}
17
18pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
19 &FLASH_REGIONS
20}
21
22pub(crate) unsafe fn lock() {
23 pac::FLASH.nscr().modify(|w| w.set_lock(true));
24}
25
26pub(crate) unsafe fn unlock() {
27 while busy() {}
28
29 if pac::FLASH.nscr().read().lock() {
30 pac::FLASH.nskeyr().write_value(0x4567_0123);
31 pac::FLASH.nskeyr().write_value(0xCDEF_89AB);
32 }
33}
34
35pub(crate) unsafe fn enable_blocking_write() {
36 assert_eq!(0, WRITE_SIZE % 4);
37 pac::FLASH.nscr().write(|w| w.set_pg(true));
38}
39
40pub(crate) unsafe fn disable_blocking_write() {
41 pac::FLASH.nscr().write(|w| w.set_pg(false));
42}
43
44pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> {
45 let mut address = start_address;
46 for val in buf.chunks(4) {
47 write_volatile(address as *mut u32, u32::from_le_bytes(val.try_into().unwrap()));
48 address += val.len() as u32;
49
50 // prevents parallelism errors
51 fence(Ordering::SeqCst);
52 }
53
54 wait_ready_blocking()
55}
56
57pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
58 assert!(sector.bank != FlashBank::Otp);
59 assert!(sector.index_in_bank < 8);
60
61 while busy() {}
62
63 interrupt::free(|_| {
64 pac::FLASH.nscr().modify(|w| {
65 w.set_bksel(match sector.bank {
66 FlashBank::Bank1 => Bksel::B_0X0,
67 FlashBank::Bank2 => Bksel::B_0X1,
68 _ => unreachable!(),
69 });
70 w.set_snb(sector.index_in_bank);
71 w.set_ser(true);
72 w.set_strt(true);
73 })
74 });
75
76 let ret = wait_ready_blocking();
77 pac::FLASH.nscr().modify(|w| w.set_ser(false));
78 ret
79}
80
81pub(crate) unsafe fn wait_ready_blocking() -> Result<(), Error> {
82 loop {
83 let sr = pac::FLASH.nssr().read();
84
85 if !sr_busy(sr) {
86 if sr.wrperr() {
87 return Err(Error::Protected);
88 }
89 if sr.pgserr() {
90 return Err(Error::Seq);
91 }
92 if sr.strberr() {
93 // writing several times to the same byte in the write buffer
94 return Err(Error::Prog);
95 }
96 if sr.incerr() {
97 // attempting write operation before completion of previous
98 // write operation
99 return Err(Error::Seq);
100 }
101
102 return Ok(());
103 }
104 }
105}
106
107pub(crate) unsafe fn clear_all_err() {
108 pac::FLASH.nsccr().modify(|w| {
109 w.set_clr_wrperr(true);
110 w.set_clr_pgserr(true);
111 w.set_clr_strberr(true);
112 w.set_clr_incerr(true);
113 })
114}
115
116fn sr_busy(sr: Nssr) -> bool {
117 // Note: RM0492 sometimes incorrectly refers to WBNE as NSWBNE
118 sr.bsy() || sr.dbne() || sr.wbne()
119}
120
121fn busy() -> bool {
122 let sr = pac::FLASH.nssr().read();
123 sr_busy(sr)
124}
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs
index 4f43a7a48..1d8031e82 100644
--- a/embassy-stm32/src/flash/mod.rs
+++ b/embassy-stm32/src/flash/mod.rs
@@ -102,10 +102,11 @@ pub enum FlashBank {
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(flash_u5, path = "u5.rs")]
105#[cfg_attr(flash_h50, path = "h50.rs")]
105#[cfg_attr( 106#[cfg_attr(
106 not(any( 107 not(any(
107 flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f0, flash_f1, flash_f3, flash_f4, flash_f7, flash_g0, 108 flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f0, flash_f1, flash_f3, flash_f4, flash_f7, flash_g0,
108 flash_g4, flash_h7, flash_h7ab, flash_u5 109 flash_g4, flash_h7, flash_h7ab, flash_u5, flash_h50
109 )), 110 )),
110 path = "other.rs" 111 path = "other.rs"
111)] 112)]