stm32l1xx_hal_sd.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_sd.h
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief Header file of SD HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32L1xx_HAL_SD_H
  39. #define __STM32L1xx_HAL_SD_H
  40. #if defined(STM32L151xD) || defined(STM32L152xD) || defined(STM32L162xD)
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /* Includes ------------------------------------------------------------------*/
  45. #include "stm32l1xx_ll_sdmmc.h"
  46. /** @addtogroup STM32L1xx_HAL_Driver
  47. * @{
  48. */
  49. /** @addtogroup SD
  50. * @{
  51. */
  52. /* Exported types ------------------------------------------------------------*/
  53. /** @defgroup SD_Exported_Types SD Exported Types
  54. * @{
  55. */
  56. #define SD_InitTypeDef SDIO_InitTypeDef
  57. #define SD_TypeDef SDIO_TypeDef
  58. /**
  59. * @brief SDIO Handle Structure definition
  60. */
  61. typedef struct
  62. {
  63. SD_TypeDef *Instance; /*!< SDIO register base address */
  64. SD_InitTypeDef Init; /*!< SD required parameters */
  65. HAL_LockTypeDef Lock; /*!< SD locking object */
  66. uint32_t CardType; /*!< SD card type */
  67. uint32_t RCA; /*!< SD relative card address */
  68. uint32_t CSD[4]; /*!< SD card specific data table */
  69. uint32_t CID[4]; /*!< SD card identification number table */
  70. __IO uint32_t SdTransferCplt; /*!< SD transfer complete flag in non blocking mode */
  71. __IO uint32_t SdTransferErr; /*!< SD transfer error flag in non blocking mode */
  72. __IO uint32_t DmaTransferCplt; /*!< SD DMA transfer complete flag */
  73. __IO uint32_t SdOperation; /*!< SD transfer operation (read/write) */
  74. DMA_HandleTypeDef *hdmarx; /*!< SD Rx DMA handle parameters */
  75. DMA_HandleTypeDef *hdmatx; /*!< SD Tx DMA handle parameters */
  76. }SD_HandleTypeDef;
  77. /**
  78. * @brief Card Specific Data: CSD Register
  79. */
  80. typedef struct
  81. {
  82. __IO uint8_t CSDStruct; /*!< CSD structure */
  83. __IO uint8_t SysSpecVersion; /*!< System specification version */
  84. __IO uint8_t Reserved1; /*!< Reserved */
  85. __IO uint8_t TAAC; /*!< Data read access time 1 */
  86. __IO uint8_t NSAC; /*!< Data read access time 2 in CLK cycles */
  87. __IO uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */
  88. __IO uint16_t CardComdClasses; /*!< Card command classes */
  89. __IO uint8_t RdBlockLen; /*!< Max. read data block length */
  90. __IO uint8_t PartBlockRead; /*!< Partial blocks for read allowed */
  91. __IO uint8_t WrBlockMisalign; /*!< Write block misalignment */
  92. __IO uint8_t RdBlockMisalign; /*!< Read block misalignment */
  93. __IO uint8_t DSRImpl; /*!< DSR implemented */
  94. __IO uint8_t Reserved2; /*!< Reserved */
  95. __IO uint32_t DeviceSize; /*!< Device Size */
  96. __IO uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */
  97. __IO uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */
  98. __IO uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */
  99. __IO uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */
  100. __IO uint8_t DeviceSizeMul; /*!< Device size multiplier */
  101. __IO uint8_t EraseGrSize; /*!< Erase group size */
  102. __IO uint8_t EraseGrMul; /*!< Erase group size multiplier */
  103. __IO uint8_t WrProtectGrSize; /*!< Write protect group size */
  104. __IO uint8_t WrProtectGrEnable; /*!< Write protect group enable */
  105. __IO uint8_t ManDeflECC; /*!< Manufacturer default ECC */
  106. __IO uint8_t WrSpeedFact; /*!< Write speed factor */
  107. __IO uint8_t MaxWrBlockLen; /*!< Max. write data block length */
  108. __IO uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */
  109. __IO uint8_t Reserved3; /*!< Reserved */
  110. __IO uint8_t ContentProtectAppli; /*!< Content protection application */
  111. __IO uint8_t FileFormatGrouop; /*!< File format group */
  112. __IO uint8_t CopyFlag; /*!< Copy flag (OTP) */
  113. __IO uint8_t PermWrProtect; /*!< Permanent write protection */
  114. __IO uint8_t TempWrProtect; /*!< Temporary write protection */
  115. __IO uint8_t FileFormat; /*!< File format */
  116. __IO uint8_t ECC; /*!< ECC code */
  117. __IO uint8_t CSD_CRC; /*!< CSD CRC */
  118. __IO uint8_t Reserved4; /*!< Always 1 */
  119. }HAL_SD_CSDTypedef;
  120. /**
  121. * @brief Card Identification Data: CID Register
  122. */
  123. typedef struct
  124. {
  125. __IO uint8_t ManufacturerID; /*!< Manufacturer ID */
  126. __IO uint16_t OEM_AppliID; /*!< OEM/Application ID */
  127. __IO uint32_t ProdName1; /*!< Product Name part1 */
  128. __IO uint8_t ProdName2; /*!< Product Name part2 */
  129. __IO uint8_t ProdRev; /*!< Product Revision */
  130. __IO uint32_t ProdSN; /*!< Product Serial Number */
  131. __IO uint8_t Reserved1; /*!< Reserved1 */
  132. __IO uint16_t ManufactDate; /*!< Manufacturing Date */
  133. __IO uint8_t CID_CRC; /*!< CID CRC */
  134. __IO uint8_t Reserved2; /*!< Always 1 */
  135. }HAL_SD_CIDTypedef;
  136. /**
  137. * @brief SD Card Status returned by ACMD13
  138. */
  139. typedef struct
  140. {
  141. __IO uint8_t DAT_BUS_WIDTH; /*!< Shows the currently defined data bus width */
  142. __IO uint8_t SECURED_MODE; /*!< Card is in secured mode of operation */
  143. __IO uint16_t SD_CARD_TYPE; /*!< Carries information about card type */
  144. __IO uint32_t SIZE_OF_PROTECTED_AREA; /*!< Carries information about the capacity of protected area */
  145. __IO uint8_t SPEED_CLASS; /*!< Carries information about the speed class of the card */
  146. __IO uint8_t PERFORMANCE_MOVE; /*!< Carries information about the card's performance move */
  147. __IO uint8_t AU_SIZE; /*!< Carries information about the card's allocation unit size */
  148. __IO uint16_t ERASE_SIZE; /*!< Determines the number of AUs to be erased in one operation */
  149. __IO uint8_t ERASE_TIMEOUT; /*!< Determines the timeout for any number of AU erase */
  150. __IO uint8_t ERASE_OFFSET; /*!< Carries information about the erase offset */
  151. }HAL_SD_CardStatusTypedef;
  152. /**
  153. * @brief SD Card information structure
  154. */
  155. typedef struct
  156. {
  157. HAL_SD_CSDTypedef SD_csd; /*!< SD card specific data register */
  158. HAL_SD_CIDTypedef SD_cid; /*!< SD card identification number register */
  159. uint64_t CardCapacity; /*!< Card capacity */
  160. uint32_t CardBlockSize; /*!< Card block size */
  161. uint16_t RCA; /*!< SD relative card address */
  162. uint8_t CardType; /*!< SD card type */
  163. }HAL_SD_CardInfoTypedef;
  164. /**
  165. * @brief SD Error status enumeration Structure definition
  166. */
  167. typedef enum
  168. {
  169. /**
  170. * @brief SD specific error defines
  171. */
  172. SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */
  173. SD_DATA_CRC_FAIL = (2), /*!< Data block sent/received (CRC check failed) */
  174. SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */
  175. SD_DATA_TIMEOUT = (4), /*!< Data timeout */
  176. SD_TX_UNDERRUN = (5), /*!< Transmit FIFO underrun */
  177. SD_RX_OVERRUN = (6), /*!< Receive FIFO overrun */
  178. SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in wide bus mode */
  179. SD_CMD_OUT_OF_RANGE = (8), /*!< Command's argument was out of range. */
  180. SD_ADDR_MISALIGNED = (9), /*!< Misaligned address */
  181. SD_BLOCK_LEN_ERR = (10), /*!< Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length */
  182. SD_ERASE_SEQ_ERR = (11), /*!< An error in the sequence of erase command occurs. */
  183. SD_BAD_ERASE_PARAM = (12), /*!< An invalid selection for erase groups */
  184. SD_WRITE_PROT_VIOLATION = (13), /*!< Attempt to program a write protect block */
  185. SD_LOCK_UNLOCK_FAILED = (14), /*!< Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card */
  186. SD_COM_CRC_FAILED = (15), /*!< CRC check of the previous command failed */
  187. SD_ILLEGAL_CMD = (16), /*!< Command is not legal for the card state */
  188. SD_CARD_ECC_FAILED = (17), /*!< Card internal ECC was applied but failed to correct the data */
  189. SD_CC_ERROR = (18), /*!< Internal card controller error */
  190. SD_GENERAL_UNKNOWN_ERROR = (19), /*!< General or unknown error */
  191. SD_STREAM_READ_UNDERRUN = (20), /*!< The card could not sustain data transfer in stream read operation. */
  192. SD_STREAM_WRITE_OVERRUN = (21), /*!< The card could not sustain data programming in stream mode */
  193. SD_CID_CSD_OVERWRITE = (22), /*!< CID/CSD overwrite error */
  194. SD_WP_ERASE_SKIP = (23), /*!< Only partial address space was erased */
  195. SD_CARD_ECC_DISABLED = (24), /*!< Command has been executed without using internal ECC */
  196. SD_ERASE_RESET = (25), /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */
  197. SD_AKE_SEQ_ERROR = (26), /*!< Error in sequence of authentication. */
  198. SD_INVALID_VOLTRANGE = (27),
  199. SD_ADDR_OUT_OF_RANGE = (28),
  200. SD_SWITCH_ERROR = (29),
  201. SD_SDIO_DISABLED = (30),
  202. SD_SDIO_FUNCTION_BUSY = (31),
  203. SD_SDIO_FUNCTION_FAILED = (32),
  204. SD_SDIO_UNKNOWN_FUNCTION = (33),
  205. /**
  206. * @brief Standard error defines
  207. */
  208. SD_INTERNAL_ERROR = (34),
  209. SD_NOT_CONFIGURED = (35),
  210. SD_REQUEST_PENDING = (36),
  211. SD_REQUEST_NOT_APPLICABLE = (37),
  212. SD_INVALID_PARAMETER = (38),
  213. SD_UNSUPPORTED_FEATURE = (39),
  214. SD_UNSUPPORTED_HW = (40),
  215. SD_ERROR = (41),
  216. SD_OK = (0)
  217. }HAL_SD_ErrorTypedef;
  218. /**
  219. * @brief SD Transfer state enumeration structure
  220. */
  221. typedef enum
  222. {
  223. SD_TRANSFER_OK = 0, /*!< Transfer success */
  224. SD_TRANSFER_BUSY = 1, /*!< Transfer is occurring */
  225. SD_TRANSFER_ERROR = 2 /*!< Transfer failed */
  226. }HAL_SD_TransferStateTypedef;
  227. /**
  228. * @brief SD Card State enumeration structure
  229. */
  230. typedef enum
  231. {
  232. SD_CARD_READY = ((uint32_t)0x00000001), /*!< Card state is ready */
  233. SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002), /*!< Card is in identification state */
  234. SD_CARD_STANDBY = ((uint32_t)0x00000003), /*!< Card is in standby state */
  235. SD_CARD_TRANSFER = ((uint32_t)0x00000004), /*!< Card is in transfer state */
  236. SD_CARD_SENDING = ((uint32_t)0x00000005), /*!< Card is sending an operation */
  237. SD_CARD_RECEIVING = ((uint32_t)0x00000006), /*!< Card is receiving operation information */
  238. SD_CARD_PROGRAMMING = ((uint32_t)0x00000007), /*!< Card is in programming state */
  239. SD_CARD_DISCONNECTED = ((uint32_t)0x00000008), /*!< Card is disconnected */
  240. SD_CARD_ERROR = ((uint32_t)0x000000FF) /*!< Card is in error state */
  241. }HAL_SD_CardStateTypedef;
  242. /**
  243. * @brief SD Operation enumeration structure
  244. */
  245. typedef enum
  246. {
  247. SD_READ_SINGLE_BLOCK = 0, /*!< Read single block operation */
  248. SD_READ_MULTIPLE_BLOCK = 1, /*!< Read multiple blocks operation */
  249. SD_WRITE_SINGLE_BLOCK = 2, /*!< Write single block operation */
  250. SD_WRITE_MULTIPLE_BLOCK = 3 /*!< Write multiple blocks operation */
  251. }HAL_SD_OperationTypedef;
  252. /**
  253. * @}
  254. */
  255. /* Exported constants --------------------------------------------------------*/
  256. /** @defgroup SD_Exported_Constants SD Exported Constants
  257. * @{
  258. */
  259. /**
  260. * @brief SD Commands Index
  261. */
  262. #define SD_CMD_GO_IDLE_STATE ((uint8_t)0) /*!< Resets the SD memory card. */
  263. #define SD_CMD_SEND_OP_COND ((uint8_t)1) /*!< Sends host capacity support information and activates the card's initialization process. */
  264. #define SD_CMD_ALL_SEND_CID ((uint8_t)2) /*!< Asks any card connected to the host to send the CID numbers on the CMD line. */
  265. #define SD_CMD_SET_REL_ADDR ((uint8_t)3) /*!< Asks the card to publish a new relative address (RCA). */
  266. #define SD_CMD_SET_DSR ((uint8_t)4) /*!< Programs the DSR of all cards. */
  267. #define SD_CMD_SDIO_SEN_OP_COND ((uint8_t)5) /*!< Sends host capacity support information (HCS) and asks the accessed card to send its
  268. operating condition register (OCR) content in the response on the CMD line. */
  269. #define SD_CMD_HS_SWITCH ((uint8_t)6) /*!< Checks switchable function (mode 0) and switch card function (mode 1). */
  270. #define SD_CMD_SEL_DESEL_CARD ((uint8_t)7) /*!< Selects the card by its own relative address and gets deselected by any other address */
  271. #define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8) /*!< Sends SD Memory Card interface condition, which includes host supply voltage information
  272. and asks the card whether card supports voltage. */
  273. #define SD_CMD_SEND_CSD ((uint8_t)9) /*!< Addressed card sends its card specific data (CSD) on the CMD line. */
  274. #define SD_CMD_SEND_CID ((uint8_t)10) /*!< Addressed card sends its card identification (CID) on the CMD line. */
  275. #define SD_CMD_READ_DAT_UNTIL_STOP ((uint8_t)11) /*!< SD card doesn't support it. */
  276. #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12) /*!< Forces the card to stop transmission. */
  277. #define SD_CMD_SEND_STATUS ((uint8_t)13) /*!< Addressed card sends its status register. */
  278. #define SD_CMD_HS_BUSTEST_READ ((uint8_t)14)
  279. #define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15) /*!< Sends an addressed card into the inactive state. */
  280. #define SD_CMD_SET_BLOCKLEN ((uint8_t)16) /*!< Sets the block length (in bytes for SDSC) for all following block commands
  281. (read, write, lock). Default block length is fixed to 512 Bytes. Not effective
  282. for SDHS and SDXC. */
  283. #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17) /*!< Reads single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of
  284. fixed 512 bytes in case of SDHC and SDXC. */
  285. #define SD_CMD_READ_MULT_BLOCK ((uint8_t)18) /*!< Continuously transfers data blocks from card to host until interrupted by
  286. STOP_TRANSMISSION command. */
  287. #define SD_CMD_HS_BUSTEST_WRITE ((uint8_t)19) /*!< 64 bytes tuning pattern is sent for SDR50 and SDR104. */
  288. #define SD_CMD_WRITE_DAT_UNTIL_STOP ((uint8_t)20) /*!< Speed class control command. */
  289. #define SD_CMD_SET_BLOCK_COUNT ((uint8_t)23) /*!< Specify block count for CMD18 and CMD25. */
  290. #define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24) /*!< Writes single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of
  291. fixed 512 bytes in case of SDHC and SDXC. */
  292. #define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25) /*!< Continuously writes blocks of data until a STOP_TRANSMISSION follows. */
  293. #define SD_CMD_PROG_CID ((uint8_t)26) /*!< Reserved for manufacturers. */
  294. #define SD_CMD_PROG_CSD ((uint8_t)27) /*!< Programming of the programmable bits of the CSD. */
  295. #define SD_CMD_SET_WRITE_PROT ((uint8_t)28) /*!< Sets the write protection bit of the addressed group. */
  296. #define SD_CMD_CLR_WRITE_PROT ((uint8_t)29) /*!< Clears the write protection bit of the addressed group. */
  297. #define SD_CMD_SEND_WRITE_PROT ((uint8_t)30) /*!< Asks the card to send the status of the write protection bits. */
  298. #define SD_CMD_SD_ERASE_GRP_START ((uint8_t)32) /*!< Sets the address of the first write block to be erased. (For SD card only). */
  299. #define SD_CMD_SD_ERASE_GRP_END ((uint8_t)33) /*!< Sets the address of the last write block of the continuous range to be erased. */
  300. #define SD_CMD_ERASE_GRP_START ((uint8_t)35) /*!< Sets the address of the first write block to be erased. Reserved for each command
  301. system set by switch function command (CMD6). */
  302. #define SD_CMD_ERASE_GRP_END ((uint8_t)36) /*!< Sets the address of the last write block of the continuous range to be erased.
  303. Reserved for each command system set by switch function command (CMD6). */
  304. #define SD_CMD_ERASE ((uint8_t)38) /*!< Reserved for SD security applications. */
  305. #define SD_CMD_FAST_IO ((uint8_t)39) /*!< SD card doesn't support it (Reserved). */
  306. #define SD_CMD_GO_IRQ_STATE ((uint8_t)40) /*!< SD card doesn't support it (Reserved). */
  307. #define SD_CMD_LOCK_UNLOCK ((uint8_t)42) /*!< Sets/resets the password or lock/unlock the card. The size of the data block is set by
  308. the SET_BLOCK_LEN command. */
  309. #define SD_CMD_APP_CMD ((uint8_t)55) /*!< Indicates to the card that the next command is an application specific command rather
  310. than a standard command. */
  311. #define SD_CMD_GEN_CMD ((uint8_t)56) /*!< Used either to transfer a data block to the card or to get a data block from the card
  312. for general purpose/application specific commands. */
  313. #define SD_CMD_NO_CMD ((uint8_t)64)
  314. /**
  315. * @brief Following commands are SD Card Specific commands.
  316. * SDIO_APP_CMD should be sent before sending these commands.
  317. */
  318. #define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6) /*!< (ACMD6) Defines the data bus width to be used for data transfer. The allowed data bus
  319. widths are given in SCR register. */
  320. #define SD_CMD_SD_APP_STAUS ((uint8_t)13) /*!< (ACMD13) Sends the SD status. */
  321. #define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22) /*!< (ACMD22) Sends the number of the written (without errors) write blocks. Responds with
  322. 32bit+CRC data block. */
  323. #define SD_CMD_SD_APP_OP_COND ((uint8_t)41) /*!< (ACMD41) Sends host capacity support information (HCS) and asks the accessed card to
  324. send its operating condition register (OCR) content in the response on the CMD line. */
  325. #define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /*!< (ACMD42) Connects/Disconnects the 50 KOhm pull-up resistor on CD/DAT3 (pin 1) of the card. */
  326. #define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51) /*!< Reads the SD Configuration Register (SCR). */
  327. #define SD_CMD_SDIO_RW_DIRECT ((uint8_t)52) /*!< For SD I/O card only, reserved for security specification. */
  328. #define SD_CMD_SDIO_RW_EXTENDED ((uint8_t)53) /*!< For SD I/O card only, reserved for security specification. */
  329. /**
  330. * @brief Following commands are SD Card Specific security commands.
  331. * SD_CMD_APP_CMD should be sent before sending these commands.
  332. */
  333. #define SD_CMD_SD_APP_GET_MKB ((uint8_t)43) /*!< For SD card only */
  334. #define SD_CMD_SD_APP_GET_MID ((uint8_t)44) /*!< For SD card only */
  335. #define SD_CMD_SD_APP_SET_CER_RN1 ((uint8_t)45) /*!< For SD card only */
  336. #define SD_CMD_SD_APP_GET_CER_RN2 ((uint8_t)46) /*!< For SD card only */
  337. #define SD_CMD_SD_APP_SET_CER_RES2 ((uint8_t)47) /*!< For SD card only */
  338. #define SD_CMD_SD_APP_GET_CER_RES1 ((uint8_t)48) /*!< For SD card only */
  339. #define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18) /*!< For SD card only */
  340. #define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /*!< For SD card only */
  341. #define SD_CMD_SD_APP_SECURE_ERASE ((uint8_t)38) /*!< For SD card only */
  342. #define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49) /*!< For SD card only */
  343. #define SD_CMD_SD_APP_SECURE_WRITE_MKB ((uint8_t)48) /*!< For SD card only */
  344. /**
  345. * @brief Supported SD Memory Cards
  346. */
  347. #define STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000)
  348. #define STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001)
  349. #define HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002)
  350. #define MULTIMEDIA_CARD ((uint32_t)0x00000003)
  351. #define SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004)
  352. #define HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005)
  353. #define SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006)
  354. #define HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007)
  355. /**
  356. * @}
  357. */
  358. /* Exported macro ------------------------------------------------------------*/
  359. /** @defgroup SD_Exported_macros SD Exported Macros
  360. * @brief macros to handle interrupts and specific clock configurations
  361. * @{
  362. */
  363. /**
  364. * @brief Enable the SD device.
  365. * @retval None
  366. */
  367. #define __HAL_SD_SDIO_ENABLE() __SDIO_ENABLE()
  368. /**
  369. * @brief Disable the SD device.
  370. * @retval None
  371. */
  372. #define __HAL_SD_SDIO_DISABLE() __SDIO_DISABLE()
  373. /**
  374. * @brief Enable the SDIO DMA transfer.
  375. * @retval None
  376. */
  377. #define __HAL_SD_SDIO_DMA_ENABLE() __SDIO_DMA_ENABLE()
  378. /**
  379. * @brief Disable the SDIO DMA transfer.
  380. * @retval None
  381. */
  382. #define __HAL_SD_SDIO_DMA_DISABLE() __SDIO_DMA_DISABLE()
  383. /**
  384. * @brief Enable the SD device interrupt.
  385. * @param __HANDLE__: SD Handle
  386. * @param __INTERRUPT__: specifies the SDIO interrupt sources to be enabled.
  387. * This parameter can be one or a combination of the following values:
  388. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  389. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  390. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  391. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  392. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  393. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  394. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  395. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  396. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  397. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  398. * bus mode interrupt
  399. * @arg SDIO_IT_DBCKEND: Data block sent/received (CRC check passed) interrupt
  400. * @arg SDIO_IT_CMDACT: Command transfer in progress interrupt
  401. * @arg SDIO_IT_TXACT: Data transmit in progress interrupt
  402. * @arg SDIO_IT_RXACT: Data receive in progress interrupt
  403. * @arg SDIO_IT_TXFIFOHE: Transmit FIFO Half Empty interrupt
  404. * @arg SDIO_IT_RXFIFOHF: Receive FIFO Half Full interrupt
  405. * @arg SDIO_IT_TXFIFOF: Transmit FIFO full interrupt
  406. * @arg SDIO_IT_RXFIFOF: Receive FIFO full interrupt
  407. * @arg SDIO_IT_TXFIFOE: Transmit FIFO empty interrupt
  408. * @arg SDIO_IT_RXFIFOE: Receive FIFO empty interrupt
  409. * @arg SDIO_IT_TXDAVL: Data available in transmit FIFO interrupt
  410. * @arg SDIO_IT_RXDAVL: Data available in receive FIFO interrupt
  411. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  412. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61 interrupt
  413. * @retval None
  414. */
  415. #define __HAL_SD_SDIO_ENABLE_IT(__HANDLE__, __INTERRUPT__) __SDIO_ENABLE_IT((__HANDLE__)->Instance, (__INTERRUPT__))
  416. /**
  417. * @brief Disable the SD device interrupt.
  418. * @param __HANDLE__: SD Handle
  419. * @param __INTERRUPT__: specifies the SDIO interrupt sources to be disabled.
  420. * This parameter can be one or a combination of the following values:
  421. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  422. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  423. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  424. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  425. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  426. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  427. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  428. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  429. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  430. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  431. * bus mode interrupt
  432. * @arg SDIO_IT_DBCKEND: Data block sent/received (CRC check passed) interrupt
  433. * @arg SDIO_IT_CMDACT: Command transfer in progress interrupt
  434. * @arg SDIO_IT_TXACT: Data transmit in progress interrupt
  435. * @arg SDIO_IT_RXACT: Data receive in progress interrupt
  436. * @arg SDIO_IT_TXFIFOHE: Transmit FIFO Half Empty interrupt
  437. * @arg SDIO_IT_RXFIFOHF: Receive FIFO Half Full interrupt
  438. * @arg SDIO_IT_TXFIFOF: Transmit FIFO full interrupt
  439. * @arg SDIO_IT_RXFIFOF: Receive FIFO full interrupt
  440. * @arg SDIO_IT_TXFIFOE: Transmit FIFO empty interrupt
  441. * @arg SDIO_IT_RXFIFOE: Receive FIFO empty interrupt
  442. * @arg SDIO_IT_TXDAVL: Data available in transmit FIFO interrupt
  443. * @arg SDIO_IT_RXDAVL: Data available in receive FIFO interrupt
  444. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  445. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61 interrupt
  446. * @retval None
  447. */
  448. #define __HAL_SD_SDIO_DISABLE_IT(__HANDLE__, __INTERRUPT__) __SDIO_DISABLE_IT((__HANDLE__)->Instance, (__INTERRUPT__))
  449. /**
  450. * @brief Check whether the specified SD flag is set or not.
  451. * @param __HANDLE__: SD Handle
  452. * @param __FLAG__: specifies the flag to check.
  453. * This parameter can be one of the following values:
  454. * @arg SDIO_FLAG_CCRCFAIL: Command response received (CRC check failed)
  455. * @arg SDIO_FLAG_DCRCFAIL: Data block sent/received (CRC check failed)
  456. * @arg SDIO_FLAG_CTIMEOUT: Command response timeout
  457. * @arg SDIO_FLAG_DTIMEOUT: Data timeout
  458. * @arg SDIO_FLAG_TXUNDERR: Transmit FIFO underrun error
  459. * @arg SDIO_FLAG_RXOVERR: Received FIFO overrun error
  460. * @arg SDIO_FLAG_CMDREND: Command response received (CRC check passed)
  461. * @arg SDIO_FLAG_CMDSENT: Command sent (no response required)
  462. * @arg SDIO_FLAG_DATAEND: Data end (data counter, SDIDCOUNT, is zero)
  463. * @arg SDIO_FLAG_STBITERR: Start bit not detected on all data signals in wide bus mode.
  464. * @arg SDIO_FLAG_DBCKEND: Data block sent/received (CRC check passed)
  465. * @arg SDIO_FLAG_CMDACT: Command transfer in progress
  466. * @arg SDIO_FLAG_TXACT: Data transmit in progress
  467. * @arg SDIO_FLAG_RXACT: Data receive in progress
  468. * @arg SDIO_FLAG_TXFIFOHE: Transmit FIFO Half Empty
  469. * @arg SDIO_FLAG_RXFIFOHF: Receive FIFO Half Full
  470. * @arg SDIO_FLAG_TXFIFOF: Transmit FIFO full
  471. * @arg SDIO_FLAG_RXFIFOF: Receive FIFO full
  472. * @arg SDIO_FLAG_TXFIFOE: Transmit FIFO empty
  473. * @arg SDIO_FLAG_RXFIFOE: Receive FIFO empty
  474. * @arg SDIO_FLAG_TXDAVL: Data available in transmit FIFO
  475. * @arg SDIO_FLAG_RXDAVL: Data available in receive FIFO
  476. * @arg SDIO_FLAG_SDIOIT: SD I/O interrupt received
  477. * @arg SDIO_FLAG_CEATAEND: CE-ATA command completion signal received for CMD61
  478. * @retval The new state of SD FLAG (SET or RESET).
  479. */
  480. #define __HAL_SD_SDIO_GET_FLAG(__HANDLE__, __FLAG__) __SDIO_GET_FLAG((__HANDLE__)->Instance, (__FLAG__))
  481. /**
  482. * @brief Clear the SD's pending flags.
  483. * @param __HANDLE__: SD Handle
  484. * @param __FLAG__: specifies the flag to clear.
  485. * This parameter can be one or a combination of the following values:
  486. * @arg SDIO_FLAG_CCRCFAIL: Command response received (CRC check failed)
  487. * @arg SDIO_FLAG_DCRCFAIL: Data block sent/received (CRC check failed)
  488. * @arg SDIO_FLAG_CTIMEOUT: Command response timeout
  489. * @arg SDIO_FLAG_DTIMEOUT: Data timeout
  490. * @arg SDIO_FLAG_TXUNDERR: Transmit FIFO underrun error
  491. * @arg SDIO_FLAG_RXOVERR: Received FIFO overrun error
  492. * @arg SDIO_FLAG_CMDREND: Command response received (CRC check passed)
  493. * @arg SDIO_FLAG_CMDSENT: Command sent (no response required)
  494. * @arg SDIO_FLAG_DATAEND: Data end (data counter, SDIDCOUNT, is zero)
  495. * @arg SDIO_FLAG_STBITERR: Start bit not detected on all data signals in wide bus mode
  496. * @arg SDIO_FLAG_DBCKEND: Data block sent/received (CRC check passed)
  497. * @arg SDIO_FLAG_SDIOIT: SD I/O interrupt received
  498. * @arg SDIO_FLAG_CEATAEND: CE-ATA command completion signal received for CMD61
  499. * @retval None
  500. */
  501. #define __HAL_SD_SDIO_CLEAR_FLAG(__HANDLE__, __FLAG__) __SDIO_CLEAR_FLAG((__HANDLE__)->Instance, (__FLAG__))
  502. /**
  503. * @brief Check whether the specified SD interrupt has occurred or not.
  504. * @param __HANDLE__: SD Handle
  505. * @param __INTERRUPT__: specifies the SDIO interrupt source to check.
  506. * This parameter can be one of the following values:
  507. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  508. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  509. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  510. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  511. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  512. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  513. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  514. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  515. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  516. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  517. * bus mode interrupt
  518. * @arg SDIO_IT_DBCKEND: Data block sent/received (CRC check passed) interrupt
  519. * @arg SDIO_IT_CMDACT: Command transfer in progress interrupt
  520. * @arg SDIO_IT_TXACT: Data transmit in progress interrupt
  521. * @arg SDIO_IT_RXACT: Data receive in progress interrupt
  522. * @arg SDIO_IT_TXFIFOHE: Transmit FIFO Half Empty interrupt
  523. * @arg SDIO_IT_RXFIFOHF: Receive FIFO Half Full interrupt
  524. * @arg SDIO_IT_TXFIFOF: Transmit FIFO full interrupt
  525. * @arg SDIO_IT_RXFIFOF: Receive FIFO full interrupt
  526. * @arg SDIO_IT_TXFIFOE: Transmit FIFO empty interrupt
  527. * @arg SDIO_IT_RXFIFOE: Receive FIFO empty interrupt
  528. * @arg SDIO_IT_TXDAVL: Data available in transmit FIFO interrupt
  529. * @arg SDIO_IT_RXDAVL: Data available in receive FIFO interrupt
  530. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  531. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61 interrupt
  532. * @retval The new state of SD IT (SET or RESET).
  533. */
  534. #define __HAL_SD_SDIO_GET_IT (__HANDLE__, __INTERRUPT__) __SDIO_GET_IT ((__HANDLE__)->Instance, __INTERRUPT__)
  535. /**
  536. * @brief Clear the SD's interrupt pending bits.
  537. * @param __HANDLE__ : SD Handle
  538. * @param __INTERRUPT__: specifies the interrupt pending bit to clear.
  539. * This parameter can be one or a combination of the following values:
  540. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  541. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  542. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  543. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  544. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  545. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  546. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  547. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  548. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIO_DCOUNT, is zero) interrupt
  549. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  550. * bus mode interrupt
  551. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  552. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61
  553. * @retval None
  554. */
  555. #define __HAL_SD_SDIO_CLEAR_IT(__HANDLE__, __INTERRUPT__) __SDIO_CLEAR_IT((__HANDLE__)->Instance, (__INTERRUPT__))
  556. /**
  557. * @}
  558. */
  559. /* Exported functions --------------------------------------------------------*/
  560. /** @addtogroup SD_Exported_Functions
  561. * @{
  562. */
  563. /* Initialization and de-initialization functions **********************************/
  564. /** @addtogroup SD_Exported_Functions_Group1
  565. * @{
  566. */
  567. HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo);
  568. HAL_StatusTypeDef HAL_SD_DeInit (SD_HandleTypeDef *hsd);
  569. void HAL_SD_MspInit(SD_HandleTypeDef *hsd);
  570. void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd);
  571. /**
  572. * @}
  573. */
  574. /* I/O operation functions *****************************************************/
  575. /** @addtogroup SD_Exported_Functions_Group2
  576. * @{
  577. */
  578. /* Blocking mode: Polling */
  579. HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks);
  580. HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks);
  581. HAL_SD_ErrorTypedef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint64_t startaddr, uint64_t endaddr);
  582. /* Non-Blocking mode: Interrupt */
  583. void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd);
  584. /* Callback in non blocking modes (DMA) */
  585. void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef *hdma);
  586. void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef *hdma);
  587. void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma);
  588. void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef *hdma);
  589. void HAL_SD_XferCpltCallback(SD_HandleTypeDef *hsd);
  590. void HAL_SD_XferErrorCallback(SD_HandleTypeDef *hsd);
  591. /* Non-Blocking mode: DMA */
  592. HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks);
  593. HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks);
  594. HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation(SD_HandleTypeDef *hsd, uint32_t Timeout);
  595. HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation(SD_HandleTypeDef *hsd, uint32_t Timeout);
  596. /**
  597. * @}
  598. */
  599. /* Peripheral Control functions ************************************************/
  600. /** @addtogroup SD_Exported_Functions_Group3
  601. * @{
  602. */
  603. HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo);
  604. HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config(SD_HandleTypeDef *hsd, uint32_t WideMode);
  605. HAL_SD_ErrorTypedef HAL_SD_StopTransfer(SD_HandleTypeDef *hsd);
  606. HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd);
  607. /**
  608. * @}
  609. */
  610. /* Peripheral State functions **************************************************/
  611. /** @addtogroup SD_Exported_Functions_Group4
  612. * @{
  613. */
  614. HAL_SD_ErrorTypedef HAL_SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
  615. HAL_SD_ErrorTypedef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus);
  616. HAL_SD_TransferStateTypedef HAL_SD_GetStatus(SD_HandleTypeDef *hsd);
  617. /**
  618. * @}
  619. */
  620. /**
  621. * @}
  622. */
  623. /**
  624. * @}
  625. */
  626. /**
  627. * @}
  628. */
  629. #ifdef __cplusplus
  630. }
  631. #endif
  632. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  633. #endif /* __STM32L1xx_HAL_SD_H */
  634. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/