sdmmc_host.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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_reset(PERIPH_SDMMC_MODULE);
  202. periph_module_enable(PERIPH_SDMMC_MODULE);
  203. // Enable clock to peripheral. Use smallest divider first.
  204. sdmmc_host_set_clk_div(2);
  205. // Reset
  206. sdmmc_host_reset();
  207. ESP_LOGD(TAG, "peripheral version %x, hardware config %08x", SDMMC.verid, SDMMC.hcon);
  208. // Clear interrupt status and set interrupt mask to known state
  209. SDMMC.rintsts.val = 0xffffffff;
  210. SDMMC.intmask.val = 0;
  211. SDMMC.ctrl.int_enable = 0;
  212. // Allocate event queue
  213. s_event_queue = xQueueCreate(SDMMC_EVENT_QUEUE_LENGTH, sizeof(sdmmc_event_t));
  214. if (!s_event_queue) {
  215. return ESP_ERR_NO_MEM;
  216. }
  217. s_io_intr_event = xSemaphoreCreateBinary();
  218. if (!s_io_intr_event) {
  219. vQueueDelete(s_event_queue);
  220. s_event_queue = NULL;
  221. return ESP_ERR_NO_MEM;
  222. }
  223. // Attach interrupt handler
  224. esp_err_t ret = esp_intr_alloc(ETS_SDIO_HOST_INTR_SOURCE, 0, &sdmmc_isr, s_event_queue, &s_intr_handle);
  225. if (ret != ESP_OK) {
  226. vQueueDelete(s_event_queue);
  227. s_event_queue = NULL;
  228. vSemaphoreDelete(s_io_intr_event);
  229. s_io_intr_event = NULL;
  230. return ret;
  231. }
  232. // Enable interrupts
  233. SDMMC.intmask.val =
  234. SDMMC_INTMASK_CD |
  235. SDMMC_INTMASK_CMD_DONE |
  236. SDMMC_INTMASK_DATA_OVER |
  237. SDMMC_INTMASK_RCRC | SDMMC_INTMASK_DCRC |
  238. SDMMC_INTMASK_RTO | SDMMC_INTMASK_DTO | SDMMC_INTMASK_HTO |
  239. SDMMC_INTMASK_SBE | SDMMC_INTMASK_EBE |
  240. SDMMC_INTMASK_RESP_ERR | SDMMC_INTMASK_HLE; //sdio is enabled only when use.
  241. SDMMC.ctrl.int_enable = 1;
  242. // Disable generation of Busy Clear Interrupt
  243. SDMMC.cardthrctl.busy_clr_int_en = 0;
  244. // Enable DMA
  245. sdmmc_host_dma_init();
  246. // Initialize transaction handler
  247. ret = sdmmc_host_transaction_handler_init();
  248. if (ret != ESP_OK) {
  249. vQueueDelete(s_event_queue);
  250. s_event_queue = NULL;
  251. vSemaphoreDelete(s_io_intr_event);
  252. s_io_intr_event = NULL;
  253. esp_intr_free(s_intr_handle);
  254. s_intr_handle = NULL;
  255. return ret;
  256. }
  257. return ESP_OK;
  258. }
  259. static void configure_pin(int pin)
  260. {
  261. const int sdmmc_func = 3;
  262. const int drive_strength = 3;
  263. assert(pin!=-1);
  264. gpio_pulldown_dis(pin);
  265. uint32_t reg = GPIO_PIN_MUX_REG[pin];
  266. assert(reg != UINT32_MAX);
  267. PIN_INPUT_ENABLE(reg);
  268. PIN_FUNC_SELECT(reg, sdmmc_func);
  269. PIN_SET_DRV(reg, drive_strength);
  270. }
  271. esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config)
  272. {
  273. if (!s_intr_handle) {
  274. return ESP_ERR_INVALID_STATE;
  275. }
  276. if (!(slot == 0 || slot == 1)) {
  277. return ESP_ERR_INVALID_ARG;
  278. }
  279. if (slot_config == NULL) {
  280. return ESP_ERR_INVALID_ARG;
  281. }
  282. bool pullup = slot_config->flags & SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  283. if (pullup) {
  284. sdmmc_host_pullup_en(slot, slot_config->width);
  285. }
  286. int gpio_cd = slot_config->gpio_cd;
  287. int gpio_wp = slot_config->gpio_wp;
  288. uint8_t slot_width = slot_config->width;
  289. // Configure pins
  290. const sdmmc_slot_info_t* pslot = &sdmmc_slot_info[slot];
  291. if (slot_width == SDMMC_SLOT_WIDTH_DEFAULT) {
  292. slot_width = pslot->width;
  293. }
  294. else if (slot_width > pslot->width) {
  295. return ESP_ERR_INVALID_ARG;
  296. }
  297. s_slot_width[slot] = slot_width;
  298. configure_pin(pslot->clk_gpio);
  299. configure_pin(pslot->cmd_gpio);
  300. configure_pin(pslot->d0_gpio);
  301. if (slot_width >= 4) {
  302. configure_pin(pslot->d1_gpio);
  303. configure_pin(pslot->d2_gpio);
  304. // Force D3 high to make slave enter SD mode.
  305. // Connect to peripheral after width configuration.
  306. gpio_config_t gpio_conf = {
  307. .pin_bit_mask = BIT(pslot->d3_gpio),
  308. .mode = GPIO_MODE_OUTPUT ,
  309. .pull_up_en = 0,
  310. .pull_down_en = 0,
  311. .intr_type = GPIO_INTR_DISABLE,
  312. };
  313. gpio_config(&gpio_conf);
  314. gpio_set_level(pslot->d3_gpio, 1);
  315. if (slot_width == 8) {
  316. configure_pin(pslot->d4_gpio);
  317. configure_pin(pslot->d5_gpio);
  318. configure_pin(pslot->d6_gpio);
  319. configure_pin(pslot->d7_gpio);
  320. }
  321. }
  322. // SDIO slave interrupt is edge sensitive to ~(int_n | card_int | card_detect)
  323. // set this and card_detect to high to enable sdio interrupt
  324. gpio_matrix_in(GPIO_FUNC_IN_HIGH, pslot->card_int, false);
  325. // Set up Card Detect input
  326. int matrix_in_cd;
  327. if (gpio_cd != SDMMC_SLOT_NO_CD) {
  328. ESP_LOGD(TAG, "using GPIO%d as CD pin", gpio_cd);
  329. gpio_pad_select_gpio(gpio_cd);
  330. gpio_set_direction(gpio_cd, GPIO_MODE_INPUT);
  331. matrix_in_cd = gpio_cd;
  332. } else {
  333. // if not set, default to CD low (card present)
  334. matrix_in_cd = GPIO_FUNC_IN_LOW;
  335. }
  336. gpio_matrix_in(matrix_in_cd, pslot->card_detect, false);
  337. // Set up Write Protect input
  338. int matrix_in_wp;
  339. if (gpio_wp != SDMMC_SLOT_NO_WP) {
  340. ESP_LOGD(TAG, "using GPIO%d as WP pin", gpio_wp);
  341. gpio_pad_select_gpio(gpio_wp);
  342. gpio_set_direction(gpio_wp, GPIO_MODE_INPUT);
  343. matrix_in_wp = gpio_wp;
  344. } else {
  345. // if not set, default to WP high (not write protected)
  346. matrix_in_wp = GPIO_FUNC_IN_HIGH;
  347. }
  348. // WP signal is normally active low, but hardware expects
  349. // an active-high signal, so invert it in GPIO matrix
  350. gpio_matrix_in(matrix_in_wp, pslot->write_protect, true);
  351. // By default, set probing frequency (400kHz) and 1-bit bus
  352. esp_err_t ret = sdmmc_host_set_card_clk(slot, 400);
  353. if (ret != ESP_OK) {
  354. return ret;
  355. }
  356. ret = sdmmc_host_set_bus_width(slot, 1);
  357. if (ret != ESP_OK) {
  358. return ret;
  359. }
  360. return ESP_OK;
  361. }
  362. esp_err_t sdmmc_host_deinit()
  363. {
  364. if (!s_intr_handle) {
  365. return ESP_ERR_INVALID_STATE;
  366. }
  367. esp_intr_free(s_intr_handle);
  368. s_intr_handle = NULL;
  369. vQueueDelete(s_event_queue);
  370. s_event_queue = NULL;
  371. vQueueDelete(s_io_intr_event);
  372. s_io_intr_event = NULL;
  373. sdmmc_host_input_clk_disable();
  374. sdmmc_host_transaction_handler_deinit();
  375. periph_module_disable(PERIPH_SDMMC_MODULE);
  376. return ESP_OK;
  377. }
  378. esp_err_t sdmmc_host_wait_for_event(int tick_count, sdmmc_event_t* out_event)
  379. {
  380. if (!out_event) {
  381. return ESP_ERR_INVALID_ARG;
  382. }
  383. if (!s_event_queue) {
  384. return ESP_ERR_INVALID_STATE;
  385. }
  386. int ret = xQueueReceive(s_event_queue, out_event, tick_count);
  387. if (ret == pdFALSE) {
  388. return ESP_ERR_TIMEOUT;
  389. }
  390. return ESP_OK;
  391. }
  392. esp_err_t sdmmc_host_set_bus_width(int slot, size_t width)
  393. {
  394. if (!(slot == 0 || slot == 1)) {
  395. return ESP_ERR_INVALID_ARG;
  396. }
  397. if (sdmmc_slot_info[slot].width < width) {
  398. return ESP_ERR_INVALID_ARG;
  399. }
  400. const uint16_t mask = BIT(slot);
  401. if (width == 1) {
  402. SDMMC.ctype.card_width_8 &= ~mask;
  403. SDMMC.ctype.card_width &= ~mask;
  404. } else if (width == 4) {
  405. SDMMC.ctype.card_width_8 &= ~mask;
  406. SDMMC.ctype.card_width |= mask;
  407. // D3 was set to GPIO high to force slave into SD mode, until 4-bit mode is set
  408. configure_pin(sdmmc_slot_info[slot].d3_gpio);
  409. } else if (width == 8) {
  410. SDMMC.ctype.card_width_8 |= mask;
  411. // D3 was set to GPIO high to force slave into SD mode, until 4-bit mode is set
  412. configure_pin(sdmmc_slot_info[slot].d3_gpio);
  413. } else {
  414. return ESP_ERR_INVALID_ARG;
  415. }
  416. ESP_LOGD(TAG, "slot=%d width=%d", slot, width);
  417. return ESP_OK;
  418. }
  419. size_t sdmmc_host_get_slot_width(int slot)
  420. {
  421. assert( slot == 0 || slot == 1 );
  422. return s_slot_width[slot];
  423. }
  424. esp_err_t sdmmc_host_set_bus_ddr_mode(int slot, bool ddr_enabled)
  425. {
  426. if (!(slot == 0 || slot == 1)) {
  427. return ESP_ERR_INVALID_ARG;
  428. }
  429. if (s_slot_width[slot] == 8 && ddr_enabled) {
  430. ESP_LOGW(TAG, "DDR mode with 8-bit bus width is not supported yet");
  431. // requires reconfiguring controller clock for 2x card frequency
  432. return ESP_ERR_NOT_SUPPORTED;
  433. }
  434. uint32_t mask = BIT(slot);
  435. if (ddr_enabled) {
  436. SDMMC.uhs.ddr |= mask;
  437. SDMMC.emmc_ddr_reg |= mask;
  438. } else {
  439. SDMMC.uhs.ddr &= ~mask;
  440. SDMMC.emmc_ddr_reg &= ~mask;
  441. }
  442. ESP_LOGD(TAG, "slot=%d ddr=%d", slot, ddr_enabled ? 1 : 0);
  443. return ESP_OK;
  444. }
  445. static void sdmmc_host_dma_init()
  446. {
  447. SDMMC.ctrl.dma_enable = 1;
  448. SDMMC.bmod.val = 0;
  449. SDMMC.bmod.sw_reset = 1;
  450. SDMMC.idinten.ni = 1;
  451. SDMMC.idinten.ri = 1;
  452. SDMMC.idinten.ti = 1;
  453. }
  454. void sdmmc_host_dma_stop()
  455. {
  456. SDMMC.ctrl.use_internal_dma = 0;
  457. SDMMC.ctrl.dma_reset = 1;
  458. SDMMC.bmod.fb = 0;
  459. SDMMC.bmod.enable = 0;
  460. }
  461. void sdmmc_host_dma_prepare(sdmmc_desc_t* desc, size_t block_size, size_t data_size)
  462. {
  463. // Set size of data and DMA descriptor pointer
  464. SDMMC.bytcnt = data_size;
  465. SDMMC.blksiz = block_size;
  466. SDMMC.dbaddr = desc;
  467. // Enable everything needed to use DMA
  468. SDMMC.ctrl.dma_enable = 1;
  469. SDMMC.ctrl.use_internal_dma = 1;
  470. SDMMC.bmod.enable = 1;
  471. SDMMC.bmod.fb = 1;
  472. sdmmc_host_dma_resume();
  473. }
  474. void sdmmc_host_dma_resume()
  475. {
  476. SDMMC.pldmnd = 1;
  477. }
  478. bool sdmmc_host_card_busy()
  479. {
  480. return SDMMC.status.data_busy == 1;
  481. }
  482. esp_err_t sdmmc_host_io_int_enable(int slot)
  483. {
  484. configure_pin(sdmmc_slot_info[slot].d1_gpio);
  485. return ESP_OK;
  486. }
  487. esp_err_t sdmmc_host_io_int_wait(int slot, TickType_t timeout_ticks)
  488. {
  489. /* SDIO interrupts are negedge sensitive ones: the status bit is only set
  490. * when first interrupt triggered.
  491. *
  492. * If D1 GPIO is low when entering this function, we know that interrupt
  493. * (in SDIO sense) has occurred and we don't need to use SDMMC peripheral
  494. * interrupt.
  495. */
  496. SDMMC.intmask.sdio &= ~BIT(slot); /* Disable SDIO interrupt */
  497. SDMMC.rintsts.sdio = BIT(slot);
  498. if (gpio_get_level(sdmmc_slot_info[slot].d1_gpio) == 0) {
  499. return ESP_OK;
  500. }
  501. /* Otherwise, need to wait for an interrupt. Since D1 was high,
  502. * SDMMC peripheral interrupt is guaranteed to trigger on negedge.
  503. */
  504. xSemaphoreTake(s_io_intr_event, 0);
  505. SDMMC.intmask.sdio |= BIT(slot); /* Re-enable SDIO interrupt */
  506. if (xSemaphoreTake(s_io_intr_event, timeout_ticks) == pdTRUE) {
  507. return ESP_OK;
  508. } else {
  509. return ESP_ERR_TIMEOUT;
  510. }
  511. }
  512. /**
  513. * @brief SDMMC interrupt handler
  514. *
  515. * All communication in SD protocol is driven by the master, and the hardware
  516. * handles things like stop commands automatically.
  517. * So the interrupt handler doesn't need to do much, we just push interrupt
  518. * status into a queue, clear interrupt flags, and let the task currently
  519. * doing communication figure out what to do next.
  520. * This also applies to SDIO interrupts which are generated by the slave.
  521. *
  522. * Card detect interrupts pose a small issue though, because if a card is
  523. * plugged in and out a few times, while there is no task to process
  524. * the events, event queue can become full and some card detect events
  525. * may be dropped. We ignore this problem for now, since the there are no other
  526. * interesting events which can get lost due to this.
  527. */
  528. static void sdmmc_isr(void* arg) {
  529. QueueHandle_t queue = (QueueHandle_t) arg;
  530. sdmmc_event_t event;
  531. int higher_priority_task_awoken = pdFALSE;
  532. uint32_t pending = SDMMC.mintsts.val & 0xFFFF;
  533. SDMMC.rintsts.val = pending;
  534. event.sdmmc_status = pending;
  535. uint32_t dma_pending = SDMMC.idsts.val;
  536. SDMMC.idsts.val = dma_pending;
  537. event.dma_status = dma_pending & 0x1f;
  538. if (pending != 0 || dma_pending != 0) {
  539. xQueueSendFromISR(queue, &event, &higher_priority_task_awoken);
  540. }
  541. uint32_t sdio_pending = SDMMC.mintsts.sdio;
  542. if (sdio_pending) {
  543. // disable the interrupt (no need to clear here, this is done in sdmmc_host_io_wait_int)
  544. SDMMC.intmask.sdio &= ~sdio_pending;
  545. xSemaphoreGiveFromISR(s_io_intr_event, &higher_priority_task_awoken);
  546. }
  547. if (higher_priority_task_awoken == pdTRUE) {
  548. portYIELD_FROM_ISR();
  549. }
  550. }
  551. esp_err_t sdmmc_host_pullup_en(int slot, int width)
  552. {
  553. if (width > sdmmc_slot_info[slot].width) {
  554. //in esp32 we only support 8 bit in slot 0, note this is occupied by the flash by default
  555. return ESP_ERR_INVALID_ARG;
  556. }
  557. //according to the spec, the host control the clk, we don't to pull it up here
  558. gpio_pullup_en(sdmmc_slot_info[slot].cmd_gpio);
  559. gpio_pullup_en(sdmmc_slot_info[slot].d0_gpio);
  560. if (width >= 4) {
  561. gpio_pullup_en(sdmmc_slot_info[slot].d1_gpio);
  562. gpio_pullup_en(sdmmc_slot_info[slot].d2_gpio);
  563. gpio_pullup_en(sdmmc_slot_info[slot].d3_gpio);
  564. }
  565. if (width == 8) {
  566. gpio_pullup_en(sdmmc_slot_info[slot].d4_gpio);
  567. gpio_pullup_en(sdmmc_slot_info[slot].d5_gpio);
  568. gpio_pullup_en(sdmmc_slot_info[slot].d6_gpio);
  569. gpio_pullup_en(sdmmc_slot_info[slot].d7_gpio);
  570. }
  571. return ESP_OK;
  572. }