|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +use defmt::{info, unwrap}; |
| 5 | +use embassy_executor::Spawner; |
| 6 | +use embassy_stm32::flash::{Flash, InterruptHandler}; |
| 7 | +use embassy_stm32::gpio::{AnyPin, Level, Output, Speed}; |
| 8 | +use embassy_stm32::{Peri, bind_interrupts}; |
| 9 | +use embassy_time::Timer; |
| 10 | +use {defmt_rtt as _, panic_probe as _}; |
| 11 | + |
| 12 | +bind_interrupts!(struct Irqs { |
| 13 | + FLASH => InterruptHandler; |
| 14 | +}); |
| 15 | + |
| 16 | +#[embassy_executor::main] |
| 17 | +async fn main(spawner: Spawner) { |
| 18 | + let p = embassy_stm32::init(Default::default()); |
| 19 | + info!("Hello Flash!"); |
| 20 | + |
| 21 | + let mut f = Flash::new(p.FLASH, Irqs); |
| 22 | + |
| 23 | + // Led should blink uninterrupted during ~2sec erase operation |
| 24 | + spawner.spawn(blinky(p.PB14.into()).unwrap()); |
| 25 | + |
| 26 | + // Test on bank 2 in order not to stall CPU. |
| 27 | + test_flash(&mut f, 1024 * 1024, 128 * 1024).await; |
| 28 | +} |
| 29 | + |
| 30 | +#[embassy_executor::task] |
| 31 | +async fn blinky(p: Peri<'static, AnyPin>) { |
| 32 | + let mut led = Output::new(p, Level::High, Speed::Low); |
| 33 | + |
| 34 | + loop { |
| 35 | + info!("high"); |
| 36 | + led.set_high(); |
| 37 | + Timer::after_millis(300).await; |
| 38 | + |
| 39 | + info!("low"); |
| 40 | + led.set_low(); |
| 41 | + Timer::after_millis(300).await; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +async fn test_flash<'a>(f: &mut Flash<'a>, offset: u32, size: u32) { |
| 46 | + info!("Testing offset: {=u32:#X}, size: {=u32:#X}", offset, size); |
| 47 | + |
| 48 | + info!("Reading..."); |
| 49 | + let mut buf = [0u8; 32]; |
| 50 | + unwrap!(f.blocking_read(offset, &mut buf)); |
| 51 | + info!("Read: {=[u8]:x}", buf); |
| 52 | + |
| 53 | + info!("Erasing..."); |
| 54 | + unwrap!(f.erase(offset, offset + size).await); |
| 55 | + |
| 56 | + info!("Reading..."); |
| 57 | + let mut buf = [0u8; 32]; |
| 58 | + unwrap!(f.blocking_read(offset, &mut buf)); |
| 59 | + info!("Read after erase: {=[u8]:x}", buf); |
| 60 | + |
| 61 | + info!("Writing..."); |
| 62 | + unwrap!( |
| 63 | + f.write( |
| 64 | + offset, |
| 65 | + &[ |
| 66 | + 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, |
| 67 | + 29, 30, 31, 32 |
| 68 | + ] |
| 69 | + ) |
| 70 | + .await |
| 71 | + ); |
| 72 | + |
| 73 | + info!("Reading..."); |
| 74 | + let mut buf = [0u8; 32]; |
| 75 | + unwrap!(f.blocking_read(offset, &mut buf)); |
| 76 | + info!("Read: {=[u8]:x}", buf); |
| 77 | + assert_eq!( |
| 78 | + &buf[..], |
| 79 | + &[ |
| 80 | + 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, |
| 81 | + 30, 31, 32 |
| 82 | + ] |
| 83 | + ); |
| 84 | +} |
0 commit comments