sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
use crate::lib_bitfield;
use crate::range::RangeU8;
use crate::result::Result;

use super::{ExtCsd, ExtCsdIndex};

pub type Timeout = RangeU8<1, 0x17>;

lib_bitfield! {
    /// Represents the `OPERATION_CODES_TIMEOUT` field of the [ExtCsd] register.
    OperationCodesTimeout: u8,
    mask: 0xff,
    default: 1,
    {
        /// Represents the exponent used to calculate the operation codes timeout value.
        operation_codes_timeout: Timeout, 7, 0;
    }
}

impl OperationCodesTimeout {
    /// Represents the timeout unit (in microseconds).
    pub const TIMEOUT_UNIT: u32 = 100;

    /// Gets the effective timeout value for the operation codes.
    ///
    /// # Note
    ///
    /// The timeout value is calculated with the algorithm:
    ///
    /// ```no_build,no_run
    /// timeout = 100us * (2 ^ OPERATION_CODES_TIMEOUT)
    /// ```
    pub const fn timeout(&self) -> Result<u32> {
        match self.operation_codes_timeout() {
            Ok(t) => Ok(Self::TIMEOUT_UNIT * (1u32 << t.into_inner())),
            Err(err) => Err(err),
        }
    }
}

impl ExtCsd {
    /// Gets the `OPERATION_CODES_TIMEOUT` field of the [ExtCsd] register.
    pub const fn operation_codes_timeout(&self) -> Result<OperationCodesTimeout> {
        OperationCodesTimeout::try_from_inner(
            self.0[ExtCsdIndex::OperationCodesTimeout.into_inner()],
        )
    }

    /// Sets the `OPERATION_CODES_TIMEOUT` field of the [ExtCsd] register.
    pub(crate) fn set_operation_codes_timeout(&mut self, val: OperationCodesTimeout) {
        self.0[ExtCsdIndex::OperationCodesTimeout.into_inner()] = val.into_inner();
    }
}

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

    #[test]
    fn test_operation_codes_timeout() {
        let mut ext_csd = ExtCsd::new();

        ext_csd.set_operation_codes_timeout(OperationCodesTimeout::new());
        assert_eq!(
            ext_csd.operation_codes_timeout(),
            Ok(OperationCodesTimeout::new())
        );

        (0..u8::MAX).for_each(|raw_timeout| {
            // set a potentially invalid OperationCodesTimeout
            ext_csd.set_operation_codes_timeout(OperationCodesTimeout(raw_timeout));

            match OperationCodesTimeout::try_from_inner(raw_timeout) {
                Ok(timeout) => {
                    assert_eq!(ext_csd.operation_codes_timeout(), Ok(timeout));
                    assert_eq!(
                        timeout.timeout(),
                        Ok(OperationCodesTimeout::TIMEOUT_UNIT * (1u32 << raw_timeout))
                    );
                }
                Err(err) => assert_eq!(ext_csd.operation_codes_timeout(), Err(err)),
            }
        });
    }
}