sdcard.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. ******************************************************************************
  3. * @file stm324x9i_eval_sdio_sd.h
  4. * @author MCD Application Team
  5. * @version V1.0.3
  6. * @date 13-November-2013
  7. * @brief This file contains all the functions prototypes for the SD Card
  8. * stm324x9i_eval_sdio_sd driver firmware library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  13. *
  14. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15. * You may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at:
  17. *
  18. * http://www.st.com/software_license_agreement_liberty_v2
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. ******************************************************************************
  27. */
  28. /* Define to prevent recursive inclusion -------------------------------------*/
  29. #ifndef __STM324x9I_EVAL_SDIO_SD_H
  30. #define __STM324x9I_EVAL_SDIO_SD_H
  31. #ifdef __cplusplus
  32. extern "C"
  33. {
  34. #endif
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "stm32f4xx.h"
  37. /** @addtogroup Utilities
  38. * @{
  39. */
  40. /** @addtogroup STM32_EVAL
  41. * @{
  42. */
  43. /** @addtogroup STM324x9I_EVAL
  44. * @{
  45. */
  46. /** @addtogroup STM324x9I_EVAL_SDIO_SD
  47. * @{
  48. */
  49. /** @defgroup STM324x9I_EVAL_SDIO_SD_Exported_Types
  50. * @{
  51. */
  52. typedef enum
  53. {
  54. /**
  55. * @brief SDIO specific error defines
  56. */
  57. SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */
  58. SD_DATA_CRC_FAIL = (2), /*!< Data bock sent/received (CRC check Failed) */
  59. SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */
  60. SD_DATA_TIMEOUT = (4), /*!< Data time out */
  61. SD_TX_UNDERRUN = (5), /*!< Transmit FIFO under-run */
  62. SD_RX_OVERRUN = (6), /*!< Receive FIFO over-run */
  63. SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in widE bus mode */
  64. SD_CMD_OUT_OF_RANGE = (8), /*!< CMD's argument was out of range.*/
  65. SD_ADDR_MISALIGNED = (9), /*!< Misaligned address */
  66. 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 */
  67. SD_ERASE_SEQ_ERR = (11), /*!< An error in the sequence of erase command occurs.*/
  68. SD_BAD_ERASE_PARAM = (12), /*!< An Invalid selection for erase groups */
  69. SD_WRITE_PROT_VIOLATION = (13), /*!< Attempt to program a write protect block */
  70. 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 */
  71. SD_COM_CRC_FAILED = (15), /*!< CRC check of the previous command failed */
  72. SD_ILLEGAL_CMD = (16), /*!< Command is not legal for the card state */
  73. SD_CARD_ECC_FAILED = (17), /*!< Card internal ECC was applied but failed to correct the data */
  74. SD_CC_ERROR = (18), /*!< Internal card controller error */
  75. SD_GENERAL_UNKNOWN_ERROR = (19), /*!< General or Unknown error */
  76. SD_STREAM_READ_UNDERRUN = (20), /*!< The card could not sustain data transfer in stream read operation. */
  77. SD_STREAM_WRITE_OVERRUN = (21), /*!< The card could not sustain data programming in stream mode */
  78. SD_CID_CSD_OVERWRITE = (22), /*!< CID/CSD overwrite error */
  79. SD_WP_ERASE_SKIP = (23), /*!< only partial address space was erased */
  80. SD_CARD_ECC_DISABLED = (24), /*!< Command has been executed without using internal ECC */
  81. SD_ERASE_RESET = (25), /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */
  82. SD_AKE_SEQ_ERROR = (26), /*!< Error in sequence of authentication. */
  83. SD_INVALID_VOLTRANGE = (27),
  84. SD_ADDR_OUT_OF_RANGE = (28),
  85. SD_SWITCH_ERROR = (29),
  86. SD_SDIO_DISABLED = (30),
  87. SD_SDIO_FUNCTION_BUSY = (31),
  88. SD_SDIO_FUNCTION_FAILED = (32),
  89. SD_SDIO_UNKNOWN_FUNCTION = (33),
  90. /**
  91. * @brief Standard error defines
  92. */
  93. SD_INTERNAL_ERROR,
  94. SD_NOT_CONFIGURED,
  95. SD_REQUEST_PENDING,
  96. SD_REQUEST_NOT_APPLICABLE,
  97. SD_INVALID_PARAMETER,
  98. SD_UNSUPPORTED_FEATURE,
  99. SD_UNSUPPORTED_HW,
  100. SD_ERROR,
  101. SD_OK = 0
  102. } SD_Error;
  103. /**
  104. * @brief SDIO Transfer state
  105. */
  106. typedef enum
  107. {
  108. SD_TRANSFER_OK = 0,
  109. SD_TRANSFER_BUSY = 1,
  110. SD_TRANSFER_ERROR
  111. } SDTransferState;
  112. /**
  113. * @brief SD Card States
  114. */
  115. typedef enum
  116. {
  117. SD_CARD_READY = ((uint32_t)0x00000001),
  118. SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002),
  119. SD_CARD_STANDBY = ((uint32_t)0x00000003),
  120. SD_CARD_TRANSFER = ((uint32_t)0x00000004),
  121. SD_CARD_SENDING = ((uint32_t)0x00000005),
  122. SD_CARD_RECEIVING = ((uint32_t)0x00000006),
  123. SD_CARD_PROGRAMMING = ((uint32_t)0x00000007),
  124. SD_CARD_DISCONNECTED = ((uint32_t)0x00000008),
  125. SD_CARD_ERROR = ((uint32_t)0x000000FF)
  126. } SDCardState;
  127. /**
  128. * @brief Card Specific Data: CSD Register
  129. */
  130. typedef struct
  131. {
  132. __IO uint8_t CSDStruct; /*!< CSD structure */
  133. __IO uint8_t SysSpecVersion; /*!< System specification version */
  134. __IO uint8_t Reserved1; /*!< Reserved */
  135. __IO uint8_t TAAC; /*!< Data read access-time 1 */
  136. __IO uint8_t NSAC; /*!< Data read access-time 2 in CLK cycles */
  137. __IO uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */
  138. __IO uint16_t CardComdClasses; /*!< Card command classes */
  139. __IO uint8_t RdBlockLen; /*!< Max. read data block length */
  140. __IO uint8_t PartBlockRead; /*!< Partial blocks for read allowed */
  141. __IO uint8_t WrBlockMisalign; /*!< Write block misalignment */
  142. __IO uint8_t RdBlockMisalign; /*!< Read block misalignment */
  143. __IO uint8_t DSRImpl; /*!< DSR implemented */
  144. __IO uint8_t Reserved2; /*!< Reserved */
  145. __IO uint32_t DeviceSize; /*!< Device Size */
  146. __IO uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */
  147. __IO uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */
  148. __IO uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */
  149. __IO uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */
  150. __IO uint8_t DeviceSizeMul; /*!< Device size multiplier */
  151. __IO uint8_t EraseGrSize; /*!< Erase group size */
  152. __IO uint8_t EraseGrMul; /*!< Erase group size multiplier */
  153. __IO uint8_t WrProtectGrSize; /*!< Write protect group size */
  154. __IO uint8_t WrProtectGrEnable; /*!< Write protect group enable */
  155. __IO uint8_t ManDeflECC; /*!< Manufacturer default ECC */
  156. __IO uint8_t WrSpeedFact; /*!< Write speed factor */
  157. __IO uint8_t MaxWrBlockLen; /*!< Max. write data block length */
  158. __IO uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */
  159. __IO uint8_t Reserved3; /*!< Reserded */
  160. __IO uint8_t ContentProtectAppli; /*!< Content protection application */
  161. __IO uint8_t FileFormatGrouop; /*!< File format group */
  162. __IO uint8_t CopyFlag; /*!< Copy flag (OTP) */
  163. __IO uint8_t PermWrProtect; /*!< Permanent write protection */
  164. __IO uint8_t TempWrProtect; /*!< Temporary write protection */
  165. __IO uint8_t FileFormat; /*!< File Format */
  166. __IO uint8_t ECC; /*!< ECC code */
  167. __IO uint8_t CSD_CRC; /*!< CSD CRC */
  168. __IO uint8_t Reserved4; /*!< always 1*/
  169. } SD_CSD;
  170. /**
  171. * @brief Card Identification Data: CID Register
  172. */
  173. typedef struct
  174. {
  175. __IO uint8_t ManufacturerID; /*!< ManufacturerID */
  176. __IO uint16_t OEM_AppliID; /*!< OEM/Application ID */
  177. __IO uint32_t ProdName1; /*!< Product Name part1 */
  178. __IO uint8_t ProdName2; /*!< Product Name part2*/
  179. __IO uint8_t ProdRev; /*!< Product Revision */
  180. __IO uint32_t ProdSN; /*!< Product Serial Number */
  181. __IO uint8_t Reserved1; /*!< Reserved1 */
  182. __IO uint16_t ManufactDate; /*!< Manufacturing Date */
  183. __IO uint8_t CID_CRC; /*!< CID CRC */
  184. __IO uint8_t Reserved2; /*!< always 1 */
  185. } SD_CID;
  186. /**
  187. * @brief SD Card Status
  188. */
  189. typedef struct
  190. {
  191. __IO uint8_t DAT_BUS_WIDTH;
  192. __IO uint8_t SECURED_MODE;
  193. __IO uint16_t SD_CARD_TYPE;
  194. __IO uint32_t SIZE_OF_PROTECTED_AREA;
  195. __IO uint8_t SPEED_CLASS;
  196. __IO uint8_t PERFORMANCE_MOVE;
  197. __IO uint8_t AU_SIZE;
  198. __IO uint16_t ERASE_SIZE;
  199. __IO uint8_t ERASE_TIMEOUT;
  200. __IO uint8_t ERASE_OFFSET;
  201. } SD_CardStatus;
  202. /**
  203. * @brief SD Card information
  204. */
  205. typedef struct
  206. {
  207. SD_CSD SD_csd;
  208. SD_CID SD_cid;
  209. uint64_t CardCapacity; /*!< Card Capacity */
  210. uint32_t CardBlockSize; /*!< Card Block Size */
  211. uint16_t RCA;
  212. uint8_t CardType;
  213. } SD_CardInfo;
  214. /**
  215. * @}
  216. */
  217. /** @defgroup STM324x9I_EVAL_SDIO_SD_Exported_Constants
  218. * @{
  219. */
  220. /**
  221. * @brief SDIO Commands Index
  222. */
  223. #define SD_CMD_GO_IDLE_STATE ((uint8_t)0)
  224. #define SD_CMD_SEND_OP_COND ((uint8_t)1)
  225. #define SD_CMD_ALL_SEND_CID ((uint8_t)2)
  226. #define SD_CMD_SET_REL_ADDR ((uint8_t)3) /*!< SDIO_SEND_REL_ADDR for SD Card */
  227. #define SD_CMD_SET_DSR ((uint8_t)4)
  228. #define SD_CMD_SDIO_SEN_OP_COND ((uint8_t)5)
  229. #define SD_CMD_HS_SWITCH ((uint8_t)6)
  230. #define SD_CMD_SEL_DESEL_CARD ((uint8_t)7)
  231. #define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8)
  232. #define SD_CMD_SEND_CSD ((uint8_t)9)
  233. #define SD_CMD_SEND_CID ((uint8_t)10)
  234. #define SD_CMD_READ_DAT_UNTIL_STOP ((uint8_t)11) /*!< SD Card doesn't support it */
  235. #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12)
  236. #define SD_CMD_SEND_STATUS ((uint8_t)13)
  237. #define SD_CMD_HS_BUSTEST_READ ((uint8_t)14)
  238. #define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15)
  239. #define SD_CMD_SET_BLOCKLEN ((uint8_t)16)
  240. #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17)
  241. #define SD_CMD_READ_MULT_BLOCK ((uint8_t)18)
  242. #define SD_CMD_HS_BUSTEST_WRITE ((uint8_t)19)
  243. #define SD_CMD_WRITE_DAT_UNTIL_STOP ((uint8_t)20) /*!< SD Card doesn't support it */
  244. #define SD_CMD_SET_BLOCK_COUNT ((uint8_t)23) /*!< SD Card doesn't support it */
  245. #define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24)
  246. #define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25)
  247. #define SD_CMD_PROG_CID ((uint8_t)26) /*!< reserved for manufacturers */
  248. #define SD_CMD_PROG_CSD ((uint8_t)27)
  249. #define SD_CMD_SET_WRITE_PROT ((uint8_t)28)
  250. #define SD_CMD_CLR_WRITE_PROT ((uint8_t)29)
  251. #define SD_CMD_SEND_WRITE_PROT ((uint8_t)30)
  252. #define SD_CMD_SD_ERASE_GRP_START ((uint8_t)32) /*!< To set the address of the first write \
  253. block to be erased. (For SD card only) */
  254. #define SD_CMD_SD_ERASE_GRP_END ((uint8_t)33) /*!< To set the address of the last write block of the \
  255. continuous range to be erased. (For SD card only) */
  256. #define SD_CMD_ERASE_GRP_START ((uint8_t)35) /*!< To set the address of the first write block to be erased. \
  257. (For MMC card only spec 3.31) */
  258. #define SD_CMD_ERASE_GRP_END ((uint8_t)36) /*!< To set the address of the last write block of the \
  259. continuous range to be erased. (For MMC card only spec 3.31) */
  260. #define SD_CMD_ERASE ((uint8_t)38)
  261. #define SD_CMD_FAST_IO ((uint8_t)39) /*!< SD Card doesn't support it */
  262. #define SD_CMD_GO_IRQ_STATE ((uint8_t)40) /*!< SD Card doesn't support it */
  263. #define SD_CMD_LOCK_UNLOCK ((uint8_t)42)
  264. #define SD_CMD_APP_CMD ((uint8_t)55)
  265. #define SD_CMD_GEN_CMD ((uint8_t)56)
  266. #define SD_CMD_NO_CMD ((uint8_t)64)
  267. /**
  268. * @brief Following commands are SD Card Specific commands.
  269. * SDIO_APP_CMD should be sent before sending these commands.
  270. */
  271. #define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6) /*!< For SD Card only */
  272. #define SD_CMD_SD_APP_STAUS ((uint8_t)13) /*!< For SD Card only */
  273. #define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22) /*!< For SD Card only */
  274. #define SD_CMD_SD_APP_OP_COND ((uint8_t)41) /*!< For SD Card only */
  275. #define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /*!< For SD Card only */
  276. #define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51) /*!< For SD Card only */
  277. #define SD_CMD_SDIO_RW_DIRECT ((uint8_t)52) /*!< For SD I/O Card only */
  278. #define SD_CMD_SDIO_RW_EXTENDED ((uint8_t)53) /*!< For SD I/O Card only */
  279. /**
  280. * @brief Following commands are SD Card Specific security commands.
  281. * SDIO_APP_CMD should be sent before sending these commands.
  282. */
  283. #define SD_CMD_SD_APP_GET_MKB ((uint8_t)43) /*!< For SD Card only */
  284. #define SD_CMD_SD_APP_GET_MID ((uint8_t)44) /*!< For SD Card only */
  285. #define SD_CMD_SD_APP_SET_CER_RN1 ((uint8_t)45) /*!< For SD Card only */
  286. #define SD_CMD_SD_APP_GET_CER_RN2 ((uint8_t)46) /*!< For SD Card only */
  287. #define SD_CMD_SD_APP_SET_CER_RES2 ((uint8_t)47) /*!< For SD Card only */
  288. #define SD_CMD_SD_APP_GET_CER_RES1 ((uint8_t)48) /*!< For SD Card only */
  289. #define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18) /*!< For SD Card only */
  290. #define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /*!< For SD Card only */
  291. #define SD_CMD_SD_APP_SECURE_ERASE ((uint8_t)38) /*!< For SD Card only */
  292. #define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49) /*!< For SD Card only */
  293. #define SD_CMD_SD_APP_SECURE_WRITE_MKB ((uint8_t)48) /*!< For SD Card only */
  294. /* Uncomment the following line to select the SDIO Data transfer mode */
  295. #if !defined(SD_DMA_MODE) && !defined(SD_POLLING_MODE)
  296. #define SD_DMA_MODE ((uint32_t)0x00000000)
  297. /*#define SD_POLLING_MODE ((uint32_t)0x00000002)*/
  298. #endif
  299. /**
  300. * @brief SD detection on its memory slot
  301. */
  302. #define SD_PRESENT ((uint8_t)0x01)
  303. #define SD_NOT_PRESENT ((uint8_t)0x00)
  304. /**
  305. * @brief Supported SD Memory Cards
  306. */
  307. #define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000)
  308. #define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001)
  309. #define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002)
  310. #define SDIO_MULTIMEDIA_CARD ((uint32_t)0x00000003)
  311. #define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004)
  312. #define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005)
  313. #define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006)
  314. #define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007)
  315. /**
  316. * @}
  317. */
  318. /** @defgroup STM324x9I_EVAL_SDIO_SD_Exported_Macros
  319. * @{
  320. */
  321. /**
  322. * @}
  323. */
  324. /** @defgroup STM324x9I_EVAL_SDIO_SD_Exported_Functions
  325. * @{
  326. */
  327. void SD_DeInit(void);
  328. SD_Error SD_Init(void);
  329. SDTransferState SD_GetStatus(void);
  330. SDCardState SD_GetState(void);
  331. uint8_t SD_Detect(void);
  332. SD_Error SD_PowerON(void);
  333. SD_Error SD_PowerOFF(void);
  334. SD_Error SD_InitializeCards(void);
  335. SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo);
  336. SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus);
  337. SD_Error SD_EnableWideBusOperation(uint32_t WideMode);
  338. SD_Error SD_SelectDeselect(uint64_t addr);
  339. SD_Error SD_ReadBlock(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize);
  340. SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
  341. SD_Error SD_WriteBlock(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize);
  342. SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
  343. SDTransferState SD_GetTransferState(void);
  344. SD_Error SD_StopTransfer(void);
  345. SD_Error SD_Erase(uint64_t startaddr, uint64_t endaddr);
  346. SD_Error SD_SendStatus(uint32_t *pcardstatus);
  347. SD_Error SD_SendSDStatus(uint32_t *psdstatus);
  348. SD_Error SD_ProcessIRQSrc(void);
  349. void SD_ProcessDMAIRQ(void);
  350. SD_Error SD_WaitReadOperation(void);
  351. SD_Error SD_WaitWriteOperation(void);
  352. SD_Error SD_HighSpeed(void);
  353. #ifdef __cplusplus
  354. }
  355. #endif
  356. #endif /* __STM324x9I_EVAL_SDIO_SD_H */
  357. /**
  358. * @}
  359. */
  360. /**
  361. * @}
  362. */
  363. /**
  364. * @}
  365. */
  366. /**
  367. * @}
  368. */
  369. /**
  370. * @}
  371. */
  372. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/