aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/Cargo.toml1
-rw-r--r--examples/rp/src/bin/flash.rs89
2 files changed, 90 insertions, 0 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
index ec9896b77..31f688305 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.3" } 31embedded-hal-async = { version = "0.1.0-alpha.3" }
32embedded-io = { version = "0.3.1", features = ["async", "defmt"] } 32embedded-io = { version = "0.3.1", 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..8d6b379f4
--- /dev/null
+++ b/examples/rp/src/bin/flash.rs
@@ -0,0 +1,89 @@
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 embassy_time::{Duration, Timer};
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 // add some delay to give an attached debug probe time to parse the
21 // defmt RTT header. Reading that header might touch flash memory, which
22 // interferes with flash write operations.
23 // https://github.com/knurling-rs/defmt/pull/683
24 Timer::after(Duration::from_millis(10)).await;
25
26 let mut flash = embassy_rp::flash::Flash::<_, FLASH_SIZE>::new(p.FLASH);
27 erase_write_sector(&mut flash, 0x00);
28
29 multiwrite_bytes(&mut flash, ERASE_SIZE as u32);
30
31 loop {}
32}
33
34fn multiwrite_bytes(flash: &mut embassy_rp::flash::Flash<'_, FLASH, FLASH_SIZE>, offset: u32) {
35 info!(">>>> [multiwrite_bytes]");
36 let mut read_buf = [0u8; ERASE_SIZE];
37 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut read_buf));
38
39 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
40 info!("Contents start with {=[u8]}", read_buf[0..4]);
41
42 defmt::unwrap!(flash.erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
43
44 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut read_buf));
45 info!("Contents after erase starts with {=[u8]}", read_buf[0..4]);
46 if read_buf.iter().any(|x| *x != 0xFF) {
47 defmt::panic!("unexpected");
48 }
49
50 defmt::unwrap!(flash.write(ADDR_OFFSET + offset, &[0x01]));
51 defmt::unwrap!(flash.write(ADDR_OFFSET + offset + 1, &[0x02]));
52 defmt::unwrap!(flash.write(ADDR_OFFSET + offset + 2, &[0x03]));
53 defmt::unwrap!(flash.write(ADDR_OFFSET + offset + 3, &[0x04]));
54
55 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut read_buf));
56 info!("Contents after write starts with {=[u8]}", read_buf[0..4]);
57 if &read_buf[0..4] != &[0x01, 0x02, 0x03, 0x04] {
58 defmt::panic!("unexpected");
59 }
60}
61
62fn erase_write_sector(flash: &mut embassy_rp::flash::Flash<'_, FLASH, FLASH_SIZE>, offset: u32) {
63 info!(">>>> [erase_write_sector]");
64 let mut buf = [0u8; ERASE_SIZE];
65 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut buf));
66
67 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
68 info!("Contents start with {=[u8]}", buf[0..4]);
69
70 defmt::unwrap!(flash.erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
71
72 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut buf));
73 info!("Contents after erase starts with {=[u8]}", buf[0..4]);
74 if buf.iter().any(|x| *x != 0xFF) {
75 defmt::panic!("unexpected");
76 }
77
78 for b in buf.iter_mut() {
79 *b = 0xDA;
80 }
81
82 defmt::unwrap!(flash.write(ADDR_OFFSET + offset, &buf));
83
84 defmt::unwrap!(flash.read(ADDR_OFFSET + offset, &mut buf));
85 info!("Contents after write starts with {=[u8]}", buf[0..4]);
86 if buf.iter().any(|x| *x != 0xDA) {
87 defmt::panic!("unexpected");
88 }
89}