sdmmc_cmd_mh.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. extern int FF_CAN_TRIM;
  14. /**
  15. * Probe and initialize SD/MMC card using given host
  16. *
  17. * @note Only SD cards (SDSC and SDHC/SDXC) are supported now.
  18. * Support for MMC/eMMC cards will be added later.
  19. *
  20. * @param host pointer to structure defining host controller
  21. * @param out_card pointer to structure which will receive information
  22. * about the card when the function completes
  23. * @return
  24. * - ESP_OK on success
  25. * - One of the error codes from SDMMC host controller
  26. */
  27. esp_err_t sdmmc_card_init(const sdmmc_host_t* host,
  28. sdmmc_card_t* out_card);
  29. /**
  30. * @brief Print information about the card to a stream
  31. * @param stream stream obtained using fopen or fdopen
  32. * @param card card information structure initialized using sdmmc_card_init
  33. */
  34. void sdmmc_card_print_info(FILE* stream, const sdmmc_card_t* card);
  35. /**
  36. * Get status of SD/MMC card
  37. *
  38. * @param card pointer to card information structure previously initialized
  39. * using sdmmc_card_init
  40. * @return
  41. * - ESP_OK on success
  42. * - One of the error codes from SDMMC host controller
  43. */
  44. esp_err_t sdmmc_get_status(sdmmc_card_t* card);
  45. /**
  46. * Write given number of sectors to SD/MMC card
  47. *
  48. * @param card pointer to card information structure previously initialized
  49. * using sdmmc_card_init
  50. * @param src pointer to data buffer to read data from; data size must be
  51. * equal to sector_count * card->csd.sector_size
  52. * @param start_sector sector where to start writing
  53. * @param sector_count number of sectors to write
  54. * @return
  55. * - ESP_OK on success
  56. * - One of the error codes from SDMMC host controller
  57. */
  58. esp_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src,
  59. size_t start_sector, size_t sector_count);
  60. /**
  61. * Read given number of sectors from the SD/MMC card
  62. *
  63. * @param card pointer to card information structure previously initialized
  64. * using sdmmc_card_init
  65. * @param dst pointer to data buffer to write into; buffer size must be
  66. * at least sector_count * card->csd.sector_size
  67. * @param start_sector sector where to start reading
  68. * @param sector_count number of sectors to read
  69. * @return
  70. * - ESP_OK on success
  71. * - One of the error codes from SDMMC host controller
  72. */
  73. esp_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst,
  74. size_t start_sector, size_t sector_count);
  75. /**
  76. * Erase given number of sectors from the SD/MMC card
  77. *
  78. * @note When sdmmc_erase_sectors used with cards in SDSPI mode, it was
  79. * observed that card requires re-init after erase operation.
  80. *
  81. * @param card pointer to card information structure previously initialized
  82. * using sdmmc_card_init
  83. * @param start_sector sector where to start erase
  84. * @param sector_count number of sectors to erase
  85. * @param arg erase command (CMD38) argument
  86. * @return
  87. * - ESP_OK on success
  88. * - One of the error codes from SDMMC host controller
  89. */
  90. esp_err_t sdmmc_erase_sectors(sdmmc_card_t* card, size_t start_sector,
  91. size_t sector_count, sdmmc_erase_arg_t arg);
  92. /**
  93. * Check if SD/MMC card supports discard
  94. *
  95. * @param card pointer to card information structure previously initialized
  96. * using sdmmc_card_init
  97. * @return
  98. * - ESP_OK if supported by the card/device
  99. * - ESP_FAIL if not supported by the card/device
  100. */
  101. esp_err_t sdmmc_can_discard(sdmmc_card_t* card);
  102. /**
  103. * Check if SD/MMC card supports trim
  104. *
  105. * @param card pointer to card information structure previously initialized
  106. * using sdmmc_card_init
  107. * @return
  108. * - ESP_OK if supported by the card/device
  109. * - ESP_FAIL if not supported by the card/device
  110. */
  111. esp_err_t sdmmc_can_trim(sdmmc_card_t* card);
  112. /**
  113. * Check if SD/MMC card supports sanitize
  114. *
  115. * @param card pointer to card information structure previously initialized
  116. * using sdmmc_card_init
  117. * @return
  118. * - ESP_OK if supported by the card/device
  119. * - ESP_FAIL if not supported by the card/device
  120. */
  121. esp_err_t sdmmc_mmc_can_sanitize(sdmmc_card_t* card);
  122. /**
  123. * Sanitize the data that was unmapped by a Discard command
  124. *
  125. * @note Discard command has to precede sanitize operation. To discard, use
  126. * MMC_DICARD_ARG with sdmmc_erase_sectors argument
  127. *
  128. * @param card pointer to card information structure previously initialized
  129. * using sdmmc_card_init
  130. * @param timeout_ms timeout value in milliseconds required to sanitize the
  131. * selected range of sectors.
  132. * @return
  133. * - ESP_OK on success
  134. * - One of the error codes from SDMMC host controller
  135. */
  136. esp_err_t sdmmc_mmc_sanitize(sdmmc_card_t* card, uint32_t timeout_ms);
  137. /**
  138. * Erase complete SD/MMC card
  139. *
  140. * @param card pointer to card information structure previously initialized
  141. * using sdmmc_card_init
  142. * @return
  143. * - ESP_OK on success
  144. * - One of the error codes from SDMMC host controller
  145. */
  146. esp_err_t sdmmc_full_erase(sdmmc_card_t* card);
  147. /**
  148. * Read one byte from an SDIO card using IO_RW_DIRECT (CMD52)
  149. *
  150. * @param card pointer to card information structure previously initialized
  151. * using sdmmc_card_init
  152. * @param function IO function number
  153. * @param reg byte address within IO function
  154. * @param[out] out_byte output, receives the value read from the card
  155. * @return
  156. * - ESP_OK on success
  157. * - One of the error codes from SDMMC host controller
  158. */
  159. esp_err_t sdmmc_io_read_byte(sdmmc_card_t* card, uint32_t function,
  160. uint32_t reg, uint8_t *out_byte);
  161. /**
  162. * Write one byte to an SDIO card using IO_RW_DIRECT (CMD52)
  163. *
  164. * @param card pointer to card information structure previously initialized
  165. * using sdmmc_card_init
  166. * @param function IO function number
  167. * @param reg byte address within IO function
  168. * @param in_byte value to be written
  169. * @param[out] out_byte if not NULL, receives new byte value read
  170. * from the card (read-after-write).
  171. * @return
  172. * - ESP_OK on success
  173. * - One of the error codes from SDMMC host controller
  174. */
  175. esp_err_t sdmmc_io_write_byte(sdmmc_card_t* card, uint32_t function,
  176. uint32_t reg, uint8_t in_byte, uint8_t* out_byte);
  177. /**
  178. * Read multiple bytes from an SDIO card using IO_RW_EXTENDED (CMD53)
  179. *
  180. * This function performs read operation using CMD53 in byte mode.
  181. * For block mode, see sdmmc_io_read_blocks.
  182. *
  183. * @param card pointer to card information structure previously initialized
  184. * using sdmmc_card_init
  185. * @param function IO function number
  186. * @param addr byte address within IO function where reading starts
  187. * @param dst buffer which receives the data read from card
  188. * @param size number of bytes to read
  189. * @return
  190. * - ESP_OK on success
  191. * - ESP_ERR_INVALID_SIZE if size exceeds 512 bytes
  192. * - One of the error codes from SDMMC host controller
  193. */
  194. esp_err_t sdmmc_io_read_bytes(sdmmc_card_t* card, uint32_t function,
  195. uint32_t addr, void* dst, size_t size);
  196. /**
  197. * Write multiple bytes to an SDIO card using IO_RW_EXTENDED (CMD53)
  198. *
  199. * This function performs write operation using CMD53 in byte mode.
  200. * For block mode, see sdmmc_io_write_blocks.
  201. *
  202. * @param card pointer to card information structure previously initialized
  203. * using sdmmc_card_init
  204. * @param function IO function number
  205. * @param addr byte address within IO function where writing starts
  206. * @param src data to be written
  207. * @param size number of bytes to write
  208. * @return
  209. * - ESP_OK on success
  210. * - ESP_ERR_INVALID_SIZE if size exceeds 512 bytes
  211. * - One of the error codes from SDMMC host controller
  212. */
  213. esp_err_t sdmmc_io_write_bytes(sdmmc_card_t* card, uint32_t function,
  214. uint32_t addr, const void* src, size_t size);
  215. /**
  216. * Read blocks of data from an SDIO card using IO_RW_EXTENDED (CMD53)
  217. *
  218. * This function performs read operation using CMD53 in block mode.
  219. * For byte mode, see sdmmc_io_read_bytes.
  220. *
  221. * @param card pointer to card information structure previously initialized
  222. * using sdmmc_card_init
  223. * @param function IO function number
  224. * @param addr byte address within IO function where writing starts
  225. * @param dst buffer which receives the data read from card
  226. * @param size number of bytes to read, must be divisible by the card block
  227. * size.
  228. * @return
  229. * - ESP_OK on success
  230. * - ESP_ERR_INVALID_SIZE if size is not divisible by 512 bytes
  231. * - One of the error codes from SDMMC host controller
  232. */
  233. esp_err_t sdmmc_io_read_blocks(sdmmc_card_t* card, uint32_t function,
  234. uint32_t addr, void* dst, size_t size);
  235. /**
  236. * Write blocks of data to an SDIO card using IO_RW_EXTENDED (CMD53)
  237. *
  238. * This function performs write operation using CMD53 in block mode.
  239. * For byte mode, see sdmmc_io_write_bytes.
  240. *
  241. * @param card pointer to card information structure previously initialized
  242. * using sdmmc_card_init
  243. * @param function IO function number
  244. * @param addr byte address within IO function where writing starts
  245. * @param src data to be written
  246. * @param size number of bytes to read, must be divisible by the card block
  247. * size.
  248. * @return
  249. * - ESP_OK on success
  250. * - ESP_ERR_INVALID_SIZE if size is not divisible by 512 bytes
  251. * - One of the error codes from SDMMC host controller
  252. */
  253. esp_err_t sdmmc_io_write_blocks(sdmmc_card_t* card, uint32_t function,
  254. uint32_t addr, const void* src, size_t size);
  255. /**
  256. * Enable SDIO interrupt in the SDMMC host
  257. *
  258. * @param card pointer to card information structure previously initialized
  259. * using sdmmc_card_init
  260. * @return
  261. * - ESP_OK on success
  262. * - ESP_ERR_NOT_SUPPORTED if the host controller does not support
  263. * IO interrupts
  264. */
  265. esp_err_t sdmmc_io_enable_int(sdmmc_card_t* card);
  266. /**
  267. * Block until an SDIO interrupt is received
  268. *
  269. * Slave uses D1 line to signal interrupt condition to the host.
  270. * This function can be used to wait for the interrupt.
  271. *
  272. * @param card pointer to card information structure previously initialized
  273. * using sdmmc_card_init
  274. * @param timeout_ticks time to wait for the interrupt, in RTOS ticks
  275. * @return
  276. * - ESP_OK if the interrupt is received
  277. * - ESP_ERR_NOT_SUPPORTED if the host controller does not support
  278. * IO interrupts
  279. * - ESP_ERR_TIMEOUT if the interrupt does not happen in timeout_ticks
  280. */
  281. esp_err_t sdmmc_io_wait_int(sdmmc_card_t* card, TickType_t timeout_ticks);
  282. /**
  283. * Get the data of CIS region of an SDIO card.
  284. *
  285. * You may provide a buffer not sufficient to store all the CIS data. In this
  286. * case, this function stores as much data into your buffer as possible. Also,
  287. * this function will try to get and return the size required for you.
  288. *
  289. * @param card pointer to card information structure previously initialized
  290. * using sdmmc_card_init
  291. * @param out_buffer Output buffer of the CIS data
  292. * @param buffer_size Size of the buffer.
  293. * @param inout_cis_size Mandatory, pointer to a size, input and output.
  294. * - input: Limitation of maximum searching range, should be 0 or larger than
  295. * buffer_size. The function searches for CIS_CODE_END until this range. Set to
  296. * 0 to search infinitely.
  297. * - output: The size required to store all the CIS data, if CIS_CODE_END is found.
  298. *
  299. * @return
  300. * - ESP_OK: on success
  301. * - ESP_ERR_INVALID_RESPONSE: if the card does not (correctly) support CIS.
  302. * - ESP_ERR_INVALID_SIZE: CIS_CODE_END found, but buffer_size is less than
  303. * required size, which is stored in the inout_cis_size then.
  304. * - ESP_ERR_NOT_FOUND: if the CIS_CODE_END not found. Increase input value of
  305. * inout_cis_size or set it to 0, if you still want to search for the end;
  306. * output value of inout_cis_size is invalid in this case.
  307. * - and other error code return from sdmmc_io_read_bytes
  308. */
  309. 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);
  310. /**
  311. * Parse and print the CIS information of an SDIO card.
  312. *
  313. * @note Not all the CIS codes and all kinds of tuples are supported. If you
  314. * see some unresolved code, you can add the parsing of these code in
  315. * sdmmc_io.c and contribute to the IDF through the Github repository.
  316. *
  317. * using sdmmc_card_init
  318. * @param buffer Buffer to parse
  319. * @param buffer_size Size of the buffer.
  320. * @param fp File pointer to print to, set to NULL to print to stdout.
  321. *
  322. * @return
  323. * - ESP_OK: on success
  324. * - ESP_ERR_NOT_SUPPORTED: if the value from the card is not supported to be parsed.
  325. * - ESP_ERR_INVALID_SIZE: if the CIS size fields are not correct.
  326. */
  327. esp_err_t sdmmc_io_print_cis_info(uint8_t* buffer, size_t buffer_size, FILE* fp);
  328. #ifdef __cplusplus
  329. }
  330. #endif