sdcard.h 14 KB

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