stm320518_eval_spi_sd.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. ******************************************************************************
  3. * @file stm320518_eval_spi_sd.h
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 20-April-2012
  7. * @brief This file contains all the functions prototypes for the stm320518_eval_spi_sd
  8. * firmware driver.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2012 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 __STM320518_EVAL_SPI_SD_H
  30. #define __STM320518_EVAL_SPI_SD_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm320518_eval.h"
  36. /** @addtogroup Utilities
  37. * @{
  38. */
  39. /** @addtogroup STM32_EVAL
  40. * @{
  41. */
  42. /** @addtogroup STM320518_EVAL
  43. * @{
  44. */
  45. /** @addtogroup STM320518_EVAL_SPI_SD
  46. * @{
  47. */
  48. /** @defgroup STM320518_EVAL_SPI_SD_Exported_Types
  49. * @{
  50. */
  51. typedef enum
  52. {
  53. /**
  54. * @brief SD reponses and error flags
  55. */
  56. SD_RESPONSE_NO_ERROR = (0x00),
  57. SD_IN_IDLE_STATE = (0x01),
  58. SD_ERASE_RESET = (0x02),
  59. SD_ILLEGAL_COMMAND = (0x04),
  60. SD_COM_CRC_ERROR = (0x08),
  61. SD_ERASE_SEQUENCE_ERROR = (0x10),
  62. SD_ADDRESS_ERROR = (0x20),
  63. SD_PARAMETER_ERROR = (0x40),
  64. SD_RESPONSE_FAILURE = (0xFF),
  65. /**
  66. * @brief Data response error
  67. */
  68. SD_DATA_OK = (0x05),
  69. SD_DATA_CRC_ERROR = (0x0B),
  70. SD_DATA_WRITE_ERROR = (0x0D),
  71. SD_DATA_OTHER_ERROR = (0xFF)
  72. } SD_Error;
  73. /**
  74. * @brief Card Specific Data: CSD Register
  75. */
  76. typedef struct
  77. {
  78. __IO uint8_t CSDStruct; /*!< CSD structure */
  79. __IO uint8_t SysSpecVersion; /*!< System specification version */
  80. __IO uint8_t Reserved1; /*!< Reserved */
  81. __IO uint8_t TAAC; /*!< Data read access-time 1 */
  82. __IO uint8_t NSAC; /*!< Data read access-time 2 in CLK cycles */
  83. __IO uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */
  84. __IO uint16_t CardComdClasses; /*!< Card command classes */
  85. __IO uint8_t RdBlockLen; /*!< Max. read data block length */
  86. __IO uint8_t PartBlockRead; /*!< Partial blocks for read allowed */
  87. __IO uint8_t WrBlockMisalign; /*!< Write block misalignment */
  88. __IO uint8_t RdBlockMisalign; /*!< Read block misalignment */
  89. __IO uint8_t DSRImpl; /*!< DSR implemented */
  90. __IO uint8_t Reserved2; /*!< Reserved */
  91. __IO uint32_t DeviceSize; /*!< Device Size */
  92. __IO uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */
  93. __IO uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */
  94. __IO uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */
  95. __IO uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */
  96. __IO uint8_t DeviceSizeMul; /*!< Device size multiplier */
  97. __IO uint8_t EraseGrSize; /*!< Erase group size */
  98. __IO uint8_t EraseGrMul; /*!< Erase group size multiplier */
  99. __IO uint8_t WrProtectGrSize; /*!< Write protect group size */
  100. __IO uint8_t WrProtectGrEnable; /*!< Write protect group enable */
  101. __IO uint8_t ManDeflECC; /*!< Manufacturer default ECC */
  102. __IO uint8_t WrSpeedFact; /*!< Write speed factor */
  103. __IO uint8_t MaxWrBlockLen; /*!< Max. write data block length */
  104. __IO uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */
  105. __IO uint8_t Reserved3; /*!< Reserded */
  106. __IO uint8_t ContentProtectAppli; /*!< Content protection application */
  107. __IO uint8_t FileFormatGrouop; /*!< File format group */
  108. __IO uint8_t CopyFlag; /*!< Copy flag (OTP) */
  109. __IO uint8_t PermWrProtect; /*!< Permanent write protection */
  110. __IO uint8_t TempWrProtect; /*!< Temporary write protection */
  111. __IO uint8_t FileFormat; /*!< File Format */
  112. __IO uint8_t ECC; /*!< ECC code */
  113. __IO uint8_t CSD_CRC; /*!< CSD CRC */
  114. __IO uint8_t Reserved4; /*!< always 1*/
  115. } SD_CSD;
  116. /**
  117. * @brief Card Identification Data: CID Register
  118. */
  119. typedef struct
  120. {
  121. __IO uint8_t ManufacturerID; /*!< ManufacturerID */
  122. __IO uint16_t OEM_AppliID; /*!< OEM/Application ID */
  123. __IO uint32_t ProdName1; /*!< Product Name part1 */
  124. __IO uint8_t ProdName2; /*!< Product Name part2*/
  125. __IO uint8_t ProdRev; /*!< Product Revision */
  126. __IO uint32_t ProdSN; /*!< Product Serial Number */
  127. __IO uint8_t Reserved1; /*!< Reserved1 */
  128. __IO uint16_t ManufactDate; /*!< Manufacturing Date */
  129. __IO uint8_t CID_CRC; /*!< CID CRC */
  130. __IO uint8_t Reserved2; /*!< always 1 */
  131. } SD_CID;
  132. /**
  133. * @brief SD Card information
  134. */
  135. typedef struct
  136. {
  137. SD_CSD SD_csd;
  138. SD_CID SD_cid;
  139. uint32_t CardCapacity; /*!< Card Capacity */
  140. uint32_t CardBlockSize; /*!< Card Block Size */
  141. } SD_CardInfo;
  142. /**
  143. * @}
  144. */
  145. /** @defgroup STM320518_EVAL_SPI_SD_Exported_Constants
  146. * @{
  147. */
  148. /**
  149. * @brief Block Size
  150. */
  151. #define SD_BLOCK_SIZE 0x200
  152. /**
  153. * @brief Dummy byte
  154. */
  155. #define SD_DUMMY_BYTE 0xFF
  156. /**
  157. * @brief Start Data tokens:
  158. * Tokens (necessary because at nop/idle (and CS active) only 0xff is
  159. * on the data/command line)
  160. */
  161. #define SD_START_DATA_SINGLE_BLOCK_READ 0xFE /*!< Data token start byte, Start Single Block Read */
  162. #define SD_START_DATA_MULTIPLE_BLOCK_READ 0xFE /*!< Data token start byte, Start Multiple Block Read */
  163. #define SD_START_DATA_SINGLE_BLOCK_WRITE 0xFE /*!< Data token start byte, Start Single Block Write */
  164. #define SD_START_DATA_MULTIPLE_BLOCK_WRITE 0xFD /*!< Data token start byte, Start Multiple Block Write */
  165. #define SD_STOP_DATA_MULTIPLE_BLOCK_WRITE 0xFD /*!< Data toke stop byte, Stop Multiple Block Write */
  166. /**
  167. * @brief SD detection on its memory slot
  168. */
  169. #define SD_PRESENT ((uint8_t)0x01)
  170. #define SD_NOT_PRESENT ((uint8_t)0x00)
  171. /**
  172. * @brief Commands: CMDxx = CMD-number | 0x40
  173. */
  174. #define SD_CMD_GO_IDLE_STATE 0 /*!< CMD0 = 0x40 */
  175. #define SD_CMD_SEND_OP_COND 1 /*!< CMD1 = 0x41 */
  176. #define SD_CMD_SEND_CSD 9 /*!< CMD9 = 0x49 */
  177. #define SD_CMD_SEND_CID 10 /*!< CMD10 = 0x4A */
  178. #define SD_CMD_STOP_TRANSMISSION 12 /*!< CMD12 = 0x4C */
  179. #define SD_CMD_SEND_STATUS 13 /*!< CMD13 = 0x4D */
  180. #define SD_CMD_SET_BLOCKLEN 16 /*!< CMD16 = 0x50 */
  181. #define SD_CMD_READ_SINGLE_BLOCK 17 /*!< CMD17 = 0x51 */
  182. #define SD_CMD_READ_MULT_BLOCK 18 /*!< CMD18 = 0x52 */
  183. #define SD_CMD_SET_BLOCK_COUNT 23 /*!< CMD23 = 0x57 */
  184. #define SD_CMD_WRITE_SINGLE_BLOCK 24 /*!< CMD24 = 0x58 */
  185. #define SD_CMD_WRITE_MULT_BLOCK 25 /*!< CMD25 = 0x59 */
  186. #define SD_CMD_PROG_CSD 27 /*!< CMD27 = 0x5B */
  187. #define SD_CMD_SET_WRITE_PROT 28 /*!< CMD28 = 0x5C */
  188. #define SD_CMD_CLR_WRITE_PROT 29 /*!< CMD29 = 0x5D */
  189. #define SD_CMD_SEND_WRITE_PROT 30 /*!< CMD30 = 0x5E */
  190. #define SD_CMD_SD_ERASE_GRP_START 32 /*!< CMD32 = 0x60 */
  191. #define SD_CMD_SD_ERASE_GRP_END 33 /*!< CMD33 = 0x61 */
  192. #define SD_CMD_UNTAG_SECTOR 34 /*!< CMD34 = 0x62 */
  193. #define SD_CMD_ERASE_GRP_START 35 /*!< CMD35 = 0x63 */
  194. #define SD_CMD_ERASE_GRP_END 36 /*!< CMD36 = 0x64 */
  195. #define SD_CMD_UNTAG_ERASE_GROUP 37 /*!< CMD37 = 0x65 */
  196. #define SD_CMD_ERASE 38 /*!< CMD38 = 0x66 */
  197. /**
  198. * @}
  199. */
  200. /** @defgroup STM320518_EVAL_SPI_SD_Exported_Macros
  201. * @{
  202. */
  203. /**
  204. * @brief Select SD Card: ChipSelect pin low
  205. */
  206. #define SD_CS_LOW() GPIO_ResetBits(SD_CS_GPIO_PORT, SD_CS_PIN)
  207. /**
  208. * @brief Deselect SD Card: ChipSelect pin high
  209. */
  210. #define SD_CS_HIGH() GPIO_SetBits(SD_CS_GPIO_PORT, SD_CS_PIN)
  211. /**
  212. * @}
  213. */
  214. /** @defgroup STM320518_EVAL_SPI_SD_Exported_Functions
  215. * @{
  216. */
  217. void SD_DeInit(void);
  218. SD_Error SD_Init(void);
  219. uint8_t SD_Detect(void);
  220. SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo);
  221. SD_Error SD_ReadBlock(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize);
  222. SD_Error SD_ReadMultiBlocks(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
  223. SD_Error SD_WriteBlock(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize);
  224. SD_Error SD_WriteMultiBlocks(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
  225. SD_Error SD_GetCSDRegister(SD_CSD* SD_csd);
  226. SD_Error SD_GetCIDRegister(SD_CID* SD_cid);
  227. void SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc);
  228. SD_Error SD_GetResponse(uint8_t Response);
  229. uint8_t SD_GetDataResponse(void);
  230. SD_Error SD_GoIdleState(void);
  231. uint16_t SD_GetStatus(void);
  232. uint8_t SD_WriteByte(uint8_t byte);
  233. uint8_t SD_ReadByte(void);
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif /* __STM320518_EVAL_SPI_SD_H */
  238. /**
  239. * @}
  240. */
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /**
  248. * @}
  249. */
  250. /**
  251. * @}
  252. */
  253. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/