aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/src/flash/mod.rs3
-rw-r--r--embassy-stm32/src/flash/u0.rs96
-rw-r--r--examples/stm32u0/src/bin/flash.rs43
-rw-r--r--examples/stm32u0/src/bin/wdt.rs41
4 files changed, 182 insertions, 1 deletions
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs
index 9d7861816..8c6ca2471 100644
--- a/embassy-stm32/src/flash/mod.rs
+++ b/embassy-stm32/src/flash/mod.rs
@@ -101,10 +101,11 @@ pub enum FlashBank {
101#[cfg_attr(flash_h7ab, path = "h7.rs")] 101#[cfg_attr(flash_h7ab, path = "h7.rs")]
102#[cfg_attr(flash_u5, path = "u5.rs")] 102#[cfg_attr(flash_u5, path = "u5.rs")]
103#[cfg_attr(flash_h50, path = "h50.rs")] 103#[cfg_attr(flash_h50, path = "h50.rs")]
104#[cfg_attr(flash_u0, path = "u0.rs")]
104#[cfg_attr( 105#[cfg_attr(
105 not(any( 106 not(any(
106 flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f0, flash_f1, flash_f3, flash_f4, flash_f7, flash_g0, 107 flash_l0, flash_l1, flash_l4, flash_wl, flash_wb, flash_f0, flash_f1, flash_f3, flash_f4, flash_f7, flash_g0,
107 flash_g4, flash_h7, flash_h7ab, flash_u5, flash_h50 108 flash_g4, flash_h7, flash_h7ab, flash_u5, flash_h50, flash_u0
108 )), 109 )),
109 path = "other.rs" 110 path = "other.rs"
110)] 111)]
diff --git a/embassy-stm32/src/flash/u0.rs b/embassy-stm32/src/flash/u0.rs
new file mode 100644
index 000000000..dfc5a2f76
--- /dev/null
+++ b/embassy-stm32/src/flash/u0.rs
@@ -0,0 +1,96 @@
1use core::ptr::write_volatile;
2use core::sync::atomic::{fence, Ordering};
3
4use cortex_m::interrupt;
5
6use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
7use crate::flash::Error;
8use crate::pac;
9
10pub(crate) const fn is_default_layout() -> bool {
11 true
12}
13
14pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
15 &FLASH_REGIONS
16}
17
18pub(crate) unsafe fn lock() {
19 pac::FLASH.cr().modify(|w| w.set_lock(true));
20}
21pub(crate) unsafe fn unlock() {
22 // Wait, while the memory interface is busy.
23 while pac::FLASH.sr().read().bsy1() {}
24
25 // Unlock flash
26 if pac::FLASH.cr().read().lock() {
27 pac::FLASH.keyr().write(|w| w.set_key(0x4567_0123));
28 pac::FLASH.keyr().write(|w| w.set_key(0xCDEF_89AB));
29 }
30}
31
32pub(crate) unsafe fn enable_blocking_write() {
33 assert_eq!(0, WRITE_SIZE % 4);
34 pac::FLASH.cr().write(|w| w.set_pg(true));
35}
36
37pub(crate) unsafe fn disable_blocking_write() {
38 pac::FLASH.cr().write(|w| w.set_pg(false));
39}
40
41pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE]) -> Result<(), Error> {
42 let mut address = start_address;
43 for val in buf.chunks(4) {
44 write_volatile(address as *mut u32, u32::from_le_bytes(val.try_into().unwrap()));
45 address += val.len() as u32;
46
47 // prevents parallelism errors
48 fence(Ordering::SeqCst);
49 }
50
51 wait_ready_blocking()
52}
53
54pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
55 let idx = (sector.start - super::FLASH_BASE as u32) / super::BANK1_REGION.erase_size as u32;
56 while pac::FLASH.sr().read().bsy1() {}
57 clear_all_err();
58
59 interrupt::free(|_| {
60 pac::FLASH.cr().modify(|w| {
61 w.set_per(true);
62 w.set_pnb(idx as u8);
63 w.set_strt(true);
64 });
65 });
66
67 let ret: Result<(), Error> = wait_ready_blocking();
68 pac::FLASH.cr().modify(|w| w.set_per(false));
69 ret
70}
71
72pub(crate) unsafe fn wait_ready_blocking() -> Result<(), Error> {
73 while pac::FLASH.sr().read().bsy1() {}
74
75 let sr = pac::FLASH.sr().read();
76
77 if sr.progerr() {
78 return Err(Error::Prog);
79 }
80
81 if sr.wrperr() {
82 return Err(Error::Protected);
83 }
84
85 if sr.pgaerr() {
86 return Err(Error::Unaligned);
87 }
88
89 Ok(())
90}
91
92pub(crate) unsafe fn clear_all_err() {
93 // read and write back the same value.
94 // This clears all "write 1 to clear" bits.
95 pac::FLASH.sr().modify(|_| {});
96}
diff --git a/examples/stm32u0/src/bin/flash.rs b/examples/stm32u0/src/bin/flash.rs
new file mode 100644
index 000000000..01b80a76b
--- /dev/null
+++ b/examples/stm32u0/src/bin/flash.rs
@@ -0,0 +1,43 @@
1#![no_std]
2#![no_main]
3
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_stm32::flash::Flash;
7use {defmt_rtt as _, panic_probe as _};
8
9#[embassy_executor::main]
10async fn main(_spawner: Spawner) {
11 let p = embassy_stm32::init(Default::default());
12 info!("Hello World!");
13
14 let addr: u32 = 0x40000 - 2 * 1024;
15
16 let mut f = Flash::new_blocking(p.FLASH).into_blocking_regions().bank1_region;
17
18 info!("Reading...");
19 let mut buf = [0u8; 32];
20 unwrap!(f.blocking_read(addr, &mut buf));
21 info!("Read: {=[u8]:x}", buf);
22 info!("Erasing...");
23 unwrap!(f.blocking_erase(addr, addr + 2 * 1024));
24
25 info!("Reading...");
26 let mut buf = [0u8; 32];
27 unwrap!(f.blocking_read(addr, &mut buf));
28 info!("Read after erase: {=[u8]:x}", buf);
29
30 info!("Writing...");
31 unwrap!(f.blocking_write(
32 addr,
33 &[
34 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,
35 30, 31, 32
36 ]
37 ));
38
39 info!("Reading...");
40 let mut buf = [0u8; 32];
41 unwrap!(f.blocking_read(addr, &mut buf));
42 info!("Read: {=[u8]:x}", buf);
43}
diff --git a/examples/stm32u0/src/bin/wdt.rs b/examples/stm32u0/src/bin/wdt.rs
new file mode 100644
index 000000000..f6276e2e9
--- /dev/null
+++ b/examples/stm32u0/src/bin/wdt.rs
@@ -0,0 +1,41 @@
1#![no_std]
2#![no_main]
3
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_stm32::gpio::{Level, Output, Speed};
7use embassy_stm32::wdg::IndependentWatchdog;
8use embassy_time::Timer;
9use {defmt_rtt as _, panic_probe as _};
10
11#[embassy_executor::main]
12async fn main(_spawner: Spawner) {
13 let p = embassy_stm32::init(Default::default());
14 info!("Hello World!");
15
16 let mut led = Output::new(p.PA5, Level::High, Speed::Low);
17
18 let mut wdt = IndependentWatchdog::new(p.IWDG, 1_000_000);
19 wdt.unleash();
20
21 let mut i = 0;
22
23 loop {
24 info!("high");
25 led.set_high();
26 Timer::after_millis(300).await;
27
28 info!("low");
29 led.set_low();
30 Timer::after_millis(300).await;
31
32 // Pet watchdog for 5 iterations and then stop.
33 // MCU should restart in 1 second after the last pet.
34 if i < 5 {
35 info!("Petting watchdog");
36 wdt.pet();
37 }
38
39 i += 1;
40 }
41}