Async F4 flash driver

This commit is contained in:
chemicstry
2022-07-15 03:02:36 +03:00
parent 26fdfdb00a
commit 9e6c94f2b1
4 changed files with 268 additions and 34 deletions

View File

@ -20,6 +20,7 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa
heapless = { version = "0.7.5", default-features = false }
nb = "1.0.0"
embedded-storage = "0.3.0"
embedded-storage-async = "0.3.0"
usb-device = "0.2"
usbd-serial = "0.1.1"

View File

@ -0,0 +1,83 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::{info, unwrap};
use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_stm32::flash::Flash;
use embassy_stm32::Peripherals;
use embassy_stm32::gpio::{AnyPin, Level, Speed, Output, Pin};
use embedded_storage_async::nor_flash::{AsyncReadNorFlash, AsyncNorFlash};
use {defmt_rtt as _, panic_probe as _};
#[embassy::task]
async fn blinky(p: AnyPin) {
let mut led = Output::new(p, Level::High, Speed::Low);
loop {
info!("high");
led.set_high();
Timer::after(Duration::from_millis(300)).await;
info!("low");
led.set_low();
Timer::after(Duration::from_millis(300)).await;
}
}
#[embassy::main]
async fn main(spawner: Spawner, p: Peripherals) {
info!("Hello Flash!");
let mut f = Flash::unlock(p.FLASH);
spawner.spawn(blinky(p.PB7.degrade())).unwrap();
// Sector 5
// test_flash(&mut f, 128 * 1024, 128 * 1024).await;
// Sectors 11..=16, across banks (128K, 16K, 16K, 16K, 16K, 64K)
// test_flash(&mut f, (1024 - 128) * 1024, 256 * 1024).await;
// Sectors 23, last in bank 2
test_flash(&mut f, (2048 - 128) * 1024, 128 * 1024).await;
}
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];
unwrap!(f.read(offset, &mut buf).await);
info!("Read: {=[u8]:x}", buf);
info!("Erasing...");
unwrap!(f.erase(offset, offset + size).await);
info!("Reading...");
let mut buf = [0u8; 32];
unwrap!(f.read(offset, &mut buf).await);
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];
unwrap!(f.read(offset, &mut buf).await);
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
]
);
}