sdmmc_cmd.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdio.h>
  8. #include "esp_err.h"
  9. #include "driver/sdmmc_types.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * Probe and initialize SD/MMC card using given host
  15. *
  16. * @note Only SD cards (SDSC and SDHC/SDXC) are supported now.
  17. * Support for MMC/eMMC cards will be added later.
  18. *
  19. * @param host pointer to structure defining host controller
  20. * @param out_card pointer to structure which will receive information
  21. * about the card when the function completes
  22. * @return
  23. * - ESP_OK on success
  24. * - One of the error codes from SDMMC host controller
  25. */
  26. esp_err_t sdmmc_card_init(const sdmmc_host_t* host,
  27. sdmmc_card_t* out_card);
  28. /**
  29. * @brief Print information about the card to a stream
  30. * @param stream stream obtained using fopen or fdopen
  31. * @param card card information structure initialized using sdmmc_card_init
  32. */
  33. void sdmmc_card_print_info(FILE* stream, const sdmmc_card_t* card);
  34. /**
  35. * Get status of SD/MMC card
  36. *
  37. * @param card pointer to card information structure previously initialized
  38. * using sdmmc_card_init
  39. * @return
  40. * - ESP_OK on success
  41. * - One of the error codes from SDMMC host controller
  42. */
  43. esp_err_t sdmmc_get_status(sdmmc_card_t* card);
  44. /**
  45. * Write given number of sectors to SD/MMC card
  46. *
  47. * @param card pointer to card information structure previously initialized
  48. * using sdmmc_card_init
  49. * @param src pointer to data buffer to read data from; data size must be
  50. * equal to sector_count * card->csd.sector_size
  51. * @param start_sector sector where to start writing
  52. * @param sector_count number of sectors to write
  53. * @return
  54. * - ESP_OK on success
  55. * - One of the error codes from SDMMC host controller
  56. */
  57. esp_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src,
  58. size_t start_sector, size_t sector_count);
  59. /**
  60. * Read given number of sectors from the SD/MMC card
  61. *
  62. * @param card pointer to card information structure previously initialized
  63. * using sdmmc_card_init
  64. * @param dst pointer to data buffer to write into; buffer size must be
  65. * at least sector_count * card->csd.sector_size
  66. * @param start_sector sector where to start reading
  67. * @param sector_count number of sectors to read
  68. * @return
  69. * - ESP_OK on success
  70. * - One of the error codes from SDMMC host controller
  71. */
  72. esp_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst,
  73. size_t start_sector, size_t sector_count);
  74. /**
  75. * Read one byte from an SDIO card using IO_RW_DIRECT (CMD52)
  76. *
  77. * @param card pointer to card information structure previously initialized
  78. * using sdmmc_card_init
  79. * @param function IO function number
  80. * @param reg byte address within IO function
  81. * @param[out] out_byte output, receives the value read from the card
  82. * @return
  83. * - ESP_OK on success
  84. * - One of the error codes from SDMMC host controller
  85. */
  86. esp_err_t sdmmc_io_read_byte(sdmmc_card_t* card, uint32_t function,
  87. uint32_t reg, uint8_t *out_byte);
  88. /**
  89. * Write one byte to an SDIO card using IO_RW_DIRECT (CMD52)
  90. *
  91. * @param card pointer to card information structure previously initialized
  92. * using sdmmc_card_init
  93. * @param function IO function number
  94. * @param reg byte address within IO function
  95. * @param in_byte value to be written
  96. * @param[out] out_byte if not NULL, receives new byte value read
  97. * from the card (read-after-write).
  98. * @return
  99. * - ESP_OK on success
  100. * - One of the error codes from SDMMC host controller
  101. */
  102. esp_err_t sdmmc_io_write_byte(sdmmc_card_t* card, uint32_t function,
  103. uint32_t reg, uint8_t in_byte, uint8_t* out_byte);
  104. /**
  105. * Read multiple bytes from an SDIO card using IO_RW_EXTENDED (CMD53)
  106. *
  107. * This function performs read operation using CMD53 in byte mode.
  108. * For block mode, see sdmmc_io_read_blocks.
  109. *
  110. * @param card pointer to card information structure previously initialized
  111. * using sdmmc_card_init
  112. * @param function IO function number
  113. * @param addr byte address within IO function where reading starts
  114. * @param dst buffer which receives the data read from card
  115. * @param size number of bytes to read
  116. * @return
  117. * - ESP_OK on success
  118. * - ESP_ERR_INVALID_SIZE if size exceeds 512 bytes
  119. * - One of the error codes from SDMMC host controller
  120. */
  121. esp_err_t sdmmc_io_read_bytes(sdmmc_card_t* card, uint32_t function,
  122. uint32_t addr, void* dst, size_t size);
  123. /**
  124. * Write multiple bytes to an SDIO card using IO_RW_EXTENDED (CMD53)
  125. *
  126. * This function performs write operation using CMD53 in byte mode.
  127. * For block mode, see sdmmc_io_write_blocks.
  128. *
  129. * @param card pointer to card information structure previously initialized
  130. * using sdmmc_card_init
  131. * @param function IO function number
  132. * @param addr byte address within IO function where writing starts
  133. * @param src data to be written
  134. * @param size number of bytes to write
  135. * @return
  136. * - ESP_OK on success
  137. * - ESP_ERR_INVALID_SIZE if size exceeds 512 bytes
  138. * - One of the error codes from SDMMC host controller
  139. */
  140. esp_err_t sdmmc_io_write_bytes(sdmmc_card_t* card, uint32_t function,
  141. uint32_t addr, const void* src, size_t size);
  142. /**
  143. * Read blocks of data from an SDIO card using IO_RW_EXTENDED (CMD53)
  144. *
  145. * This function performs read operation using CMD53 in block mode.
  146. * For byte mode, see sdmmc_io_read_bytes.
  147. *
  148. * @param card pointer to card information structure previously initialized
  149. * using sdmmc_card_init
  150. * @param function IO function number
  151. * @param addr byte address within IO function where writing starts
  152. * @param dst buffer which receives the data read from card
  153. * @param size number of bytes to read, must be divisible by the card block
  154. * size.
  155. * @return
  156. * - ESP_OK on success
  157. * - ESP_ERR_INVALID_SIZE if size is not divisible by 512 bytes
  158. * - One of the error codes from SDMMC host controller
  159. */
  160. esp_err_t sdmmc_io_read_blocks(sdmmc_card_t* card, uint32_t function,
  161. uint32_t addr, void* dst, size_t size);
  162. /**
  163. * Write blocks of data to an SDIO card using IO_RW_EXTENDED (CMD53)
  164. *
  165. * This function performs write operation using CMD53 in block mode.
  166. * For byte mode, see sdmmc_io_write_bytes.
  167. *
  168. * @param card pointer to card information structure previously initialized
  169. * using sdmmc_card_init
  170. * @param function IO function number
  171. * @param addr byte address within IO function where writing starts
  172. * @param src data to be written
  173. * @param size number of bytes to read, must be divisible by the card block
  174. * size.
  175. * @return
  176. * - ESP_OK on success
  177. * - ESP_ERR_INVALID_SIZE if size is not divisible by 512 bytes
  178. * - One of the error codes from SDMMC host controller
  179. */
  180. esp_err_t sdmmc_io_write_blocks(sdmmc_card_t* card, uint32_t function,
  181. uint32_t addr, const void* src, size_t size);
  182. /**
  183. * Enable SDIO interrupt in the SDMMC host
  184. *
  185. * @param card pointer to card information structure previously initialized
  186. * using sdmmc_card_init
  187. * @return
  188. * - ESP_OK on success
  189. * - ESP_ERR_NOT_SUPPORTED if the host controller does not support
  190. * IO interrupts
  191. */
  192. esp_err_t sdmmc_io_enable_int(sdmmc_card_t* card);
  193. /**
  194. * Block until an SDIO interrupt is received
  195. *
  196. * Slave uses D1 line to signal interrupt condition to the host.
  197. * This function can be used to wait for the interrupt.
  198. *
  199. * @param card pointer to card information structure previously initialized
  200. * using sdmmc_card_init
  201. * @param timeout_ticks time to wait for the interrupt, in RTOS ticks
  202. * @return
  203. * - ESP_OK if the interrupt is received
  204. * - ESP_ERR_NOT_SUPPORTED if the host controller does not support
  205. * IO interrupts
  206. * - ESP_ERR_TIMEOUT if the interrupt does not happen in timeout_ticks
  207. */
  208. esp_err_t sdmmc_io_wait_int(sdmmc_card_t* card, TickType_t timeout_ticks);
  209. /**
  210. * Get the data of CIS region of a SDIO card.
  211. *
  212. * You may provide a buffer not sufficient to store all the CIS data. In this
  213. * case, this functions store as much data into your buffer as possible. Also,
  214. * this function will try to get and return the size required for you.
  215. *
  216. * @param card pointer to card information structure previously initialized
  217. * using sdmmc_card_init
  218. * @param out_buffer Output buffer of the CIS data
  219. * @param buffer_size Size of the buffer.
  220. * @param inout_cis_size Mandatory, pointer to a size, input and output.
  221. * - input: Limitation of maximum searching range, should be 0 or larger than
  222. * buffer_size. The function searches for CIS_CODE_END until this range. Set to
  223. * 0 to search infinitely.
  224. * - output: The size required to store all the CIS data, if CIS_CODE_END is found.
  225. *
  226. * @return
  227. * - ESP_OK: on success
  228. * - ESP_ERR_INVALID_RESPONSE: if the card does not (correctly) support CIS.
  229. * - ESP_ERR_INVALID_SIZE: CIS_CODE_END found, but buffer_size is less than
  230. * required size, which is stored in the inout_cis_size then.
  231. * - ESP_ERR_NOT_FOUND: if the CIS_CODE_END not found. Increase input value of
  232. * inout_cis_size or set it to 0, if you still want to search for the end;
  233. * output value of inout_cis_size is invalid in this case.
  234. * - and other error code return from sdmmc_io_read_bytes
  235. */
  236. esp_err_t sdmmc_io_get_cis_data(sdmmc_card_t* card, uint8_t* out_buffer, size_t buffer_size, size_t* inout_cis_size);
  237. /**
  238. * Parse and print the CIS information of a SDIO card.
  239. *
  240. * @note Not all the CIS codes and all kinds of tuples are supported. If you
  241. * see some unresolved code, you can add the parsing of these code in
  242. * sdmmc_io.c and contribute to the IDF through the Github repository.
  243. *
  244. * using sdmmc_card_init
  245. * @param buffer Buffer to parse
  246. * @param buffer_size Size of the buffer.
  247. * @param fp File pointer to print to, set to NULL to print to stdout.
  248. *
  249. * @return
  250. * - ESP_OK: on success
  251. * - ESP_ERR_NOT_SUPPORTED: if the value from the card is not supported to be parsed.
  252. * - ESP_ERR_INVALID_SIZE: if the CIS size fields are not correct.
  253. */
  254. esp_err_t sdmmc_io_print_cis_info(uint8_t* buffer, size_t buffer_size, FILE* fp);
  255. #ifdef __cplusplus
  256. }
  257. #endif