1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
34
35
45
46
47
48
49
50
51
52
53
54
55
56
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
119
120
121
126
127
128
129
130
131
132
133
134
135
136
137
140
141
142
143
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
/* ... */
#include "esp_private/sdmmc_common.h"
#include "sd_pwr_ctrl_by_on_chip_ldo.h"
#include "sd_pwr_ctrl.h"
static const char* TAG = "sdmmc_init";
#define SDMMC_INIT_STEP(condition, function) \
do { \
if ((condition)) { \
esp_err_t err = (function)(card); \
if (err != ESP_OK) { \
ESP_LOGD(TAG, "%s: %s returned 0x%x", __func__, #function, err); \
return err; \
}{...} \
}{...} \
}{...} while(0);...
#define SDMMC_INIT_STEP_PARAM(condition, function, param) \
do { \
if ((condition)) { \
esp_err_t err = (function)(card, param); \
if (err != ESP_OK) { \
ESP_LOGD(TAG, "%s: %s returned 0x%x", __func__, #function, err); \
return err; \
}{...} \
}{...} \
}{...} while(0);...
esp_err_t sdmmc_card_init(const sdmmc_host_t* config, sdmmc_card_t* card)
{
esp_err_t ret = ESP_FAIL;
memset(card, 0, sizeof(*card));
memcpy(&card->host, config, sizeof(*config));
const bool is_spi = host_is_spi(card);
const bool always = true;
const bool io_supported = true;
if (config->pwr_ctrl_handle) {
int voltage_mv = config->io_voltage * 1000;
ret = sd_pwr_ctrl_set_io_voltage(config->pwr_ctrl_handle, voltage_mv);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "failed to set voltage (0x%x)", ret);
return ret;
}{...}
}{...}
SDMMC_INIT_STEP(!is_spi, sdmmc_allocate_aligned_buf);
SDMMC_INIT_STEP(!is_spi, sdmmc_fix_host_flags);
SDMMC_INIT_STEP(io_supported, sdmmc_io_reset);
SDMMC_INIT_STEP(always, sdmmc_send_cmd_go_idle_state);
SDMMC_INIT_STEP(always, sdmmc_init_sd_if_cond);
SDMMC_INIT_STEP(io_supported, sdmmc_init_io);
const bool is_mem = card->is_mem;
const bool is_sdio = !is_mem;
SDMMC_INIT_STEP(is_spi, sdmmc_init_spi_crc);
SDMMC_INIT_STEP(is_mem, sdmmc_init_ocr);
const bool is_mmc = is_mem && card->is_mmc;
const bool is_sdmem = is_mem && !is_mmc;
ESP_LOGD(TAG, "%s: card type is %s", __func__,
is_sdio ? "SDIO" : is_mmc ? "MMC" : "SD");
bool is_uhs1 = is_sdmem && (card->ocr & SD_OCR_S18_RA) && (card->ocr & SD_OCR_SDHC_CAP);
ESP_LOGV(TAG, "is_uhs1: %d", is_uhs1);
SDMMC_INIT_STEP(is_uhs1, sdmmc_init_sd_uhs1);
SDMMC_INIT_STEP(is_mem, sdmmc_init_cid);
SDMMC_INIT_STEP(!is_spi, sdmmc_init_rca);
SDMMC_INIT_STEP(is_mem, sdmmc_init_csd);
SDMMC_INIT_STEP(is_mmc && !is_spi, sdmmc_init_mmc_decode_cid);
/* ... */
SDMMC_INIT_STEP(!is_spi, sdmmc_init_select_card);
/* ... */
SDMMC_INIT_STEP(is_sdmem, sdmmc_init_sd_blocklen);
SDMMC_INIT_STEP(is_sdmem, sdmmc_init_sd_scr);
SDMMC_INIT_STEP(is_sdmem, sdmmc_init_sd_wait_data_ready);
SDMMC_INIT_STEP(is_mmc, sdmmc_init_mmc_read_ext_csd);
uint8_t card_cap = 0;
SDMMC_INIT_STEP_PARAM(is_sdio, sdmmc_io_init_read_card_cap, &card_cap);
/* ... */
SDMMC_INIT_STEP(always, sdmmc_init_card_hs_mode);
if (!is_spi) {
SDMMC_INIT_STEP(is_sdmem, sdmmc_init_sd_bus_width);
SDMMC_INIT_STEP(is_sdio, sdmmc_init_io_bus_width);
SDMMC_INIT_STEP(is_mmc, sdmmc_init_mmc_bus_width);
SDMMC_INIT_STEP(always, sdmmc_init_host_bus_width);
}{...}
SDMMC_INIT_STEP(is_uhs1, sdmmc_init_sd_driver_strength);
SDMMC_INIT_STEP(is_uhs1, sdmmc_init_sd_current_limit);
SDMMC_INIT_STEP(is_sdmem, sdmmc_init_sd_ssr);
SDMMC_INIT_STEP(always, sdmmc_init_host_frequency);
SDMMC_INIT_STEP(is_uhs1, sdmmc_init_sd_timing_tuning);
SDMMC_INIT_STEP(is_sdmem, sdmmc_check_scr);
SDMMC_INIT_STEP(is_mmc, sdmmc_init_mmc_check_ext_csd);
SDMMC_INIT_STEP_PARAM(is_sdio, sdmmc_io_init_check_card_cap, &card_cap);
return ESP_OK;
}{ ... }