diff options
Diffstat (limited to 'examples/nrf/src/bin/qspi.rs')
| -rw-r--r-- | examples/nrf/src/bin/qspi.rs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/qspi.rs b/examples/nrf/src/bin/qspi.rs new file mode 100644 index 000000000..6e49887a4 --- /dev/null +++ b/examples/nrf/src/bin/qspi.rs | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(min_type_alias_impl_trait)] | ||
| 4 | #![feature(impl_trait_in_bindings)] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | #![allow(incomplete_features)] | ||
| 7 | |||
| 8 | #[path = "../example_common.rs"] | ||
| 9 | mod example_common; | ||
| 10 | |||
| 11 | use defmt::{assert_eq, panic}; | ||
| 12 | use embassy::executor::Spawner; | ||
| 13 | use embassy::traits::flash::Flash; | ||
| 14 | use embassy_nrf::Peripherals; | ||
| 15 | use embassy_nrf::{interrupt, qspi}; | ||
| 16 | use example_common::*; | ||
| 17 | |||
| 18 | const PAGE_SIZE: usize = 4096; | ||
| 19 | |||
| 20 | // Workaround for alignment requirements. | ||
| 21 | // Nicer API will probably come in the future. | ||
| 22 | #[repr(C, align(4))] | ||
| 23 | struct AlignedBuf([u8; 4096]); | ||
| 24 | |||
| 25 | #[embassy::main] | ||
| 26 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 27 | // Config for the MX25R64 present in the nRF52840 DK | ||
| 28 | let mut config = qspi::Config::default(); | ||
| 29 | config.read_opcode = qspi::ReadOpcode::READ4IO; | ||
| 30 | config.write_opcode = qspi::WriteOpcode::PP4IO; | ||
| 31 | config.write_page_size = qspi::WritePageSize::_256BYTES; | ||
| 32 | |||
| 33 | let irq = interrupt::take!(QSPI); | ||
| 34 | let mut q = qspi::Qspi::new( | ||
| 35 | p.QSPI, irq, p.P0_19, p.P0_17, p.P0_20, p.P0_21, p.P0_22, p.P0_23, config, | ||
| 36 | ) | ||
| 37 | .await; | ||
| 38 | |||
| 39 | let mut id = [1; 3]; | ||
| 40 | q.custom_instruction(0x9F, &[], &mut id).await.unwrap(); | ||
| 41 | info!("id: {}", id); | ||
| 42 | |||
| 43 | // Read status register | ||
| 44 | let mut status = [4; 1]; | ||
| 45 | q.custom_instruction(0x05, &[], &mut status).await.unwrap(); | ||
| 46 | |||
| 47 | info!("status: {:?}", status[0]); | ||
| 48 | |||
| 49 | if status[0] & 0x40 == 0 { | ||
| 50 | status[0] |= 0x40; | ||
| 51 | |||
| 52 | q.custom_instruction(0x01, &status, &mut []).await.unwrap(); | ||
| 53 | |||
| 54 | info!("enabled quad in status"); | ||
| 55 | } | ||
| 56 | |||
| 57 | let mut buf = AlignedBuf([0u8; PAGE_SIZE]); | ||
| 58 | |||
| 59 | let pattern = |a: u32| (a ^ (a >> 8) ^ (a >> 16) ^ (a >> 24)) as u8; | ||
| 60 | |||
| 61 | for i in 0..8 { | ||
| 62 | info!("page {:?}: erasing... ", i); | ||
| 63 | q.erase(i * PAGE_SIZE).await.unwrap(); | ||
| 64 | |||
| 65 | for j in 0..PAGE_SIZE { | ||
| 66 | buf.0[j] = pattern((j + i * PAGE_SIZE) as u32); | ||
| 67 | } | ||
| 68 | |||
| 69 | info!("programming..."); | ||
| 70 | q.write(i * PAGE_SIZE, &buf.0).await.unwrap(); | ||
| 71 | } | ||
| 72 | |||
| 73 | for i in 0..8 { | ||
| 74 | info!("page {:?}: reading... ", i); | ||
| 75 | q.read(i * PAGE_SIZE, &mut buf.0).await.unwrap(); | ||
| 76 | |||
| 77 | info!("verifying..."); | ||
| 78 | for j in 0..PAGE_SIZE { | ||
| 79 | assert_eq!(buf.0[j], pattern((j + i * PAGE_SIZE) as u32)); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | info!("done!") | ||
| 84 | } | ||
