aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDion Dokter <[email protected]>2024-04-14 00:45:53 +0200
committerDion Dokter <[email protected]>2024-04-14 00:45:53 +0200
commitca84be80bcbfb4459212fa02e75cfafc85d6df51 (patch)
treed45541d1305b621e3e63eead1e7ffc1c58a7b39e /examples
parent0a785585bcc268ca1eb5341b1bcc54113cf96298 (diff)
Add wdt and flash
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32u0/src/bin/flash.rs43
-rw-r--r--examples/stm32u0/src/bin/wdt.rs41
2 files changed, 84 insertions, 0 deletions
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}