#![macro_use]
pub mod enums;
use core::marker::PhantomData;
use embassy_embedded_hal::{GetConfig, SetConfig};
use embassy_hal_internal::PeripheralType;
pub use enums::*;
use stm32_metapac::octospi::vals::{PhaseMode, SizeInBits};
use crate::dma::{ChannelAndRequest, word};
use crate::gpio::{AfType, Flex, OutputType, Pull, Speed};
use crate::mode::{Async, Blocking, Mode as PeriMode};
use crate::pac::octospi::{Octospi as Regs, vals};
#[cfg(octospim_v1)]
use crate::pac::octospim::Octospim;
use crate::rcc::{self, RccPeripheral};
use crate::{Peri, peripherals};
#[derive(Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Config {
pub fifo_threshold: FIFOThresholdLevel,
pub memory_type: MemoryType, pub device_size: MemorySize,
pub chip_select_high_time: ChipSelectHighTime,
pub free_running_clock: bool,
pub clock_mode: bool,
pub wrap_size: WrapSize,
pub clock_prescaler: u8,
pub sample_shifting: bool,
pub delay_hold_quarter_cycle: bool,
pub chip_select_boundary: u8,
pub delay_block_bypass: bool,
pub max_transfer: u8,
pub refresh: u32,
}
impl Default for Config {
fn default() -> Self {
Self {
fifo_threshold: FIFOThresholdLevel::_16Bytes, memory_type: MemoryType::Micron,
device_size: MemorySize::Other(0),
chip_select_high_time: ChipSelectHighTime::_5Cycle,
free_running_clock: false,
clock_mode: false,
wrap_size: WrapSize::None,
clock_prescaler: 0,
sample_shifting: false,
delay_hold_quarter_cycle: false,
chip_select_boundary: 0, delay_block_bypass: true,
max_transfer: 0,
refresh: 0,
}
}
}
#[derive(Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TransferConfig {
pub iwidth: OspiWidth,
pub instruction: Option<u32>,
pub isize: AddressSize,
pub idtr: bool,
pub adwidth: OspiWidth,
pub address: Option<u32>,
pub adsize: AddressSize,
pub addtr: bool,
pub abwidth: OspiWidth,
pub alternate_bytes: Option<u32>,
pub absize: AddressSize,
pub abdtr: bool,
pub dwidth: OspiWidth,
pub ddtr: bool,
pub dummy: DummyCycles,
pub dqse: bool,
pub sioo: bool,
}
impl Default for TransferConfig {
fn default() -> Self {
Self {
iwidth: OspiWidth::NONE,
instruction: None,
isize: AddressSize::_8Bit,
idtr: false,
adwidth: OspiWidth::NONE,
address: None,
adsize: AddressSize::_8Bit,
addtr: false,
abwidth: OspiWidth::NONE,
alternate_bytes: None,
absize: AddressSize::_8Bit,
abdtr: false,
dwidth: OspiWidth::NONE,
ddtr: false,
dummy: DummyCycles::_0,
dqse: false,
sioo: true,
}
}
}
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum OspiError {
InvalidConfiguration,
InvalidCommand,
EmptyBuffer,
}
pub struct Ospi<'d, T: Instance, M: PeriMode> {
_peri: Peri<'d, T>,
_sck: Option<Flex<'d>>,
_d0: Option<Flex<'d>>,
_d1: Option<Flex<'d>>,
_d2: Option<Flex<'d>>,
_d3: Option<Flex<'d>>,
_d4: Option<Flex<'d>>,
_d5: Option<Flex<'d>>,
_d6: Option<Flex<'d>>,
_d7: Option<Flex<'d>>,
_nss: Option<Flex<'d>>,
_dqs: Option<Flex<'d>>,
dma: Option<ChannelAndRequest<'d>>,
_phantom: PhantomData<M>,
config: Config,
width: OspiWidth,
}
impl<'d, T: Instance, M: PeriMode> Ospi<'d, T, M> {
pub fn enable_memory_mapped_mode(
&mut self,
read_config: TransferConfig,
write_config: TransferConfig,
) -> Result<(), OspiError> {
self.configure_command(&read_config, None)?;
let reg = T::REGS;
while reg.sr().read().busy() {}
if let Some(instruction) = write_config.instruction {
reg.wir().write(|r| {
r.set_instruction(instruction);
});
}
reg.wccr().modify(|w| {
w.set_imode(PhaseMode::from_bits(write_config.iwidth.into()));
w.set_idtr(write_config.idtr);
w.set_isize(SizeInBits::from_bits(write_config.isize.into()));
w.set_admode(PhaseMode::from_bits(write_config.adwidth.into()));
w.set_addtr(write_config.addtr);
w.set_adsize(SizeInBits::from_bits(write_config.adsize.into()));
w.set_dmode(PhaseMode::from_bits(write_config.dwidth.into()));
w.set_ddtr(write_config.ddtr);
w.set_abmode(PhaseMode::from_bits(write_config.abwidth.into()));
w.set_dqse(write_config.dqse);
});
reg.wtcr().modify(|w| w.set_dcyc(write_config.dummy.into()));
reg.cr().modify(|r| {
r.set_fmode(crate::ospi::vals::FunctionalMode::MEMORY_MAPPED);
r.set_tcen(false);
});
Ok(())
}
pub fn disable_memory_mapped_mode(&mut self) {
let reg = T::REGS;
reg.cr().modify(|r| {
r.set_fmode(crate::ospi::vals::FunctionalMode::INDIRECT_WRITE);
r.set_abort(true);
r.set_dmaen(false);
r.set_en(false);
});
reg.fcr().write(|w| w.set_ctcf(true));
reg.cr().modify(|r| {
r.set_en(true);
});
}
fn new_inner(
peri: Peri<'d, T>,
d0: Option<Flex<'d>>,
d1: Option<Flex<'d>>,
d2: Option<Flex<'d>>,
d3: Option<Flex<'d>>,
d4: Option<Flex<'d>>,
d5: Option<Flex<'d>>,
d6: Option<Flex<'d>>,
d7: Option<Flex<'d>>,
sck: Option<Flex<'d>>,
nss: Option<Flex<'d>>,
dqs: Option<Flex<'d>>,
dma: Option<ChannelAndRequest<'d>>,
config: Config,
width: OspiWidth,
dual_quad: bool,
) -> Self {
#[cfg(octospim_v1)]
{
#[cfg(stm32l4)]
crate::pac::RCC.ahb2smenr().modify(|w| w.set_octospimsmen(true));
#[cfg(stm32u5)]
crate::pac::RCC.ahb2enr1().modify(|w| w.set_octospimen(true));
#[cfg(not(any(stm32l4, stm32u5)))]
crate::pac::RCC.ahb3enr().modify(|w| w.set_iomngren(true));
T::REGS.cr().modify(|w| {
w.set_en(false);
});
T::OCTOSPIM_REGS.cr().modify(|w| {
w.set_muxen(false);
w.set_req2ack_time(0xff);
});
T::OCTOSPIM_REGS.p1cr().modify(|w| {
w.set_clksrc(false);
w.set_dqssrc(false);
w.set_ncssrc(false);
w.set_clken(false);
w.set_dqsen(false);
w.set_ncsen(false);
w.set_iolsrc(0);
w.set_iohsrc(0);
});
T::OCTOSPIM_REGS.p1cr().modify(|w| {
let octospi_src = if T::OCTOSPI_IDX == 1 { false } else { true };
w.set_ncsen(true);
w.set_ncssrc(octospi_src);
w.set_clken(true);
w.set_clksrc(octospi_src);
if dqs.is_some() {
w.set_dqsen(true);
w.set_dqssrc(octospi_src);
}
if T::OCTOSPI_IDX == 1 {
w.set_iolen(true);
w.set_iolsrc(0);
if let OspiWidth::OCTO = width {
w.set_iohen(true);
w.set_iohsrc(0b01);
} else if dual_quad {
w.set_iohen(true);
w.set_iohsrc(0b00);
} else {
w.set_iohen(false);
w.set_iohsrc(0b00);
}
} else {
w.set_iolen(true);
w.set_iolsrc(0b10);
if let OspiWidth::OCTO = width {
w.set_iohen(true);
w.set_iohsrc(0b11);
} else if dual_quad {
w.set_iohen(true);
w.set_iohsrc(0b10);
} else {
w.set_iohen(false);
w.set_iohsrc(0b00);
}
}
});
}
rcc::enable_and_reset::<T>();
while T::REGS.sr().read().busy() {}
T::REGS.dcr1().modify(|w| {
w.set_devsize(config.device_size.into());
w.set_mtyp(vals::MemType::from_bits(config.memory_type.into()));
w.set_csht(config.chip_select_high_time.into());
w.set_dlybyp(config.delay_block_bypass);
w.set_frck(false);
w.set_ckmode(config.clock_mode);
});
T::REGS.dcr2().modify(|w| {
w.set_wrapsize(config.wrap_size.into());
});
T::REGS.dcr3().modify(|w| {
w.set_csbound(config.chip_select_boundary);
#[cfg(octospi_v1)]
{
w.set_maxtran(config.max_transfer);
}
});
T::REGS.dcr4().modify(|w| {
w.set_refresh(config.refresh);
});
T::REGS.cr().modify(|w| {
w.set_fthres(vals::Threshold::from_bits(config.fifo_threshold.into()));
});
while T::REGS.sr().read().busy() {}
T::REGS.dcr2().modify(|w| {
w.set_prescaler(config.clock_prescaler);
});
T::REGS.cr().modify(|w| {
w.set_dmm(dual_quad);
});
T::REGS.tcr().modify(|w| {
w.set_sshift(match config.sample_shifting {
true => vals::SampleShift::HALF_CYCLE,
false => vals::SampleShift::NONE,
});
w.set_dhqc(config.delay_hold_quarter_cycle);
});
T::REGS.cr().modify(|w| {
w.set_en(true);
});
if config.free_running_clock {
T::REGS.dcr1().modify(|w| {
w.set_frck(config.free_running_clock);
});
}
Self {
_peri: peri,
_sck: sck,
_d0: d0,
_d1: d1,
_d2: d2,
_d3: d3,
_d4: d4,
_d5: d5,
_d6: d6,
_d7: d7,
_nss: nss,
_dqs: dqs,
dma,
_phantom: PhantomData,
config,
width,
}
}
fn configure_command(&mut self, command: &TransferConfig, data_len: Option<usize>) -> Result<(), OspiError> {
if <enums::OspiWidth as Into<u8>>::into(command.iwidth) > <enums::OspiWidth as Into<u8>>::into(self.width)
|| <enums::OspiWidth as Into<u8>>::into(command.adwidth) > <enums::OspiWidth as Into<u8>>::into(self.width)
|| <enums::OspiWidth as Into<u8>>::into(command.abwidth) > <enums::OspiWidth as Into<u8>>::into(self.width)
|| <enums::OspiWidth as Into<u8>>::into(command.dwidth) > <enums::OspiWidth as Into<u8>>::into(self.width)
{
return Err(OspiError::InvalidCommand);
}
T::REGS.cr().modify(|w| {
w.set_fmode(vals::FunctionalMode::INDIRECT_WRITE);
});
if let Some(ab) = command.alternate_bytes {
T::REGS.abr().write(|v| v.set_alternate(ab));
}
T::REGS.tcr().modify(|w| {
w.set_dcyc(command.dummy.into());
});
if let Some(data_length) = data_len {
T::REGS.dlr().write(|v| {
v.set_dl((data_length - 1) as u32);
});
} else {
T::REGS.dlr().write(|v| {
v.set_dl((0) as u32);
});
}
T::REGS.ccr().modify(|w| {
w.set_imode(PhaseMode::from_bits(command.iwidth.into()));
w.set_idtr(command.idtr);
w.set_isize(SizeInBits::from_bits(command.isize.into()));
w.set_admode(PhaseMode::from_bits(command.adwidth.into()));
w.set_addtr(command.addtr);
w.set_adsize(SizeInBits::from_bits(command.adsize.into()));
w.set_abmode(PhaseMode::from_bits(command.abwidth.into()));
w.set_abdtr(command.abdtr);
w.set_absize(SizeInBits::from_bits(command.absize.into()));
w.set_dmode(PhaseMode::from_bits(command.dwidth.into()));
w.set_ddtr(command.ddtr);
w.set_dqse(command.dqse);
w.set_sioo(command.sioo);
});
if let Some(instruction) = command.instruction {
if let Some(address) = command.address {
T::REGS.ir().write(|v| {
v.set_instruction(instruction);
});
T::REGS.ar().write(|v| {
v.set_address(address);
});
} else {
T::REGS.ir().write(|v| {
v.set_instruction(instruction);
});
}
} else {
if let Some(address) = command.address {
T::REGS.ar().write(|v| {
v.set_address(address);
});
} else {
return Err(OspiError::InvalidCommand);
}
}
if T::REGS.sr().read().tef() {
T::REGS.fcr().write(|w| w.set_ctef(true));
return Err(OspiError::InvalidCommand);
}
Ok(())
}
pub fn blocking_command(&mut self, command: &TransferConfig) -> Result<(), OspiError> {
while T::REGS.sr().read().busy() {}
self.configure_command(command, None)?;
while !T::REGS.sr().read().tcf() {}
T::REGS.fcr().write(|w| {
w.set_ctcf(true);
});
Ok(())
}
pub fn blocking_read<W: Word>(&mut self, buf: &mut [W], transaction: TransferConfig) -> Result<(), OspiError> {
if buf.is_empty() {
return Err(OspiError::EmptyBuffer);
}
while T::REGS.sr().read().busy() {}
T::REGS.cr().modify(|w| {
w.set_dmaen(false);
});
let transfer_size_bytes = buf.len() * W::size().bytes();
self.configure_command(&transaction, Some(transfer_size_bytes))?;
let current_address = T::REGS.ar().read().address();
let current_instruction = T::REGS.ir().read().instruction();
T::REGS
.cr()
.modify(|v| v.set_fmode(vals::FunctionalMode::INDIRECT_READ));
if T::REGS.ccr().read().admode() == vals::PhaseMode::NONE {
T::REGS.ir().write(|v| v.set_instruction(current_instruction));
} else {
T::REGS.ar().write(|v| v.set_address(current_address));
}
for idx in 0..buf.len() {
while !T::REGS.sr().read().tcf() && !T::REGS.sr().read().ftf() {}
buf[idx] = unsafe { (T::REGS.dr().as_ptr() as *mut W).read_volatile() };
}
while !T::REGS.sr().read().tcf() {}
T::REGS.fcr().write(|v| v.set_ctcf(true));
Ok(())
}
pub fn blocking_write<W: Word>(&mut self, buf: &[W], transaction: TransferConfig) -> Result<(), OspiError> {
if buf.is_empty() {
return Err(OspiError::EmptyBuffer);
}
while T::REGS.sr().read().busy() {}
T::REGS.cr().modify(|w| {
w.set_dmaen(false);
});
let transfer_size_bytes = buf.len() * W::size().bytes();
self.configure_command(&transaction, Some(transfer_size_bytes))?;
T::REGS
.cr()
.modify(|v| v.set_fmode(vals::FunctionalMode::INDIRECT_WRITE));
for idx in 0..buf.len() {
while !T::REGS.sr().read().ftf() {}
unsafe { (T::REGS.dr().as_ptr() as *mut W).write_volatile(buf[idx]) };
}
while !T::REGS.sr().read().tcf() {}
T::REGS.fcr().write(|v| v.set_ctcf(true));
Ok(())
}
pub fn set_config(&mut self, config: &Config) {
while T::REGS.sr().read().busy() {}
T::REGS.cr().modify(|w| {
w.set_dmaen(false);
});
T::REGS.dcr1().modify(|w| {
w.set_devsize(config.device_size.into());
w.set_mtyp(vals::MemType::from_bits(config.memory_type.into()));
w.set_csht(config.chip_select_high_time.into());
w.set_dlybyp(config.delay_block_bypass);
w.set_frck(false);
w.set_ckmode(config.clock_mode);
});
T::REGS.dcr2().modify(|w| {
w.set_wrapsize(config.wrap_size.into());
});
T::REGS.dcr3().modify(|w| {
w.set_csbound(config.chip_select_boundary);
#[cfg(octospi_v1)]
{
w.set_maxtran(config.max_transfer);
}
});
T::REGS.dcr4().modify(|w| {
w.set_refresh(config.refresh);
});
T::REGS.cr().modify(|w| {
w.set_fthres(vals::Threshold::from_bits(config.fifo_threshold.into()));
});
while T::REGS.sr().read().busy() {}
T::REGS.dcr2().modify(|w| {
w.set_prescaler(config.clock_prescaler);
});
T::REGS.tcr().modify(|w| {
w.set_sshift(match config.sample_shifting {
true => vals::SampleShift::HALF_CYCLE,
false => vals::SampleShift::NONE,
});
w.set_dhqc(config.delay_hold_quarter_cycle);
});
T::REGS.cr().modify(|w| {
w.set_en(true);
});
if config.free_running_clock {
T::REGS.dcr1().modify(|w| {
w.set_frck(config.free_running_clock);
});
}
self.config = *config;
}
pub fn get_config(&self) -> Config {
self.config
}
}
impl<'d, T: Instance> Ospi<'d, T, Blocking> {
pub fn new_blocking_singlespi(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::input(Pull::None)),
None,
None,
None,
None,
None,
None,
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
None,
config,
OspiWidth::SING,
false,
)
}
pub fn new_blocking_dualspi(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
None,
None,
None,
None,
None,
None,
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
None,
config,
OspiWidth::DUAL,
false,
)
}
pub fn new_blocking_quadspi(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
d2: Peri<'d, impl D2Pin<T>>,
d3: Peri<'d, impl D3Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d2, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d3, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
None,
None,
None,
None,
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
None,
config,
OspiWidth::QUAD,
false,
)
}
pub fn new_blocking_dualquadspi(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
d2: Peri<'d, impl D2Pin<T>>,
d3: Peri<'d, impl D3Pin<T>>,
d4: Peri<'d, impl D4Pin<T>>,
d5: Peri<'d, impl D5Pin<T>>,
d6: Peri<'d, impl D6Pin<T>>,
d7: Peri<'d, impl D7Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d2, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d3, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d4, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d5, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d6, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d7, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
None,
config,
OspiWidth::QUAD,
true,
)
}
pub fn new_blocking_octospi(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
d2: Peri<'d, impl D2Pin<T>>,
d3: Peri<'d, impl D3Pin<T>>,
d4: Peri<'d, impl D4Pin<T>>,
d5: Peri<'d, impl D5Pin<T>>,
d6: Peri<'d, impl D6Pin<T>>,
d7: Peri<'d, impl D7Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d2, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d3, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d4, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d5, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d6, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d7, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
None,
config,
OspiWidth::OCTO,
false,
)
}
pub fn new_blocking_octospi_with_dqs(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
d2: Peri<'d, impl D2Pin<T>>,
d3: Peri<'d, impl D3Pin<T>>,
d4: Peri<'d, impl D4Pin<T>>,
d5: Peri<'d, impl D5Pin<T>>,
d6: Peri<'d, impl D6Pin<T>>,
d7: Peri<'d, impl D7Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
dqs: Peri<'d, impl DQSPin<T>>,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d2, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d3, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d4, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d5, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d6, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d7, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
new_pin!(dqs, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
None,
config,
OspiWidth::OCTO,
false,
)
}
}
impl<'d, T: Instance> Ospi<'d, T, Async> {
pub fn new_singlespi<D: OctoDma<T>>(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
dma: Peri<'d, D>,
_irq: impl crate::interrupt::typelevel::Binding<D::Interrupt, crate::dma::InterruptHandler<D>> + 'd,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::input(Pull::None)),
None,
None,
None,
None,
None,
None,
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
new_dma!(dma, _irq),
config,
OspiWidth::SING,
false,
)
}
pub fn new_dualspi<D: OctoDma<T>>(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
dma: Peri<'d, D>,
_irq: impl crate::interrupt::typelevel::Binding<D::Interrupt, crate::dma::InterruptHandler<D>> + 'd,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
None,
None,
None,
None,
None,
None,
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
new_dma!(dma, _irq),
config,
OspiWidth::DUAL,
false,
)
}
pub fn new_quadspi<D: OctoDma<T>>(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
d2: Peri<'d, impl D2Pin<T>>,
d3: Peri<'d, impl D3Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
dma: Peri<'d, D>,
_irq: impl crate::interrupt::typelevel::Binding<D::Interrupt, crate::dma::InterruptHandler<D>> + 'd,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d2, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d3, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
None,
None,
None,
None,
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
new_dma!(dma, _irq),
config,
OspiWidth::QUAD,
false,
)
}
pub fn new_dualquadspi<D: OctoDma<T>>(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
d2: Peri<'d, impl D2Pin<T>>,
d3: Peri<'d, impl D3Pin<T>>,
d4: Peri<'d, impl D4Pin<T>>,
d5: Peri<'d, impl D5Pin<T>>,
d6: Peri<'d, impl D6Pin<T>>,
d7: Peri<'d, impl D7Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
dma: Peri<'d, D>,
_irq: impl crate::interrupt::typelevel::Binding<D::Interrupt, crate::dma::InterruptHandler<D>> + 'd,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d2, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d3, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d4, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d5, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d6, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d7, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
new_dma!(dma, _irq),
config,
OspiWidth::QUAD,
true,
)
}
pub fn new_octospi<D: OctoDma<T>>(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
d2: Peri<'d, impl D2Pin<T>>,
d3: Peri<'d, impl D3Pin<T>>,
d4: Peri<'d, impl D4Pin<T>>,
d5: Peri<'d, impl D5Pin<T>>,
d6: Peri<'d, impl D6Pin<T>>,
d7: Peri<'d, impl D7Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
dma: Peri<'d, D>,
_irq: impl crate::interrupt::typelevel::Binding<D::Interrupt, crate::dma::InterruptHandler<D>> + 'd,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d2, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d3, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d4, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d5, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d6, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d7, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
None,
new_dma!(dma, _irq),
config,
OspiWidth::OCTO,
false,
)
}
pub fn new_octospi_with_dqs<D: OctoDma<T>>(
peri: Peri<'d, T>,
sck: Peri<'d, impl SckPin<T>>,
d0: Peri<'d, impl D0Pin<T>>,
d1: Peri<'d, impl D1Pin<T>>,
d2: Peri<'d, impl D2Pin<T>>,
d3: Peri<'d, impl D3Pin<T>>,
d4: Peri<'d, impl D4Pin<T>>,
d5: Peri<'d, impl D5Pin<T>>,
d6: Peri<'d, impl D6Pin<T>>,
d7: Peri<'d, impl D7Pin<T>>,
nss: Peri<'d, impl NSSPin<T>>,
dqs: Peri<'d, impl DQSPin<T>>,
dma: Peri<'d, D>,
_irq: impl crate::interrupt::typelevel::Binding<D::Interrupt, crate::dma::InterruptHandler<D>> + 'd,
config: Config,
) -> Self {
Self::new_inner(
peri,
new_pin!(d0, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d1, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d2, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d3, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d4, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d5, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d6, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(d7, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(sck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_pin!(
nss,
AfType::output_pull(OutputType::PushPull, Speed::VeryHigh, Pull::Up)
),
new_pin!(dqs, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
new_dma!(dma, _irq),
config,
OspiWidth::OCTO,
false,
)
}
pub fn blocking_read_dma<W: Word>(&mut self, buf: &mut [W], transaction: TransferConfig) -> Result<(), OspiError> {
if buf.is_empty() {
return Err(OspiError::EmptyBuffer);
}
while T::REGS.sr().read().busy() {}
let transfer_size_bytes = buf.len() * W::size().bytes();
self.configure_command(&transaction, Some(transfer_size_bytes))?;
let current_address = T::REGS.ar().read().address();
let current_instruction = T::REGS.ir().read().instruction();
T::REGS
.cr()
.modify(|v| v.set_fmode(vals::FunctionalMode::INDIRECT_READ));
if T::REGS.ccr().read().admode() == vals::PhaseMode::NONE {
T::REGS.ir().write(|v| v.set_instruction(current_instruction));
} else {
T::REGS.ar().write(|v| v.set_address(current_address));
}
for chunk in buf.chunks_mut(0xFFFF / W::size().bytes()) {
let transfer = unsafe {
self.dma
.as_mut()
.unwrap()
.read(T::REGS.dr().as_ptr() as *mut W, chunk, Default::default())
};
T::REGS.cr().modify(|w| w.set_dmaen(true));
transfer.blocking_wait();
}
finish_dma(T::REGS);
Ok(())
}
pub fn blocking_write_dma<W: Word>(&mut self, buf: &[W], transaction: TransferConfig) -> Result<(), OspiError> {
if buf.is_empty() {
return Err(OspiError::EmptyBuffer);
}
while T::REGS.sr().read().busy() {}
let transfer_size_bytes = buf.len() * W::size().bytes();
self.configure_command(&transaction, Some(transfer_size_bytes))?;
T::REGS
.cr()
.modify(|v| v.set_fmode(vals::FunctionalMode::INDIRECT_WRITE));
for chunk in buf.chunks(0xFFFF / W::size().bytes()) {
let transfer = unsafe {
self.dma
.as_mut()
.unwrap()
.write(chunk, T::REGS.dr().as_ptr() as *mut W, Default::default())
};
T::REGS.cr().modify(|w| w.set_dmaen(true));
transfer.blocking_wait();
}
finish_dma(T::REGS);
Ok(())
}
pub async fn read<W: Word>(&mut self, buf: &mut [W], transaction: TransferConfig) -> Result<(), OspiError> {
if buf.is_empty() {
return Err(OspiError::EmptyBuffer);
}
while T::REGS.sr().read().busy() {}
let transfer_size_bytes = buf.len() * W::size().bytes();
self.configure_command(&transaction, Some(transfer_size_bytes))?;
let current_address = T::REGS.ar().read().address();
let current_instruction = T::REGS.ir().read().instruction();
T::REGS
.cr()
.modify(|v| v.set_fmode(vals::FunctionalMode::INDIRECT_READ));
if T::REGS.ccr().read().admode() == vals::PhaseMode::NONE {
T::REGS.ir().write(|v| v.set_instruction(current_instruction));
} else {
T::REGS.ar().write(|v| v.set_address(current_address));
}
for chunk in buf.chunks_mut(0xFFFF / W::size().bytes()) {
let transfer = unsafe {
self.dma
.as_mut()
.unwrap()
.read(T::REGS.dr().as_ptr() as *mut W, chunk, Default::default())
};
T::REGS.cr().modify(|w| w.set_dmaen(true));
transfer.await;
}
finish_dma(T::REGS);
Ok(())
}
pub async fn write<W: Word>(&mut self, buf: &[W], transaction: TransferConfig) -> Result<(), OspiError> {
if buf.is_empty() {
return Err(OspiError::EmptyBuffer);
}
while T::REGS.sr().read().busy() {}
let transfer_size_bytes = buf.len() * W::size().bytes();
self.configure_command(&transaction, Some(transfer_size_bytes))?;
T::REGS
.cr()
.modify(|v| v.set_fmode(vals::FunctionalMode::INDIRECT_WRITE));
for chunk in buf.chunks(0xFFFF / W::size().bytes()) {
let transfer = unsafe {
self.dma
.as_mut()
.unwrap()
.write(chunk, T::REGS.dr().as_ptr() as *mut W, Default::default())
};
T::REGS.cr().modify(|w| w.set_dmaen(true));
transfer.await;
}
finish_dma(T::REGS);
Ok(())
}
}
impl<'d, T: Instance, M: PeriMode> Drop for Ospi<'d, T, M> {
fn drop(&mut self) {
rcc::disable::<T>();
}
}
fn finish_dma(regs: Regs) {
while !regs.sr().read().tcf() {}
regs.fcr().write(|v| v.set_ctcf(true));
regs.cr().modify(|w| {
w.set_dmaen(false);
});
}
#[cfg(octospim_v1)]
pub(crate) trait SealedOctospimInstance {
const OCTOSPIM_REGS: Octospim;
const OCTOSPI_IDX: u8;
}
pub(crate) trait SealedInstance {
const REGS: Regs;
}
#[cfg(octospim_v1)]
#[allow(private_bounds)]
pub trait Instance: SealedInstance + PeripheralType + RccPeripheral + SealedOctospimInstance {}
#[cfg(not(octospim_v1))]
#[allow(private_bounds)]
pub trait Instance: SealedInstance + PeripheralType + RccPeripheral {}
pin_trait!(SckPin, Instance);
pin_trait!(NckPin, Instance);
pin_trait!(D0Pin, Instance);
pin_trait!(D1Pin, Instance);
pin_trait!(D2Pin, Instance);
pin_trait!(D3Pin, Instance);
pin_trait!(D4Pin, Instance);
pin_trait!(D5Pin, Instance);
pin_trait!(D6Pin, Instance);
pin_trait!(D7Pin, Instance);
pin_trait!(DQSPin, Instance);
pin_trait!(NSSPin, Instance);
dma_trait!(OctoDma, Instance);
#[cfg(octospim_v1)]
impl SealedOctospimInstance for peripherals::OCTOSPI1 {
const OCTOSPIM_REGS: Octospim = crate::pac::OCTOSPIM;
const OCTOSPI_IDX: u8 = 1;
}
#[cfg(all(octospim_v1, peri_octospi2))]
impl SealedOctospimInstance for peripherals::OCTOSPI2 {
const OCTOSPIM_REGS: Octospim = crate::pac::OCTOSPIM;
const OCTOSPI_IDX: u8 = 2;
}
#[cfg(octospim_v1)]
foreach_peripheral!(
(octospi, $inst:ident) => {
impl SealedInstance for peripherals::$inst {
const REGS: Regs = crate::pac::$inst;
}
impl Instance for peripherals::$inst {}
};
);
#[cfg(not(octospim_v1))]
foreach_peripheral!(
(octospi, $inst:ident) => {
impl SealedInstance for peripherals::$inst {
const REGS: Regs = crate::pac::$inst;
}
impl Instance for peripherals::$inst {}
};
);
impl<'d, T: Instance, M: PeriMode> SetConfig for Ospi<'d, T, M> {
type Config = Config;
type ConfigError = ();
fn set_config(&mut self, config: &Self::Config) -> Result<(), ()> {
self.set_config(config);
Ok(())
}
}
impl<'d, T: Instance, M: PeriMode> GetConfig for Ospi<'d, T, M> {
type Config = Config;
fn get_config(&self) -> Self::Config {
self.get_config()
}
}
#[allow(private_bounds)]
pub trait Word: word::Word {}
macro_rules! impl_word {
($T:ty) => {
impl Word for $T {}
};
}
impl_word!(u8);
impl_word!(u16);
impl_word!(u32);