sdmmc_host.c 20 KB

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