use crate::Steal;
use rp2350_pac::QMI;
pub use rp2350_pac::qmi::DirectTxVal;
macro_rules! with_window {
($qmi:expr, $window:expr, $m0:ident, $m1:ident, $reg:ident, $body:block) => {
match $window {
Window::M0 => {
let $reg = &$qmi.$m0;
$body
}
Window::M1 => {
let $reg = &$qmi.$m1;
$body
}
}
};
}
macro_rules! write_format_reg {
($reg:expr, $fmt:expr) => {{
let f = $fmt;
$reg.write_prefix_width(f.prefix_width as u32);
$reg.write_addr_width(f.addr_width as u32);
$reg.write_suffix_width(f.suffix_width as u32);
$reg.write_dummy_width(f.dummy_width as u32);
$reg.write_data_width(f.data_width as u32);
if f.prefix_len {
$reg.set_prefix_len();
} else {
$reg.clr_prefix_len();
}
$reg.write_suffix_len(f.suffix_len);
$reg.write_dummy_len(f.dummy_len);
if f.dtr {
$reg.set_dtr();
} else {
$reg.clr_dtr();
}
}};
}
macro_rules! write_atrans {
($reg:expr, $base:expr, $size:expr) => {{
$reg.write_size($size);
$reg.write_base($base);
}};
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u32)]
pub enum BusWidth {
Single = 0,
Dual = 1,
Quad = 2,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Window {
M0,
M1,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u32)]
pub enum PageBreak {
None = 0,
Bytes256 = 1,
Bytes1024 = 2,
Bytes4096 = 3,
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Format {
pub prefix_width: BusWidth,
pub addr_width: BusWidth,
pub suffix_width: BusWidth,
pub dummy_width: BusWidth,
pub data_width: BusWidth,
pub prefix_len: bool,
pub suffix_len: u32,
pub dummy_len: u32,
pub dtr: bool,
}
impl Default for Format {
fn default() -> Self {
Self {
prefix_width: BusWidth::Single,
addr_width: BusWidth::Single,
suffix_width: BusWidth::Single,
dummy_width: BusWidth::Single,
data_width: BusWidth::Single,
prefix_len: true,
suffix_len: 0,
dummy_len: 0,
dtr: false,
}
}
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TimingConfig {
pub clkdiv: u8,
pub rxdelay: u8,
pub min_deselect: u8,
pub max_select: u8,
pub select_hold: u8,
pub select_setup: bool,
pub pagebreak: PageBreak,
pub cooldown: u8,
}
impl Default for TimingConfig {
fn default() -> Self {
Self {
clkdiv: 4,
rxdelay: 0,
min_deselect: 0,
max_select: 0,
select_hold: 0,
select_setup: false,
pagebreak: PageBreak::None,
cooldown: 1,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
TxFull,
RxEmpty,
Busy,
}
pub struct Qspi {
qmi: QMI,
}
impl Qspi {
pub fn init(qmi: QMI) -> Self {
Self { qmi }
}
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self::init(unsafe { QMI::steal() }))
}
#[inline]
pub fn release(self) -> QMI {
self.qmi
}
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::qmi::RegisterBlock {
&self.qmi
}
#[inline]
pub fn direct(&self) -> Direct<'_> {
Direct { qmi: &self.qmi }
}
#[inline]
pub fn set_clkdiv(&self, window: Window, div: u8) {
with_window!(self.qmi, window, m0_timing, m1_timing, reg, {
reg.write_clkdiv(div as u32);
});
}
#[inline]
pub fn clkdiv(&self, window: Window) -> u8 {
with_window!(self.qmi, window, m0_timing, m1_timing, reg, {
reg.clkdiv() as u8
})
}
pub fn configure_timing(&self, window: Window, cfg: TimingConfig) {
with_window!(self.qmi, window, m0_timing, m1_timing, reg, {
reg.write_clkdiv(cfg.clkdiv as u32);
reg.write_rxdelay(cfg.rxdelay as u32);
reg.write_min_deselect(cfg.min_deselect as u32);
reg.write_max_select(cfg.max_select as u32);
reg.write_select_hold(cfg.select_hold as u32);
if cfg.select_setup {
reg.set_select_setup();
} else {
reg.clr_select_setup();
}
reg.write_pagebreak(cfg.pagebreak as u32);
reg.write_cooldown(cfg.cooldown as u32);
});
}
pub fn configure_read_format(&self, window: Window, fmt: Format) {
with_window!(self.qmi, window, m0_rfmt, m1_rfmt, reg, {
write_format_reg!(reg, fmt);
});
}
pub fn configure_write_format(&self, window: Window, fmt: Format) {
with_window!(self.qmi, window, m0_wfmt, m1_wfmt, reg, {
write_format_reg!(reg, fmt);
});
}
pub fn set_read_command(&self, window: Window, prefix: u8, suffix: u8) {
with_window!(self.qmi, window, m0_rcmd, m1_rcmd, reg, {
reg.write_prefix(prefix as u32);
reg.write_suffix(suffix as u32);
});
}
pub fn set_write_command(&self, window: Window, prefix: u8, suffix: u8) {
with_window!(self.qmi, window, m0_wcmd, m1_wcmd, reg, {
reg.write_prefix(prefix as u32);
reg.write_suffix(suffix as u32);
});
}
pub fn set_address_translation(&self, window: u8, base: u32, size: u32) {
assert!(
window < 8,
"address translation window must be 0-7, got {window}"
);
match window {
0 => write_atrans!(&self.qmi.atrans0, base, size),
1 => write_atrans!(&self.qmi.atrans1, base, size),
2 => write_atrans!(&self.qmi.atrans2, base, size),
3 => write_atrans!(&self.qmi.atrans3, base, size),
4 => write_atrans!(&self.qmi.atrans4, base, size),
5 => write_atrans!(&self.qmi.atrans5, base, size),
6 => write_atrans!(&self.qmi.atrans6, base, size),
_ => write_atrans!(&self.qmi.atrans7, base, size),
}
}
pub fn configure_standard_read(&self, window: Window, clkdiv: u8) {
self.set_clkdiv(window, clkdiv);
self.configure_read_format(window, Format::default());
self.set_read_command(window, 0x03, 0x00);
}
pub fn configure_fast_read(&self, window: Window, clkdiv: u8) {
self.set_clkdiv(window, clkdiv);
self.configure_read_format(
window,
Format {
dummy_len: 2, ..Format::default()
},
);
self.set_read_command(window, 0x0B, 0x00);
}
pub fn configure_dual_read(&self, window: Window, clkdiv: u8) {
self.set_clkdiv(window, clkdiv);
self.configure_read_format(
window,
Format {
data_width: BusWidth::Dual,
dummy_len: 2,
..Format::default()
},
);
self.set_read_command(window, 0x3B, 0x00);
}
pub fn configure_quad_read(&self, window: Window, clkdiv: u8) {
self.set_clkdiv(window, clkdiv);
self.configure_read_format(
window,
Format {
data_width: BusWidth::Quad,
dummy_len: 2,
..Format::default()
},
);
self.set_read_command(window, 0x6B, 0x00);
}
pub fn configure_quad_io_read(&self, window: Window, clkdiv: u8) {
self.set_clkdiv(window, clkdiv);
self.configure_read_format(
window,
Format {
addr_width: BusWidth::Quad,
suffix_width: BusWidth::Quad,
dummy_width: BusWidth::Quad,
data_width: BusWidth::Quad,
suffix_len: 2, dummy_len: 1, ..Format::default()
},
);
self.set_read_command(window, 0xEB, 0xA0);
}
pub fn configure_standard_write(&self, window: Window) {
self.configure_write_format(window, Format::default());
self.set_write_command(window, 0x02, 0x00);
}
pub fn configure_quad_write(&self, window: Window) {
self.configure_write_format(
window,
Format {
data_width: BusWidth::Quad,
..Format::default()
},
);
self.set_write_command(window, 0x32, 0x00);
}
}
pub struct Direct<'a> {
qmi: &'a QMI,
}
impl Direct<'_> {
pub const TX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::XipQmiTx;
pub const RX_DREQ: crate::dma::TreqSel = crate::dma::TreqSel::XipQmiRx;
#[inline]
pub fn tx_address(&self) -> u32 {
core::ptr::addr_of!(self.qmi.direct_tx) as u32
}
#[inline]
pub fn rx_address(&self) -> u32 {
core::ptr::addr_of!(self.qmi.direct_rx) as u32
}
#[inline]
pub fn enable(&self) {
self.qmi.direct_csr.set_en();
}
#[inline]
pub fn disable(&self) {
self.qmi.direct_csr.clr_en();
}
#[inline]
pub fn is_enabled(&self) -> bool {
self.qmi.direct_csr.en()
}
#[inline]
pub fn is_busy(&self) -> bool {
self.qmi.direct_csr.busy()
}
#[inline]
pub fn wait_idle(&self) {
crate::spin::spin_wait("QMI idle", || !self.qmi.direct_csr.busy());
}
#[inline]
pub fn try_wait_idle(&self) -> Result<(), Error> {
if crate::spin::try_spin_wait(|| !self.qmi.direct_csr.busy()) {
Ok(())
} else {
Err(Error::Busy)
}
}
#[inline]
pub fn select(&self, cs: Window, asserted: bool) {
match cs {
Window::M0 => {
if asserted {
self.qmi.direct_csr.set_assert_cs0n();
} else {
self.qmi.direct_csr.clr_assert_cs0n();
}
}
Window::M1 => {
if asserted {
self.qmi.direct_csr.set_assert_cs1n();
} else {
self.qmi.direct_csr.clr_assert_cs1n();
}
}
}
}
#[inline]
pub fn set_auto_cs(&self, cs: Window, enabled: bool) {
match cs {
Window::M0 => {
if enabled {
self.qmi.direct_csr.set_auto_cs0n();
} else {
self.qmi.direct_csr.clr_auto_cs0n();
}
}
Window::M1 => {
if enabled {
self.qmi.direct_csr.set_auto_cs1n();
} else {
self.qmi.direct_csr.clr_auto_cs1n();
}
}
}
}
#[inline]
pub fn set_clkdiv(&self, div: u8) {
self.qmi.direct_csr.write_clkdiv(div as u32);
}
#[inline]
pub fn clkdiv(&self) -> u8 {
self.qmi.direct_csr.clkdiv() as u8
}
#[inline]
pub fn set_rxdelay(&self, delay: u8) {
self.qmi.direct_csr.write_rxdelay((delay & 0x3) as u32);
}
#[inline]
pub fn tx_full(&self) -> bool {
self.qmi.direct_csr.txfull()
}
#[inline]
pub fn tx_empty(&self) -> bool {
self.qmi.direct_csr.txempty()
}
#[inline]
pub fn rx_full(&self) -> bool {
self.qmi.direct_csr.rxfull()
}
#[inline]
pub fn rx_empty(&self) -> bool {
self.qmi.direct_csr.rxempty()
}
#[inline]
pub fn tx_level(&self) -> u32 {
self.qmi.direct_csr.txlevel()
}
#[inline]
pub fn rx_level(&self) -> u32 {
self.qmi.direct_csr.rxlevel()
}
#[inline]
pub fn tx_raw(&self, val: DirectTxVal) {
self.qmi.direct_tx.write_val(val);
}
#[inline]
pub fn rx_raw(&self) -> u16 {
self.qmi.direct_rx.direct_rx() as u16
}
fn push(&self, data: u32, width: BusWidth, oe: bool, no_push: bool, d16: bool) {
if !crate::spin::try_spin_wait(|| !self.qmi.direct_csr.txfull()) {
panic!("timed out waiting for QMI TX FIFO space");
}
self.write_frame(data, width, oe, no_push, d16);
}
fn try_push(
&self,
data: u32,
width: BusWidth,
oe: bool,
no_push: bool,
d16: bool,
) -> Result<(), Error> {
if !crate::spin::try_spin_wait(|| !self.qmi.direct_csr.txfull()) {
return Err(Error::TxFull);
}
self.write_frame(data, width, oe, no_push, d16);
Ok(())
}
fn write_frame(&self, data: u32, width: BusWidth, oe: bool, no_push: bool, d16: bool) {
let val = DirectTxVal::reset_val()
.with_data(data)
.with_iwidth(width as u32)
.with_oe(oe)
.with_nopush(no_push)
.with_dwidth(d16);
self.qmi.direct_tx.write_val(val);
}
pub fn write_byte(&self, data: u8, width: BusWidth, no_push: bool) {
self.push(data as u32, width, true, no_push, false);
}
pub fn write16(&self, data: u16, width: BusWidth, no_push: bool) {
self.push(data as u32, width, true, no_push, true);
}
pub fn read_byte(&self, width: BusWidth) -> u8 {
self.push(0, width, false, false, false);
crate::spin::spin_wait("QMI RX data", || !self.qmi.direct_csr.rxempty());
self.qmi.direct_rx.direct_rx() as u8
}
pub fn write(&self, data: &[u8]) {
for &byte in data {
self.write_byte(byte, BusWidth::Single, true);
}
self.wait_idle();
}
pub fn read(&self, buf: &mut [u8]) {
for slot in buf.iter_mut() {
*slot = self.read_byte(BusWidth::Single);
}
}
pub fn transfer(&self, rx: &mut [u8], tx: &[u8]) {
let len = rx.len().min(tx.len());
for i in 0..len {
self.push(tx[i] as u32, BusWidth::Single, true, false, false);
crate::spin::spin_wait("QMI RX data", || !self.qmi.direct_csr.rxempty());
rx[i] = self.qmi.direct_rx.direct_rx() as u8;
}
}
#[inline]
pub fn drain_rx(&self) {
while !self.qmi.direct_csr.rxempty() {
let _ = self.qmi.direct_rx.direct_rx();
}
}
pub fn try_write_byte(&self, data: u8, width: BusWidth, no_push: bool) -> Result<(), Error> {
self.try_push(data as u32, width, true, no_push, false)
}
pub fn try_write16(&self, data: u16, width: BusWidth, no_push: bool) -> Result<(), Error> {
self.try_push(data as u32, width, true, no_push, true)
}
pub fn try_read_byte(&self, width: BusWidth) -> Result<u8, Error> {
self.try_push(0, width, false, false, false)?;
if !crate::spin::try_spin_wait(|| !self.qmi.direct_csr.rxempty()) {
return Err(Error::RxEmpty);
}
Ok(self.qmi.direct_rx.direct_rx() as u8)
}
pub fn try_write(&self, data: &[u8]) -> Result<(), Error> {
for &byte in data {
self.try_write_byte(byte, BusWidth::Single, true)?;
}
self.try_wait_idle()
}
pub fn try_read(&self, buf: &mut [u8]) -> Result<(), Error> {
for slot in buf.iter_mut() {
*slot = self.try_read_byte(BusWidth::Single)?;
}
Ok(())
}
pub fn try_transfer(&self, rx: &mut [u8], tx: &[u8]) -> Result<(), Error> {
let len = rx.len().min(tx.len());
for i in 0..len {
self.try_push(tx[i] as u32, BusWidth::Single, true, false, false)?;
if !crate::spin::try_spin_wait(|| !self.qmi.direct_csr.rxempty()) {
return Err(Error::RxEmpty);
}
rx[i] = self.qmi.direct_rx.direct_rx() as u8;
}
Ok(())
}
pub fn command(&self, cs: Window, cmd: u8) {
self.select(cs, true);
self.write_byte(cmd, BusWidth::Single, true);
self.wait_idle();
self.select(cs, false);
}
pub fn try_command(&self, cs: Window, cmd: u8) -> Result<(), Error> {
self.select(cs, true);
self.try_write_byte(cmd, BusWidth::Single, true)?;
self.try_wait_idle()?;
self.select(cs, false);
Ok(())
}
pub fn read_status(&self, cs: Window) -> u8 {
self.select(cs, true);
self.write_byte(0x05, BusWidth::Single, true);
let status = self.read_byte(BusWidth::Single);
self.wait_idle();
self.select(cs, false);
status
}
pub fn try_read_status(&self, cs: Window) -> Result<u8, Error> {
self.select(cs, true);
self.try_write_byte(0x05, BusWidth::Single, true)?;
let status = self.try_read_byte(BusWidth::Single)?;
self.try_wait_idle()?;
self.select(cs, false);
Ok(status)
}
pub fn wait_ready(&self, cs: Window) {
self.try_wait_ready(cs)
.unwrap_or_else(|_| panic!("timed out waiting for flash to be ready"));
}
pub fn try_wait_ready(&self, cs: Window) -> Result<(), Error> {
for _ in 0..crate::spin::SPIN_TIMEOUT {
if self.try_read_status(cs)? & 0x01 == 0 {
return Ok(());
}
core::hint::spin_loop();
}
Err(Error::Busy)
}
pub fn read_jedec_id(&self, cs: Window) -> (u8, u8, u8) {
self.select(cs, true);
self.write_byte(0x9F, BusWidth::Single, true);
let mfr = self.read_byte(BusWidth::Single);
let typ = self.read_byte(BusWidth::Single);
let cap = self.read_byte(BusWidth::Single);
self.wait_idle();
self.select(cs, false);
(mfr, typ, cap)
}
pub fn try_read_jedec_id(&self, cs: Window) -> Result<(u8, u8, u8), Error> {
self.select(cs, true);
self.try_write_byte(0x9F, BusWidth::Single, true)?;
let mfr = self.try_read_byte(BusWidth::Single)?;
let typ = self.try_read_byte(BusWidth::Single)?;
let cap = self.try_read_byte(BusWidth::Single)?;
self.try_wait_idle()?;
self.select(cs, false);
Ok((mfr, typ, cap))
}
}