sdmmc_host.c 20 KB

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