1use crate::pins::SpiSdPins;
10
11pub const DEFAULT_SPI_HZ: u32 = 25_000_000;
13pub const CARD_DETECT_P0_BIT: u8 = 4;
15
16#[cfg_attr(feature = "defmt", derive(defmt::Format))]
18#[derive(Clone, Copy, Debug, Eq, PartialEq)]
19pub struct SdCardSlot {
20 pub spi: SpiSdPins,
22 pub spi_hz: u32,
24 pub detect: SdCardDetect,
26}
27
28impl SdCardSlot {
29 pub const CORE_S3: Self = Self {
31 spi: SpiSdPins::TF_CARD,
32 spi_hz: DEFAULT_SPI_HZ,
33 detect: SdCardDetect::AW9523B_P0_4_ACTIVE_LOW,
34 };
35}
36
37#[cfg_attr(feature = "defmt", derive(defmt::Format))]
39#[derive(Clone, Copy, Debug, Eq, PartialEq)]
40pub struct SdCardDetect {
41 pub port: u8,
43 pub bit: u8,
45 pub active_low: bool,
47}
48
49impl SdCardDetect {
50 pub const AW9523B_P0_4_ACTIVE_LOW: Self = Self {
52 port: 0,
53 bit: CARD_DETECT_P0_BIT,
54 active_low: true,
55 };
56
57 pub const fn present_from_port_value(self, value: u8) -> bool {
59 let high = (value & (1 << self.bit)) != 0;
60 if self.active_low { !high } else { high }
61 }
62}
63
64pub const fn core_s3_card_present_from_aw9523_p0(value: u8) -> bool {
66 SdCardDetect::AW9523B_P0_4_ACTIVE_LOW.present_from_port_value(value)
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn card_detect_is_active_low_on_p0_bit4() {
75 assert!(core_s3_card_present_from_aw9523_p0(0b1110_1111));
76 assert!(!core_s3_card_present_from_aw9523_p0(0b0001_0000));
77 }
78}