sdmmc_host.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "soc/soc_caps.h"
  8. #if SOC_SDMMC_HOST_SUPPORTED
  9. #include <stdint.h>
  10. #include <stddef.h>
  11. #include "esp_err.h"
  12. #include "driver/sdmmc_types.h"
  13. #include "driver/sdmmc_default_configs.h"
  14. #include "driver/gpio.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * Extra configuration for SDMMC peripheral slot
  20. */
  21. typedef struct {
  22. #ifdef SOC_SDMMC_USE_GPIO_MATRIX
  23. gpio_num_t clk; ///< GPIO number of CLK signal.
  24. gpio_num_t cmd; ///< GPIO number of CMD signal.
  25. gpio_num_t d0; ///< GPIO number of D0 signal.
  26. gpio_num_t d1; ///< GPIO number of D1 signal.
  27. gpio_num_t d2; ///< GPIO number of D2 signal.
  28. gpio_num_t d3; ///< GPIO number of D3 signal.
  29. gpio_num_t d4; ///< GPIO number of D4 signal. Ignored in 1- or 4- line mode.
  30. gpio_num_t d5; ///< GPIO number of D5 signal. Ignored in 1- or 4- line mode.
  31. gpio_num_t d6; ///< GPIO number of D6 signal. Ignored in 1- or 4- line mode.
  32. gpio_num_t d7; ///< GPIO number of D7 signal. Ignored in 1- or 4- line mode.
  33. #endif // SOC_SDMMC_USE_GPIO_MATRIX
  34. union {
  35. gpio_num_t gpio_cd; ///< GPIO number of card detect signal
  36. gpio_num_t cd; ///< GPIO number of card detect signal; shorter name.
  37. };
  38. union {
  39. gpio_num_t gpio_wp; ///< GPIO number of write protect signal
  40. gpio_num_t wp; ///< GPIO number of write protect signal; shorter name.
  41. };
  42. uint8_t width; ///< Bus width used by the slot (might be less than the max width supported)
  43. uint32_t flags; ///< Features used by this slot
  44. #define SDMMC_SLOT_FLAG_INTERNAL_PULLUP BIT(0)
  45. /**< Enable internal pullups on enabled pins. The internal pullups
  46. are insufficient however, please make sure external pullups are
  47. connected on the bus. This is for debug / example purpose only.
  48. */
  49. #define SDMMC_SLOT_FLAG_WP_ACTIVE_HIGH BIT(1)
  50. /**< GPIO write protect polarity.
  51. * 0 means "active low", i.e. card is protected when the GPIO is low;
  52. * 1 means "active high", i.e. card is protected when GPIO is high.
  53. */
  54. } sdmmc_slot_config_t;
  55. /**
  56. * @brief Initialize SDMMC host peripheral
  57. *
  58. * @note This function is not thread safe
  59. *
  60. * @return
  61. * - ESP_OK on success
  62. * - ESP_ERR_INVALID_STATE if sdmmc_host_init was already called
  63. * - ESP_ERR_NO_MEM if memory can not be allocated
  64. */
  65. esp_err_t sdmmc_host_init(void);
  66. /**
  67. * @brief Initialize given slot of SDMMC peripheral
  68. *
  69. * On the ESP32, SDMMC peripheral has two slots:
  70. * - Slot 0: 8-bit wide, maps to HS1_* signals in PIN MUX
  71. * - Slot 1: 4-bit wide, maps to HS2_* signals in PIN MUX
  72. *
  73. * Card detect and write protect signals can be routed to
  74. * arbitrary GPIOs using GPIO matrix.
  75. *
  76. * @note This function is not thread safe
  77. *
  78. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  79. * @param slot_config additional configuration for the slot
  80. * @return
  81. * - ESP_OK on success
  82. * - ESP_ERR_INVALID_STATE if host has not been initialized using sdmmc_host_init
  83. */
  84. esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config);
  85. /**
  86. * @brief Select bus width to be used for data transfer
  87. *
  88. * SD/MMC card must be initialized prior to this command, and a command to set
  89. * bus width has to be sent to the card (e.g. SD_APP_SET_BUS_WIDTH)
  90. *
  91. * @note This function is not thread safe
  92. *
  93. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  94. * @param width bus width (1, 4, or 8 for slot 0; 1 or 4 for slot 1)
  95. * @return
  96. * - ESP_OK on success
  97. * - ESP_ERR_INVALID_ARG if slot number or width is not valid
  98. */
  99. esp_err_t sdmmc_host_set_bus_width(int slot, size_t width);
  100. /**
  101. * @brief Get bus width configured in ``sdmmc_host_init_slot`` to be used for data transfer
  102. *
  103. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  104. * @return configured bus width of the specified slot.
  105. */
  106. size_t sdmmc_host_get_slot_width(int slot);
  107. /**
  108. * @brief Set card clock frequency
  109. *
  110. * Currently only integer fractions of 40MHz clock can be used.
  111. * For High Speed cards, 40MHz can be used.
  112. * For Default Speed cards, 20MHz can be used.
  113. *
  114. * @note This function is not thread safe
  115. *
  116. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  117. * @param freq_khz card clock frequency, in kHz
  118. * @return
  119. * - ESP_OK on success
  120. * - other error codes may be returned in the future
  121. */
  122. esp_err_t sdmmc_host_set_card_clk(int slot, uint32_t freq_khz);
  123. /**
  124. * @brief Enable or disable DDR mode of SD interface
  125. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  126. * @param ddr_enabled enable or disable DDR mode
  127. * @return
  128. * - ESP_OK on success
  129. * - ESP_ERR_NOT_SUPPORTED if DDR mode is not supported on this slot
  130. */
  131. esp_err_t sdmmc_host_set_bus_ddr_mode(int slot, bool ddr_enabled);
  132. /**
  133. * @brief Enable or disable always-on card clock
  134. * When cclk_always_on is false, the host controller is allowed to shut down
  135. * the card clock between the commands. When cclk_always_on is true, the clock
  136. * is generated even if no command is in progress.
  137. * @param slot slot number
  138. * @param cclk_always_on enable or disable always-on clock
  139. * @return
  140. * - ESP_OK on success
  141. * - ESP_ERR_INVALID_ARG if the slot number is invalid
  142. */
  143. esp_err_t sdmmc_host_set_cclk_always_on(int slot, bool cclk_always_on);
  144. /**
  145. * @brief Send command to the card and get response
  146. *
  147. * This function returns when command is sent and response is received,
  148. * or data is transferred, or timeout occurs.
  149. *
  150. * @note This function is not thread safe w.r.t. init/deinit functions,
  151. * and bus width/clock speed configuration functions. Multiple tasks
  152. * can call sdmmc_host_do_transaction as long as other sdmmc_host_*
  153. * functions are not called.
  154. *
  155. * @attention Data buffer passed in cmdinfo->data must be in DMA capable memory
  156. *
  157. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  158. * @param cmdinfo pointer to structure describing command and data to transfer
  159. * @return
  160. * - ESP_OK on success
  161. * - ESP_ERR_TIMEOUT if response or data transfer has timed out
  162. * - ESP_ERR_INVALID_CRC if response or data transfer CRC check has failed
  163. * - ESP_ERR_INVALID_RESPONSE if the card has sent an invalid response
  164. * - ESP_ERR_INVALID_SIZE if the size of data transfer is not valid in SD protocol
  165. * - ESP_ERR_INVALID_ARG if the data buffer is not in DMA capable memory
  166. */
  167. esp_err_t sdmmc_host_do_transaction(int slot, sdmmc_command_t* cmdinfo);
  168. /**
  169. * @brief Enable IO interrupts
  170. *
  171. * This function configures the host to accept SDIO interrupts.
  172. *
  173. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  174. * @return returns ESP_OK, other errors possible in the future
  175. */
  176. esp_err_t sdmmc_host_io_int_enable(int slot);
  177. /**
  178. * @brief Block until an SDIO interrupt is received, or timeout occurs
  179. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  180. * @param timeout_ticks number of RTOS ticks to wait for the interrupt
  181. * @return
  182. * - ESP_OK on success (interrupt received)
  183. * - ESP_ERR_TIMEOUT if the interrupt did not occur within timeout_ticks
  184. */
  185. esp_err_t sdmmc_host_io_int_wait(int slot, TickType_t timeout_ticks);
  186. /**
  187. * @brief Disable SDMMC host and release allocated resources
  188. *
  189. * @note This function is not thread safe
  190. *
  191. * @return
  192. * - ESP_OK on success
  193. * - ESP_ERR_INVALID_STATE if sdmmc_host_init function has not been called
  194. */
  195. esp_err_t sdmmc_host_deinit(void);
  196. /**
  197. * @brief Provides a real frequency used for an SD card installed on specific slot
  198. * of SD/MMC host controller
  199. *
  200. * This function calculates real working frequency given by current SD/MMC host
  201. * controller setup for required slot: it reads associated host and card dividers
  202. * from corresponding SDMMC registers, calculates respective frequency and stores
  203. * the value into the 'real_freq_khz' parameter
  204. *
  205. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  206. * @param[out] real_freq_khz output parameter for the result frequency (in kHz)
  207. * @return
  208. * - ESP_OK on success
  209. * - ESP_ERR_INVALID_ARG on real_freq_khz == NULL or invalid slot number used
  210. */
  211. esp_err_t sdmmc_host_get_real_freq(int slot, int* real_freq_khz);
  212. /**
  213. * @brief set input delay
  214. *
  215. * @note ESP32 doesn't support this feature, you will get an `ESP_ERR_NOT_SUPPORTED`
  216. *
  217. * - This API sets delay when the SDMMC Host samples the signal from the SD Slave.
  218. * - This API will check if the given `delay_phase` is valid or not.
  219. * - This API will print out the delay time, in picosecond (ps)
  220. *
  221. * @param slot slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
  222. * @param delay_phase delay phase, this API will convert the phase into picoseconds and print it out
  223. *
  224. * @return
  225. * - ESP_OK: ON success.
  226. * - ESP_ERR_INVALID_ARG: Invalid argument.
  227. * - ESP_ERR_NOT_SUPPORTED: ESP32 doesn't support this feature.
  228. */
  229. esp_err_t sdmmc_host_set_input_delay(int slot, sdmmc_delay_phase_t delay_phase);
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif //SOC_SDMMC_HOST_SUPPORTED