embassy/examples/stm32f4/src/bin/flash_async.rs

86 lines
2.2 KiB
Rust
Raw Normal View History

2023-05-24 12:41:52 +02:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::{info, unwrap};
use embassy_executor::Spawner;
2023-05-25 22:48:17 +02:00
use embassy_stm32::bind_interrupts;
2023-05-25 13:42:42 +02:00
use embassy_stm32::flash::{Flash, InterruptHandler};
2023-05-24 12:41:52 +02:00
use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Speed};
use embassy_time::Timer;
2023-05-24 12:41:52 +02:00
use {defmt_rtt as _, panic_probe as _};
2023-05-25 13:42:42 +02:00
bind_interrupts!(struct Irqs {
FLASH => InterruptHandler;
});
2023-05-24 12:41:52 +02:00
#[embassy_executor::main]
async fn main(spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello Flash!");
2023-05-25 13:42:42 +02:00
let mut f = Flash::new(p.FLASH, Irqs);
2023-05-24 12:41:52 +02:00
// Led should blink uninterrupted during ~2sec erase operation
spawner.spawn(blinky(p.PB7.degrade())).unwrap();
// Test on bank 2 in order not to stall CPU.
test_flash(&mut f, 1024 * 1024, 128 * 1024).await;
}
#[embassy_executor::task]
async fn blinky(p: AnyPin) {
let mut led = Output::new(p, Level::High, Speed::Low);
loop {
info!("high");
led.set_high();
Timer::after_millis(300).await;
2023-05-24 12:41:52 +02:00
info!("low");
led.set_low();
Timer::after_millis(300).await;
2023-05-24 12:41:52 +02:00
}
}
async fn test_flash<'a>(f: &mut Flash<'a>, offset: u32, size: u32) {
info!("Testing offset: {=u32:#X}, size: {=u32:#X}", offset, size);
info!("Reading...");
let mut buf = [0u8; 32];
2023-12-17 22:09:14 +01:00
unwrap!(f.blocking_read(offset, &mut buf));
2023-05-24 12:41:52 +02:00
info!("Read: {=[u8]:x}", buf);
info!("Erasing...");
unwrap!(f.erase(offset, offset + size).await);
info!("Reading...");
let mut buf = [0u8; 32];
2023-12-17 22:09:14 +01:00
unwrap!(f.blocking_read(offset, &mut buf));
2023-05-24 12:41:52 +02:00
info!("Read after erase: {=[u8]:x}", buf);
info!("Writing...");
unwrap!(
f.write(
offset,
&[
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, 30, 31, 32
]
)
.await
);
info!("Reading...");
let mut buf = [0u8; 32];
2023-12-17 22:09:14 +01:00
unwrap!(f.blocking_read(offset, &mut buf));
2023-05-24 12:41:52 +02:00
info!("Read: {=[u8]:x}", buf);
assert_eq!(
&buf[..],
&[
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,
30, 31, 32
]
);
2023-05-25 22:48:17 +02:00
}