sdmmc_host.c 20 KB

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