aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMathias <[email protected]>2022-10-26 10:01:52 +0200
committerMathias <[email protected]>2022-10-26 12:24:04 +0200
commit80e58426fcf40b6cea28368e43a7289e827461cf (patch)
tree48ce94c3404b3b95c082914376d5510658a18f38 /examples
parentad0eb3f4bd2b124fcb7cda6d6bd88e2b12632ea7 (diff)
Add flash example & flash HIL test
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/Cargo.toml1
-rw-r--r--examples/rp/src/bin/flash.rs84
2 files changed, 85 insertions, 0 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
index 747dde515..38355bbf8 100644
--- a/examples/rp/Cargo.toml
+++ b/examples/rp/Cargo.toml
@@ -30,6 +30,7 @@ byte-slice-cast = { version = "1.2.0", default-features = false }
30embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" } 30embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" }
31embedded-hal-async = { version = "0.1.0-alpha.1" } 31embedded-hal-async = { version = "0.1.0-alpha.1" }
32embedded-io = { version = "0.3.0", features = ["async", "defmt"] } 32embedded-io = { version = "0.3.0", features = ["async", "defmt"] }
33embedded-storage = { version = "0.3" }
33static_cell = "1.0.0" 34static_cell = "1.0.0"
34 35
35[profile.release] 36[profile.release]
diff --git a/examples/rp/src/bin/flash.rs b/examples/rp/src/bin/flash.rs
new file mode 100644
index 000000000..17549e4be
--- /dev/null
+++ b/examples/rp/src/bin/flash.rs
@@ -0,0 +1,84 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_rp::flash::{ERASE_SIZE, FLASH_BASE};
8use embassy_rp::peripherals::FLASH;
9use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
10use {defmt_rtt as _, panic_probe as _};
11
12const ADDR_OFFSET: u32 = 0x100000;
13const FLASH_SIZE: usize = 2 * 1024 * 1024;
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) {
17 let p = embassy_rp::init(Default::default());
18 info!("Hello World!");
19
20 let mut flash = embassy_rp::flash::Flash::<_, FLASH_SIZE>::new(p.FLASH);
21
22 erase_write_sector(&mut flash, 0x00);
23
24 multiwrite_bytes(&mut flash, ERASE_SIZE as u32);
25
26 loop {}
27}
28
29fn multiwrite_bytes(flash: &mut embassy_rp::flash::Flash<'_, FLASH, FLASH_SIZE>, offset: u32) {
30 info!(">>>> [multiwrite_bytes]");
31 let mut read_buf = [0u8; ERASE_SIZE];
32 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut read_buf));
33
34 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
35 info!("Contents start with {=[u8]}", read_buf[0..4]);
36
37 defmt::unwrap!(flash.erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
38
39 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut read_buf));
40 info!("Contents after erase starts with {=[u8]}", read_buf[0..4]);
41 if read_buf.iter().any(|x| *x != 0xFF) {
42 defmt::panic!("unexpected");
43 }
44
45 defmt::unwrap!(flash.write(ADDR_OFFSET + offset, &[0x01]));
46 defmt::unwrap!(flash.write(ADDR_OFFSET + offset + 1, &[0x02]));
47 defmt::unwrap!(flash.write(ADDR_OFFSET + offset + 2, &[0x03]));
48 defmt::unwrap!(flash.write(ADDR_OFFSET + offset + 3, &[0x04]));
49
50 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut read_buf));
51 info!("Contents after write starts with {=[u8]}", read_buf[0..4]);
52 if &read_buf[0..4] != &[0x01, 0x02, 0x03, 0x04] {
53 defmt::panic!("unexpected");
54 }
55}
56
57fn erase_write_sector(flash: &mut embassy_rp::flash::Flash<'_, FLASH, FLASH_SIZE>, offset: u32) {
58 info!(">>>> [erase_write_sector]");
59 let mut buf = [0u8; ERASE_SIZE];
60 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut buf));
61
62 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
63 info!("Contents start with {=[u8]}", buf[0..4]);
64
65 defmt::unwrap!(flash.erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
66
67 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut buf));
68 info!("Contents after erase starts with {=[u8]}", buf[0..4]);
69 if buf.iter().any(|x| *x != 0xFF) {
70 defmt::panic!("unexpected");
71 }
72
73 for b in buf.iter_mut() {
74 *b = 0xDA;
75 }
76
77 defmt::unwrap!(flash.write(ADDR_OFFSET + offset, &buf));
78
79 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut buf));
80 info!("Contents after write starts with {=[u8]}", buf[0..4]);
81 if buf.iter().any(|x| *x != 0xDA) {
82 defmt::panic!("unexpected");
83 }
84}