diff options
| author | chemicstry <[email protected]> | 2022-07-11 03:57:46 +0300 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-07-11 03:57:46 +0300 |
| commit | 734c38eb9c6385dfb51e3934211f140c623fefc8 (patch) | |
| tree | fca7bbf4c44d0556836c30cd9555c642f0e6e194 /examples/stm32f4 | |
| parent | c6a11db39ef829adfb8b4903065446c4bb7a2ef4 (diff) | |
Add F4 flash driver
Diffstat (limited to 'examples/stm32f4')
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/flash.rs | 57 |
2 files changed, 58 insertions, 0 deletions
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index de33ffad8..100c0e608 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -19,6 +19,7 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } | |||
| 19 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 19 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 20 | heapless = { version = "0.7.5", default-features = false } | 20 | heapless = { version = "0.7.5", default-features = false } |
| 21 | nb = "1.0.0" | 21 | nb = "1.0.0" |
| 22 | embedded-storage = "0.3.0" | ||
| 22 | 23 | ||
| 23 | usb-device = "0.2" | 24 | usb-device = "0.2" |
| 24 | usbd-serial = "0.1.1" | 25 | usbd-serial = "0.1.1" |
diff --git a/examples/stm32f4/src/bin/flash.rs b/examples/stm32f4/src/bin/flash.rs new file mode 100644 index 000000000..b531d6f13 --- /dev/null +++ b/examples/stm32f4/src/bin/flash.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{info, unwrap}; | ||
| 6 | use embassy::executor::Spawner; | ||
| 7 | use embassy::time::{Duration, Timer}; | ||
| 8 | use embassy_stm32::flash::Flash; | ||
| 9 | use embassy_stm32::Peripherals; | ||
| 10 | use embedded_storage::nor_flash::{NorFlash, ReadNorFlash}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | #[embassy::main] | ||
| 14 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 15 | info!("Hello Flash!"); | ||
| 16 | |||
| 17 | const ADDR: u32 = 0x10_0000; | ||
| 18 | |||
| 19 | // wait a bit before accessing the flash | ||
| 20 | Timer::after(Duration::from_millis(300)).await; | ||
| 21 | |||
| 22 | let mut f = Flash::unlock(p.FLASH); | ||
| 23 | |||
| 24 | info!("Reading..."); | ||
| 25 | let mut buf = [0u8; 32]; | ||
| 26 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 27 | info!("Read: {=[u8]:x}", buf); | ||
| 28 | |||
| 29 | info!("Erasing..."); | ||
| 30 | unwrap!(f.erase(ADDR, ADDR + 128 * 1024)); | ||
| 31 | |||
| 32 | info!("Reading..."); | ||
| 33 | let mut buf = [0u8; 32]; | ||
| 34 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 35 | info!("Read after erase: {=[u8]:x}", buf); | ||
| 36 | |||
| 37 | info!("Writing..."); | ||
| 38 | unwrap!(f.write( | ||
| 39 | ADDR, | ||
| 40 | &[ | ||
| 41 | 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, | ||
| 42 | 30, 31, 32 | ||
| 43 | ] | ||
| 44 | )); | ||
| 45 | |||
| 46 | info!("Reading..."); | ||
| 47 | let mut buf = [0u8; 32]; | ||
| 48 | unwrap!(f.read(ADDR, &mut buf)); | ||
| 49 | info!("Read: {=[u8]:x}", buf); | ||
| 50 | assert_eq!( | ||
| 51 | &buf[..], | ||
| 52 | &[ | ||
| 53 | 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, | ||
| 54 | 30, 31, 32 | ||
| 55 | ] | ||
| 56 | ); | ||
| 57 | } | ||
