1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::sys::*;

#[cfg(esp_idf_soc_sdmmc_host_supported)]
use super::mmc::SlotConfiguration;

use super::{config::Configuration, spi::SpiDevice};

pub enum SdDevice<'d> {
    Spi(SpiDevice<'d>),
    #[cfg(esp_idf_soc_sdmmc_host_supported)]
    Mmc(SlotConfiguration<'d>),
}

pub struct SdHost<'d> {
    device: SdDevice<'d>,
    host: sdmmc_host_t,
}

const _HOST_FLAG_SPI: u32 = 1 << 3;
const _HOST_FLAG_DEINIT_ARG: u32 = 1 << 5;
const _DEFAULT_FREQUENCY: i32 = 20000;
const _HIGH_SPEED_FREQUENCY: i32 = 40000;

const _DEFAULT_IO_VOLTAGE: f32 = 3.3;
const _SDMMC_HOST_FLAG_1BIT: u32 = 1 << 0;
const _SDMMC_HOST_FLAG_4BIT: u32 = 1 << 1;
const _SDMMC_HOST_FLAG_8BIT: u32 = 1 << 2;
const _SDMMC_HOST_FLAG_DDR: u32 = 1 << 3;

impl<'d> SdHost<'d> {
    fn set_from_configuration(mut self, configuration: &Configuration) -> Self {
        self.host.command_timeout_ms = configuration.command_timeout_ms as i32;
        self.host.io_voltage = configuration.io_voltage;
        self.host.max_freq_khz = if configuration.high_speed {
            _HIGH_SPEED_FREQUENCY
        } else {
            _DEFAULT_FREQUENCY
        };

        #[cfg(not(any(
            esp_idf_version_major = "4",
            all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
            all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
        )))] // For ESP-IDF v5.2 and later
        {
            self.host.input_delay_phase = configuration.input_delay_phase as sdmmc_delay_phase_t;
        }

        self
    }

    pub fn new_with_spi(configuration: &Configuration, device: SpiDevice<'d>) -> Self {
        let host = sdmmc_host_t {
            flags: _HOST_FLAG_SPI | _HOST_FLAG_DEINIT_ARG,
            slot: device.get_host() as i32,
            max_freq_khz: _DEFAULT_FREQUENCY,
            io_voltage: 3.3,
            init: Some(sdspi_host_init),
            set_bus_width: None,
            get_bus_width: None,
            set_bus_ddr_mode: None,
            set_card_clk: Some(sdspi_host_set_card_clk),
            set_cclk_always_on: None,
            do_transaction: Some(sdspi_host_do_transaction),
            __bindgen_anon_1: sdmmc_host_t__bindgen_ty_1 {
                deinit_p: Some(sdspi_host_remove_device),
            },
            io_int_enable: Some(sdspi_host_io_int_enable),
            io_int_wait: Some(sdspi_host_io_int_wait),
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
            )))]    // For ESP-IDF v5.1 and later
            get_real_freq: Some(sdspi_host_get_real_freq),
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
            )))]    // For ESP-IDF v5.2 and later
            input_delay_phase: sdmmc_delay_phase_t_SDMMC_DELAY_PHASE_0,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
            )))]   // For ESP-IDF v5.2 and later
            set_input_delay: None,
            command_timeout_ms: 0,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "2"),
            )))]   // For ESP-IDF v5.3 and later
            dma_aligned_buffer: core::ptr::null_mut(),
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "2"),
            )))]   // For ESP-IDF v5.3 and later
            get_dma_info: None,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "2"),
            )))]   // For ESP-IDF v5.3 and later
            pwr_ctrl_handle: core::ptr::null_mut() as _,
        };

        Self {
            device: SdDevice::Spi(device),
            host,
        }
        .set_from_configuration(configuration)
    }

    /// Create a new SD/MMC host with the default configuration.
    /// This host will use the MMC slot 1, with 4-bit mode enabled, and max frequency set to 20MHz
    #[cfg(esp_idf_soc_sdmmc_host_supported)]
    pub fn new_with_mmc(
        configuration: &Configuration,
        mmc_configuration: SlotConfiguration<'d>,
    ) -> Self {
        let host = sdmmc_host_t {
            flags: _SDMMC_HOST_FLAG_8BIT
                | _SDMMC_HOST_FLAG_4BIT
                | _SDMMC_HOST_FLAG_1BIT
                | _SDMMC_HOST_FLAG_DDR,
            slot: mmc_configuration.get_slot() as i32,
            max_freq_khz: _DEFAULT_FREQUENCY,
            io_voltage: _DEFAULT_IO_VOLTAGE,
            init: Some(sdmmc_host_init),
            set_bus_width: Some(sdmmc_host_set_bus_width),
            get_bus_width: Some(sdmmc_host_get_slot_width),
            set_bus_ddr_mode: Some(sdmmc_host_set_bus_ddr_mode),
            set_card_clk: Some(sdmmc_host_set_card_clk),
            set_cclk_always_on: Some(sdmmc_host_set_cclk_always_on),
            do_transaction: Some(sdmmc_host_do_transaction),
            __bindgen_anon_1: sdmmc_host_t__bindgen_ty_1 {
                deinit: Some(sdmmc_host_deinit),
            },
            io_int_enable: Some(sdmmc_host_io_int_enable),
            io_int_wait: Some(sdmmc_host_io_int_wait),
            get_real_freq: Some(sdmmc_host_get_real_freq),
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
             )))] // For ESP-IDF v5.2 and later            
            input_delay_phase: sdmmc_delay_phase_t_SDMMC_DELAY_PHASE_0,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
             )))] // For ESP-IDF v5.2 and later            
            set_input_delay: Some(sdmmc_host_set_input_delay),
            command_timeout_ms: 0,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "2"),
            )))]   // For ESP-IDF v5.3 and later
            dma_aligned_buffer: core::ptr::null_mut(),
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "2"),
            )))]   // For ESP-IDF v5.3 and later
            get_dma_info: None,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "2"),
            )))]   // For ESP-IDF v5.3 and later
            pwr_ctrl_handle: core::ptr::null_mut() as _,
        };

        Self {
            device: SdDevice::Mmc(mmc_configuration),
            host,
        }
        .set_from_configuration(configuration)
    }

    pub(crate) fn get_inner_handle(&self) -> &sdmmc_host_t {
        &self.host
    }

    pub(crate) fn get_device(&self) -> &SdDevice {
        &self.device
    }
}