embassy/embassy-nrf-examples/src/bin/qspi.rs

145 lines
3.4 KiB
Rust
Raw Normal View History

2020-09-22 16:03:43 +00:00
#![no_std]
#![no_main]
2021-03-17 01:48:16 +00:00
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
2020-09-22 16:03:43 +00:00
#![feature(type_alias_impl_trait)]
#[path = "../example_common.rs"]
mod example_common;
use example_common::*;
use cortex_m_rt::entry;
2021-02-14 00:41:36 +00:00
use defmt::{assert_eq, panic};
2021-02-28 21:05:37 +00:00
use futures::pin_mut;
2020-09-22 16:03:43 +00:00
use nrf52840_hal::gpio;
use embassy::executor::{task, Executor};
2021-03-01 23:32:23 +00:00
use embassy::traits::flash::Flash;
2020-10-31 21:37:24 +00:00
use embassy::util::Forever;
2020-12-28 23:05:52 +00:00
use embassy_nrf::{interrupt, qspi};
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]);
2020-09-24 20:04:45 +00:00
#[task]
2020-09-22 16:03:43 +00:00
async fn run() {
let p = unwrap!(embassy_nrf::pac::Peripherals::take());
2020-09-22 16:03:43 +00:00
let port0 = gpio::p0::Parts::new(p.P0);
let pins = qspi::Pins {
csn: port0
.p0_17
.into_push_pull_output(gpio::Level::High)
.degrade(),
sck: port0
.p0_19
.into_push_pull_output(gpio::Level::High)
.degrade(),
io0: port0
.p0_20
.into_push_pull_output(gpio::Level::High)
.degrade(),
io1: port0
.p0_21
.into_push_pull_output(gpio::Level::High)
.degrade(),
io2: Some(
port0
.p0_22
.into_push_pull_output(gpio::Level::High)
.degrade(),
),
io3: Some(
port0
.p0_23
.into_push_pull_output(gpio::Level::High)
.degrade(),
),
};
let config = qspi::Config {
pins,
read_opcode: qspi::ReadOpcode::READ4IO,
write_opcode: qspi::WriteOpcode::PP4IO,
xip_offset: 0,
write_page_size: qspi::WritePageSize::_256BYTES,
2020-11-27 17:42:59 +00:00
deep_power_down: None,
2020-09-22 16:03:43 +00:00
};
2020-12-28 23:05:52 +00:00
let irq = interrupt::take!(QSPI);
2021-02-28 21:05:37 +00:00
let q = qspi::Qspi::new(p.QSPI, irq, config);
pin_mut!(q);
2020-09-22 16:03:43 +00:00
let mut id = [1; 3];
2021-02-28 21:05:37 +00:00
q.as_mut()
.custom_instruction(0x9F, &[], &mut id)
.await
.unwrap();
2021-02-24 07:57:06 +00:00
info!("id: {}", id);
2020-09-22 16:03:43 +00:00
// Read status register
let mut status = [0; 1];
2021-02-28 21:05:37 +00:00
q.as_mut()
.custom_instruction(0x05, &[], &mut status)
.await
.unwrap();
2020-09-22 16:03:43 +00:00
info!("status: {:?}", status[0]);
if status[0] & 0x40 == 0 {
status[0] |= 0x40;
2021-02-28 21:05:37 +00:00
q.as_mut()
.custom_instruction(0x01, &status, &mut [])
.await
.unwrap();
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);
2021-02-28 21:05:37 +00:00
q.as_mut().erase(i * PAGE_SIZE).await.unwrap();
2020-09-22 16:03:43 +00:00
for j in 0..PAGE_SIZE {
buf.0[j] = pattern((j + i * PAGE_SIZE) as u32);
}
info!("programming...");
2021-02-28 21:05:37 +00:00
q.as_mut().write(i * PAGE_SIZE, &buf.0).await.unwrap();
2020-09-22 16:03:43 +00:00
}
for i in 0..8 {
info!("page {:?}: reading... ", i);
2021-02-28 21:05:37 +00:00
q.as_mut().read(i * PAGE_SIZE, &mut buf.0).await.unwrap();
2020-09-22 16:03:43 +00:00
info!("verifying...");
for j in 0..PAGE_SIZE {
assert_eq!(buf.0[j], pattern((j + i * PAGE_SIZE) as u32));
}
}
info!("done!")
}
2020-10-31 21:37:24 +00:00
static EXECUTOR: Forever<Executor> = Forever::new();
2020-09-22 16:03:43 +00:00
#[entry]
fn main() -> ! {
info!("Hello World!");
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(run()));
});
2020-09-22 16:03:43 +00:00
}