sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
/// Represents the operation  modes of an SD card.
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum OperationMode {
    Inactive,
    CardIdentification,
    DataTransfer,
}

impl OperationMode {
    /// Creates a new [OperationMode].
    pub const fn new() -> Self {
        Self::Inactive
    }
}

impl Default for OperationMode {
    fn default() -> Self {
        Self::new()
    }
}

/// Represents the card states of an SD card.
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CardState {
    Inactive,
    Idle,
    Ready,
    Identification,
    Standby,
    Transfer,
    SendingData,
    ReceivingData,
    Programming,
    Disconnect,
}

impl CardState {
    /// Creates a new [CardState].
    pub const fn new() -> Self {
        Self::Inactive
    }

    /// Gets the [OperationMode] associated with the [CardState].
    ///
    /// For details, see `Table 4-1` of [Part1 Physical Layer](https://www.sdcard.org/downloads/pls/pdf/?f=Part1PhysicalLayerSimplifiedSpecificationVer9.10Fin_20231201.pdf) SD specification.
    pub const fn operation_mode(&self) -> OperationMode {
        match self {
            Self::Inactive => OperationMode::Inactive,
            Self::Idle | Self::Ready | Self::Identification => OperationMode::CardIdentification,
            _ => OperationMode::DataTransfer,
        }
    }
}

impl Default for CardState {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_card_state() {
        [
            CardState::Inactive,
            CardState::Idle,
            CardState::Ready,
            CardState::Identification,
            CardState::Standby,
            CardState::Transfer,
            CardState::SendingData,
            CardState::ReceivingData,
            CardState::Programming,
            CardState::Disconnect,
        ]
        .into_iter()
        .zip([
            OperationMode::Inactive,
            OperationMode::CardIdentification,
            OperationMode::CardIdentification,
            OperationMode::CardIdentification,
            OperationMode::DataTransfer,
            OperationMode::DataTransfer,
            OperationMode::DataTransfer,
            OperationMode::DataTransfer,
            OperationMode::DataTransfer,
            OperationMode::DataTransfer,
        ])
        .for_each(|(exp_cs, exp_om)| {
            assert_eq!(
                exp_cs.operation_mode(),
                exp_om,
                "CardState: {exp_cs:?}, OperationMode: {exp_om:?}"
            );
        });
    }
}