aboutsummaryrefslogtreecommitdiff
path: root/examples/rp235x/src/bin/flash.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rp235x/src/bin/flash.rs')
-rw-r--r--examples/rp235x/src/bin/flash.rs125
1 files changed, 125 insertions, 0 deletions
diff --git a/examples/rp235x/src/bin/flash.rs b/examples/rp235x/src/bin/flash.rs
new file mode 100644
index 000000000..31ad4aafc
--- /dev/null
+++ b/examples/rp235x/src/bin/flash.rs
@@ -0,0 +1,125 @@
1//! This example test the flash connected to the RP2350 chip.
2
3#![no_std]
4#![no_main]
5
6use defmt::*;
7use embassy_executor::Spawner;
8use embassy_rp::flash::{Async, ERASE_SIZE, FLASH_BASE};
9use embassy_rp::peripherals::FLASH;
10use embassy_time::Timer;
11use {defmt_rtt as _, panic_probe as _};
12
13const ADDR_OFFSET: u32 = 0x100000;
14const FLASH_SIZE: usize = 2 * 1024 * 1024;
15
16#[embassy_executor::main]
17async fn main(_spawner: Spawner) {
18 let p = embassy_rp::init(Default::default());
19 info!("Hello World!");
20
21 // add some delay to give an attached debug probe time to parse the
22 // defmt RTT header. Reading that header might touch flash memory, which
23 // interferes with flash write operations.
24 // https://github.com/knurling-rs/defmt/pull/683
25 Timer::after_millis(10).await;
26
27 let mut flash = embassy_rp::flash::Flash::<_, Async, FLASH_SIZE>::new(p.FLASH, p.DMA_CH0);
28
29 erase_write_sector(&mut flash, 0x00);
30
31 multiwrite_bytes(&mut flash, ERASE_SIZE as u32);
32
33 background_read(&mut flash, (ERASE_SIZE * 2) as u32).await;
34
35 info!("Flash Works!");
36}
37
38fn multiwrite_bytes(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) {
39 info!(">>>> [multiwrite_bytes]");
40 let mut read_buf = [0u8; ERASE_SIZE];
41 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut read_buf));
42
43 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
44 info!("Contents start with {=[u8]}", read_buf[0..4]);
45
46 defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
47
48 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut read_buf));
49 info!("Contents after erase starts with {=[u8]}", read_buf[0..4]);
50 if read_buf.iter().any(|x| *x != 0xFF) {
51 defmt::panic!("unexpected");
52 }
53
54 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset, &[0x01]));
55 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset + 1, &[0x02]));
56 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset + 2, &[0x03]));
57 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset + 3, &[0x04]));
58
59 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut read_buf));
60 info!("Contents after write starts with {=[u8]}", read_buf[0..4]);
61 if read_buf[0..4] != [0x01, 0x02, 0x03, 0x04] {
62 defmt::panic!("unexpected");
63 }
64}
65
66fn erase_write_sector(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) {
67 info!(">>>> [erase_write_sector]");
68 let mut buf = [0u8; ERASE_SIZE];
69 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut buf));
70
71 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
72 info!("Contents start with {=[u8]}", buf[0..4]);
73
74 defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
75
76 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut buf));
77 info!("Contents after erase starts with {=[u8]}", buf[0..4]);
78 if buf.iter().any(|x| *x != 0xFF) {
79 defmt::panic!("unexpected");
80 }
81
82 for b in buf.iter_mut() {
83 *b = 0xDA;
84 }
85
86 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset, &buf));
87
88 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut buf));
89 info!("Contents after write starts with {=[u8]}", buf[0..4]);
90 if buf.iter().any(|x| *x != 0xDA) {
91 defmt::panic!("unexpected");
92 }
93}
94
95async fn background_read(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) {
96 info!(">>>> [background_read]");
97
98 let mut buf = [0u32; 8];
99 defmt::unwrap!(flash.background_read(ADDR_OFFSET + offset, &mut buf)).await;
100
101 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
102 info!("Contents start with {=u32:x}", buf[0]);
103
104 defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
105
106 defmt::unwrap!(flash.background_read(ADDR_OFFSET + offset, &mut buf)).await;
107 info!("Contents after erase starts with {=u32:x}", buf[0]);
108 if buf.iter().any(|x| *x != 0xFFFFFFFF) {
109 defmt::panic!("unexpected");
110 }
111
112 for b in buf.iter_mut() {
113 *b = 0xDABA1234;
114 }
115
116 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset, unsafe {
117 core::slice::from_raw_parts(buf.as_ptr() as *const u8, buf.len() * 4)
118 }));
119
120 defmt::unwrap!(flash.background_read(ADDR_OFFSET + offset, &mut buf)).await;
121 info!("Contents after write starts with {=u32:x}", buf[0]);
122 if buf.iter().any(|x| *x != 0xDABA1234) {
123 defmt::panic!("unexpected");
124 }
125}