From 661db6f1b0a56f4844472a810685f18aee7ef58c Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 20 Jul 2022 23:33:29 +0300 Subject: [PATCH] Cleanup example --- examples/stm32f4/src/bin/flash_async.rs | 52 ++++++++++++------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/examples/stm32f4/src/bin/flash_async.rs b/examples/stm32f4/src/bin/flash_async.rs index b5c1f1ad..47afb052 100644 --- a/examples/stm32f4/src/bin/flash_async.rs +++ b/examples/stm32f4/src/bin/flash_async.rs @@ -6,11 +6,24 @@ use defmt::{info, unwrap}; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_stm32::flash::Flash; +use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Speed}; use embassy_stm32::Peripherals; -use embassy_stm32::gpio::{AnyPin, Level, Speed, Output, Pin}; -use embedded_storage_async::nor_flash::{AsyncReadNorFlash, AsyncNorFlash}; +use embedded_storage_async::nor_flash::{AsyncNorFlash, AsyncReadNorFlash}; use {defmt_rtt as _, panic_probe as _}; +#[embassy::main] +async fn main(spawner: Spawner, p: Peripherals) { + info!("Hello Flash!"); + + let mut f = Flash::unlock(p.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::task] async fn blinky(p: AnyPin) { let mut led = Output::new(p, Level::High, Speed::Low); @@ -26,24 +39,6 @@ async fn blinky(p: AnyPin) { } } -#[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); @@ -61,13 +56,16 @@ async fn test_flash<'a>(f: &mut Flash<'a>, offset: u32, size: u32) { 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); + 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];