2021-05-17 02:04:51 +02:00
|
|
|
#![macro_use]
|
|
|
|
|
2022-03-15 18:58:19 +01:00
|
|
|
use core::default::Default;
|
2022-09-22 16:42:49 +02:00
|
|
|
use core::future::poll_fn;
|
2023-05-25 00:29:56 +02:00
|
|
|
use core::marker::PhantomData;
|
2023-02-18 00:35:35 +01:00
|
|
|
use core::ops::{Deref, DerefMut};
|
2022-03-15 18:58:19 +01:00
|
|
|
use core::task::Poll;
|
2021-05-17 02:04:51 +02:00
|
|
|
|
2022-03-15 18:58:19 +01:00
|
|
|
use embassy_hal_common::drop::OnDrop;
|
2022-07-23 14:00:19 +02:00
|
|
|
use embassy_hal_common::{into_ref, PeripheralRef};
|
2022-08-22 21:46:09 +02:00
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2022-03-15 18:58:19 +01:00
|
|
|
use sdio_host::{BusWidth, CardCapacity, CardStatus, CurrentState, SDStatus, CID, CSD, OCR, SCR};
|
|
|
|
|
2022-03-16 19:20:39 +01:00
|
|
|
use crate::dma::NoDma;
|
2022-07-23 01:29:35 +02:00
|
|
|
use crate::gpio::sealed::{AFType, Pin};
|
|
|
|
use crate::gpio::{AnyPin, Pull, Speed};
|
2023-06-08 16:08:40 +02:00
|
|
|
use crate::interrupt::typelevel::Interrupt;
|
2022-03-15 18:58:19 +01:00
|
|
|
use crate::pac::sdmmc::Sdmmc as RegBlock;
|
2022-03-16 17:52:27 +01:00
|
|
|
use crate::rcc::RccPeripheral;
|
2022-03-15 18:58:19 +01:00
|
|
|
use crate::time::Hertz;
|
2023-05-25 00:29:56 +02:00
|
|
|
use crate::{interrupt, peripherals, Peripheral};
|
|
|
|
|
|
|
|
/// Interrupt handler.
|
|
|
|
pub struct InterruptHandler<T: Instance> {
|
|
|
|
_phantom: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Instance> InterruptHandler<T> {
|
|
|
|
fn data_interrupts(enable: bool) {
|
|
|
|
let regs = T::regs();
|
|
|
|
// NOTE(unsafe) Atomic write
|
|
|
|
unsafe {
|
|
|
|
regs.maskr().write(|w| {
|
|
|
|
w.set_dcrcfailie(enable);
|
|
|
|
w.set_dtimeoutie(enable);
|
|
|
|
w.set_dataendie(enable);
|
|
|
|
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
w.set_dabortie(enable);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:08:40 +02:00
|
|
|
impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
|
2023-05-25 00:29:56 +02:00
|
|
|
unsafe fn on_interrupt() {
|
|
|
|
Self::data_interrupts(false);
|
|
|
|
T::state().wake();
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2022-12-06 15:36:07 +01:00
|
|
|
/// Frequency used for SD Card initialization. Must be no higher than 400 kHz.
|
|
|
|
const SD_INIT_FREQ: Hertz = Hertz(400_000);
|
|
|
|
|
2022-03-15 18:58:19 +01:00
|
|
|
/// The signalling scheme used on the SDMMC bus
|
|
|
|
#[non_exhaustive]
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Signalling {
|
|
|
|
SDR12,
|
|
|
|
SDR25,
|
|
|
|
SDR50,
|
|
|
|
SDR104,
|
|
|
|
DDR50,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Signalling {
|
|
|
|
fn default() -> Self {
|
|
|
|
Signalling::SDR12
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(align(4))]
|
2023-04-17 00:54:22 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2023-02-18 00:35:35 +01:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub struct DataBlock(pub [u8; 512]);
|
|
|
|
|
|
|
|
impl Deref for DataBlock {
|
|
|
|
type Target = [u8; 512];
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for DataBlock {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
/// Errors
|
|
|
|
#[non_exhaustive]
|
2023-04-17 00:54:22 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2022-03-15 18:58:19 +01:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Error {
|
|
|
|
Timeout,
|
|
|
|
SoftwareTimeout,
|
|
|
|
UnsupportedCardVersion,
|
|
|
|
UnsupportedCardType,
|
|
|
|
Crc,
|
|
|
|
DataCrcFail,
|
|
|
|
RxOverFlow,
|
|
|
|
NoCard,
|
|
|
|
BadClock,
|
|
|
|
SignalingSwitchFailed,
|
|
|
|
PeripheralBusy,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A SD command
|
|
|
|
struct Cmd {
|
|
|
|
cmd: u8,
|
|
|
|
arg: u32,
|
|
|
|
resp: Response,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
|
|
/// SD Card
|
|
|
|
pub struct Card {
|
|
|
|
/// The type of this card
|
|
|
|
pub card_type: CardCapacity,
|
|
|
|
/// Operation Conditions Register
|
|
|
|
pub ocr: OCR,
|
|
|
|
/// Relative Card Address
|
|
|
|
pub rca: u32,
|
|
|
|
/// Card ID
|
|
|
|
pub cid: CID,
|
|
|
|
/// Card Specific Data
|
|
|
|
pub csd: CSD,
|
|
|
|
/// SD CARD Configuration Register
|
|
|
|
pub scr: SCR,
|
|
|
|
/// SD Status
|
|
|
|
pub status: SDStatus,
|
|
|
|
}
|
2022-03-16 22:55:07 +01:00
|
|
|
|
2022-03-15 18:58:19 +01:00
|
|
|
impl Card {
|
|
|
|
/// Size in bytes
|
|
|
|
pub fn size(&self) -> u64 {
|
|
|
|
// SDHC / SDXC / SDUC
|
|
|
|
u64::from(self.csd.block_count()) * 512
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(u8)]
|
|
|
|
enum PowerCtrl {
|
|
|
|
Off = 0b00,
|
|
|
|
On = 0b11,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(u32)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
enum CmdAppOper {
|
|
|
|
VOLTAGE_WINDOW_SD = 0x8010_0000,
|
|
|
|
HIGH_CAPACITY = 0x4000_0000,
|
|
|
|
SDMMC_STD_CAPACITY = 0x0000_0000,
|
|
|
|
SDMMC_CHECK_PATTERN = 0x0000_01AA,
|
|
|
|
SD_SWITCH_1_8V_CAPACITY = 0x0100_0000,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Copy, Clone)]
|
|
|
|
enum Response {
|
|
|
|
None = 0,
|
|
|
|
Short = 1,
|
|
|
|
Long = 3,
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:13:25 +02:00
|
|
|
/// Calculate clock divisor. Returns a SDMMC_CK less than or equal to
|
|
|
|
/// `sdmmc_ck` in Hertz.
|
|
|
|
///
|
|
|
|
/// Returns `(bypass, clk_div, clk_f)`, where `bypass` enables clock divisor bypass (only sdmmc_v1),
|
|
|
|
/// `clk_div` is the divisor register value and `clk_f` is the resulting new clock frequency.
|
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
fn clk_div(ker_ck: Hertz, sdmmc_ck: u32) -> Result<(bool, u8, Hertz), Error> {
|
|
|
|
// sdmmc_v1 maximum clock is 50 MHz
|
|
|
|
if sdmmc_ck > 50_000_000 {
|
|
|
|
return Err(Error::BadClock);
|
|
|
|
}
|
2023-02-23 15:57:21 +01:00
|
|
|
|
2023-04-17 00:13:25 +02:00
|
|
|
// bypass divisor
|
|
|
|
if ker_ck.0 <= sdmmc_ck {
|
|
|
|
return Ok((true, 0, ker_ck));
|
|
|
|
}
|
2023-02-23 15:57:21 +01:00
|
|
|
|
2023-04-17 00:13:25 +02:00
|
|
|
// `ker_ck / sdmmc_ck` rounded up
|
|
|
|
let clk_div = match (ker_ck.0 + sdmmc_ck - 1) / sdmmc_ck {
|
|
|
|
0 | 1 => Ok(0),
|
|
|
|
x @ 2..=258 => Ok((x - 2) as u8),
|
|
|
|
_ => Err(Error::BadClock),
|
|
|
|
}?;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:13:25 +02:00
|
|
|
// SDIO_CK frequency = SDIOCLK / [CLKDIV + 2]
|
|
|
|
let clk_f = Hertz(ker_ck.0 / (clk_div as u32 + 2));
|
|
|
|
Ok((false, clk_div, clk_f))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculate clock divisor. Returns a SDMMC_CK less than or equal to
|
|
|
|
/// `sdmmc_ck` in Hertz.
|
|
|
|
///
|
|
|
|
/// Returns `(bypass, clk_div, clk_f)`, where `bypass` enables clock divisor bypass (only sdmmc_v1),
|
|
|
|
/// `clk_div` is the divisor register value and `clk_f` is the resulting new clock frequency.
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
fn clk_div(ker_ck: Hertz, sdmmc_ck: u32) -> Result<(bool, u16, Hertz), Error> {
|
|
|
|
// `ker_ck / sdmmc_ck` rounded up
|
|
|
|
match (ker_ck.0 + sdmmc_ck - 1) / sdmmc_ck {
|
|
|
|
0 | 1 => Ok((false, 0, ker_ck)),
|
|
|
|
x @ 2..=2046 => {
|
2023-04-17 00:54:22 +02:00
|
|
|
// SDMMC_CK frequency = SDMMCCLK / [CLKDIV * 2]
|
2023-04-17 00:13:25 +02:00
|
|
|
let clk_div = ((x + 1) / 2) as u16;
|
|
|
|
let clk = Hertz(ker_ck.0 / (clk_div as u32 * 2));
|
|
|
|
|
|
|
|
Ok((false, clk_div, clk))
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
2023-04-17 00:13:25 +02:00
|
|
|
_ => Err(Error::BadClock),
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
type Transfer<'a, C> = crate::dma::Transfer<'a, C>;
|
|
|
|
#[cfg(sdmmc_v2)]
|
2023-05-19 18:00:33 +02:00
|
|
|
struct Transfer<'a, C> {
|
|
|
|
_dummy: core::marker::PhantomData<&'a mut C>,
|
|
|
|
}
|
2023-04-17 00:04:54 +02:00
|
|
|
|
|
|
|
#[cfg(all(sdmmc_v1, dma))]
|
|
|
|
const DMA_TRANSFER_OPTIONS: crate::dma::TransferOptions = crate::dma::TransferOptions {
|
|
|
|
pburst: crate::dma::Burst::Incr4,
|
|
|
|
mburst: crate::dma::Burst::Incr4,
|
|
|
|
flow_ctrl: crate::dma::FlowControl::Peripheral,
|
|
|
|
fifo_threshold: Some(crate::dma::FifoThreshold::Full),
|
|
|
|
};
|
|
|
|
#[cfg(all(sdmmc_v1, not(dma)))]
|
|
|
|
const DMA_TRANSFER_OPTIONS: crate::dma::TransferOptions = crate::dma::TransferOptions {};
|
|
|
|
|
2022-03-15 18:58:19 +01:00
|
|
|
/// SDMMC configuration
|
|
|
|
///
|
|
|
|
/// Default values:
|
|
|
|
/// data_transfer_timeout: 5_000_000
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct Config {
|
|
|
|
/// The timeout to be set for data transfers, in card bus clock periods
|
|
|
|
pub data_transfer_timeout: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
data_transfer_timeout: 5_000_000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sdmmc device
|
2023-04-17 00:54:22 +02:00
|
|
|
pub struct Sdmmc<'d, T: Instance, Dma: SdmmcDma<T> = NoDma> {
|
2022-07-23 14:00:19 +02:00
|
|
|
_peri: PeripheralRef<'d, T>,
|
2023-04-17 00:54:22 +02:00
|
|
|
#[allow(unused)]
|
2022-07-23 14:00:19 +02:00
|
|
|
dma: PeripheralRef<'d, Dma>,
|
2022-07-23 01:29:35 +02:00
|
|
|
|
2022-07-23 14:00:19 +02:00
|
|
|
clk: PeripheralRef<'d, AnyPin>,
|
|
|
|
cmd: PeripheralRef<'d, AnyPin>,
|
|
|
|
d0: PeripheralRef<'d, AnyPin>,
|
|
|
|
d1: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
d2: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
d3: Option<PeripheralRef<'d, AnyPin>>,
|
2022-07-23 01:29:35 +02:00
|
|
|
|
2022-03-15 18:58:19 +01:00
|
|
|
config: Config,
|
|
|
|
/// Current clock to card
|
2022-03-16 22:55:07 +01:00
|
|
|
clock: Hertz,
|
2022-03-15 18:58:19 +01:00
|
|
|
/// Current signalling scheme to card
|
|
|
|
signalling: Signalling,
|
|
|
|
/// Card
|
|
|
|
card: Option<Card>,
|
|
|
|
}
|
|
|
|
|
2022-03-16 19:20:39 +01:00
|
|
|
#[cfg(sdmmc_v1)]
|
2022-07-23 01:29:35 +02:00
|
|
|
impl<'d, T: Instance, Dma: SdmmcDma<T>> Sdmmc<'d, T, Dma> {
|
|
|
|
pub fn new_1bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
sdmmc: impl Peripheral<P = T> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
|
|
|
clk: impl Peripheral<P = impl CkPin<T>> + 'd,
|
|
|
|
cmd: impl Peripheral<P = impl CmdPin<T>> + 'd,
|
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
2022-07-23 01:29:35 +02:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2022-07-23 14:00:19 +02:00
|
|
|
into_ref!(clk, cmd, d0);
|
2022-07-23 01:29:35 +02:00
|
|
|
|
|
|
|
critical_section::with(|_| unsafe {
|
|
|
|
clk.set_as_af_pull(clk.af_num(), AFType::OutputPushPull, Pull::None);
|
|
|
|
cmd.set_as_af_pull(cmd.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d0.set_as_af_pull(d0.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
|
|
|
|
clk.set_speed(Speed::VeryHigh);
|
|
|
|
cmd.set_speed(Speed::VeryHigh);
|
|
|
|
d0.set_speed(Speed::VeryHigh);
|
|
|
|
});
|
|
|
|
|
|
|
|
Self::new_inner(
|
|
|
|
sdmmc,
|
|
|
|
dma,
|
|
|
|
clk.map_into(),
|
|
|
|
cmd.map_into(),
|
|
|
|
d0.map_into(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_4bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
sdmmc: impl Peripheral<P = T> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
|
|
|
clk: impl Peripheral<P = impl CkPin<T>> + 'd,
|
|
|
|
cmd: impl Peripheral<P = impl CmdPin<T>> + 'd,
|
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
2022-03-15 18:58:19 +01:00
|
|
|
config: Config,
|
2022-07-23 01:29:35 +02:00
|
|
|
) -> Self {
|
2022-07-23 14:00:19 +02:00
|
|
|
into_ref!(clk, cmd, d0, d1, d2, d3);
|
2022-07-23 01:29:35 +02:00
|
|
|
|
|
|
|
critical_section::with(|_| unsafe {
|
|
|
|
clk.set_as_af_pull(clk.af_num(), AFType::OutputPushPull, Pull::None);
|
|
|
|
cmd.set_as_af_pull(cmd.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d0.set_as_af_pull(d0.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d1.set_as_af_pull(d1.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d2.set_as_af_pull(d2.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d3.set_as_af_pull(d3.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
|
|
|
|
clk.set_speed(Speed::VeryHigh);
|
|
|
|
cmd.set_speed(Speed::VeryHigh);
|
|
|
|
d0.set_speed(Speed::VeryHigh);
|
|
|
|
d1.set_speed(Speed::VeryHigh);
|
|
|
|
d2.set_speed(Speed::VeryHigh);
|
|
|
|
d3.set_speed(Speed::VeryHigh);
|
|
|
|
});
|
|
|
|
|
|
|
|
Self::new_inner(
|
|
|
|
sdmmc,
|
|
|
|
dma,
|
|
|
|
clk.map_into(),
|
|
|
|
cmd.map_into(),
|
|
|
|
d0.map_into(),
|
|
|
|
Some(d1.map_into()),
|
|
|
|
Some(d2.map_into()),
|
|
|
|
Some(d3.map_into()),
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
}
|
2022-03-16 19:20:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(sdmmc_v2)]
|
2022-07-23 01:29:35 +02:00
|
|
|
impl<'d, T: Instance> Sdmmc<'d, T, NoDma> {
|
|
|
|
pub fn new_1bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
sdmmc: impl Peripheral<P = T> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
clk: impl Peripheral<P = impl CkPin<T>> + 'd,
|
|
|
|
cmd: impl Peripheral<P = impl CmdPin<T>> + 'd,
|
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
2022-07-23 01:29:35 +02:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2022-07-23 14:00:19 +02:00
|
|
|
into_ref!(clk, cmd, d0);
|
2022-07-23 01:29:35 +02:00
|
|
|
|
|
|
|
critical_section::with(|_| unsafe {
|
|
|
|
clk.set_as_af_pull(clk.af_num(), AFType::OutputPushPull, Pull::None);
|
|
|
|
cmd.set_as_af_pull(cmd.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d0.set_as_af_pull(d0.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
|
|
|
|
clk.set_speed(Speed::VeryHigh);
|
|
|
|
cmd.set_speed(Speed::VeryHigh);
|
|
|
|
d0.set_speed(Speed::VeryHigh);
|
|
|
|
});
|
|
|
|
|
|
|
|
Self::new_inner(
|
|
|
|
sdmmc,
|
2023-04-17 00:54:22 +02:00
|
|
|
NoDma.into_ref(),
|
2022-07-23 01:29:35 +02:00
|
|
|
clk.map_into(),
|
|
|
|
cmd.map_into(),
|
|
|
|
d0.map_into(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_4bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
sdmmc: impl Peripheral<P = T> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
clk: impl Peripheral<P = impl CkPin<T>> + 'd,
|
|
|
|
cmd: impl Peripheral<P = impl CmdPin<T>> + 'd,
|
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
2022-07-23 01:29:35 +02:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2022-07-23 14:00:19 +02:00
|
|
|
into_ref!(clk, cmd, d0, d1, d2, d3);
|
2022-07-23 01:29:35 +02:00
|
|
|
|
|
|
|
critical_section::with(|_| unsafe {
|
|
|
|
clk.set_as_af_pull(clk.af_num(), AFType::OutputPushPull, Pull::None);
|
|
|
|
cmd.set_as_af_pull(cmd.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d0.set_as_af_pull(d0.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d1.set_as_af_pull(d1.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d2.set_as_af_pull(d2.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
d3.set_as_af_pull(d3.af_num(), AFType::OutputPushPull, Pull::Up);
|
|
|
|
|
|
|
|
clk.set_speed(Speed::VeryHigh);
|
|
|
|
cmd.set_speed(Speed::VeryHigh);
|
|
|
|
d0.set_speed(Speed::VeryHigh);
|
|
|
|
d1.set_speed(Speed::VeryHigh);
|
|
|
|
d2.set_speed(Speed::VeryHigh);
|
|
|
|
d3.set_speed(Speed::VeryHigh);
|
|
|
|
});
|
|
|
|
|
|
|
|
Self::new_inner(
|
|
|
|
sdmmc,
|
2023-04-17 00:54:22 +02:00
|
|
|
NoDma.into_ref(),
|
2022-07-23 01:29:35 +02:00
|
|
|
clk.map_into(),
|
|
|
|
cmd.map_into(),
|
|
|
|
d0.map_into(),
|
|
|
|
Some(d1.map_into()),
|
|
|
|
Some(d2.map_into()),
|
|
|
|
Some(d3.map_into()),
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
}
|
2023-04-17 00:54:22 +02:00
|
|
|
}
|
2022-07-23 01:29:35 +02:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
impl<'d, T: Instance, Dma: SdmmcDma<T> + 'd> Sdmmc<'d, T, Dma> {
|
2022-07-23 01:29:35 +02:00
|
|
|
fn new_inner(
|
2022-07-23 14:00:19 +02:00
|
|
|
sdmmc: impl Peripheral<P = T> + 'd,
|
2023-04-17 00:54:22 +02:00
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
clk: PeripheralRef<'d, AnyPin>,
|
|
|
|
cmd: PeripheralRef<'d, AnyPin>,
|
|
|
|
d0: PeripheralRef<'d, AnyPin>,
|
|
|
|
d1: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
d2: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
d3: Option<PeripheralRef<'d, AnyPin>>,
|
2022-03-16 19:20:39 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-05-25 00:29:56 +02:00
|
|
|
into_ref!(sdmmc, dma);
|
2022-03-16 19:20:39 +01:00
|
|
|
|
|
|
|
T::enable();
|
|
|
|
T::reset();
|
|
|
|
|
2023-06-01 02:22:46 +02:00
|
|
|
T::Interrupt::unpend();
|
|
|
|
unsafe { T::Interrupt::enable() };
|
2022-03-16 19:20:39 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let regs = T::regs();
|
|
|
|
unsafe {
|
|
|
|
regs.clkcr().write(|w| {
|
|
|
|
w.set_pwrsav(false);
|
|
|
|
w.set_negedge(false);
|
|
|
|
|
|
|
|
// Hardware flow control is broken on SDIOv1 and causes clock glitches, which result in CRC errors.
|
|
|
|
// See chip erratas for more details.
|
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
w.set_hwfc_en(false);
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
w.set_hwfc_en(true);
|
|
|
|
|
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
w.set_clken(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Power off, writen 00: Clock to the card is stopped;
|
|
|
|
// D[7:0], CMD, and CK are driven high.
|
|
|
|
regs.power().modify(|w| w.set_pwrctrl(PowerCtrl::Off as u8));
|
|
|
|
}
|
|
|
|
|
2022-03-16 19:20:39 +01:00
|
|
|
Self {
|
2022-07-23 01:29:35 +02:00
|
|
|
_peri: sdmmc,
|
2023-04-17 00:54:22 +02:00
|
|
|
dma,
|
2022-07-23 01:29:35 +02:00
|
|
|
|
|
|
|
clk,
|
|
|
|
cmd,
|
|
|
|
d0,
|
|
|
|
d1,
|
|
|
|
d2,
|
|
|
|
d3,
|
|
|
|
|
2022-03-16 19:20:39 +01:00
|
|
|
config,
|
2022-12-06 15:36:07 +01:00
|
|
|
clock: SD_INIT_FREQ,
|
2022-03-16 19:20:39 +01:00
|
|
|
signalling: Default::default(),
|
|
|
|
card: None,
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Data transfer is in progress
|
2022-03-15 18:58:19 +01:00
|
|
|
#[inline(always)]
|
2023-04-17 00:54:22 +02:00
|
|
|
fn data_active() -> bool {
|
|
|
|
let regs = T::regs();
|
2022-07-23 01:29:35 +02:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// NOTE(unsafe) Atomic read with no side-effects
|
|
|
|
unsafe {
|
|
|
|
let status = regs.star().read();
|
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
return status.rxact() || status.txact();
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
return status.dpsmact();
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Coammand transfer is in progress
|
2022-03-15 18:58:19 +01:00
|
|
|
#[inline(always)]
|
2023-04-17 00:54:22 +02:00
|
|
|
fn cmd_active() -> bool {
|
|
|
|
let regs = T::regs();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// NOTE(unsafe) Atomic read with no side-effects
|
|
|
|
unsafe {
|
|
|
|
let status = regs.star().read();
|
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
return status.cmdact();
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
return status.cpsmact();
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Wait idle on CMDACT, RXACT and TXACT (v1) or DOSNACT and CPSMACT (v2)
|
|
|
|
#[inline(always)]
|
|
|
|
fn wait_idle() {
|
|
|
|
while Self::data_active() || Self::cmd_active() {}
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// # Safety
|
2022-03-15 18:58:19 +01:00
|
|
|
///
|
2023-04-17 00:54:22 +02:00
|
|
|
/// `buffer` must be valid for the whole transfer and word aligned
|
2023-04-17 00:04:54 +02:00
|
|
|
fn prepare_datapath_read<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
buffer: &'a mut [u32],
|
|
|
|
length_bytes: u32,
|
|
|
|
block_size: u8,
|
|
|
|
) -> Transfer<'a, Dma> {
|
2023-04-17 00:54:22 +02:00
|
|
|
assert!(block_size <= 14, "Block size up to 2^14 bytes");
|
|
|
|
let regs = T::regs();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Command AND Data state machines must be idle
|
|
|
|
Self::wait_idle();
|
|
|
|
Self::clear_interrupt_flags();
|
2022-03-16 22:55:07 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// NOTE(unsafe) We have exclusive access to the regisers
|
2023-04-17 00:04:54 +02:00
|
|
|
unsafe {
|
|
|
|
regs.dtimer()
|
|
|
|
.write(|w| w.set_datatime(self.config.data_transfer_timeout));
|
|
|
|
regs.dlenr().write(|w| w.set_datalength(length_bytes));
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
let transfer = {
|
|
|
|
let request = self.dma.request();
|
|
|
|
Transfer::new_read(
|
|
|
|
&mut self.dma,
|
|
|
|
request,
|
|
|
|
regs.fifor().ptr() as *mut u32,
|
|
|
|
buffer,
|
|
|
|
DMA_TRANSFER_OPTIONS,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
let transfer = {
|
|
|
|
regs.idmabase0r().write(|w| w.set_idmabase0(buffer.as_mut_ptr() as u32));
|
|
|
|
regs.idmactrlr().modify(|w| w.set_idmaen(true));
|
2023-05-19 18:00:33 +02:00
|
|
|
Transfer {
|
|
|
|
_dummy: core::marker::PhantomData,
|
|
|
|
}
|
2023-04-17 00:04:54 +02:00
|
|
|
};
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
regs.dctrl().modify(|w| {
|
|
|
|
w.set_dblocksize(block_size);
|
|
|
|
w.set_dtdir(true);
|
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
{
|
|
|
|
w.set_dmaen(true);
|
|
|
|
w.set_dten(true);
|
|
|
|
}
|
|
|
|
});
|
2022-07-23 01:29:35 +02:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
transfer
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
///
|
2023-04-17 00:54:22 +02:00
|
|
|
/// `buffer` must be valid for the whole transfer and word aligned
|
2023-04-17 00:04:54 +02:00
|
|
|
fn prepare_datapath_write<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
buffer: &'a [u32],
|
|
|
|
length_bytes: u32,
|
|
|
|
block_size: u8,
|
|
|
|
) -> Transfer<'a, Dma> {
|
2023-04-17 00:54:22 +02:00
|
|
|
assert!(block_size <= 14, "Block size up to 2^14 bytes");
|
|
|
|
let regs = T::regs();
|
|
|
|
|
|
|
|
// Command AND Data state machines must be idle
|
|
|
|
Self::wait_idle();
|
|
|
|
Self::clear_interrupt_flags();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// NOTE(unsafe) We have exclusive access to the regisers
|
2023-04-17 00:04:54 +02:00
|
|
|
unsafe {
|
|
|
|
regs.dtimer()
|
|
|
|
.write(|w| w.set_datatime(self.config.data_transfer_timeout));
|
|
|
|
regs.dlenr().write(|w| w.set_datalength(length_bytes));
|
2023-02-18 00:35:35 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
let transfer = {
|
|
|
|
let request = self.dma.request();
|
|
|
|
Transfer::new_write(
|
|
|
|
&mut self.dma,
|
|
|
|
request,
|
|
|
|
buffer,
|
|
|
|
regs.fifor().ptr() as *mut u32,
|
|
|
|
DMA_TRANSFER_OPTIONS,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
let transfer = {
|
|
|
|
regs.idmabase0r().write(|w| w.set_idmabase0(buffer.as_ptr() as u32));
|
|
|
|
regs.idmactrlr().modify(|w| w.set_idmaen(true));
|
2023-05-19 18:00:33 +02:00
|
|
|
Transfer {
|
|
|
|
_dummy: core::marker::PhantomData,
|
|
|
|
}
|
2023-04-17 00:04:54 +02:00
|
|
|
};
|
2023-04-17 00:54:22 +02:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
regs.dctrl().modify(|w| {
|
|
|
|
w.set_dblocksize(block_size);
|
|
|
|
w.set_dtdir(false);
|
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
{
|
|
|
|
w.set_dmaen(true);
|
|
|
|
w.set_dten(true);
|
|
|
|
}
|
|
|
|
});
|
2022-03-16 17:52:27 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
transfer
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Stops the DMA datapath
|
|
|
|
fn stop_datapath() {
|
|
|
|
let regs = T::regs();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
unsafe {
|
2023-04-17 00:54:22 +02:00
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
regs.dctrl().modify(|w| {
|
|
|
|
w.set_dmaen(false);
|
|
|
|
w.set_dten(false);
|
|
|
|
});
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
regs.idmactrlr().modify(|w| w.set_idmaen(false));
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 15:36:07 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Sets the CLKDIV field in CLKCR. Updates clock field in self
|
|
|
|
fn clkcr_set_clkdiv(&mut self, freq: u32, width: BusWidth) -> Result<(), Error> {
|
|
|
|
let regs = T::regs();
|
|
|
|
|
|
|
|
let width_u32 = match width {
|
|
|
|
BusWidth::One => 1u32,
|
|
|
|
BusWidth::Four => 4u32,
|
|
|
|
BusWidth::Eight => 8u32,
|
|
|
|
_ => panic!("Invalid Bus Width"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let ker_ck = T::kernel_clk();
|
|
|
|
let (_bypass, clkdiv, new_clock) = clk_div(ker_ck, freq)?;
|
|
|
|
|
|
|
|
// Enforce AHB and SDMMC_CK clock relation. See RM0433 Rev 7
|
|
|
|
// Section 55.5.8
|
|
|
|
let sdmmc_bus_bandwidth = new_clock.0 * width_u32;
|
|
|
|
assert!(ker_ck.0 > 3 * sdmmc_bus_bandwidth / 32);
|
|
|
|
self.clock = new_clock;
|
2022-12-06 15:36:07 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// NOTE(unsafe) We have exclusive access to the regblock
|
|
|
|
unsafe {
|
|
|
|
// CPSMACT and DPSMACT must be 0 to set CLKDIV
|
|
|
|
Self::wait_idle();
|
2022-12-06 15:36:07 +01:00
|
|
|
regs.clkcr().modify(|w| {
|
|
|
|
w.set_clkdiv(clkdiv);
|
2023-02-23 15:57:21 +01:00
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
w.set_bypass(_bypass);
|
2022-12-06 15:36:07 +01:00
|
|
|
});
|
2023-04-17 00:54:22 +02:00
|
|
|
}
|
2022-12-06 15:36:07 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Switch mode using CMD6.
|
|
|
|
///
|
|
|
|
/// Attempt to set a new signalling mode. The selected
|
|
|
|
/// signalling mode is returned. Expects the current clock
|
|
|
|
/// frequency to be > 12.5MHz.
|
|
|
|
async fn switch_signalling_mode(&mut self, signalling: Signalling) -> Result<Signalling, Error> {
|
|
|
|
// NB PLSS v7_10 4.3.10.4: "the use of SET_BLK_LEN command is not
|
|
|
|
// necessary"
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let set_function = 0x8000_0000
|
|
|
|
| match signalling {
|
|
|
|
// See PLSS v7_10 Table 4-11
|
|
|
|
Signalling::DDR50 => 0xFF_FF04,
|
|
|
|
Signalling::SDR104 => 0xFF_1F03,
|
|
|
|
Signalling::SDR50 => 0xFF_1F02,
|
|
|
|
Signalling::SDR25 => 0xFF_FF01,
|
|
|
|
Signalling::SDR12 => 0xFF_FF00,
|
2022-03-15 18:58:19 +01:00
|
|
|
};
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let mut status = [0u32; 16];
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Arm `OnDrop` after the buffer, so it will be dropped first
|
|
|
|
let regs = T::regs();
|
|
|
|
let on_drop = OnDrop::new(|| unsafe { Self::on_drop() });
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
let transfer = self.prepare_datapath_read(&mut status, 64, 6);
|
2023-05-25 00:29:56 +02:00
|
|
|
InterruptHandler::<T>::data_interrupts(true);
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::cmd6(set_function), true)?; // CMD6
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let res = poll_fn(|cx| {
|
|
|
|
T::state().register(cx.waker());
|
|
|
|
let status = unsafe { regs.star().read() };
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
if status.dcrcfail() {
|
|
|
|
return Poll::Ready(Err(Error::Crc));
|
|
|
|
} else if status.dtimeout() {
|
|
|
|
return Poll::Ready(Err(Error::Timeout));
|
|
|
|
} else if status.dataend() {
|
|
|
|
return Poll::Ready(Ok(()));
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
2023-04-17 00:54:22 +02:00
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
Self::clear_interrupt_flags();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Host is allowed to use the new functions at least 8
|
|
|
|
// clocks after the end of the switch command
|
|
|
|
// transaction. We know the current clock period is < 80ns,
|
|
|
|
// so a total delay of 640ns is required here
|
|
|
|
for _ in 0..300 {
|
|
|
|
cortex_m::asm::nop();
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
match res {
|
|
|
|
Ok(_) => {
|
|
|
|
on_drop.defuse();
|
|
|
|
Self::stop_datapath();
|
2023-04-17 00:04:54 +02:00
|
|
|
drop(transfer);
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Function Selection of Function Group 1
|
|
|
|
let selection = (u32::from_be(status[4]) >> 24) & 0xF;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
match selection {
|
|
|
|
0 => Ok(Signalling::SDR12),
|
|
|
|
1 => Ok(Signalling::SDR25),
|
|
|
|
2 => Ok(Signalling::SDR50),
|
|
|
|
3 => Ok(Signalling::SDR104),
|
|
|
|
4 => Ok(Signalling::DDR50),
|
|
|
|
_ => Err(Error::UnsupportedCardType),
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-17 00:54:22 +02:00
|
|
|
Err(e) => Err(e),
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
2023-04-17 00:54:22 +02:00
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Query the card status (CMD13, returns R1)
|
|
|
|
fn read_status(&self, card: &Card) -> Result<CardStatus, Error> {
|
|
|
|
let regs = T::regs();
|
|
|
|
let rca = card.rca;
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::card_status(rca << 16), false)?; // CMD13
|
2023-04-17 00:54:22 +02:00
|
|
|
|
|
|
|
// NOTE(unsafe) Atomic read with no side-effects
|
|
|
|
let r1 = unsafe { regs.respr(0).read().cardstatus() };
|
|
|
|
Ok(r1.into())
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Reads the SD Status (ACMD13)
|
|
|
|
async fn read_sd_status(&mut self) -> Result<(), Error> {
|
|
|
|
let card = self.card.as_mut().ok_or(Error::NoCard)?;
|
|
|
|
let rca = card.rca;
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::set_block_length(64), false)?; // CMD16
|
|
|
|
Self::cmd(Cmd::app_cmd(rca << 16), false)?; // APP
|
2023-04-17 00:54:22 +02:00
|
|
|
|
|
|
|
let mut status = [0u32; 16];
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Arm `OnDrop` after the buffer, so it will be dropped first
|
|
|
|
let regs = T::regs();
|
|
|
|
let on_drop = OnDrop::new(|| unsafe { Self::on_drop() });
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
let transfer = self.prepare_datapath_read(&mut status, 64, 6);
|
2023-05-25 00:29:56 +02:00
|
|
|
InterruptHandler::<T>::data_interrupts(true);
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::card_status(0), true)?;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
let res = poll_fn(|cx| {
|
2023-04-17 00:54:22 +02:00
|
|
|
T::state().register(cx.waker());
|
2022-03-16 17:52:27 +01:00
|
|
|
let status = unsafe { regs.star().read() };
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
if status.dcrcfail() {
|
|
|
|
return Poll::Ready(Err(Error::Crc));
|
|
|
|
} else if status.dtimeout() {
|
|
|
|
return Poll::Ready(Err(Error::Timeout));
|
|
|
|
} else if status.dataend() {
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
2023-04-17 00:54:22 +02:00
|
|
|
Self::clear_interrupt_flags();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
if res.is_ok() {
|
|
|
|
on_drop.defuse();
|
2023-04-17 00:54:22 +02:00
|
|
|
Self::stop_datapath();
|
2023-04-17 00:04:54 +02:00
|
|
|
drop(transfer);
|
2023-04-17 00:54:22 +02:00
|
|
|
|
|
|
|
for byte in status.iter_mut() {
|
|
|
|
*byte = u32::from_be(*byte);
|
|
|
|
}
|
|
|
|
self.card.as_mut().unwrap().status = status.into();
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Select one card and place it into the _Tranfer State_
|
|
|
|
///
|
|
|
|
/// If `None` is specifed for `card`, all cards are put back into
|
|
|
|
/// _Stand-by State_
|
|
|
|
fn select_card(&self, card: Option<&Card>) -> Result<(), Error> {
|
|
|
|
// Determine Relative Card Address (RCA) of given card
|
|
|
|
let rca = card.map(|c| c.rca << 16).unwrap_or(0);
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
let r = Self::cmd(Cmd::sel_desel_card(rca), false);
|
2023-04-17 00:54:22 +02:00
|
|
|
match (r, rca) {
|
|
|
|
(Err(Error::Timeout), 0) => Ok(()),
|
|
|
|
_ => r,
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Clear flags in interrupt clear register
|
|
|
|
#[inline(always)]
|
|
|
|
fn clear_interrupt_flags() {
|
|
|
|
let regs = T::regs();
|
|
|
|
// NOTE(unsafe) Atomic write
|
|
|
|
unsafe {
|
|
|
|
regs.icr().write(|w| {
|
|
|
|
w.set_ccrcfailc(true);
|
|
|
|
w.set_dcrcfailc(true);
|
|
|
|
w.set_ctimeoutc(true);
|
|
|
|
w.set_dtimeoutc(true);
|
|
|
|
w.set_txunderrc(true);
|
|
|
|
w.set_rxoverrc(true);
|
|
|
|
w.set_cmdrendc(true);
|
|
|
|
w.set_cmdsentc(true);
|
|
|
|
w.set_dataendc(true);
|
|
|
|
w.set_dbckendc(true);
|
|
|
|
w.set_sdioitc(true);
|
|
|
|
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
{
|
|
|
|
w.set_dholdc(true);
|
|
|
|
w.set_dabortc(true);
|
|
|
|
w.set_busyd0endc(true);
|
|
|
|
w.set_ackfailc(true);
|
|
|
|
w.set_acktimeoutc(true);
|
|
|
|
w.set_vswendc(true);
|
|
|
|
w.set_ckstopc(true);
|
|
|
|
w.set_idmatec(true);
|
|
|
|
w.set_idmabtcc(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-02-18 00:35:35 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
async fn get_scr(&mut self, card: &mut Card) -> Result<(), Error> {
|
|
|
|
// Read the the 64-bit SCR register
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::set_block_length(8), false)?; // CMD16
|
|
|
|
Self::cmd(Cmd::app_cmd(card.rca << 16), false)?;
|
2023-04-17 00:54:22 +02:00
|
|
|
|
|
|
|
let mut scr = [0u32; 2];
|
|
|
|
|
|
|
|
// Arm `OnDrop` after the buffer, so it will be dropped first
|
|
|
|
let regs = T::regs();
|
|
|
|
let on_drop = OnDrop::new(|| unsafe { Self::on_drop() });
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
let transfer = self.prepare_datapath_read(&mut scr[..], 8, 3);
|
2023-05-25 00:29:56 +02:00
|
|
|
InterruptHandler::<T>::data_interrupts(true);
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::cmd51(), true)?;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
let res = poll_fn(|cx| {
|
2023-04-17 00:54:22 +02:00
|
|
|
T::state().register(cx.waker());
|
2022-03-16 17:52:27 +01:00
|
|
|
let status = unsafe { regs.star().read() };
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
if status.dcrcfail() {
|
|
|
|
return Poll::Ready(Err(Error::Crc));
|
|
|
|
} else if status.dtimeout() {
|
|
|
|
return Poll::Ready(Err(Error::Timeout));
|
|
|
|
} else if status.dataend() {
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
2023-04-17 00:54:22 +02:00
|
|
|
Self::clear_interrupt_flags();
|
2022-03-16 17:52:27 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
if res.is_ok() {
|
|
|
|
on_drop.defuse();
|
|
|
|
Self::stop_datapath();
|
2023-04-17 00:04:54 +02:00
|
|
|
drop(transfer);
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
unsafe {
|
|
|
|
let scr_bytes = &*(&scr as *const [u32; 2] as *const [u8; 8]);
|
|
|
|
card.scr = SCR(u64::from_be_bytes(*scr_bytes));
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-17 00:54:22 +02:00
|
|
|
res
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Send command to card
|
|
|
|
#[allow(unused_variables)]
|
2023-04-17 00:04:54 +02:00
|
|
|
fn cmd(cmd: Cmd, data: bool) -> Result<(), Error> {
|
2023-04-17 00:54:22 +02:00
|
|
|
let regs = T::regs();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
Self::clear_interrupt_flags();
|
|
|
|
// NOTE(safety) Atomic operations
|
2022-03-15 18:58:19 +01:00
|
|
|
unsafe {
|
2023-04-17 00:54:22 +02:00
|
|
|
// CP state machine must be idle
|
|
|
|
while Self::cmd_active() {}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Command arg
|
|
|
|
regs.argr().write(|w| w.set_cmdarg(cmd.arg));
|
2022-03-16 17:52:27 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Command index and start CP State Machine
|
|
|
|
regs.cmdr().write(|w| {
|
|
|
|
w.set_waitint(false);
|
|
|
|
w.set_waitresp(cmd.resp as u8);
|
|
|
|
w.set_cmdindex(cmd.cmd);
|
|
|
|
w.set_cpsmen(true);
|
2022-03-16 17:52:27 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
{
|
|
|
|
// Special mode in CP State Machine
|
|
|
|
// CMD12: Stop Transmission
|
|
|
|
let cpsm_stop_transmission = cmd.cmd == 12;
|
|
|
|
w.set_cmdstop(cpsm_stop_transmission);
|
|
|
|
w.set_cmdtrans(data);
|
|
|
|
}
|
|
|
|
});
|
2022-03-16 17:52:27 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let mut status;
|
|
|
|
if cmd.resp == Response::None {
|
|
|
|
// Wait for CMDSENT or a timeout
|
|
|
|
while {
|
|
|
|
status = regs.star().read();
|
|
|
|
!(status.ctimeout() || status.cmdsent())
|
|
|
|
} {}
|
|
|
|
} else {
|
|
|
|
// Wait for CMDREND or CCRCFAIL or a timeout
|
|
|
|
while {
|
|
|
|
status = regs.star().read();
|
|
|
|
!(status.ctimeout() || status.cmdrend() || status.ccrcfail())
|
|
|
|
} {}
|
|
|
|
}
|
2022-03-16 17:52:27 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
if status.ctimeout() {
|
|
|
|
return Err(Error::Timeout);
|
|
|
|
} else if status.ccrcfail() {
|
|
|
|
return Err(Error::Crc);
|
2022-03-16 17:52:27 +01:00
|
|
|
}
|
2023-04-17 00:54:22 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-03-16 17:52:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
///
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Ensure that `regs` has exclusive access to the regblocks
|
|
|
|
unsafe fn on_drop() {
|
|
|
|
let regs = T::regs();
|
|
|
|
if Self::data_active() {
|
|
|
|
Self::clear_interrupt_flags();
|
|
|
|
// Send abort
|
|
|
|
// CP state machine must be idle
|
|
|
|
while Self::cmd_active() {}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Command arg
|
|
|
|
regs.argr().write(|w| w.set_cmdarg(0));
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Command index and start CP State Machine
|
|
|
|
regs.cmdr().write(|w| {
|
|
|
|
w.set_waitint(false);
|
|
|
|
w.set_waitresp(Response::Short as u8);
|
|
|
|
w.set_cmdindex(12);
|
|
|
|
w.set_cpsmen(true);
|
2022-03-16 17:52:27 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
{
|
|
|
|
w.set_cmdstop(true);
|
|
|
|
w.set_cmdtrans(false);
|
|
|
|
}
|
2023-04-17 00:13:25 +02:00
|
|
|
});
|
2023-04-17 00:54:22 +02:00
|
|
|
|
|
|
|
// Wait for the abort
|
|
|
|
while Self::data_active() {}
|
2022-03-16 17:52:27 +01:00
|
|
|
}
|
2023-05-25 00:29:56 +02:00
|
|
|
InterruptHandler::<T>::data_interrupts(false);
|
2023-04-17 00:54:22 +02:00
|
|
|
Self::clear_interrupt_flags();
|
|
|
|
Self::stop_datapath();
|
2022-03-16 17:52:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Initializes card (if present) and sets the bus at the
|
|
|
|
/// specified frequency.
|
|
|
|
pub async fn init_card(&mut self, freq: Hertz) -> Result<(), Error> {
|
|
|
|
let regs = T::regs();
|
|
|
|
let ker_ck = T::kernel_clk();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let bus_width = match self.d3.is_some() {
|
|
|
|
true => BusWidth::Four,
|
|
|
|
false => BusWidth::One,
|
2022-08-17 14:44:18 +02:00
|
|
|
};
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// NOTE(unsafe) We have exclusive access to the peripheral
|
|
|
|
unsafe {
|
|
|
|
// While the SD/SDIO card or eMMC is in identification mode,
|
|
|
|
// the SDMMC_CK frequency must be no more than 400 kHz.
|
|
|
|
let (_bypass, clkdiv, init_clock) = unwrap!(clk_div(ker_ck, SD_INIT_FREQ.0));
|
|
|
|
self.clock = init_clock;
|
2023-02-23 15:57:21 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// CPSMACT and DPSMACT must be 0 to set WIDBUS
|
|
|
|
Self::wait_idle();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-02-23 15:57:21 +01:00
|
|
|
regs.clkcr().modify(|w| {
|
2023-04-17 00:54:22 +02:00
|
|
|
w.set_widbus(0);
|
2023-02-23 15:57:21 +01:00
|
|
|
w.set_clkdiv(clkdiv);
|
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
w.set_bypass(_bypass);
|
|
|
|
});
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
regs.power().modify(|w| w.set_pwrctrl(PowerCtrl::On as u8));
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::idle(), false)?;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Check if cards supports CMD8 (with pattern)
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::hs_send_ext_csd(0x1AA), false)?;
|
2023-04-17 00:54:22 +02:00
|
|
|
let r1 = regs.respr(0).read().cardstatus();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let mut card = if r1 == 0x1AA {
|
|
|
|
// Card echoed back the pattern. Must be at least v2
|
|
|
|
Card::default()
|
|
|
|
} else {
|
|
|
|
return Err(Error::UnsupportedCardVersion);
|
2022-03-15 18:58:19 +01:00
|
|
|
};
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let ocr = loop {
|
|
|
|
// Signal that next command is a app command
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::app_cmd(0), false)?; // CMD55
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let arg = CmdAppOper::VOLTAGE_WINDOW_SD as u32
|
|
|
|
| CmdAppOper::HIGH_CAPACITY as u32
|
|
|
|
| CmdAppOper::SD_SWITCH_1_8V_CAPACITY as u32;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Initialize card
|
2023-04-17 00:04:54 +02:00
|
|
|
match Self::cmd(Cmd::app_op_cmd(arg), false) {
|
2023-04-17 00:54:22 +02:00
|
|
|
// ACMD41
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(Error::Crc) => (),
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
|
|
|
let ocr: OCR = regs.respr(0).read().cardstatus().into();
|
|
|
|
if !ocr.is_busy() {
|
|
|
|
// Power up done
|
|
|
|
break ocr;
|
|
|
|
}
|
|
|
|
};
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
if ocr.high_capacity() {
|
|
|
|
// Card is SDHC or SDXC or SDUC
|
|
|
|
card.card_type = CardCapacity::SDHC;
|
|
|
|
} else {
|
|
|
|
card.card_type = CardCapacity::SDSC;
|
|
|
|
}
|
|
|
|
card.ocr = ocr;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::all_send_cid(), false)?; // CMD2
|
2023-04-17 00:54:22 +02:00
|
|
|
let cid0 = regs.respr(0).read().cardstatus() as u128;
|
|
|
|
let cid1 = regs.respr(1).read().cardstatus() as u128;
|
|
|
|
let cid2 = regs.respr(2).read().cardstatus() as u128;
|
|
|
|
let cid3 = regs.respr(3).read().cardstatus() as u128;
|
|
|
|
let cid = (cid0 << 96) | (cid1 << 64) | (cid2 << 32) | (cid3);
|
|
|
|
card.cid = cid.into();
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::send_rel_addr(), false)?;
|
2023-04-17 00:54:22 +02:00
|
|
|
card.rca = regs.respr(0).read().cardstatus() >> 16;
|
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::send_csd(card.rca << 16), false)?;
|
2023-04-17 00:54:22 +02:00
|
|
|
let csd0 = regs.respr(0).read().cardstatus() as u128;
|
|
|
|
let csd1 = regs.respr(1).read().cardstatus() as u128;
|
|
|
|
let csd2 = regs.respr(2).read().cardstatus() as u128;
|
|
|
|
let csd3 = regs.respr(3).read().cardstatus() as u128;
|
|
|
|
let csd = (csd0 << 96) | (csd1 << 64) | (csd2 << 32) | (csd3);
|
|
|
|
card.csd = csd.into();
|
|
|
|
|
|
|
|
self.select_card(Some(&card))?;
|
|
|
|
|
|
|
|
self.get_scr(&mut card).await?;
|
|
|
|
|
|
|
|
// Set bus width
|
|
|
|
let (width, acmd_arg) = match bus_width {
|
|
|
|
BusWidth::Eight => unimplemented!(),
|
|
|
|
BusWidth::Four if card.scr.bus_width_four() => (BusWidth::Four, 2),
|
|
|
|
_ => (BusWidth::One, 0),
|
|
|
|
};
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::app_cmd(card.rca << 16), false)?;
|
|
|
|
Self::cmd(Cmd::cmd6(acmd_arg), false)?;
|
2023-04-17 00:54:22 +02:00
|
|
|
|
|
|
|
// CPSMACT and DPSMACT must be 0 to set WIDBUS
|
|
|
|
Self::wait_idle();
|
|
|
|
|
|
|
|
regs.clkcr().modify(|w| {
|
|
|
|
w.set_widbus(match width {
|
|
|
|
BusWidth::One => 0,
|
|
|
|
BusWidth::Four => 1,
|
|
|
|
BusWidth::Eight => 2,
|
|
|
|
_ => panic!("Invalid Bus Width"),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set Clock
|
|
|
|
if freq.0 <= 25_000_000 {
|
|
|
|
// Final clock frequency
|
|
|
|
self.clkcr_set_clkdiv(freq.0, width)?;
|
|
|
|
} else {
|
|
|
|
// Switch to max clock for SDR12
|
|
|
|
self.clkcr_set_clkdiv(25_000_000, width)?;
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
self.card = Some(card);
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Read status
|
|
|
|
self.read_sd_status().await?;
|
|
|
|
|
|
|
|
if freq.0 > 25_000_000 {
|
|
|
|
// Switch to SDR25
|
|
|
|
self.signalling = self.switch_signalling_mode(Signalling::SDR25).await?;
|
2022-03-16 17:52:27 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
if self.signalling == Signalling::SDR25 {
|
|
|
|
// Set final clock frequency
|
|
|
|
self.clkcr_set_clkdiv(freq.0, width)?;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
if self.read_status(&card)?.state() != CurrentState::Transfer {
|
|
|
|
return Err(Error::SignalingSwitchFailed);
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-17 00:54:22 +02:00
|
|
|
// Read status after signalling change
|
|
|
|
self.read_sd_status().await?;
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
Ok(())
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
#[inline(always)]
|
|
|
|
pub async fn read_block(&mut self, block_idx: u32, buffer: &mut DataBlock) -> Result<(), Error> {
|
|
|
|
let card_capacity = self.card()?.card_type;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// NOTE(unsafe) DataBlock uses align 4
|
|
|
|
let buffer = unsafe { &mut *((&mut buffer.0) as *mut [u8; 512] as *mut [u32; 128]) };
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Always read 1 block of 512 bytes
|
|
|
|
// SDSC cards are byte addressed hence the blockaddress is in multiples of 512 bytes
|
|
|
|
let address = match card_capacity {
|
|
|
|
CardCapacity::SDSC => block_idx * 512,
|
|
|
|
_ => block_idx,
|
|
|
|
};
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::set_block_length(512), false)?; // CMD16
|
2023-04-17 00:54:22 +02:00
|
|
|
|
|
|
|
let regs = T::regs();
|
|
|
|
let on_drop = OnDrop::new(|| unsafe { Self::on_drop() });
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
let transfer = self.prepare_datapath_read(buffer, 512, 9);
|
2023-05-25 00:29:56 +02:00
|
|
|
InterruptHandler::<T>::data_interrupts(true);
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::read_single_block(address), true)?;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
let res = poll_fn(|cx| {
|
2023-04-17 00:54:22 +02:00
|
|
|
T::state().register(cx.waker());
|
2022-03-16 17:52:27 +01:00
|
|
|
let status = unsafe { regs.star().read() };
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
if status.dcrcfail() {
|
|
|
|
return Poll::Ready(Err(Error::Crc));
|
|
|
|
} else if status.dtimeout() {
|
|
|
|
return Poll::Ready(Err(Error::Timeout));
|
|
|
|
} else if status.dataend() {
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
2023-04-17 00:54:22 +02:00
|
|
|
Self::clear_interrupt_flags();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
if res.is_ok() {
|
|
|
|
on_drop.defuse();
|
2023-04-17 00:54:22 +02:00
|
|
|
Self::stop_datapath();
|
2023-04-17 00:04:54 +02:00
|
|
|
drop(transfer);
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
pub async fn write_block(&mut self, block_idx: u32, buffer: &DataBlock) -> Result<(), Error> {
|
|
|
|
let card = self.card.as_mut().ok_or(Error::NoCard)?;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// NOTE(unsafe) DataBlock uses align 4
|
|
|
|
let buffer = unsafe { &*((&buffer.0) as *const [u8; 512] as *const [u32; 128]) };
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Always read 1 block of 512 bytes
|
|
|
|
// SDSC cards are byte addressed hence the blockaddress is in multiples of 512 bytes
|
|
|
|
let address = match card.card_type {
|
|
|
|
CardCapacity::SDSC => block_idx * 512,
|
|
|
|
_ => block_idx,
|
|
|
|
};
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::set_block_length(512), false)?; // CMD16
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
let regs = T::regs();
|
|
|
|
let on_drop = OnDrop::new(|| unsafe { Self::on_drop() });
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// sdmmc_v1 uses different cmd/dma order than v2, but only for writes
|
|
|
|
#[cfg(sdmmc_v1)]
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::write_single_block(address), true)?;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
let transfer = self.prepare_datapath_write(buffer, 512, 9);
|
2023-05-25 00:29:56 +02:00
|
|
|
InterruptHandler::<T>::data_interrupts(true);
|
2023-04-17 00:54:22 +02:00
|
|
|
|
|
|
|
#[cfg(sdmmc_v2)]
|
2023-04-17 00:04:54 +02:00
|
|
|
Self::cmd(Cmd::write_single_block(address), true)?;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
let res = poll_fn(|cx| {
|
2023-04-17 00:54:22 +02:00
|
|
|
T::state().register(cx.waker());
|
2022-03-16 17:52:27 +01:00
|
|
|
let status = unsafe { regs.star().read() };
|
2022-03-15 18:58:19 +01:00
|
|
|
|
|
|
|
if status.dcrcfail() {
|
|
|
|
return Poll::Ready(Err(Error::Crc));
|
|
|
|
} else if status.dtimeout() {
|
|
|
|
return Poll::Ready(Err(Error::Timeout));
|
|
|
|
} else if status.dataend() {
|
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
2023-04-17 00:54:22 +02:00
|
|
|
Self::clear_interrupt_flags();
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
match res {
|
|
|
|
Ok(_) => {
|
|
|
|
on_drop.defuse();
|
|
|
|
Self::stop_datapath();
|
2023-04-17 00:04:54 +02:00
|
|
|
drop(transfer);
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// TODO: Make this configurable
|
|
|
|
let mut timeout: u32 = 0x00FF_FFFF;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
// Try to read card status (ACMD13)
|
|
|
|
while timeout > 0 {
|
|
|
|
match self.read_sd_status().await {
|
|
|
|
Ok(_) => return Ok(()),
|
|
|
|
Err(Error::Timeout) => (), // Try again
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
timeout -= 1;
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
2023-04-17 00:54:22 +02:00
|
|
|
Err(Error::SoftwareTimeout)
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
2023-04-17 00:54:22 +02:00
|
|
|
Err(e) => Err(e),
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Get a reference to the initialized card
|
2022-03-15 18:58:19 +01:00
|
|
|
///
|
2023-04-17 00:54:22 +02:00
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Returns Error::NoCard if [`init_card`](#method.init_card)
|
|
|
|
/// has not previously succeeded
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn card(&self) -> Result<&Card, Error> {
|
|
|
|
self.card.as_ref().ok_or(Error::NoCard)
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
/// Get the current SDMMC bus clock
|
|
|
|
pub fn clock(&self) -> Hertz {
|
|
|
|
self.clock
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
impl<'d, T: Instance, Dma: SdmmcDma<T> + 'd> Drop for Sdmmc<'d, T, Dma> {
|
|
|
|
fn drop(&mut self) {
|
2023-06-01 02:22:46 +02:00
|
|
|
T::Interrupt::disable();
|
2023-04-17 00:54:22 +02:00
|
|
|
unsafe { Self::on_drop() };
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
critical_section::with(|_| unsafe {
|
|
|
|
self.clk.set_as_disconnected();
|
|
|
|
self.cmd.set_as_disconnected();
|
|
|
|
self.d0.set_as_disconnected();
|
|
|
|
if let Some(x) = &mut self.d1 {
|
|
|
|
x.set_as_disconnected();
|
|
|
|
}
|
|
|
|
if let Some(x) = &mut self.d2 {
|
|
|
|
x.set_as_disconnected();
|
|
|
|
}
|
|
|
|
if let Some(x) = &mut self.d3 {
|
|
|
|
x.set_as_disconnected();
|
|
|
|
}
|
|
|
|
});
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SD card Commands
|
|
|
|
impl Cmd {
|
|
|
|
const fn new(cmd: u8, arg: u32, resp: Response) -> Cmd {
|
|
|
|
Cmd { cmd, arg, resp }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD0: Idle
|
|
|
|
const fn idle() -> Cmd {
|
|
|
|
Cmd::new(0, 0, Response::None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD2: Send CID
|
|
|
|
const fn all_send_cid() -> Cmd {
|
|
|
|
Cmd::new(2, 0, Response::Long)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD3: Send Relative Address
|
|
|
|
const fn send_rel_addr() -> Cmd {
|
|
|
|
Cmd::new(3, 0, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD6: Switch Function Command
|
|
|
|
/// ACMD6: Bus Width
|
|
|
|
const fn cmd6(arg: u32) -> Cmd {
|
|
|
|
Cmd::new(6, arg, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD7: Select one card and put it into the _Tranfer State_
|
|
|
|
const fn sel_desel_card(rca: u32) -> Cmd {
|
|
|
|
Cmd::new(7, rca, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD8:
|
|
|
|
const fn hs_send_ext_csd(arg: u32) -> Cmd {
|
|
|
|
Cmd::new(8, arg, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD9:
|
|
|
|
const fn send_csd(rca: u32) -> Cmd {
|
|
|
|
Cmd::new(9, rca, Response::Long)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD12:
|
|
|
|
//const fn stop_transmission() -> Cmd {
|
|
|
|
// Cmd::new(12, 0, Response::Short)
|
|
|
|
//}
|
|
|
|
|
|
|
|
/// CMD13: Ask card to send status register
|
|
|
|
/// ACMD13: SD Status
|
|
|
|
const fn card_status(rca: u32) -> Cmd {
|
|
|
|
Cmd::new(13, rca, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD16:
|
|
|
|
const fn set_block_length(blocklen: u32) -> Cmd {
|
|
|
|
Cmd::new(16, blocklen, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD17: Block Read
|
|
|
|
const fn read_single_block(addr: u32) -> Cmd {
|
|
|
|
Cmd::new(17, addr, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CMD18: Multiple Block Read
|
|
|
|
//const fn read_multiple_blocks(addr: u32) -> Cmd {
|
|
|
|
// Cmd::new(18, addr, Response::Short)
|
|
|
|
//}
|
|
|
|
|
|
|
|
/// CMD24: Block Write
|
|
|
|
const fn write_single_block(addr: u32) -> Cmd {
|
|
|
|
Cmd::new(24, addr, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn app_op_cmd(arg: u32) -> Cmd {
|
|
|
|
Cmd::new(41, arg, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn cmd51() -> Cmd {
|
|
|
|
Cmd::new(51, 0, Response::Short)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// App Command. Indicates that next command will be a app command
|
|
|
|
const fn app_cmd(rca: u32) -> Cmd {
|
|
|
|
Cmd::new(55, rca, Response::Short)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Instance {
|
2023-06-08 16:08:40 +02:00
|
|
|
type Interrupt: interrupt::typelevel::Interrupt;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
fn regs() -> RegBlock;
|
2022-03-15 18:58:19 +01:00
|
|
|
fn state() -> &'static AtomicWaker;
|
2023-02-23 16:38:52 +01:00
|
|
|
fn kernel_clk() -> Hertz;
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Pins<T: Instance> {}
|
|
|
|
}
|
|
|
|
|
2022-03-16 17:52:27 +01:00
|
|
|
pub trait Instance: sealed::Instance + RccPeripheral + 'static {}
|
2022-03-15 18:58:19 +01:00
|
|
|
pin_trait!(CkPin, Instance);
|
|
|
|
pin_trait!(CmdPin, 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);
|
|
|
|
|
2023-04-17 00:13:34 +02:00
|
|
|
#[cfg(sdmmc_v1)]
|
|
|
|
dma_trait!(SdmmcDma, Instance);
|
|
|
|
|
|
|
|
// SDMMCv2 uses internal DMA
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
pub trait SdmmcDma<T: Instance> {}
|
|
|
|
#[cfg(sdmmc_v2)]
|
|
|
|
impl<T: Instance> SdmmcDma<T> for NoDma {}
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-02-23 16:38:52 +01:00
|
|
|
cfg_if::cfg_if! {
|
|
|
|
// TODO, these could not be implemented, because required clocks are not exposed in RCC:
|
|
|
|
// - H7 uses pll1_q_ck or pll2_r_ck depending on SDMMCSEL
|
|
|
|
// - L1 uses pll48
|
|
|
|
// - L4 uses clk48(pll48)
|
|
|
|
// - L4+, L5, U5 uses clk48(pll48) or PLLSAI3CLK(PLLP) depending on SDMMCSEL
|
|
|
|
if #[cfg(stm32f1)] {
|
|
|
|
// F1 uses AHB1(HCLK), which is correct in PAC
|
|
|
|
macro_rules! kernel_clk {
|
|
|
|
($inst:ident) => {
|
2023-02-23 17:00:55 +01:00
|
|
|
<peripherals::$inst as crate::rcc::sealed::RccPeripheral>::frequency()
|
2023-02-23 16:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if #[cfg(any(stm32f2, stm32f4))] {
|
|
|
|
// F2, F4 always use pll48
|
|
|
|
macro_rules! kernel_clk {
|
|
|
|
($inst:ident) => {
|
|
|
|
critical_section::with(|_| unsafe {
|
|
|
|
crate::rcc::get_freqs().pll48
|
|
|
|
}).expect("PLL48 is required for SDIO")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if #[cfg(stm32f7)] {
|
|
|
|
macro_rules! kernel_clk {
|
|
|
|
(SDMMC1) => {
|
|
|
|
critical_section::with(|_| unsafe {
|
|
|
|
let sdmmcsel = crate::pac::RCC.dckcfgr2().read().sdmmc1sel();
|
|
|
|
if sdmmcsel == crate::pac::rcc::vals::Sdmmcsel::SYSCLK {
|
|
|
|
crate::rcc::get_freqs().sys
|
|
|
|
} else {
|
|
|
|
crate::rcc::get_freqs().pll48.expect("PLL48 is required for SDMMC")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
(SDMMC2) => {
|
|
|
|
critical_section::with(|_| unsafe {
|
|
|
|
let sdmmcsel = crate::pac::RCC.dckcfgr2().read().sdmmc2sel();
|
|
|
|
if sdmmcsel == crate::pac::rcc::vals::Sdmmcsel::SYSCLK {
|
|
|
|
crate::rcc::get_freqs().sys
|
|
|
|
} else {
|
|
|
|
crate::rcc::get_freqs().pll48.expect("PLL48 is required for SDMMC")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use default peripheral clock and hope it works
|
|
|
|
macro_rules! kernel_clk {
|
|
|
|
($inst:ident) => {
|
2023-02-23 17:00:55 +01:00
|
|
|
<peripherals::$inst as crate::rcc::sealed::RccPeripheral>::frequency()
|
2023-02-23 16:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 18:58:19 +01:00
|
|
|
foreach_peripheral!(
|
|
|
|
(sdmmc, $inst:ident) => {
|
|
|
|
impl sealed::Instance for peripherals::$inst {
|
2023-06-08 16:08:40 +02:00
|
|
|
type Interrupt = crate::interrupt::typelevel::$inst;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-17 00:54:22 +02:00
|
|
|
fn regs() -> RegBlock {
|
|
|
|
crate::pac::$inst
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2022-08-22 21:46:09 +02:00
|
|
|
fn state() -> &'static ::embassy_sync::waitqueue::AtomicWaker {
|
|
|
|
static WAKER: ::embassy_sync::waitqueue::AtomicWaker = ::embassy_sync::waitqueue::AtomicWaker::new();
|
2022-03-15 18:58:19 +01:00
|
|
|
&WAKER
|
|
|
|
}
|
2023-02-23 16:38:52 +01:00
|
|
|
|
|
|
|
fn kernel_clk() -> Hertz {
|
|
|
|
kernel_clk!($inst)
|
|
|
|
}
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Instance for peripherals::$inst {}
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
2023-01-21 00:24:07 +01:00
|
|
|
#[cfg(feature = "embedded-sdmmc")]
|
2022-03-15 18:58:19 +01:00
|
|
|
mod sdmmc_rs {
|
|
|
|
use embedded_sdmmc::{Block, BlockCount, BlockDevice, BlockIdx};
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
use super::*;
|
|
|
|
|
2023-01-21 00:24:07 +01:00
|
|
|
impl<'d, T: Instance, Dma: SdmmcDma<T>> BlockDevice for Sdmmc<'d, T, Dma> {
|
2022-03-15 18:58:19 +01:00
|
|
|
type Error = Error;
|
2022-10-26 16:47:29 +02:00
|
|
|
|
2023-04-14 22:49:56 +02:00
|
|
|
async fn read(
|
|
|
|
&mut self,
|
|
|
|
blocks: &mut [Block],
|
2022-03-15 18:58:19 +01:00
|
|
|
start_block_idx: BlockIdx,
|
|
|
|
_reason: &str,
|
2023-04-14 22:49:56 +02:00
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
let mut address = start_block_idx.0;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-14 22:49:56 +02:00
|
|
|
for block in blocks.iter_mut() {
|
|
|
|
let block: &mut [u8; 512] = &mut block.contents;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-14 22:49:56 +02:00
|
|
|
// NOTE(unsafe) Block uses align(4)
|
|
|
|
let block = unsafe { &mut *(block as *mut _ as *mut DataBlock) };
|
|
|
|
self.read_block(address, block).await?;
|
|
|
|
address += 1;
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
2023-04-14 22:49:56 +02:00
|
|
|
Ok(())
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 22:49:56 +02:00
|
|
|
async fn write(&mut self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error> {
|
|
|
|
let mut address = start_block_idx.0;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-14 22:49:56 +02:00
|
|
|
for block in blocks.iter() {
|
|
|
|
let block: &[u8; 512] = &block.contents;
|
2022-03-15 18:58:19 +01:00
|
|
|
|
2023-04-14 22:49:56 +02:00
|
|
|
// NOTE(unsafe) DataBlock uses align 4
|
|
|
|
let block = unsafe { &*(block as *const _ as *const DataBlock) };
|
|
|
|
self.write_block(address, block).await?;
|
|
|
|
address += 1;
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
2023-04-14 22:49:56 +02:00
|
|
|
Ok(())
|
2022-03-15 18:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn num_blocks(&self) -> Result<BlockCount, Self::Error> {
|
|
|
|
let card = self.card()?;
|
|
|
|
let count = card.csd.block_count();
|
|
|
|
Ok(BlockCount(count))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|