2023-02-01 00:48:33 +01:00
|
|
|
//! Quad Serial Peripheral Interface (QSPI) flash driver.
|
|
|
|
|
2021-05-11 03:04:59 +02:00
|
|
|
#![macro_use]
|
|
|
|
|
2022-09-22 16:42:49 +02:00
|
|
|
use core::future::poll_fn;
|
2023-03-05 21:37:21 +01:00
|
|
|
use core::marker::PhantomData;
|
2021-05-26 23:26:07 +02:00
|
|
|
use core::ptr;
|
2021-02-28 22:05:37 +01:00
|
|
|
use core::task::Poll;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
use embassy_hal_common::drop::OnDrop;
|
2022-07-23 14:00:19 +02:00
|
|
|
use embassy_hal_common::{into_ref, PeripheralRef};
|
2023-03-05 02:33:02 +01:00
|
|
|
use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash};
|
2021-03-08 00:15:40 +01:00
|
|
|
|
2021-05-26 23:26:07 +02:00
|
|
|
use crate::gpio::{self, Pin as GpioPin};
|
2023-06-01 02:22:46 +02:00
|
|
|
use crate::interrupt::{self, Interrupt};
|
2022-06-12 22:15:44 +02:00
|
|
|
pub use crate::pac::qspi::ifconfig0::{
|
|
|
|
ADDRMODE_A as AddressMode, PPSIZE_A as WritePageSize, READOC_A as ReadOpcode, WRITEOC_A as WriteOpcode,
|
|
|
|
};
|
2022-08-01 12:54:49 +02:00
|
|
|
pub use crate::pac::qspi::ifconfig1::SPIMODE_A as SpiMode;
|
2023-03-05 21:37:21 +01:00
|
|
|
use crate::Peripheral;
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Deep power-down config.
|
2020-11-27 18:42:59 +01:00
|
|
|
pub struct DeepPowerDownConfig {
|
2021-05-26 23:26:07 +02:00
|
|
|
/// Time required for entering DPM, in units of 16us
|
2020-11-27 18:42:59 +01:00
|
|
|
pub enter_time: u16,
|
2021-05-26 23:26:07 +02:00
|
|
|
/// Time required for exiting DPM, in units of 16us
|
2020-11-27 18:42:59 +01:00
|
|
|
pub exit_time: u16,
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// QSPI bus frequency.
|
2022-08-01 12:54:49 +02:00
|
|
|
pub enum Frequency {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 32 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M32 = 0,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 16 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M16 = 1,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 10.7 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M10_7 = 2,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 8 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M8 = 3,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 6.4 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M6_4 = 4,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 5.3 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M5_3 = 5,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 4.6 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M4_6 = 6,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 4 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M4 = 7,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 3.6 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M3_6 = 8,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 3.2 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M3_2 = 9,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 2.9 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M2_9 = 10,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 2.7 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M2_7 = 11,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 2.5 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M2_5 = 12,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 2.3 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M2_3 = 13,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 2.1 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M2_1 = 14,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// 2 Mhz
|
2022-08-01 12:54:49 +02:00
|
|
|
M2 = 15,
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// QSPI config.
|
2021-03-29 00:55:05 +02:00
|
|
|
#[non_exhaustive]
|
2020-09-22 18:03:43 +02:00
|
|
|
pub struct Config {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// XIP offset.
|
2020-09-22 18:03:43 +02:00
|
|
|
pub xip_offset: u32,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Opcode used for read operations.
|
2020-09-22 18:03:43 +02:00
|
|
|
pub read_opcode: ReadOpcode,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Opcode used for write operations.
|
2020-09-22 18:03:43 +02:00
|
|
|
pub write_opcode: WriteOpcode,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Page size for write operations.
|
2020-09-22 18:03:43 +02:00
|
|
|
pub write_page_size: WritePageSize,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Configuration for deep power down. If None, deep power down is disabled.
|
2020-11-27 18:42:59 +01:00
|
|
|
pub deep_power_down: Option<DeepPowerDownConfig>,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// QSPI bus frequency.
|
2022-08-01 12:54:49 +02:00
|
|
|
pub frequency: Frequency,
|
|
|
|
/// Value is specified in number of 16 MHz periods (62.5 ns)
|
|
|
|
pub sck_delay: u8,
|
|
|
|
/// Whether data is captured on the clock rising edge and data is output on a falling edge (MODE0) or vice-versa (MODE3)
|
|
|
|
pub spi_mode: SpiMode,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Addressing mode (24-bit or 32-bit)
|
2022-08-01 12:54:49 +02:00
|
|
|
pub address_mode: AddressMode,
|
2023-03-05 02:33:02 +01:00
|
|
|
/// Flash memory capacity in bytes. This is the value reported by the `embedded-storage` traits.
|
|
|
|
pub capacity: u32,
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
|
|
|
|
2021-03-29 00:55:05 +02:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
read_opcode: ReadOpcode::READ4IO,
|
|
|
|
write_opcode: WriteOpcode::PP4IO,
|
|
|
|
xip_offset: 0,
|
|
|
|
write_page_size: WritePageSize::_256BYTES,
|
|
|
|
deep_power_down: None,
|
2022-08-01 12:54:49 +02:00
|
|
|
frequency: Frequency::M8,
|
|
|
|
sck_delay: 80,
|
|
|
|
spi_mode: SpiMode::MODE0,
|
|
|
|
address_mode: AddressMode::_24BIT,
|
2023-03-05 02:33:02 +01:00
|
|
|
capacity: 0,
|
2021-03-29 00:55:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Error
|
2022-01-13 23:05:31 +01:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum Error {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Operation address was out of bounds.
|
2022-04-19 14:39:59 +02:00
|
|
|
OutOfBounds,
|
2022-01-13 23:05:31 +01:00
|
|
|
// TODO add "not in data memory" error and check for it
|
|
|
|
}
|
|
|
|
|
2023-03-05 21:37:21 +01:00
|
|
|
/// Interrupt handler.
|
|
|
|
pub struct InterruptHandler<T: Instance> {
|
|
|
|
_phantom: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Instance> interrupt::Handler<T::Interrupt> for InterruptHandler<T> {
|
|
|
|
unsafe fn on_interrupt() {
|
|
|
|
let r = T::regs();
|
|
|
|
let s = T::state();
|
|
|
|
|
|
|
|
if r.events_ready.read().bits() != 0 {
|
|
|
|
s.waker.wake();
|
|
|
|
r.intenclr.write(|w| w.ready().clear());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// QSPI flash driver.
|
2023-03-05 02:33:02 +01:00
|
|
|
pub struct Qspi<'d, T: Instance> {
|
2023-03-05 21:37:21 +01:00
|
|
|
_peri: PeripheralRef<'d, T>,
|
2021-05-26 23:26:07 +02:00
|
|
|
dpm_enabled: bool,
|
2023-03-05 02:33:02 +01:00
|
|
|
capacity: u32,
|
2021-02-28 22:05:37 +01:00
|
|
|
}
|
|
|
|
|
2023-03-05 02:33:02 +01:00
|
|
|
impl<'d, T: Instance> Qspi<'d, T> {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Create a new QSPI driver.
|
2022-04-19 14:39:59 +02:00
|
|
|
pub fn new(
|
2023-03-05 21:37:21 +01:00
|
|
|
qspi: impl Peripheral<P = T> + 'd,
|
|
|
|
_irq: impl interrupt::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
sck: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
csn: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
io0: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
io1: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
io2: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
io3: impl Peripheral<P = impl GpioPin> + 'd,
|
2021-03-21 20:54:09 +01:00
|
|
|
config: Config,
|
2023-03-05 02:33:02 +01:00
|
|
|
) -> Self {
|
2023-03-05 21:37:21 +01:00
|
|
|
into_ref!(qspi, sck, csn, io0, io1, io2, io3);
|
2021-03-21 20:54:09 +01:00
|
|
|
|
2021-04-14 17:00:28 +02:00
|
|
|
let r = T::regs();
|
2021-03-21 20:54:09 +01:00
|
|
|
|
2023-02-23 22:36:10 +01:00
|
|
|
macro_rules! config_pin {
|
|
|
|
($pin:ident) => {
|
|
|
|
$pin.set_high();
|
|
|
|
$pin.conf().write(|w| {
|
|
|
|
w.dir().output();
|
|
|
|
w.drive().h0h1();
|
|
|
|
#[cfg(feature = "_nrf5340-s")]
|
|
|
|
w.mcusel().peripheral();
|
|
|
|
w
|
|
|
|
});
|
|
|
|
r.psel.$pin.write(|w| unsafe { w.bits($pin.psel_bits()) });
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
config_pin!(sck);
|
|
|
|
config_pin!(csn);
|
|
|
|
config_pin!(io0);
|
|
|
|
config_pin!(io1);
|
|
|
|
config_pin!(io2);
|
|
|
|
config_pin!(io3);
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2021-05-26 23:26:07 +02:00
|
|
|
r.ifconfig0.write(|w| {
|
2022-08-01 12:54:49 +02:00
|
|
|
w.addrmode().variant(config.address_mode);
|
2021-05-26 23:26:07 +02:00
|
|
|
w.dpmenable().bit(config.deep_power_down.is_some());
|
|
|
|
w.ppsize().variant(config.write_page_size);
|
|
|
|
w.readoc().variant(config.read_opcode);
|
|
|
|
w.writeoc().variant(config.write_opcode);
|
2020-09-22 18:03:43 +02:00
|
|
|
w
|
|
|
|
});
|
|
|
|
|
2020-11-27 18:42:59 +01:00
|
|
|
if let Some(dpd) = &config.deep_power_down {
|
2021-05-26 23:26:07 +02:00
|
|
|
r.dpmdur.write(|w| unsafe {
|
|
|
|
w.enter().bits(dpd.enter_time);
|
|
|
|
w.exit().bits(dpd.exit_time);
|
2020-11-27 18:42:59 +01:00
|
|
|
w
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-26 23:26:07 +02:00
|
|
|
r.ifconfig1.write(|w| unsafe {
|
2022-08-01 12:54:49 +02:00
|
|
|
w.sckdelay().bits(config.sck_delay);
|
2021-05-26 23:26:07 +02:00
|
|
|
w.dpmen().exit();
|
2022-08-01 12:54:49 +02:00
|
|
|
w.spimode().variant(config.spi_mode);
|
|
|
|
w.sckfreq().bits(config.frequency as u8);
|
2021-05-26 23:26:07 +02:00
|
|
|
w
|
|
|
|
});
|
|
|
|
|
|
|
|
r.xipoffset.write(|w| unsafe {
|
|
|
|
w.xipoffset().bits(config.xip_offset);
|
2020-09-22 18:03:43 +02:00
|
|
|
w
|
|
|
|
});
|
|
|
|
|
2023-06-01 02:22:46 +02:00
|
|
|
T::Interrupt::unpend();
|
|
|
|
unsafe { T::Interrupt::enable() };
|
2020-09-22 18:03:43 +02:00
|
|
|
|
|
|
|
// Enable it
|
2021-03-21 20:54:09 +01:00
|
|
|
r.enable.write(|w| w.enable().enabled());
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
let res = Self {
|
2023-03-05 21:37:21 +01:00
|
|
|
_peri: qspi,
|
2021-05-26 23:26:07 +02:00
|
|
|
dpm_enabled: config.deep_power_down.is_some(),
|
2023-03-05 02:33:02 +01:00
|
|
|
capacity: config.capacity,
|
2021-05-26 23:26:07 +02:00
|
|
|
};
|
|
|
|
|
2021-03-21 20:54:09 +01:00
|
|
|
r.events_ready.reset();
|
2021-05-26 23:26:07 +02:00
|
|
|
r.intenset.write(|w| w.ready().set());
|
|
|
|
|
2021-03-21 20:54:09 +01:00
|
|
|
r.tasks_activate.write(|w| w.tasks_activate().bit(true));
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
Self::blocking_wait_ready();
|
2021-04-14 17:00:28 +02:00
|
|
|
|
2021-05-26 23:26:07 +02:00
|
|
|
res
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Do a custom QSPI instruction.
|
2022-06-12 22:15:44 +02:00
|
|
|
pub async fn custom_instruction(&mut self, opcode: u8, req: &[u8], resp: &mut [u8]) -> Result<(), Error> {
|
2023-02-10 22:47:17 +01:00
|
|
|
let ondrop = OnDrop::new(Self::blocking_wait_ready);
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2022-04-19 14:39:59 +02:00
|
|
|
let len = core::cmp::max(req.len(), resp.len()) as u8;
|
|
|
|
self.custom_instruction_start(opcode, req, len)?;
|
|
|
|
|
|
|
|
self.wait_ready().await;
|
|
|
|
|
|
|
|
self.custom_instruction_finish(resp)?;
|
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
ondrop.defuse();
|
2022-04-19 14:39:59 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Do a custom QSPI instruction, blocking version.
|
2022-06-12 22:15:44 +02:00
|
|
|
pub fn blocking_custom_instruction(&mut self, opcode: u8, req: &[u8], resp: &mut [u8]) -> Result<(), Error> {
|
2022-04-19 14:39:59 +02:00
|
|
|
let len = core::cmp::max(req.len(), resp.len()) as u8;
|
|
|
|
self.custom_instruction_start(opcode, req, len)?;
|
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
Self::blocking_wait_ready();
|
2022-04-19 14:39:59 +02:00
|
|
|
|
|
|
|
self.custom_instruction_finish(resp)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn custom_instruction_start(&mut self, opcode: u8, req: &[u8], len: u8) -> Result<(), Error> {
|
2021-02-14 01:41:36 +01:00
|
|
|
assert!(req.len() <= 8);
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2021-02-14 01:41:36 +01:00
|
|
|
let mut dat0: u32 = 0;
|
|
|
|
let mut dat1: u32 = 0;
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2021-02-14 01:41:36 +01:00
|
|
|
for i in 0..4 {
|
|
|
|
if i < req.len() {
|
|
|
|
dat0 |= (req[i] as u32) << (i * 8);
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
2021-02-14 01:41:36 +01:00
|
|
|
}
|
|
|
|
for i in 0..4 {
|
|
|
|
if i + 4 < req.len() {
|
|
|
|
dat1 |= (req[i + 4] as u32) << (i * 8);
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
2021-02-14 01:41:36 +01:00
|
|
|
}
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2021-04-14 17:00:28 +02:00
|
|
|
let r = T::regs();
|
2021-03-21 20:54:09 +01:00
|
|
|
r.cinstrdat0.write(|w| unsafe { w.bits(dat0) });
|
|
|
|
r.cinstrdat1.write(|w| unsafe { w.bits(dat1) });
|
|
|
|
|
|
|
|
r.events_ready.reset();
|
|
|
|
r.intenset.write(|w| w.ready().set());
|
|
|
|
|
|
|
|
r.cinstrconf.write(|w| {
|
|
|
|
let w = unsafe { w.opcode().bits(opcode) };
|
|
|
|
let w = unsafe { w.length().bits(len + 1) };
|
|
|
|
let w = w.lio2().bit(true);
|
|
|
|
let w = w.lio3().bit(true);
|
|
|
|
let w = w.wipwait().bit(true);
|
|
|
|
let w = w.wren().bit(true);
|
|
|
|
let w = w.lfen().bit(false);
|
|
|
|
let w = w.lfstop().bit(false);
|
|
|
|
w
|
2021-02-14 01:41:36 +01:00
|
|
|
});
|
2022-04-19 14:39:59 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-22 18:03:43 +02:00
|
|
|
|
2022-04-19 14:39:59 +02:00
|
|
|
fn custom_instruction_finish(&mut self, resp: &mut [u8]) -> Result<(), Error> {
|
2021-04-14 17:00:28 +02:00
|
|
|
let r = T::regs();
|
2021-03-21 20:54:09 +01:00
|
|
|
|
|
|
|
let dat0 = r.cinstrdat0.read().bits();
|
|
|
|
let dat1 = r.cinstrdat1.read().bits();
|
|
|
|
for i in 0..4 {
|
|
|
|
if i < resp.len() {
|
|
|
|
resp[i] = (dat0 >> (i * 8)) as u8;
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
2021-03-21 20:54:09 +01:00
|
|
|
}
|
|
|
|
for i in 0..4 {
|
|
|
|
if i + 4 < resp.len() {
|
|
|
|
resp[i] = (dat1 >> (i * 8)) as u8;
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
2021-03-21 20:54:09 +01:00
|
|
|
}
|
2021-02-14 01:41:36 +01:00
|
|
|
Ok(())
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
2021-02-28 22:05:37 +01:00
|
|
|
|
2021-04-14 17:00:28 +02:00
|
|
|
async fn wait_ready(&mut self) {
|
2021-02-28 22:05:37 +01:00
|
|
|
poll_fn(move |cx| {
|
2021-04-14 17:00:28 +02:00
|
|
|
let r = T::regs();
|
|
|
|
let s = T::state();
|
2023-03-05 21:37:21 +01:00
|
|
|
s.waker.register(cx.waker());
|
2021-03-21 20:54:09 +01:00
|
|
|
if r.events_ready.read().bits() != 0 {
|
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
Poll::Pending
|
2021-02-28 22:05:37 +01:00
|
|
|
})
|
2021-03-21 20:54:09 +01:00
|
|
|
.await
|
2021-02-28 22:05:37 +01:00
|
|
|
}
|
2022-01-13 23:05:31 +01:00
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
fn blocking_wait_ready() {
|
2022-04-19 14:39:59 +02:00
|
|
|
loop {
|
|
|
|
let r = T::regs();
|
|
|
|
if r.events_ready.read().bits() != 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-13 23:05:31 +01:00
|
|
|
|
2023-03-05 02:24:52 +01:00
|
|
|
fn start_read(&mut self, address: u32, data: &mut [u8]) -> Result<(), Error> {
|
2023-03-05 02:55:00 +01:00
|
|
|
// TODO: Return these as errors instead.
|
2022-01-13 23:05:31 +01:00
|
|
|
assert_eq!(data.as_ptr() as u32 % 4, 0);
|
|
|
|
assert_eq!(data.len() as u32 % 4, 0);
|
2023-03-05 02:24:52 +01:00
|
|
|
assert_eq!(address % 4, 0);
|
2022-01-13 23:05:31 +01:00
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
|
2023-03-05 02:24:52 +01:00
|
|
|
r.read.src.write(|w| unsafe { w.src().bits(address) });
|
2022-06-12 22:15:44 +02:00
|
|
|
r.read.dst.write(|w| unsafe { w.dst().bits(data.as_ptr() as u32) });
|
|
|
|
r.read.cnt.write(|w| unsafe { w.cnt().bits(data.len() as u32) });
|
2022-01-13 23:05:31 +01:00
|
|
|
|
|
|
|
r.events_ready.reset();
|
|
|
|
r.intenset.write(|w| w.ready().set());
|
|
|
|
r.tasks_readstart.write(|w| w.tasks_readstart().bit(true));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:24:52 +01:00
|
|
|
fn start_write(&mut self, address: u32, data: &[u8]) -> Result<(), Error> {
|
2023-03-05 02:55:00 +01:00
|
|
|
// TODO: Return these as errors instead.
|
2022-01-13 23:05:31 +01:00
|
|
|
assert_eq!(data.as_ptr() as u32 % 4, 0);
|
|
|
|
assert_eq!(data.len() as u32 % 4, 0);
|
2023-03-05 02:24:52 +01:00
|
|
|
assert_eq!(address % 4, 0);
|
2022-01-13 23:05:31 +01:00
|
|
|
|
|
|
|
let r = T::regs();
|
2022-06-12 22:15:44 +02:00
|
|
|
r.write.src.write(|w| unsafe { w.src().bits(data.as_ptr() as u32) });
|
2023-03-05 02:24:52 +01:00
|
|
|
r.write.dst.write(|w| unsafe { w.dst().bits(address) });
|
2022-06-12 22:15:44 +02:00
|
|
|
r.write.cnt.write(|w| unsafe { w.cnt().bits(data.len() as u32) });
|
2022-01-13 23:05:31 +01:00
|
|
|
|
|
|
|
r.events_ready.reset();
|
|
|
|
r.intenset.write(|w| w.ready().set());
|
|
|
|
r.tasks_writestart.write(|w| w.tasks_writestart().bit(true));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:24:52 +01:00
|
|
|
fn start_erase(&mut self, address: u32) -> Result<(), Error> {
|
2023-03-05 02:55:00 +01:00
|
|
|
// TODO: Return these as errors instead.
|
2023-03-05 02:24:52 +01:00
|
|
|
assert_eq!(address % 4096, 0);
|
2022-01-13 23:05:31 +01:00
|
|
|
|
|
|
|
let r = T::regs();
|
2023-03-05 02:24:52 +01:00
|
|
|
r.erase.ptr.write(|w| unsafe { w.ptr().bits(address) });
|
2022-01-13 23:05:31 +01:00
|
|
|
r.erase.len.write(|w| w.len()._4kb());
|
|
|
|
|
|
|
|
r.events_ready.reset();
|
|
|
|
r.intenset.write(|w| w.ready().set());
|
|
|
|
r.tasks_erasestart.write(|w| w.tasks_erasestart().bit(true));
|
|
|
|
|
2022-04-19 14:39:59 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:55:00 +01:00
|
|
|
/// Raw QSPI read.
|
|
|
|
///
|
|
|
|
/// The difference with `read` is that this does not do bounds checks
|
|
|
|
/// against the flash capacity. It is intended for use when QSPI is used as
|
|
|
|
/// a raw bus, not with flash memory.
|
|
|
|
pub async fn read_raw(&mut self, address: u32, data: &mut [u8]) -> Result<(), Error> {
|
2023-02-10 22:47:17 +01:00
|
|
|
let ondrop = OnDrop::new(Self::blocking_wait_ready);
|
2022-04-19 14:39:59 +02:00
|
|
|
|
|
|
|
self.start_read(address, data)?;
|
2022-01-13 23:05:31 +01:00
|
|
|
self.wait_ready().await;
|
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
ondrop.defuse();
|
2022-01-13 23:05:31 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-19 14:39:59 +02:00
|
|
|
|
2023-03-05 02:55:00 +01:00
|
|
|
/// Raw QSPI write.
|
|
|
|
///
|
|
|
|
/// The difference with `write` is that this does not do bounds checks
|
|
|
|
/// against the flash capacity. It is intended for use when QSPI is used as
|
|
|
|
/// a raw bus, not with flash memory.
|
|
|
|
pub async fn write_raw(&mut self, address: u32, data: &[u8]) -> Result<(), Error> {
|
2023-02-10 22:47:17 +01:00
|
|
|
let ondrop = OnDrop::new(Self::blocking_wait_ready);
|
2022-04-19 14:39:59 +02:00
|
|
|
|
|
|
|
self.start_write(address, data)?;
|
|
|
|
self.wait_ready().await;
|
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
ondrop.defuse();
|
2022-04-19 14:39:59 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:55:00 +01:00
|
|
|
/// Raw QSPI read, blocking version.
|
|
|
|
///
|
|
|
|
/// The difference with `blocking_read` is that this does not do bounds checks
|
|
|
|
/// against the flash capacity. It is intended for use when QSPI is used as
|
|
|
|
/// a raw bus, not with flash memory.
|
|
|
|
pub fn blocking_read_raw(&mut self, address: u32, data: &mut [u8]) -> Result<(), Error> {
|
|
|
|
self.start_read(address, data)?;
|
|
|
|
Self::blocking_wait_ready();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Raw QSPI write, blocking version.
|
|
|
|
///
|
|
|
|
/// The difference with `blocking_write` is that this does not do bounds checks
|
|
|
|
/// against the flash capacity. It is intended for use when QSPI is used as
|
|
|
|
/// a raw bus, not with flash memory.
|
|
|
|
pub fn blocking_write_raw(&mut self, address: u32, data: &[u8]) -> Result<(), Error> {
|
|
|
|
self.start_write(address, data)?;
|
|
|
|
Self::blocking_wait_ready();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read data from the flash memory.
|
|
|
|
pub async fn read(&mut self, address: u32, data: &mut [u8]) -> Result<(), Error> {
|
|
|
|
self.bounds_check(address, data.len())?;
|
|
|
|
self.read_raw(address, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write data to the flash memory.
|
|
|
|
pub async fn write(&mut self, address: u32, data: &[u8]) -> Result<(), Error> {
|
|
|
|
self.bounds_check(address, data.len())?;
|
|
|
|
self.write_raw(address, data).await
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Erase a sector on the flash memory.
|
2023-03-05 02:24:52 +01:00
|
|
|
pub async fn erase(&mut self, address: u32) -> Result<(), Error> {
|
2023-03-05 02:55:00 +01:00
|
|
|
if address >= self.capacity {
|
|
|
|
return Err(Error::OutOfBounds);
|
|
|
|
}
|
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
let ondrop = OnDrop::new(Self::blocking_wait_ready);
|
2022-04-19 14:39:59 +02:00
|
|
|
|
|
|
|
self.start_erase(address)?;
|
|
|
|
self.wait_ready().await;
|
|
|
|
|
2023-02-10 22:47:17 +01:00
|
|
|
ondrop.defuse();
|
2022-04-19 14:39:59 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Read data from the flash memory, blocking version.
|
2023-03-05 02:24:52 +01:00
|
|
|
pub fn blocking_read(&mut self, address: u32, data: &mut [u8]) -> Result<(), Error> {
|
2023-03-05 02:55:00 +01:00
|
|
|
self.bounds_check(address, data.len())?;
|
|
|
|
self.blocking_read_raw(address, data)
|
2022-04-19 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Write data to the flash memory, blocking version.
|
2023-03-05 02:24:52 +01:00
|
|
|
pub fn blocking_write(&mut self, address: u32, data: &[u8]) -> Result<(), Error> {
|
2023-03-05 02:55:00 +01:00
|
|
|
self.bounds_check(address, data.len())?;
|
|
|
|
self.blocking_write_raw(address, data)
|
2022-04-19 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Erase a sector on the flash memory, blocking version.
|
2023-03-05 02:24:52 +01:00
|
|
|
pub fn blocking_erase(&mut self, address: u32) -> Result<(), Error> {
|
2023-03-05 02:55:00 +01:00
|
|
|
if address >= self.capacity {
|
|
|
|
return Err(Error::OutOfBounds);
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:39:59 +02:00
|
|
|
self.start_erase(address)?;
|
2023-02-10 22:47:17 +01:00
|
|
|
Self::blocking_wait_ready();
|
2022-04-19 14:39:59 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-03-05 02:55:00 +01:00
|
|
|
|
|
|
|
fn bounds_check(&self, address: u32, len: usize) -> Result<(), Error> {
|
|
|
|
let len_u32: u32 = len.try_into().map_err(|_| Error::OutOfBounds)?;
|
|
|
|
let end_address = address.checked_add(len_u32).ok_or(Error::OutOfBounds)?;
|
|
|
|
if end_address > self.capacity {
|
|
|
|
return Err(Error::OutOfBounds);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
|
|
|
|
2023-03-05 02:33:02 +01:00
|
|
|
impl<'d, T: Instance> Drop for Qspi<'d, T> {
|
2021-05-26 23:26:07 +02:00
|
|
|
fn drop(&mut self) {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
if self.dpm_enabled {
|
2021-12-23 13:43:14 +01:00
|
|
|
trace!("qspi: doing deep powerdown...");
|
2021-05-26 23:26:07 +02:00
|
|
|
|
|
|
|
r.ifconfig1.modify(|_, w| w.dpmen().enter());
|
|
|
|
|
|
|
|
// Wait for DPM enter.
|
|
|
|
// Unfortunately we must spin. There's no way to do this interrupt-driven.
|
|
|
|
// The READY event does NOT fire on DPM enter (but it does fire on DPM exit :shrug:)
|
|
|
|
while r.status.read().dpm().is_disabled() {}
|
2021-05-27 00:42:29 +02:00
|
|
|
|
|
|
|
// Wait MORE for DPM enter.
|
|
|
|
// I have absolutely no idea why, but the wait above is not enough :'(
|
|
|
|
// Tested with mx25r64 in nrf52840-dk, and with mx25r16 in custom board
|
|
|
|
cortex_m::asm::delay(4096);
|
2021-05-26 23:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// it seems events_ready is not generated in response to deactivate. nrfx doesn't wait for it.
|
|
|
|
r.tasks_deactivate.write(|w| w.tasks_deactivate().set_bit());
|
|
|
|
|
|
|
|
// Workaround https://infocenter.nordicsemi.com/topic/errata_nRF52840_Rev1/ERR/nRF52840/Rev1/latest/anomaly_840_122.html?cp=4_0_1_2_1_7
|
|
|
|
// Note that the doc has 2 register writes, but the first one is really the write to tasks_deactivate,
|
|
|
|
// so we only do the second one here.
|
|
|
|
unsafe { ptr::write_volatile(0x40029054 as *mut u32, 1) }
|
|
|
|
|
|
|
|
r.enable.write(|w| w.enable().disabled());
|
|
|
|
|
|
|
|
// Note: we do NOT deconfigure CSN here. If DPM is in use and we disconnect CSN,
|
|
|
|
// leaving it floating, the flash chip might read it as zero which would cause it to
|
|
|
|
// spuriously exit DPM.
|
|
|
|
gpio::deconfigure_pin(r.psel.sck.read().bits());
|
|
|
|
gpio::deconfigure_pin(r.psel.io0.read().bits());
|
|
|
|
gpio::deconfigure_pin(r.psel.io1.read().bits());
|
|
|
|
gpio::deconfigure_pin(r.psel.io2.read().bits());
|
|
|
|
gpio::deconfigure_pin(r.psel.io3.read().bits());
|
|
|
|
|
2021-12-23 13:43:14 +01:00
|
|
|
trace!("qspi: dropped");
|
2021-05-26 23:26:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:33:02 +01:00
|
|
|
impl<'d, T: Instance> ErrorType for Qspi<'d, T> {
|
2022-04-19 14:39:59 +02:00
|
|
|
type Error = Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NorFlashError for Error {
|
|
|
|
fn kind(&self) -> NorFlashErrorKind {
|
|
|
|
NorFlashErrorKind::Other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:33:02 +01:00
|
|
|
impl<'d, T: Instance> ReadNorFlash for Qspi<'d, T> {
|
2022-04-19 14:39:59 +02:00
|
|
|
const READ_SIZE: usize = 4;
|
|
|
|
|
|
|
|
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
|
2023-03-05 02:24:52 +01:00
|
|
|
self.blocking_read(offset, bytes)?;
|
2022-04-19 14:39:59 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn capacity(&self) -> usize {
|
2023-03-05 02:33:02 +01:00
|
|
|
self.capacity as usize
|
2022-04-19 14:39:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 02:33:02 +01:00
|
|
|
impl<'d, T: Instance> NorFlash for Qspi<'d, T> {
|
2022-04-19 14:39:59 +02:00
|
|
|
const WRITE_SIZE: usize = 4;
|
|
|
|
const ERASE_SIZE: usize = 4096;
|
|
|
|
|
|
|
|
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
2023-03-05 02:24:52 +01:00
|
|
|
for address in (from..to).step_by(<Self as NorFlash>::ERASE_SIZE) {
|
2022-04-19 14:39:59 +02:00
|
|
|
self.blocking_erase(address)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
2023-03-05 02:24:52 +01:00
|
|
|
self.blocking_write(offset, bytes)?;
|
2022-04-19 14:39:59 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 22:23:01 +01:00
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
mod _eh1 {
|
2023-03-07 10:46:59 +01:00
|
|
|
use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash};
|
2022-04-19 14:39:59 +02:00
|
|
|
|
2023-02-23 22:23:01 +01:00
|
|
|
use super::*;
|
2022-04-19 14:39:59 +02:00
|
|
|
|
2023-03-05 02:33:02 +01:00
|
|
|
impl<'d, T: Instance> AsyncNorFlash for Qspi<'d, T> {
|
2023-02-23 22:23:01 +01:00
|
|
|
const WRITE_SIZE: usize = <Self as NorFlash>::WRITE_SIZE;
|
|
|
|
const ERASE_SIZE: usize = <Self as NorFlash>::ERASE_SIZE;
|
2022-04-19 14:39:59 +02:00
|
|
|
|
2023-03-07 10:46:59 +01:00
|
|
|
async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
self.write(offset, data).await
|
2022-04-19 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 10:46:59 +01:00
|
|
|
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
|
|
|
|
for address in (from..to).step_by(<Self as AsyncNorFlash>::ERASE_SIZE) {
|
|
|
|
self.erase(address).await?
|
2022-04-19 14:39:59 +02:00
|
|
|
}
|
2023-03-07 10:46:59 +01:00
|
|
|
Ok(())
|
2022-04-19 14:39:59 +02:00
|
|
|
}
|
2023-02-23 22:23:01 +01:00
|
|
|
}
|
2022-04-19 14:39:59 +02:00
|
|
|
|
2023-03-05 02:33:02 +01:00
|
|
|
impl<'d, T: Instance> AsyncReadNorFlash for Qspi<'d, T> {
|
2023-02-23 22:23:01 +01:00
|
|
|
const READ_SIZE: usize = 4;
|
2023-03-07 10:46:59 +01:00
|
|
|
async fn read(&mut self, address: u32, data: &mut [u8]) -> Result<(), Self::Error> {
|
|
|
|
self.read(address, data).await
|
2023-02-23 22:23:01 +01:00
|
|
|
}
|
2022-04-19 14:39:59 +02:00
|
|
|
|
2023-02-23 22:23:01 +01:00
|
|
|
fn capacity(&self) -> usize {
|
2023-03-05 02:33:02 +01:00
|
|
|
self.capacity as usize
|
2022-04-19 14:39:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 03:04:59 +02:00
|
|
|
pub(crate) mod sealed {
|
2022-08-22 21:46:09 +02:00
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2021-09-11 01:53:53 +02:00
|
|
|
|
2023-03-05 21:37:21 +01:00
|
|
|
/// Peripheral static state
|
2021-04-14 17:00:28 +02:00
|
|
|
pub struct State {
|
2023-03-05 21:37:21 +01:00
|
|
|
pub waker: AtomicWaker,
|
2021-04-14 17:00:28 +02:00
|
|
|
}
|
2023-03-05 21:37:21 +01:00
|
|
|
|
2021-04-14 17:00:28 +02:00
|
|
|
impl State {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
2023-03-05 21:37:21 +01:00
|
|
|
waker: AtomicWaker::new(),
|
2021-04-14 17:00:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 20:54:09 +01:00
|
|
|
pub trait Instance {
|
2023-03-05 21:37:21 +01:00
|
|
|
fn regs() -> &'static crate::pac::qspi::RegisterBlock;
|
2021-04-14 17:00:28 +02:00
|
|
|
fn state() -> &'static State;
|
2020-09-22 18:03:43 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-21 20:54:09 +01:00
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// QSPI peripheral instance.
|
2023-03-05 21:37:21 +01:00
|
|
|
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Interrupt for this peripheral.
|
2021-03-21 20:54:09 +01:00
|
|
|
type Interrupt: Interrupt;
|
|
|
|
}
|
|
|
|
|
2021-05-11 03:04:59 +02:00
|
|
|
macro_rules! impl_qspi {
|
|
|
|
($type:ident, $pac_type:ident, $irq:ident) => {
|
|
|
|
impl crate::qspi::sealed::Instance for peripherals::$type {
|
2023-03-05 21:37:21 +01:00
|
|
|
fn regs() -> &'static crate::pac::qspi::RegisterBlock {
|
2021-05-11 03:04:59 +02:00
|
|
|
unsafe { &*pac::$pac_type::ptr() }
|
2021-03-21 20:54:09 +01:00
|
|
|
}
|
2021-05-11 03:04:59 +02:00
|
|
|
fn state() -> &'static crate::qspi::sealed::State {
|
|
|
|
static STATE: crate::qspi::sealed::State = crate::qspi::sealed::State::new();
|
2021-04-14 17:00:28 +02:00
|
|
|
&STATE
|
|
|
|
}
|
2021-03-21 20:54:09 +01:00
|
|
|
}
|
2021-05-11 03:04:59 +02:00
|
|
|
impl crate::qspi::Instance for peripherals::$type {
|
|
|
|
type Interrupt = crate::interrupt::$irq;
|
2021-03-21 20:54:09 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|