sdspi_host.h 4.9 KB

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