use crate::pins::SpiSdPins;
pub const DEFAULT_SPI_HZ: u32 = 25_000_000;
pub const CARD_DETECT_P0_BIT: u8 = 4;
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SdCardSlot {
pub spi: SpiSdPins,
pub spi_hz: u32,
pub detect: SdCardDetect,
}
impl SdCardSlot {
pub const CORE_S3: Self = Self {
spi: SpiSdPins::TF_CARD,
spi_hz: DEFAULT_SPI_HZ,
detect: SdCardDetect::AW9523B_P0_4_ACTIVE_LOW,
};
}
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SdCardDetect {
pub port: u8,
pub bit: u8,
pub active_low: bool,
}
impl SdCardDetect {
pub const AW9523B_P0_4_ACTIVE_LOW: Self = Self {
port: 0,
bit: CARD_DETECT_P0_BIT,
active_low: true,
};
pub const fn present_from_port_value(self, value: u8) -> bool {
let high = (value & (1 << self.bit)) != 0;
if self.active_low { !high } else { high }
}
}
pub const fn core_s3_card_present_from_aw9523_p0(value: u8) -> bool {
SdCardDetect::AW9523B_P0_4_ACTIVE_LOW.present_from_port_value(value)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn card_detect_is_active_low_on_p0_bit4() {
assert!(core_s3_card_present_from_aw9523_p0(0b1110_1111));
assert!(!core_s3_card_present_from_aw9523_p0(0b0001_0000));
}
}