sdspi_host.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2015-2017 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. #pragma once
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #include "esp_err.h"
  18. #include "sdmmc_types.h"
  19. #include "driver/gpio.h"
  20. #include "driver/spi_master.h"
  21. #include "driver/sdmmc_host.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /**
  26. * @brief Default sdmmc_host_t structure initializer for SD over SPI driver
  27. *
  28. * Uses SPI mode and max frequency set to 20MHz
  29. *
  30. * 'slot' can be set to one of HSPI_HOST, VSPI_HOST.
  31. */
  32. #define SDSPI_HOST_DEFAULT() {\
  33. .flags = SDMMC_HOST_FLAG_SPI, \
  34. .slot = HSPI_HOST, \
  35. .max_freq_khz = SDMMC_FREQ_DEFAULT, \
  36. .io_voltage = 3.3f, \
  37. .init = &sdspi_host_init, \
  38. .set_bus_width = NULL, \
  39. .get_bus_width = NULL, \
  40. .set_bus_ddr_mode = NULL, \
  41. .set_card_clk = &sdspi_host_set_card_clk, \
  42. .do_transaction = &sdspi_host_do_transaction, \
  43. .deinit = &sdspi_host_deinit, \
  44. .io_int_enable = NULL, \
  45. .io_int_wait = NULL, \
  46. .command_timeout_ms = 0, \
  47. }
  48. /**
  49. * Extra configuration for SPI host
  50. */
  51. typedef struct {
  52. gpio_num_t gpio_miso; ///< GPIO number of MISO signal
  53. gpio_num_t gpio_mosi; ///< GPIO number of MOSI signal
  54. gpio_num_t gpio_sck; ///< GPIO number of SCK signal
  55. gpio_num_t gpio_cs; ///< GPIO number of CS signal
  56. gpio_num_t gpio_cd; ///< GPIO number of card detect signal
  57. gpio_num_t gpio_wp; ///< GPIO number of write protect signal
  58. int dma_channel; ///< DMA channel to be used by SPI driver (1 or 2)
  59. } sdspi_slot_config_t;
  60. #define SDSPI_SLOT_NO_CD ((gpio_num_t) -1) ///< indicates that card detect line is not used
  61. #define SDSPI_SLOT_NO_WP ((gpio_num_t) -1) ///< indicates that write protect line is not used
  62. /**
  63. * Macro defining default configuration of SPI host
  64. */
  65. #define SDSPI_SLOT_CONFIG_DEFAULT() {\
  66. .gpio_miso = GPIO_NUM_2, \
  67. .gpio_mosi = GPIO_NUM_15, \
  68. .gpio_sck = GPIO_NUM_14, \
  69. .gpio_cs = GPIO_NUM_13, \
  70. .gpio_cd = SDSPI_SLOT_NO_CD, \
  71. .gpio_wp = SDSPI_SLOT_NO_WP, \
  72. .dma_channel = 1 \
  73. }
  74. /**
  75. * @brief Initialize SD SPI driver
  76. *
  77. * @note This function is not thread safe
  78. *
  79. * @return
  80. * - ESP_OK on success
  81. * - other error codes may be returned in future versions
  82. */
  83. esp_err_t sdspi_host_init();
  84. /**
  85. * @brief Initialize SD SPI driver for the specific SPI controller
  86. *
  87. * @note This function is not thread safe
  88. *
  89. * @param slot SPI controller to use (HSPI_HOST or VSPI_HOST)
  90. * @param slot_config pointer to slot configuration structure
  91. *
  92. * @return
  93. * - ESP_OK on success
  94. * - ESP_ERR_INVALID_ARG if sdspi_init_slot has invalid arguments
  95. * - ESP_ERR_NO_MEM if memory can not be allocated
  96. * - other errors from the underlying spi_master and gpio drivers
  97. */
  98. esp_err_t sdspi_host_init_slot(int slot, const sdspi_slot_config_t* slot_config);
  99. /**
  100. * @brief Send command to the card and get response
  101. *
  102. * This function returns when command is sent and response is received,
  103. * or data is transferred, or timeout occurs.
  104. *
  105. * @note This function is not thread safe w.r.t. init/deinit functions,
  106. * and bus width/clock speed configuration functions. Multiple tasks
  107. * can call sdspi_host_do_transaction as long as other sdspi_host_*
  108. * functions are not called.
  109. *
  110. * @param slot SPI controller (HSPI_HOST or VSPI_HOST)
  111. * @param cmdinfo pointer to structure describing command and data to transfer
  112. * @return
  113. * - ESP_OK on success
  114. * - ESP_ERR_TIMEOUT if response or data transfer has timed out
  115. * - ESP_ERR_INVALID_CRC if response or data transfer CRC check has failed
  116. * - ESP_ERR_INVALID_RESPONSE if the card has sent an invalid response
  117. */
  118. esp_err_t sdspi_host_do_transaction(int slot, sdmmc_command_t *cmdinfo);
  119. /**
  120. * @brief Set card clock frequency
  121. *
  122. * Currently only integer fractions of 40MHz clock can be used.
  123. * For High Speed cards, 40MHz can be used.
  124. * For Default Speed cards, 20MHz can be used.
  125. *
  126. * @note This function is not thread safe
  127. *
  128. * @param slot SPI controller (HSPI_HOST or VSPI_HOST)
  129. * @param freq_khz card clock frequency, in kHz
  130. * @return
  131. * - ESP_OK on success
  132. * - other error codes may be returned in the future
  133. */
  134. esp_err_t sdspi_host_set_card_clk(int slot, uint32_t freq_khz);
  135. /**
  136. * @brief Release resources allocated using sdspi_host_init
  137. *
  138. * @note This function is not thread safe
  139. *
  140. * @return
  141. * - ESP_OK on success
  142. * - ESP_ERR_INVALID_STATE if sdspi_host_init function has not been called
  143. */
  144. esp_err_t sdspi_host_deinit();
  145. #ifdef __cplusplus
  146. }
  147. #endif