diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-10-28 12:19:56 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-10-28 12:19:56 +0000 |
| commit | e7fdd500d8354a03fcd105c8298cf7b4798a4107 (patch) | |
| tree | 8f0ccc58ed21a79a5b70fd38e57c8c2f30ebc00c /examples | |
| parent | 1f246d0e37f9044d8949358081b75e3cca0c4800 (diff) | |
| parent | bc21b6efafe607e6ed582b048baedb7803483ee7 (diff) | |
Merge #951
951: (embassy-rp): Implementation of generic flash mutation access r=Dirbaio a=MathiasKoch
I have attempted to utilize the work done in `rp2040-flash` by implementing `embedded-storage` traits on top, for RP2040.
Concerns:
1. ~~Should the DMA be paused where I have put a FIXME note? `DMA_CHx.ctrl_trig().write(|w| { w.set_en(false) })`? If so, how to properly do that without have control over the peripheral for the DMA channels? And if so, I assume we should only re-enable/unpause the ones that were enabled before?~~
2. ~~Should I make sure core2 is halted as part of this code? I am not sure if https://github.com/jannic/rp2040-flash/blob/ea8ab1ac807a7ab2b28a18bb5ca2e42495bb744d/examples/flash_example.rs#L103-L109 is heavy/slow code to run?~~
3. ~~Any good way of making this configurable over `FLASH_SIZE`, `WRITE_SIZE` and `ERASE_SIZE` without doing it as generics or parameters, as those make it possible to do differing configs throughout the same program, which feels wrong? Preferably, a compile-time option?~~
**EDIT:**
I have implemented the flash API here under the assumption that all external QSPI nor flashes are infact `Multiwrite` capable, as this makes it possible to use the ROM function for writes of 1 bytes at a time.
I have also added a HIL test for this, but because HIL tests are running 100% from RAM and I wanted to make sure it still works when running from flash, I have also added an example testing erase/write cycles of entire sectors, as well as single bytes in multi-write style.
Ping `@Dirbaio`
Co-authored-by: Mathias <[email protected]>
Co-authored-by: Vincent Stakenburg <[email protected]>
Co-authored-by: Joakim Hulthe <[email protected]>
Co-authored-by: Alex Martens <[email protected]>
Co-authored-by: Ulf Lilleengen <[email protected]>
Co-authored-by: Dario Nieuwenhuis <[email protected]>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/rp/src/bin/flash.rs | 89 |
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 } | |||
| 30 | embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" } | 30 | embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" } |
| 31 | embedded-hal-async = { version = "0.1.0-alpha.3" } | 31 | embedded-hal-async = { version = "0.1.0-alpha.3" } |
| 32 | embedded-io = { version = "0.3.1", features = ["async", "defmt"] } | 32 | embedded-io = { version = "0.3.1", features = ["async", "defmt"] } |
| 33 | embedded-storage = { version = "0.3" } | ||
| 33 | static_cell = "1.0.0" | 34 | static_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 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_rp::flash::{ERASE_SIZE, FLASH_BASE}; | ||
| 8 | use embassy_rp::peripherals::FLASH; | ||
| 9 | use embassy_time::{Duration, Timer}; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | const ADDR_OFFSET: u32 = 0x100000; | ||
| 13 | const FLASH_SIZE: usize = 2 * 1024 * 1024; | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async 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 | |||
| 34 | fn 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 | |||
| 62 | fn 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 | } | ||
