1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <sys/param.h>
18 #include "esp_log.h"
19 #include "esp_intr_alloc.h"
20 #include "soc/soc_caps.h"
21 #include "soc/soc_pins.h"
22 #include "soc/gpio_periph.h"
23 #include "esp_rom_gpio.h"
24 #include "esp_rom_sys.h"
25 #include "driver/gpio.h"
26 #include "driver/sdmmc_host.h"
27 #include "driver/periph_ctrl.h"
28 #include "sdmmc_private.h"
29 #include "freertos/FreeRTOS.h"
30 #include "freertos/semphr.h"
31 #include "soc/sdmmc_periph.h"
32 #include "hal/gpio_hal.h"
33
34 #define SDMMC_EVENT_QUEUE_LENGTH 32
35
36
37 static void sdmmc_isr(void* arg);
38 static void sdmmc_host_dma_init(void);
39
40
41 static const char* TAG = "sdmmc_periph";
42 static intr_handle_t s_intr_handle;
43 static QueueHandle_t s_event_queue;
44 static SemaphoreHandle_t s_io_intr_event;
45
46 size_t s_slot_width[2] = {1,1};
47
sdmmc_host_reset(void)48 void sdmmc_host_reset(void)
49 {
50 // Set reset bits
51 SDMMC.ctrl.controller_reset = 1;
52 SDMMC.ctrl.dma_reset = 1;
53 SDMMC.ctrl.fifo_reset = 1;
54 // Wait for the reset bits to be cleared by hardware
55 while (SDMMC.ctrl.controller_reset || SDMMC.ctrl.fifo_reset || SDMMC.ctrl.dma_reset) {
56 ;
57 }
58 }
59
60 /* We have two clock divider stages:
61 * - one is the clock generator which drives SDMMC peripheral,
62 * it can be configured using SDMMC.clock register. It can generate
63 * frequencies 160MHz/(N + 1), where 0 < N < 16, I.e. from 10 to 80 MHz.
64 * - 4 clock dividers inside SDMMC peripheral, which can divide clock
65 * from the first stage by 2 * M, where 0 < M < 255
66 * (they can also be bypassed).
67 *
68 * For cards which aren't UHS-1 or UHS-2 cards, which we don't support,
69 * maximum bus frequency in high speed (HS) mode is 50 MHz.
70 * Note: for non-UHS-1 cards, HS mode is optional.
71 * Default speed (DS) mode is mandatory, it works up to 25 MHz.
72 * Whether the card supports HS or not can be determined using TRAN_SPEED
73 * field of card's CSD register.
74 *
75 * 50 MHz can not be obtained exactly, closest we can get is 53 MHz.
76 *
77 * The first stage divider is set to the highest possible value for the given
78 * frequency, and the the second stage dividers are used if division factor
79 * is >16.
80 *
81 * Of the second stage dividers, div0 is used for card 0, and div1 is used
82 * for card 1.
83 */
84
sdmmc_host_set_clk_div(int div)85 static void sdmmc_host_set_clk_div(int div)
86 {
87 // Set frequency to 160MHz / div
88 // div = p + 1
89 // duty cycle = (h + 1)/(p + 1) (should be = 1/2)
90 assert (div > 1 && div <= 16);
91 int p = div - 1;
92 int h = div / 2 - 1;
93
94 SDMMC.clock.div_factor_p = p;
95 SDMMC.clock.div_factor_h = h;
96 SDMMC.clock.div_factor_m = p;
97 // Set phases for in/out clocks
98 SDMMC.clock.phase_dout = 4; // 180 degree phase on the output clock
99 SDMMC.clock.phase_din = 4; // 180 degree phase on the input clock
100 SDMMC.clock.phase_core = 0;
101 // Wait for the clock to propagate
102 esp_rom_delay_us(10);
103 }
104
sdmmc_host_input_clk_disable(void)105 static void sdmmc_host_input_clk_disable(void)
106 {
107 SDMMC.clock.val = 0;
108 }
109
sdmmc_host_clock_update_command(int slot)110 static void sdmmc_host_clock_update_command(int slot)
111 {
112 // Clock update command (not a real command; just updates CIU registers)
113 sdmmc_hw_cmd_t cmd_val = {
114 .card_num = slot,
115 .update_clk_reg = 1,
116 .wait_complete = 1
117 };
118 bool repeat = true;
119 while(repeat) {
120 sdmmc_host_start_command(slot, cmd_val, 0);
121 while (true) {
122 // Sending clock update command to the CIU can generate HLE error.
123 // According to the manual, this is okay and we must retry the command.
124 if (SDMMC.rintsts.hle) {
125 SDMMC.rintsts.hle = 1;
126 repeat = true;
127 break;
128 }
129 // When the command is accepted by CIU, start_command bit will be
130 // cleared in SDMMC.cmd register.
131 if (SDMMC.cmd.start_command == 0) {
132 repeat = false;
133 break;
134 }
135 }
136 }
137 }
138
sdmmc_host_set_card_clk(int slot,uint32_t freq_khz)139 esp_err_t sdmmc_host_set_card_clk(int slot, uint32_t freq_khz)
140 {
141 if (!(slot == 0 || slot == 1)) {
142 return ESP_ERR_INVALID_ARG;
143 }
144 const int clk40m = 40000;
145
146 // Disable clock first
147 SDMMC.clkena.cclk_enable &= ~BIT(slot);
148 sdmmc_host_clock_update_command(slot);
149
150 int host_div = 0; /* clock divider of the host (SDMMC.clock) */
151 int card_div = 0; /* 1/2 of card clock divider (SDMMC.clkdiv) */
152
153 // Calculate new dividers
154 if (freq_khz >= SDMMC_FREQ_HIGHSPEED) {
155 host_div = 4; // 160 MHz / 4 = 40 MHz
156 card_div = 0;
157 } else if (freq_khz == SDMMC_FREQ_DEFAULT) {
158 host_div = 8; // 160 MHz / 8 = 20 MHz
159 card_div = 0;
160 } else if (freq_khz == SDMMC_FREQ_PROBING) {
161 host_div = 10; // 160 MHz / 10 / (20 * 2) = 400 kHz
162 card_div = 20;
163 } else {
164 host_div = 2;
165 card_div = (clk40m + freq_khz * 2 - 1) / (freq_khz * 2); // round up
166 }
167
168 ESP_LOGD(TAG, "slot=%d host_div=%d card_div=%d freq=%dkHz",
169 slot, host_div, card_div,
170 2 * APB_CLK_FREQ / host_div / ((card_div == 0) ? 1 : card_div * 2) / 1000);
171
172 // Program CLKDIV and CLKSRC, send them to the CIU
173 switch(slot) {
174 case 0:
175 SDMMC.clksrc.card0 = 0;
176 SDMMC.clkdiv.div0 = card_div;
177 break;
178 case 1:
179 SDMMC.clksrc.card1 = 1;
180 SDMMC.clkdiv.div1 = card_div;
181 break;
182 }
183 sdmmc_host_set_clk_div(host_div);
184 sdmmc_host_clock_update_command(slot);
185
186 // Re-enable clocks
187 SDMMC.clkena.cclk_enable |= BIT(slot);
188 SDMMC.clkena.cclk_low_power |= BIT(slot);
189 sdmmc_host_clock_update_command(slot);
190
191 // set data timeout
192 const uint32_t data_timeout_ms = 100;
193 uint32_t data_timeout_cycles = data_timeout_ms * freq_khz;
194 const uint32_t data_timeout_cycles_max = 0xffffff;
195 if (data_timeout_cycles > data_timeout_cycles_max) {
196 data_timeout_cycles = data_timeout_cycles_max;
197 }
198 SDMMC.tmout.data = data_timeout_cycles;
199 // always set response timeout to highest value, it's small enough anyway
200 SDMMC.tmout.response = 255;
201 return ESP_OK;
202 }
203
sdmmc_host_start_command(int slot,sdmmc_hw_cmd_t cmd,uint32_t arg)204 esp_err_t sdmmc_host_start_command(int slot, sdmmc_hw_cmd_t cmd, uint32_t arg) {
205 if (!(slot == 0 || slot == 1)) {
206 return ESP_ERR_INVALID_ARG;
207 }
208 if ((SDMMC.cdetect.cards & BIT(slot)) != 0) {
209 return ESP_ERR_NOT_FOUND;
210 }
211 if (cmd.data_expected && cmd.rw && (SDMMC.wrtprt.cards & BIT(slot)) != 0) {
212 return ESP_ERR_INVALID_STATE;
213 }
214 while (SDMMC.cmd.start_command == 1) {
215 ;
216 }
217 SDMMC.cmdarg = arg;
218 cmd.card_num = slot;
219 cmd.start_command = 1;
220 SDMMC.cmd = cmd;
221 return ESP_OK;
222 }
223
sdmmc_host_init(void)224 esp_err_t sdmmc_host_init(void)
225 {
226 if (s_intr_handle) {
227 return ESP_ERR_INVALID_STATE;
228 }
229
230 periph_module_reset(PERIPH_SDMMC_MODULE);
231 periph_module_enable(PERIPH_SDMMC_MODULE);
232
233 // Enable clock to peripheral. Use smallest divider first.
234 sdmmc_host_set_clk_div(2);
235
236 // Reset
237 sdmmc_host_reset();
238 ESP_LOGD(TAG, "peripheral version %x, hardware config %08x", SDMMC.verid, SDMMC.hcon);
239
240 // Clear interrupt status and set interrupt mask to known state
241 SDMMC.rintsts.val = 0xffffffff;
242 SDMMC.intmask.val = 0;
243 SDMMC.ctrl.int_enable = 0;
244
245 // Allocate event queue
246 s_event_queue = xQueueCreate(SDMMC_EVENT_QUEUE_LENGTH, sizeof(sdmmc_event_t));
247 if (!s_event_queue) {
248 return ESP_ERR_NO_MEM;
249 }
250 s_io_intr_event = xSemaphoreCreateBinary();
251 if (!s_io_intr_event) {
252 vQueueDelete(s_event_queue);
253 s_event_queue = NULL;
254 return ESP_ERR_NO_MEM;
255 }
256 // Attach interrupt handler
257 esp_err_t ret = esp_intr_alloc(ETS_SDIO_HOST_INTR_SOURCE, 0, &sdmmc_isr, s_event_queue, &s_intr_handle);
258 if (ret != ESP_OK) {
259 vQueueDelete(s_event_queue);
260 s_event_queue = NULL;
261 vSemaphoreDelete(s_io_intr_event);
262 s_io_intr_event = NULL;
263 return ret;
264 }
265 // Enable interrupts
266 SDMMC.intmask.val =
267 SDMMC_INTMASK_CD |
268 SDMMC_INTMASK_CMD_DONE |
269 SDMMC_INTMASK_DATA_OVER |
270 SDMMC_INTMASK_RCRC | SDMMC_INTMASK_DCRC |
271 SDMMC_INTMASK_RTO | SDMMC_INTMASK_DTO | SDMMC_INTMASK_HTO |
272 SDMMC_INTMASK_SBE | SDMMC_INTMASK_EBE |
273 SDMMC_INTMASK_RESP_ERR | SDMMC_INTMASK_HLE; //sdio is enabled only when use.
274 SDMMC.ctrl.int_enable = 1;
275
276 // Disable generation of Busy Clear Interrupt
277 SDMMC.cardthrctl.busy_clr_int_en = 0;
278
279 // Enable DMA
280 sdmmc_host_dma_init();
281
282 // Initialize transaction handler
283 ret = sdmmc_host_transaction_handler_init();
284 if (ret != ESP_OK) {
285 vQueueDelete(s_event_queue);
286 s_event_queue = NULL;
287 vSemaphoreDelete(s_io_intr_event);
288 s_io_intr_event = NULL;
289 esp_intr_free(s_intr_handle);
290 s_intr_handle = NULL;
291 return ret;
292 }
293
294 return ESP_OK;
295 }
296
configure_pin(int pin)297 static void configure_pin(int pin)
298 {
299 const int sdmmc_func = 3;
300 const int drive_strength = 3;
301 assert(pin!=GPIO_NUM_NC);
302 gpio_pulldown_dis(pin);
303
304 uint32_t reg = GPIO_PIN_MUX_REG[pin];
305 assert(reg != UINT32_MAX);
306 PIN_INPUT_ENABLE(reg);
307 gpio_hal_iomux_func_sel(reg, sdmmc_func);
308 PIN_SET_DRV(reg, drive_strength);
309 }
310
sdmmc_host_init_slot(int slot,const sdmmc_slot_config_t * slot_config)311 esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config)
312 {
313 if (!s_intr_handle) {
314 return ESP_ERR_INVALID_STATE;
315 }
316 if (!(slot == 0 || slot == 1)) {
317 return ESP_ERR_INVALID_ARG;
318 }
319 if (slot_config == NULL) {
320 return ESP_ERR_INVALID_ARG;
321 }
322 bool pullup = slot_config->flags & SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
323 if (pullup) {
324 sdmmc_host_pullup_en(slot, slot_config->width);
325 }
326 int gpio_cd = slot_config->gpio_cd;
327 int gpio_wp = slot_config->gpio_wp;
328 uint8_t slot_width = slot_config->width;
329
330 // Configure pins
331 const sdmmc_slot_info_t* pslot = &sdmmc_slot_info[slot];
332
333 if (slot_width == SDMMC_SLOT_WIDTH_DEFAULT) {
334 slot_width = pslot->width;
335 }
336 else if (slot_width > pslot->width) {
337 return ESP_ERR_INVALID_ARG;
338 }
339 s_slot_width[slot] = slot_width;
340
341 configure_pin(pslot->clk_gpio);
342 configure_pin(pslot->cmd_gpio);
343 configure_pin(pslot->d0_gpio);
344
345 if (slot_width >= 4) {
346 configure_pin(pslot->d1_gpio);
347 configure_pin(pslot->d2_gpio);
348 // Force D3 high to make slave enter SD mode.
349 // Connect to peripheral after width configuration.
350 gpio_config_t gpio_conf = {
351 .pin_bit_mask = BIT64(pslot->d3_gpio),
352 .mode = GPIO_MODE_OUTPUT ,
353 .pull_up_en = 0,
354 .pull_down_en = 0,
355 .intr_type = GPIO_INTR_DISABLE,
356 };
357 gpio_config(&gpio_conf);
358 gpio_set_level(pslot->d3_gpio, 1);
359 if (slot_width == 8) {
360 configure_pin(pslot->d4_gpio);
361 configure_pin(pslot->d5_gpio);
362 configure_pin(pslot->d6_gpio);
363 configure_pin(pslot->d7_gpio);
364 }
365 }
366
367 // SDIO slave interrupt is edge sensitive to ~(int_n | card_int | card_detect)
368 // set this and card_detect to high to enable sdio interrupt
369 esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ONE_INPUT, pslot->card_int, false);
370
371 // Set up Card Detect input
372 int matrix_in_cd;
373 if (gpio_cd != SDMMC_SLOT_NO_CD) {
374 ESP_LOGD(TAG, "using GPIO%d as CD pin", gpio_cd);
375 esp_rom_gpio_pad_select_gpio(gpio_cd);
376 gpio_set_direction(gpio_cd, GPIO_MODE_INPUT);
377 matrix_in_cd = gpio_cd;
378 } else {
379 // if not set, default to CD low (card present)
380 matrix_in_cd = GPIO_MATRIX_CONST_ZERO_INPUT;
381 }
382 esp_rom_gpio_connect_in_signal(matrix_in_cd, pslot->card_detect, false);
383
384 // Set up Write Protect input
385 int matrix_in_wp;
386 if (gpio_wp != SDMMC_SLOT_NO_WP) {
387 ESP_LOGD(TAG, "using GPIO%d as WP pin", gpio_wp);
388 esp_rom_gpio_pad_select_gpio(gpio_wp);
389 gpio_set_direction(gpio_wp, GPIO_MODE_INPUT);
390 matrix_in_wp = gpio_wp;
391 } else {
392 // if not set, default to WP high (not write protected)
393 matrix_in_wp = GPIO_MATRIX_CONST_ONE_INPUT;
394 }
395 // WP signal is normally active low, but hardware expects
396 // an active-high signal, so invert it in GPIO matrix
397 esp_rom_gpio_connect_in_signal(matrix_in_wp, pslot->write_protect, true);
398
399 // By default, set probing frequency (400kHz) and 1-bit bus
400 esp_err_t ret = sdmmc_host_set_card_clk(slot, 400);
401 if (ret != ESP_OK) {
402 return ret;
403 }
404 ret = sdmmc_host_set_bus_width(slot, 1);
405 if (ret != ESP_OK) {
406 return ret;
407 }
408 return ESP_OK;
409 }
410
sdmmc_host_deinit(void)411 esp_err_t sdmmc_host_deinit(void)
412 {
413 if (!s_intr_handle) {
414 return ESP_ERR_INVALID_STATE;
415 }
416 esp_intr_free(s_intr_handle);
417 s_intr_handle = NULL;
418 vQueueDelete(s_event_queue);
419 s_event_queue = NULL;
420 vQueueDelete(s_io_intr_event);
421 s_io_intr_event = NULL;
422 sdmmc_host_input_clk_disable();
423 sdmmc_host_transaction_handler_deinit();
424 periph_module_disable(PERIPH_SDMMC_MODULE);
425 return ESP_OK;
426 }
427
sdmmc_host_wait_for_event(int tick_count,sdmmc_event_t * out_event)428 esp_err_t sdmmc_host_wait_for_event(int tick_count, sdmmc_event_t* out_event)
429 {
430 if (!out_event) {
431 return ESP_ERR_INVALID_ARG;
432 }
433 if (!s_event_queue) {
434 return ESP_ERR_INVALID_STATE;
435 }
436 int ret = xQueueReceive(s_event_queue, out_event, tick_count);
437 if (ret == pdFALSE) {
438 return ESP_ERR_TIMEOUT;
439 }
440 return ESP_OK;
441 }
442
sdmmc_host_set_bus_width(int slot,size_t width)443 esp_err_t sdmmc_host_set_bus_width(int slot, size_t width)
444 {
445 if (!(slot == 0 || slot == 1)) {
446 return ESP_ERR_INVALID_ARG;
447 }
448 if (sdmmc_slot_info[slot].width < width) {
449 return ESP_ERR_INVALID_ARG;
450 }
451 const uint16_t mask = BIT(slot);
452 if (width == 1) {
453 SDMMC.ctype.card_width_8 &= ~mask;
454 SDMMC.ctype.card_width &= ~mask;
455 } else if (width == 4) {
456 SDMMC.ctype.card_width_8 &= ~mask;
457 SDMMC.ctype.card_width |= mask;
458 // D3 was set to GPIO high to force slave into SD mode, until 4-bit mode is set
459 configure_pin(sdmmc_slot_info[slot].d3_gpio);
460 } else if (width == 8) {
461 SDMMC.ctype.card_width_8 |= mask;
462 // D3 was set to GPIO high to force slave into SD mode, until 4-bit mode is set
463 configure_pin(sdmmc_slot_info[slot].d3_gpio);
464 } else {
465 return ESP_ERR_INVALID_ARG;
466 }
467 ESP_LOGD(TAG, "slot=%d width=%d", slot, width);
468 return ESP_OK;
469 }
470
sdmmc_host_get_slot_width(int slot)471 size_t sdmmc_host_get_slot_width(int slot)
472 {
473 assert( slot == 0 || slot == 1 );
474 return s_slot_width[slot];
475 }
476
sdmmc_host_set_bus_ddr_mode(int slot,bool ddr_enabled)477 esp_err_t sdmmc_host_set_bus_ddr_mode(int slot, bool ddr_enabled)
478 {
479 if (!(slot == 0 || slot == 1)) {
480 return ESP_ERR_INVALID_ARG;
481 }
482 if (s_slot_width[slot] == 8 && ddr_enabled) {
483 ESP_LOGW(TAG, "DDR mode with 8-bit bus width is not supported yet");
484 // requires reconfiguring controller clock for 2x card frequency
485 return ESP_ERR_NOT_SUPPORTED;
486 }
487 uint32_t mask = BIT(slot);
488 if (ddr_enabled) {
489 SDMMC.uhs.ddr |= mask;
490 SDMMC.emmc_ddr_reg |= mask;
491 } else {
492 SDMMC.uhs.ddr &= ~mask;
493 SDMMC.emmc_ddr_reg &= ~mask;
494 }
495 ESP_LOGD(TAG, "slot=%d ddr=%d", slot, ddr_enabled ? 1 : 0);
496 return ESP_OK;
497 }
498
sdmmc_host_dma_init(void)499 static void sdmmc_host_dma_init(void)
500 {
501 SDMMC.ctrl.dma_enable = 1;
502 SDMMC.bmod.val = 0;
503 SDMMC.bmod.sw_reset = 1;
504 SDMMC.idinten.ni = 1;
505 SDMMC.idinten.ri = 1;
506 SDMMC.idinten.ti = 1;
507 }
508
509
sdmmc_host_dma_stop(void)510 void sdmmc_host_dma_stop(void)
511 {
512 SDMMC.ctrl.use_internal_dma = 0;
513 SDMMC.ctrl.dma_reset = 1;
514 SDMMC.bmod.fb = 0;
515 SDMMC.bmod.enable = 0;
516 }
517
sdmmc_host_dma_prepare(sdmmc_desc_t * desc,size_t block_size,size_t data_size)518 void sdmmc_host_dma_prepare(sdmmc_desc_t* desc, size_t block_size, size_t data_size)
519 {
520 // Set size of data and DMA descriptor pointer
521 SDMMC.bytcnt = data_size;
522 SDMMC.blksiz = block_size;
523 SDMMC.dbaddr = desc;
524
525 // Enable everything needed to use DMA
526 SDMMC.ctrl.dma_enable = 1;
527 SDMMC.ctrl.use_internal_dma = 1;
528 SDMMC.bmod.enable = 1;
529 SDMMC.bmod.fb = 1;
530 sdmmc_host_dma_resume();
531 }
532
sdmmc_host_dma_resume(void)533 void sdmmc_host_dma_resume(void)
534 {
535 SDMMC.pldmnd = 1;
536 }
537
sdmmc_host_card_busy(void)538 bool sdmmc_host_card_busy(void)
539 {
540 return SDMMC.status.data_busy == 1;
541 }
542
sdmmc_host_io_int_enable(int slot)543 esp_err_t sdmmc_host_io_int_enable(int slot)
544 {
545 configure_pin(sdmmc_slot_info[slot].d1_gpio);
546 return ESP_OK;
547 }
548
sdmmc_host_io_int_wait(int slot,TickType_t timeout_ticks)549 esp_err_t sdmmc_host_io_int_wait(int slot, TickType_t timeout_ticks)
550 {
551 /* SDIO interrupts are negedge sensitive ones: the status bit is only set
552 * when first interrupt triggered.
553 *
554 * If D1 GPIO is low when entering this function, we know that interrupt
555 * (in SDIO sense) has occurred and we don't need to use SDMMC peripheral
556 * interrupt.
557 */
558
559 SDMMC.intmask.sdio &= ~BIT(slot); /* Disable SDIO interrupt */
560 SDMMC.rintsts.sdio = BIT(slot);
561 if (gpio_get_level(sdmmc_slot_info[slot].d1_gpio) == 0) {
562 return ESP_OK;
563 }
564 /* Otherwise, need to wait for an interrupt. Since D1 was high,
565 * SDMMC peripheral interrupt is guaranteed to trigger on negedge.
566 */
567 xSemaphoreTake(s_io_intr_event, 0);
568 SDMMC.intmask.sdio |= BIT(slot); /* Re-enable SDIO interrupt */
569
570 if (xSemaphoreTake(s_io_intr_event, timeout_ticks) == pdTRUE) {
571 return ESP_OK;
572 } else {
573 return ESP_ERR_TIMEOUT;
574 }
575 }
576
577 /**
578 * @brief SDMMC interrupt handler
579 *
580 * All communication in SD protocol is driven by the master, and the hardware
581 * handles things like stop commands automatically.
582 * So the interrupt handler doesn't need to do much, we just push interrupt
583 * status into a queue, clear interrupt flags, and let the task currently
584 * doing communication figure out what to do next.
585 * This also applies to SDIO interrupts which are generated by the slave.
586 *
587 * Card detect interrupts pose a small issue though, because if a card is
588 * plugged in and out a few times, while there is no task to process
589 * the events, event queue can become full and some card detect events
590 * may be dropped. We ignore this problem for now, since the there are no other
591 * interesting events which can get lost due to this.
592 */
sdmmc_isr(void * arg)593 static void sdmmc_isr(void* arg) {
594 QueueHandle_t queue = (QueueHandle_t) arg;
595 sdmmc_event_t event;
596 int higher_priority_task_awoken = pdFALSE;
597
598 uint32_t pending = SDMMC.mintsts.val & 0xFFFF;
599 SDMMC.rintsts.val = pending;
600 event.sdmmc_status = pending;
601
602 uint32_t dma_pending = SDMMC.idsts.val;
603 SDMMC.idsts.val = dma_pending;
604 event.dma_status = dma_pending & 0x1f;
605
606 if (pending != 0 || dma_pending != 0) {
607 xQueueSendFromISR(queue, &event, &higher_priority_task_awoken);
608 }
609
610 uint32_t sdio_pending = SDMMC.mintsts.sdio;
611 if (sdio_pending) {
612 // disable the interrupt (no need to clear here, this is done in sdmmc_host_io_wait_int)
613 SDMMC.intmask.sdio &= ~sdio_pending;
614 xSemaphoreGiveFromISR(s_io_intr_event, &higher_priority_task_awoken);
615 }
616
617 if (higher_priority_task_awoken == pdTRUE) {
618 portYIELD_FROM_ISR();
619 }
620 }
621
sdmmc_host_pullup_en(int slot,int width)622 esp_err_t sdmmc_host_pullup_en(int slot, int width)
623 {
624 if (width > sdmmc_slot_info[slot].width) {
625 //in esp32 we only support 8 bit in slot 0, note this is occupied by the flash by default
626 return ESP_ERR_INVALID_ARG;
627 }
628 //according to the spec, the host control the clk, we don't to pull it up here
629 gpio_pullup_en(sdmmc_slot_info[slot].cmd_gpio);
630 gpio_pullup_en(sdmmc_slot_info[slot].d0_gpio);
631 if (width >= 4) {
632 gpio_pullup_en(sdmmc_slot_info[slot].d1_gpio);
633 gpio_pullup_en(sdmmc_slot_info[slot].d2_gpio);
634 gpio_pullup_en(sdmmc_slot_info[slot].d3_gpio);
635 }
636 if (width == 8) {
637 gpio_pullup_en(sdmmc_slot_info[slot].d4_gpio);
638 gpio_pullup_en(sdmmc_slot_info[slot].d5_gpio);
639 gpio_pullup_en(sdmmc_slot_info[slot].d6_gpio);
640 gpio_pullup_en(sdmmc_slot_info[slot].d7_gpio);
641 }
642 return ESP_OK;
643 }
644