Align examples

This commit is contained in:
Rasmus Melchior Jacobsen 2023-05-24 12:41:52 +02:00
parent b2775fc90c
commit 0a26870d36
8 changed files with 100 additions and 25 deletions

View file

@ -4,8 +4,7 @@
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_stm32::flash::Flash;
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use embassy_stm32::{flash::Flash, interrupt};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
@ -15,7 +14,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x26000;
let mut f = Flash::new(p.FLASH).into_regions().bank1_region;
let mut f = unsafe { Flash::new(p.FLASH, interrupt::take!(FLASH)).into_regions().bank1_region.into_blocking() };
info!("Reading...");
let mut buf = [0u8; 8];

View file

@ -4,7 +4,7 @@
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_stm32::flash::Flash;
use embassy_stm32::{flash::Flash, interrupt};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
@ -14,7 +14,7 @@ async fn main(_spawner: Spawner) {
// Once can also call `into_regions()` to get access to NorFlash implementations
// for each of the unique characteristics.
let mut f = Flash::new(p.FLASH);
let mut f = Flash::new(p.FLASH, interrupt::take!(FLASH));
// Sector 5
test_flash(&mut f, 128 * 1024, 128 * 1024);
@ -31,19 +31,19 @@ fn test_flash(f: &mut Flash, offset: u32, size: u32) {
info!("Reading...");
let mut buf = [0u8; 32];
unwrap!(f.blocking_read(offset, &mut buf));
unwrap!(f.read(offset, &mut buf));
info!("Read: {=[u8]:x}", buf);
info!("Erasing...");
unwrap!(f.blocking_erase(offset, offset + size));
unwrap!(f.erase_blocking(offset, offset + size));
info!("Reading...");
let mut buf = [0u8; 32];
unwrap!(f.blocking_read(offset, &mut buf));
unwrap!(f.read(offset, &mut buf));
info!("Read after erase: {=[u8]:x}", buf);
info!("Writing...");
unwrap!(f.blocking_write(
unwrap!(f.write_blocking(
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,
@ -53,7 +53,7 @@ fn test_flash(f: &mut Flash, offset: u32, size: u32) {
info!("Reading...");
let mut buf = [0u8; 32];
unwrap!(f.blocking_read(offset, &mut buf));
unwrap!(f.read(offset, &mut buf));
info!("Read: {=[u8]:x}", buf);
assert_eq!(
&buf[..],

View file

@ -0,0 +1,81 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_time::{Timer, Duration};
use embassy_stm32::flash::Flash;
use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Speed};
use embassy_stm32::{interrupt};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello Flash!");
let mut f = Flash::new(p.FLASH, interrupt::take!(FLASH));
// 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(Duration::from_millis(300)).await;
info!("low");
led.set_low();
Timer::after(Duration::from_millis(300)).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));
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));
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));
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
]
);
}

View file

@ -4,9 +4,8 @@
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_stm32::flash::Flash;
use embassy_stm32::{flash::Flash, interrupt};
use embassy_time::{Duration, Timer};
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
@ -19,7 +18,7 @@ async fn main(_spawner: Spawner) {
// wait a bit before accessing the flash
Timer::after(Duration::from_millis(300)).await;
let mut f = Flash::new(p.FLASH).into_regions().bank1_region3;
let mut f = unsafe { Flash::new(p.FLASH, interrupt::take!(FLASH)).into_regions().bank1_region3.into_blocking() };
info!("Reading...");
let mut buf = [0u8; 32];

View file

@ -4,9 +4,8 @@
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_stm32::flash::Flash;
use embassy_stm32::{flash::Flash, interrupt};
use embassy_time::{Duration, Timer};
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
@ -19,7 +18,7 @@ async fn main(_spawner: Spawner) {
// wait a bit before accessing the flash
Timer::after(Duration::from_millis(300)).await;
let mut f = Flash::new(p.FLASH).into_regions().bank2_region;
let mut f = unsafe { Flash::new(p.FLASH, interrupt::take!(FLASH)).into_regions().bank2_region.into_blocking() };
info!("Reading...");
let mut buf = [0u8; 32];

View file

@ -4,8 +4,7 @@
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_stm32::flash::Flash;
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use embassy_stm32::{flash::Flash, interrupt};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
@ -15,7 +14,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x26000;
let mut f = Flash::new(p.FLASH).into_regions().bank1_region;
let mut f = unsafe { Flash::new(p.FLASH, interrupt::take!(FLASH)).into_regions().bank1_region.into_blocking() };
info!("Reading...");
let mut buf = [0u8; 8];

View file

@ -4,8 +4,7 @@
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_stm32::flash::Flash;
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use embassy_stm32::{flash::Flash, interrupt};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
@ -15,7 +14,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x26000;
let mut f = Flash::new(p.FLASH).into_regions().bank1_region;
let mut f = unsafe { Flash::new(p.FLASH, interrupt::take!(FLASH)).into_regions().bank1_region.into_blocking() };
info!("Reading...");
let mut buf = [0u8; 8];

View file

@ -4,8 +4,7 @@
use defmt::{info, unwrap};
use embassy_executor::Spawner;
use embassy_stm32::flash::Flash;
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
use embassy_stm32::{flash::Flash, interrupt};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
@ -15,7 +14,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x36000;
let mut f = Flash::new(p.FLASH).into_regions().bank1_region;
let mut f = unsafe { Flash::new(p.FLASH, interrupt::take!(FLASH)).into_regions().bank1_region.into_blocking() };
info!("Reading...");
let mut buf = [0u8; 8];