sdmmc_host.c 20 KB

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