sdmmc_cmd.h 11 KB

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