aboutsummaryrefslogtreecommitdiff
path: root/examples/rp23/src/bin/flash.rs
diff options
context:
space:
mode:
authorCaleb Jamison <[email protected]>2024-08-07 23:20:26 -0400
committerCaleb Jamison <[email protected]>2024-08-08 21:35:21 -0400
commitb185e02a42ad751ec6c31ffa6a1b87503f15489d (patch)
tree0f0c66747267d24d95b5957b22db7e5c525cb00e /examples/rp23/src/bin/flash.rs
parent891c5ee10584cd990dad529e3506fe1328e4e69d (diff)
Initial rp235x support
Examples have been run, but there is not yet a test suite.
Diffstat (limited to 'examples/rp23/src/bin/flash.rs')
-rw-r--r--examples/rp23/src/bin/flash.rs150
1 files changed, 150 insertions, 0 deletions
diff --git a/examples/rp23/src/bin/flash.rs b/examples/rp23/src/bin/flash.rs
new file mode 100644
index 000000000..42620ca93
--- /dev/null
+++ b/examples/rp23/src/bin/flash.rs
@@ -0,0 +1,150 @@
1//! This example test the flash connected to the RP2040 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 _};
12use embassy_rp::block::ImageDef;
13
14#[link_section = ".start_block"]
15#[used]
16pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe();
17
18// Program metadata for `picotool info`
19#[link_section = ".bi_entries"]
20#[used]
21pub static PICOTOOL_ENTRIES: [embassy_rp::binary_info::EntryAddr; 4] = [
22 embassy_rp::binary_info_rp_cargo_bin_name!(),
23 embassy_rp::binary_info_rp_cargo_version!(),
24 embassy_rp::binary_info_rp_program_description!(c"Blinky"),
25 embassy_rp::binary_info_rp_program_build_attribute!(),
26];
27
28
29const ADDR_OFFSET: u32 = 0x100000;
30const FLASH_SIZE: usize = 2 * 1024 * 1024;
31
32#[embassy_executor::main]
33async fn main(_spawner: Spawner) {
34 let p = embassy_rp::init(Default::default());
35 info!("Hello World!");
36
37 // add some delay to give an attached debug probe time to parse the
38 // defmt RTT header. Reading that header might touch flash memory, which
39 // interferes with flash write operations.
40 // https://github.com/knurling-rs/defmt/pull/683
41 Timer::after_millis(10).await;
42
43 let mut flash = embassy_rp::flash::Flash::<_, Async, FLASH_SIZE>::new(p.FLASH, p.DMA_CH0);
44
45 // Get JEDEC id
46 let jedec = flash.blocking_jedec_id().unwrap();
47 info!("jedec id: 0x{:x}", jedec);
48
49 // Get unique id
50 let mut uid = [0; 8];
51 flash.blocking_unique_id(&mut uid).unwrap();
52 info!("unique id: {:?}", uid);
53
54 erase_write_sector(&mut flash, 0x00);
55
56 multiwrite_bytes(&mut flash, ERASE_SIZE as u32);
57
58 background_read(&mut flash, (ERASE_SIZE * 2) as u32).await;
59
60 loop {}
61}
62
63fn multiwrite_bytes(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) {
64 info!(">>>> [multiwrite_bytes]");
65 let mut read_buf = [0u8; ERASE_SIZE];
66 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut read_buf));
67
68 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
69 info!("Contents start with {=[u8]}", read_buf[0..4]);
70
71 defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
72
73 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut read_buf));
74 info!("Contents after erase starts with {=[u8]}", read_buf[0..4]);
75 if read_buf.iter().any(|x| *x != 0xFF) {
76 defmt::panic!("unexpected");
77 }
78
79 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset, &[0x01]));
80 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset + 1, &[0x02]));
81 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset + 2, &[0x03]));
82 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset + 3, &[0x04]));
83
84 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut read_buf));
85 info!("Contents after write starts with {=[u8]}", read_buf[0..4]);
86 if &read_buf[0..4] != &[0x01, 0x02, 0x03, 0x04] {
87 defmt::panic!("unexpected");
88 }
89}
90
91fn erase_write_sector(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) {
92 info!(">>>> [erase_write_sector]");
93 let mut buf = [0u8; ERASE_SIZE];
94 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut buf));
95
96 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
97 info!("Contents start with {=[u8]}", buf[0..4]);
98
99 defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
100
101 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut buf));
102 info!("Contents after erase starts with {=[u8]}", buf[0..4]);
103 if buf.iter().any(|x| *x != 0xFF) {
104 defmt::panic!("unexpected");
105 }
106
107 for b in buf.iter_mut() {
108 *b = 0xDA;
109 }
110
111 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset, &buf));
112
113 defmt::unwrap!(flash.blocking_read(ADDR_OFFSET + offset, &mut buf));
114 info!("Contents after write starts with {=[u8]}", buf[0..4]);
115 if buf.iter().any(|x| *x != 0xDA) {
116 defmt::panic!("unexpected");
117 }
118}
119
120async fn background_read(flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, offset: u32) {
121 info!(">>>> [background_read]");
122
123 let mut buf = [0u32; 8];
124 defmt::unwrap!(flash.background_read(ADDR_OFFSET + offset, &mut buf)).await;
125
126 info!("Addr of flash block is {:x}", ADDR_OFFSET + offset + FLASH_BASE as u32);
127 info!("Contents start with {=u32:x}", buf[0]);
128
129 defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET + offset, ADDR_OFFSET + offset + ERASE_SIZE as u32));
130
131 defmt::unwrap!(flash.background_read(ADDR_OFFSET + offset, &mut buf)).await;
132 info!("Contents after erase starts with {=u32:x}", buf[0]);
133 if buf.iter().any(|x| *x != 0xFFFFFFFF) {
134 defmt::panic!("unexpected");
135 }
136
137 for b in buf.iter_mut() {
138 *b = 0xDABA1234;
139 }
140
141 defmt::unwrap!(flash.blocking_write(ADDR_OFFSET + offset, unsafe {
142 core::slice::from_raw_parts(buf.as_ptr() as *const u8, buf.len() * 4)
143 }));
144
145 defmt::unwrap!(flash.background_read(ADDR_OFFSET + offset, &mut buf)).await;
146 info!("Contents after write starts with {=u32:x}", buf[0]);
147 if buf.iter().any(|x| *x != 0xDABA1234) {
148 defmt::panic!("unexpected");
149 }
150}