sdcard.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. ******************************************************************************
  3. * @file SDIO/sdcard.h
  4. * @author MCD Application Team
  5. * @version V3.1.2
  6. * @date 09/28/2009
  7. * @brief This file contains all the functions prototypes for the SD Card
  8. * driver firmware library.
  9. ******************************************************************************
  10. * @copy
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  20. */
  21. /** @addtogroup STM32F10x_StdPeriph_Examples
  22. * @{
  23. */
  24. /** @addtogroup SDIO_Example
  25. * @{
  26. */
  27. /* Define to prevent recursive inclusion -------------------------------------*/
  28. #ifndef __SDCARD_H
  29. #define __SDCARD_H
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm32f10x.h"
  32. /* Exported types ------------------------------------------------------------*/
  33. typedef enum
  34. {
  35. /* SDIO specific error defines */
  36. SD_CMD_CRC_FAIL = (1), /* Command response received (but CRC check failed) */
  37. SD_DATA_CRC_FAIL = (2), /* Data bock sent/received (CRC check Failed) */
  38. SD_CMD_RSP_TIMEOUT = (3), /* Command response timeout */
  39. SD_DATA_TIMEOUT = (4), /* Data time out */
  40. SD_TX_UNDERRUN = (5), /* Transmit FIFO under-run */
  41. SD_RX_OVERRUN = (6), /* Receive FIFO over-run */
  42. SD_START_BIT_ERR = (7), /* Start bit not detected on all data signals in widE bus mode */
  43. SD_CMD_OUT_OF_RANGE = (8), /* CMD's argument was out of range.*/
  44. SD_ADDR_MISALIGNED = (9), /* Misaligned address */
  45. 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 */
  46. SD_ERASE_SEQ_ERR = (11), /* An error in the sequence of erase command occurs.*/
  47. SD_BAD_ERASE_PARAM = (12), /* An Invalid selection for erase groups */
  48. SD_WRITE_PROT_VIOLATION = (13), /* Attempt to program a write protect block */
  49. 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 */
  50. SD_COM_CRC_FAILED = (15), /* CRC check of the previous command failed */
  51. SD_ILLEGAL_CMD = (16), /* Command is not legal for the card state */
  52. SD_CARD_ECC_FAILED = (17), /* Card internal ECC was applied but failed to correct the data */
  53. SD_CC_ERROR = (18), /* Internal card controller error */
  54. SD_GENERAL_UNKNOWN_ERROR = (19), /* General or Unknown error */
  55. SD_STREAM_READ_UNDERRUN = (20), /* The card could not sustain data transfer in stream read operation. */
  56. SD_STREAM_WRITE_OVERRUN = (21), /* The card could not sustain data programming in stream mode */
  57. SD_CID_CSD_OVERWRITE = (22), /* CID/CSD overwrite error */
  58. SD_WP_ERASE_SKIP = (23), /* only partial address space was erased */
  59. SD_CARD_ECC_DISABLED = (24), /* Command has been executed without using internal ECC */
  60. SD_ERASE_RESET = (25), /* Erase sequence was cleared before executing because an out of erase sequence command was received */
  61. SD_AKE_SEQ_ERROR = (26), /* Error in sequence of authentication. */
  62. SD_INVALID_VOLTRANGE = (27),
  63. SD_ADDR_OUT_OF_RANGE = (28),
  64. SD_SWITCH_ERROR = (29),
  65. SD_SDIO_DISABLED = (30),
  66. SD_SDIO_FUNCTION_BUSY = (31),
  67. SD_SDIO_FUNCTION_FAILED = (32),
  68. SD_SDIO_UNKNOWN_FUNCTION = (33),
  69. /* Standard error defines */
  70. SD_INTERNAL_ERROR,
  71. SD_NOT_CONFIGURED,
  72. SD_REQUEST_PENDING,
  73. SD_REQUEST_NOT_APPLICABLE,
  74. SD_INVALID_PARAMETER,
  75. SD_UNSUPPORTED_FEATURE,
  76. SD_UNSUPPORTED_HW,
  77. SD_ERROR,
  78. SD_OK,
  79. } SD_Error;
  80. /* SDIO Commands Index */
  81. #define SDIO_GO_IDLE_STATE ((uint8_t)0)
  82. #define SDIO_SEND_OP_COND ((uint8_t)1)
  83. #define SDIO_ALL_SEND_CID ((uint8_t)2)
  84. #define SDIO_SET_REL_ADDR ((uint8_t)3) /* SDIO_SEND_REL_ADDR for SD Card */
  85. #define SDIO_SET_DSR ((uint8_t)4)
  86. #define SDIO_SDIO_SEN_OP_COND ((uint8_t)5)
  87. #define SDIO_HS_SWITCH ((uint8_t)6)
  88. #define SDIO_SEL_DESEL_CARD ((uint8_t)7)
  89. #define SDIO_HS_SEND_EXT_CSD ((uint8_t)8)
  90. #define SDIO_SEND_CSD ((uint8_t)9)
  91. #define SDIO_SEND_CID ((uint8_t)10)
  92. #define SDIO_READ_DAT_UNTIL_STOP ((uint8_t)11) /* SD Card doesn't support it */
  93. #define SDIO_STOP_TRANSMISSION ((uint8_t)12)
  94. #define SDIO_SEND_STATUS ((uint8_t)13)
  95. #define SDIO_HS_BUSTEST_READ ((uint8_t)14)
  96. #define SDIO_GO_INACTIVE_STATE ((uint8_t)15)
  97. #define SDIO_SET_BLOCKLEN ((uint8_t)16)
  98. #define SDIO_READ_SINGLE_BLOCK ((uint8_t)17)
  99. #define SDIO_READ_MULT_BLOCK ((uint8_t)18)
  100. #define SDIO_HS_BUSTEST_WRITE ((uint8_t)19)
  101. #define SDIO_WRITE_DAT_UNTIL_STOP ((uint8_t)20) /* SD Card doesn't support it */
  102. #define SDIO_SET_BLOCK_COUNT ((uint8_t)23) /* SD Card doesn't support it */
  103. #define SDIO_WRITE_SINGLE_BLOCK ((uint8_t)24)
  104. #define SDIO_WRITE_MULT_BLOCK ((uint8_t)25)
  105. #define SDIO_PROG_CID ((uint8_t)26) /* reserved for manufacturers */
  106. #define SDIO_PROG_CSD ((uint8_t)27)
  107. #define SDIO_SET_WRITE_PROT ((uint8_t)28)
  108. #define SDIO_CLR_WRITE_PROT ((uint8_t)29)
  109. #define SDIO_SEND_WRITE_PROT ((uint8_t)30)
  110. #define SDIO_SD_ERASE_GRP_START ((uint8_t)32) /* To set the address of the first write
  111. block to be erased. (For SD card only) */
  112. #define SDIO_SD_ERASE_GRP_END ((uint8_t)33) /* To set the address of the last write block of the
  113. continuous range to be erased. (For SD card only) */
  114. #define SDIO_ERASE_GRP_START ((uint8_t)35) /* To set the address of the first write block to be erased.
  115. (For MMC card only spec 3.31) */
  116. #define SDIO_ERASE_GRP_END ((uint8_t)36) /* To set the address of the last write block of the
  117. continuous range to be erased. (For MMC card only spec 3.31) */
  118. #define SDIO_ERASE ((uint8_t)38)
  119. #define SDIO_FAST_IO ((uint8_t)39) /* SD Card doesn't support it */
  120. #define SDIO_GO_IRQ_STATE ((uint8_t)40) /* SD Card doesn't support it */
  121. #define SDIO_LOCK_UNLOCK ((uint8_t)42)
  122. #define SDIO_APP_CMD ((uint8_t)55)
  123. #define SDIO_GEN_CMD ((uint8_t)56)
  124. #define SDIO_NO_CMD ((uint8_t)64)
  125. /* Following commands are SD Card Specific commands.
  126. SDIO_APP_CMD should be sent before sending these
  127. commands. */
  128. #define SDIO_APP_SD_SET_BUSWIDTH ((uint8_t)6) /* For SD Card only */
  129. #define SDIO_SD_APP_STAUS ((uint8_t)13) /* For SD Card only */
  130. #define SDIO_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22) /* For SD Card only */
  131. #define SDIO_SD_APP_OP_COND ((uint8_t)41) /* For SD Card only */
  132. #define SDIO_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /* For SD Card only */
  133. #define SDIO_SD_APP_SEND_SCR ((uint8_t)51) /* For SD Card only */
  134. #define SDIO_SDIO_RW_DIRECT ((uint8_t)52) /* For SD I/O Card only */
  135. #define SDIO_SDIO_RW_EXTENDED ((uint8_t)53) /* For SD I/O Card only */
  136. /* Following commands are SD Card Specific security commands.
  137. SDIO_APP_CMD should be sent before sending these commands. */
  138. #define SDIO_SD_APP_GET_MKB ((uint8_t)43) /* For SD Card only */
  139. #define SDIO_SD_APP_GET_MID ((uint8_t)44) /* For SD Card only */
  140. #define SDIO_SD_APP_SET_CER_RN1 ((uint8_t)45) /* For SD Card only */
  141. #define SDIO_SD_APP_GET_CER_RN2 ((uint8_t)46) /* For SD Card only */
  142. #define SDIO_SD_APP_SET_CER_RES2 ((uint8_t)47) /* For SD Card only */
  143. #define SDIO_SD_APP_GET_CER_RES1 ((uint8_t)48) /* For SD Card only */
  144. #define SDIO_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18) /* For SD Card only */
  145. #define SDIO_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /* For SD Card only */
  146. #define SDIO_SD_APP_SECURE_ERASE ((uint8_t)38) /* For SD Card only */
  147. #define SDIO_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49) /* For SD Card only */
  148. #define SDIO_SD_APP_SECURE_WRITE_MKB ((uint8_t)48) /* For SD Card only */
  149. typedef enum
  150. {
  151. SD_NO_TRANSFER = 0,
  152. SD_TRANSFER_IN_PROGRESS
  153. } SDTransferState;
  154. typedef struct
  155. {
  156. uint16_t TransferredBytes;
  157. SD_Error TransferError;
  158. uint8_t padding;
  159. } SDLastTransferInfo;
  160. typedef struct /* Card Specific Data */
  161. {
  162. __IO uint8_t CSDStruct; /* CSD structure */
  163. __IO uint8_t SysSpecVersion; /* System specification version */
  164. __IO uint8_t Reserved1; /* Reserved */
  165. __IO uint8_t TAAC; /* Data read access-time 1 */
  166. __IO uint8_t NSAC; /* Data read access-time 2 in CLK cycles */
  167. __IO uint8_t MaxBusClkFrec; /* Max. bus clock frequency */
  168. __IO uint16_t CardComdClasses; /* Card command classes */
  169. __IO uint8_t RdBlockLen; /* Max. read data block length */
  170. __IO uint8_t PartBlockRead; /* Partial blocks for read allowed */
  171. __IO uint8_t WrBlockMisalign; /* Write block misalignment */
  172. __IO uint8_t RdBlockMisalign; /* Read block misalignment */
  173. __IO uint8_t DSRImpl; /* DSR implemented */
  174. __IO uint8_t Reserved2; /* Reserved */
  175. __IO uint32_t DeviceSize; /* Device Size */
  176. __IO uint8_t MaxRdCurrentVDDMin; /* Max. read current @ VDD min */
  177. __IO uint8_t MaxRdCurrentVDDMax; /* Max. read current @ VDD max */
  178. __IO uint8_t MaxWrCurrentVDDMin; /* Max. write current @ VDD min */
  179. __IO uint8_t MaxWrCurrentVDDMax; /* Max. write current @ VDD max */
  180. __IO uint8_t DeviceSizeMul; /* Device size multiplier */
  181. __IO uint8_t EraseGrSize; /* Erase group size */
  182. __IO uint8_t EraseGrMul; /* Erase group size multiplier */
  183. __IO uint8_t WrProtectGrSize; /* Write protect group size */
  184. __IO uint8_t WrProtectGrEnable; /* Write protect group enable */
  185. __IO uint8_t ManDeflECC; /* Manufacturer default ECC */
  186. __IO uint8_t WrSpeedFact; /* Write speed factor */
  187. __IO uint8_t MaxWrBlockLen; /* Max. write data block length */
  188. __IO uint8_t WriteBlockPaPartial; /* Partial blocks for write allowed */
  189. __IO uint8_t Reserved3; /* Reserded */
  190. __IO uint8_t ContentProtectAppli; /* Content protection application */
  191. __IO uint8_t FileFormatGrouop; /* File format group */
  192. __IO uint8_t CopyFlag; /* Copy flag (OTP) */
  193. __IO uint8_t PermWrProtect; /* Permanent write protection */
  194. __IO uint8_t TempWrProtect; /* Temporary write protection */
  195. __IO uint8_t FileFormat; /* File Format */
  196. __IO uint8_t ECC; /* ECC code */
  197. __IO uint8_t CSD_CRC; /* CSD CRC */
  198. __IO uint8_t Reserved4; /* always 1*/
  199. } SD_CSD;
  200. typedef struct /*Card Identification Data*/
  201. {
  202. __IO uint8_t ManufacturerID; /* ManufacturerID */
  203. __IO uint16_t OEM_AppliID; /* OEM/Application ID */
  204. __IO uint32_t ProdName1; /* Product Name part1 */
  205. __IO uint8_t ProdName2; /* Product Name part2*/
  206. __IO uint8_t ProdRev; /* Product Revision */
  207. __IO uint32_t ProdSN; /* Product Serial Number */
  208. __IO uint8_t Reserved1; /* Reserved1 */
  209. __IO uint16_t ManufactDate; /* Manufacturing Date */
  210. __IO uint8_t CID_CRC; /* CID CRC */
  211. __IO uint8_t Reserved2; /* always 1 */
  212. } SD_CID;
  213. typedef struct
  214. {
  215. SD_CSD SD_csd;
  216. SD_CID SD_cid;
  217. uint32_t CardCapacity; /* Card Capacity */
  218. uint32_t CardBlockSize; /* Card Block Size */
  219. uint16_t RCA;
  220. uint8_t CardType;
  221. } SD_CardInfo;
  222. /* Exported constants --------------------------------------------------------*/
  223. #define SD_DMA_MODE ((uint32_t)0x00000000)
  224. #define SD_INTERRUPT_MODE ((uint32_t)0x00000001)
  225. #define SD_POLLING_MODE ((uint32_t)0x00000002)
  226. /* Supported Memory Cards */
  227. #define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x0)
  228. #define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x1)
  229. #define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x2)
  230. #define SDIO_MULTIMEDIA_CARD ((uint32_t)0x3)
  231. #define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x4)
  232. #define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x5)
  233. #define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x6)
  234. #define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x7)
  235. /* Exported macro ------------------------------------------------------------*/
  236. /* Exported functions ------------------------------------------------------- */
  237. SD_Error SD_Init(void);
  238. SD_Error SD_PowerON(void);
  239. SD_Error SD_PowerOFF(void);
  240. SD_Error SD_InitializeCards(void);
  241. SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo);
  242. SD_Error SD_EnableWideBusOperation(uint32_t WideMode);
  243. SD_Error SD_SetDeviceMode(uint32_t Mode);
  244. SD_Error SD_SelectDeselect(uint32_t addr);
  245. SD_Error SD_ReadBlock(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize);
  246. SD_Error SD_ReadMultiBlocks(uint32_t addr, uint32_t *readbuff, uint16_t BlockSize, uint32_t NumberOfBlocks);
  247. SD_Error SD_WriteBlock(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize);
  248. SD_Error SD_WriteMultiBlocks(uint32_t addr, uint32_t *writebuff, uint16_t BlockSize, uint32_t NumberOfBlocks);
  249. SDTransferState SD_GetTransferState(void);
  250. SD_Error SD_StopTransfer(void);
  251. SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr);
  252. SD_Error SD_SendStatus(uint32_t *pcardstatus);
  253. SD_Error SD_SendSDStatus(uint32_t *psdstatus);
  254. SD_Error SD_ProcessIRQSrc(void);
  255. #endif /* __SDCARD_H */
  256. /**
  257. * @}
  258. */
  259. /**
  260. * @}
  261. */
  262. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/