2020-09-22 16:03:43 +00:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2022-04-02 02:35:06 +00:00
|
|
|
use defmt::{assert_eq, info, unwrap};
|
2022-08-17 21:40:16 +00:00
|
|
|
use embassy_executor::Spawner;
|
2023-03-05 01:55:00 +00:00
|
|
|
use embassy_nrf::qspi::Frequency;
|
2023-03-05 20:37:21 +00:00
|
|
|
use embassy_nrf::{bind_interrupts, peripherals, qspi};
|
2022-06-12 20:15:44 +00:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2020-09-24 20:04:45 +00:00
|
|
|
|
2020-09-22 16:03:43 +00:00
|
|
|
const PAGE_SIZE: usize = 4096;
|
|
|
|
|
|
|
|
// Workaround for alignment requirements.
|
|
|
|
// Nicer API will probably come in the future.
|
|
|
|
#[repr(C, align(4))]
|
|
|
|
struct AlignedBuf([u8; 4096]);
|
|
|
|
|
2023-03-05 20:37:21 +00:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
QSPI => qspi::InterruptHandler<peripherals::QSPI>;
|
|
|
|
});
|
|
|
|
|
2022-07-29 19:58:35 +00:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 16:49:55 +00:00
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
2021-05-26 22:42:46 +00:00
|
|
|
// Config for the MX25R64 present in the nRF52840 DK
|
|
|
|
let mut config = qspi::Config::default();
|
2023-03-05 01:55:00 +00:00
|
|
|
config.capacity = 8 * 1024 * 1024; // 8 MB
|
|
|
|
config.frequency = Frequency::M32;
|
2021-05-26 22:42:46 +00:00
|
|
|
config.read_opcode = qspi::ReadOpcode::READ4IO;
|
|
|
|
config.write_opcode = qspi::WriteOpcode::PP4IO;
|
|
|
|
config.write_page_size = qspi::WritePageSize::_256BYTES;
|
|
|
|
|
2023-03-05 01:33:02 +00:00
|
|
|
let mut q = qspi::Qspi::new(
|
2023-03-05 20:37:21 +00:00
|
|
|
p.QSPI, Irqs, p.P0_19, p.P0_17, p.P0_20, p.P0_21, p.P0_22, p.P0_23, config,
|
2022-04-19 12:39:59 +00:00
|
|
|
);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
let mut id = [1; 3];
|
2021-07-31 15:51:40 +00:00
|
|
|
unwrap!(q.custom_instruction(0x9F, &[], &mut id).await);
|
2021-02-24 07:57:06 +00:00
|
|
|
info!("id: {}", id);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
// Read status register
|
2021-03-21 19:54:09 +00:00
|
|
|
let mut status = [4; 1];
|
2021-07-31 15:51:40 +00:00
|
|
|
unwrap!(q.custom_instruction(0x05, &[], &mut status).await);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
info!("status: {:?}", status[0]);
|
|
|
|
|
|
|
|
if status[0] & 0x40 == 0 {
|
|
|
|
status[0] |= 0x40;
|
|
|
|
|
2021-07-31 15:51:40 +00:00
|
|
|
unwrap!(q.custom_instruction(0x01, &status, &mut []).await);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
info!("enabled quad in status");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut buf = AlignedBuf([0u8; PAGE_SIZE]);
|
|
|
|
|
|
|
|
let pattern = |a: u32| (a ^ (a >> 8) ^ (a >> 16) ^ (a >> 24)) as u8;
|
|
|
|
|
|
|
|
for i in 0..8 {
|
|
|
|
info!("page {:?}: erasing... ", i);
|
2023-03-05 01:24:52 +00:00
|
|
|
unwrap!(q.erase(i * PAGE_SIZE as u32).await);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
for j in 0..PAGE_SIZE {
|
2023-03-05 01:24:52 +00:00
|
|
|
buf.0[j] = pattern((j as u32 + i * PAGE_SIZE as u32) as u32);
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("programming...");
|
2023-03-05 01:24:52 +00:00
|
|
|
unwrap!(q.write(i * PAGE_SIZE as u32, &buf.0).await);
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..8 {
|
|
|
|
info!("page {:?}: reading... ", i);
|
2023-03-05 01:24:52 +00:00
|
|
|
unwrap!(q.read(i * PAGE_SIZE as u32, &mut buf.0).await);
|
2020-09-22 16:03:43 +00:00
|
|
|
|
|
|
|
info!("verifying...");
|
|
|
|
for j in 0..PAGE_SIZE {
|
2023-03-05 01:24:52 +00:00
|
|
|
assert_eq!(buf.0[j], pattern((j as u32 + i * PAGE_SIZE as u32) as u32));
|
2020-09-22 16:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("done!")
|
|
|
|
}
|