stm32_adafruit_sd.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. ******************************************************************************
  3. * @file stm32_adafruit_sd.h
  4. * @author MCD Application Team
  5. * @brief This file contains the common defines and functions prototypes for
  6. * the stm32_adafruit_sd.c driver.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* Define to prevent recursive inclusion -------------------------------------*/
  37. #ifndef __STM32_ADAFRUIT_SD_H
  38. #define __STM32_ADAFRUIT_SD_H
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /* Includes ------------------------------------------------------------------*/
  43. #include <stdint.h>
  44. /** @addtogroup BSP
  45. * @{
  46. */
  47. #define __IO volatile
  48. /** @addtogroup STM32_ADAFRUIT
  49. * @{
  50. */
  51. /** @defgroup STM32_ADAFRUIT_SD
  52. * @{
  53. */
  54. /** @defgroup STM32_ADAFRUIT_SD_Exported_Types
  55. * @{
  56. */
  57. /**
  58. * @brief SD status structure definition
  59. */
  60. enum {
  61. BSP_SD_OK = 0x00,
  62. MSD_OK = 0x00,
  63. BSP_SD_ERROR = 0x01,
  64. BSP_SD_TIMEOUT
  65. };
  66. typedef struct
  67. {
  68. uint8_t Reserved1:2; /* Reserved */
  69. uint16_t DeviceSize:12; /* Device Size */
  70. uint8_t MaxRdCurrentVDDMin:3; /* Max. read current @ VDD min */
  71. uint8_t MaxRdCurrentVDDMax:3; /* Max. read current @ VDD max */
  72. uint8_t MaxWrCurrentVDDMin:3; /* Max. write current @ VDD min */
  73. uint8_t MaxWrCurrentVDDMax:3; /* Max. write current @ VDD max */
  74. uint8_t DeviceSizeMul:3; /* Device size multiplier */
  75. } struct_v1;
  76. typedef struct
  77. {
  78. uint8_t Reserved1:6; /* Reserved */
  79. uint32_t DeviceSize:22; /* Device Size */
  80. uint8_t Reserved2:1; /* Reserved */
  81. } struct_v2;
  82. /**
  83. * @brief Card Specific Data: CSD Register
  84. */
  85. typedef struct
  86. {
  87. /* Header part */
  88. uint8_t CSDStruct:2; /* CSD structure */
  89. uint8_t Reserved1:6; /* Reserved */
  90. uint8_t TAAC:8; /* Data read access-time 1 */
  91. uint8_t NSAC:8; /* Data read access-time 2 in CLK cycles */
  92. uint8_t MaxBusClkFrec:8; /* Max. bus clock frequency */
  93. uint16_t CardComdClasses:12; /* Card command classes */
  94. uint8_t RdBlockLen:4; /* Max. read data block length */
  95. uint8_t PartBlockRead:1; /* Partial blocks for read allowed */
  96. uint8_t WrBlockMisalign:1; /* Write block misalignment */
  97. uint8_t RdBlockMisalign:1; /* Read block misalignment */
  98. uint8_t DSRImpl:1; /* DSR implemented */
  99. /* v1 or v2 struct */
  100. union csd_version {
  101. struct_v1 v1;
  102. struct_v2 v2;
  103. } version;
  104. uint8_t EraseSingleBlockEnable:1; /* Erase single block enable */
  105. uint8_t EraseSectorSize:7; /* Erase group size multiplier */
  106. uint8_t WrProtectGrSize:7; /* Write protect group size */
  107. uint8_t WrProtectGrEnable:1; /* Write protect group enable */
  108. uint8_t Reserved2:2; /* Reserved */
  109. uint8_t WrSpeedFact:3; /* Write speed factor */
  110. uint8_t MaxWrBlockLen:4; /* Max. write data block length */
  111. uint8_t WriteBlockPartial:1; /* Partial blocks for write allowed */
  112. uint8_t Reserved3:5; /* Reserved */
  113. uint8_t FileFormatGrouop:1; /* File format group */
  114. uint8_t CopyFlag:1; /* Copy flag (OTP) */
  115. uint8_t PermWrProtect:1; /* Permanent write protection */
  116. uint8_t TempWrProtect:1; /* Temporary write protection */
  117. uint8_t FileFormat:2; /* File Format */
  118. uint8_t Reserved4:2; /* Reserved */
  119. uint8_t crc:7; /* Reserved */
  120. uint8_t Reserved5:1; /* always 1*/
  121. } SD_CSD;
  122. /**
  123. * @brief Card Identification Data: CID Register
  124. */
  125. typedef struct
  126. {
  127. __IO uint8_t ManufacturerID; /* ManufacturerID */
  128. __IO uint16_t OEM_AppliID; /* OEM/Application ID */
  129. __IO uint32_t ProdName1; /* Product Name part1 */
  130. __IO uint8_t ProdName2; /* Product Name part2*/
  131. __IO uint8_t ProdRev; /* Product Revision */
  132. __IO uint32_t ProdSN; /* Product Serial Number */
  133. __IO uint8_t Reserved1; /* Reserved1 */
  134. __IO uint16_t ManufactDate; /* Manufacturing Date */
  135. __IO uint8_t CID_CRC; /* CID CRC */
  136. __IO uint8_t Reserved2; /* always 1 */
  137. } SD_CID;
  138. /**
  139. * @brief SD Card information
  140. */
  141. typedef struct
  142. {
  143. SD_CSD Csd;
  144. SD_CID Cid;
  145. uint32_t CardCapacity; /*!< Card Capacity */
  146. uint32_t CardBlockSize; /*!< Card Block Size */
  147. uint32_t LogBlockNbr; /*!< Specifies the Card logical Capacity in blocks */
  148. uint32_t LogBlockSize; /*!< Specifies logical block size in bytes */
  149. } SD_CardInfo;
  150. /**
  151. * @}
  152. */
  153. /** @defgroup STM32_ADAFRUIT_SPI_SD_Exported_Constants
  154. * @{
  155. */
  156. /**
  157. * @brief Block Size
  158. */
  159. #define SD_BLOCK_SIZE 0x200
  160. /**
  161. * @brief SD detection on its memory slot
  162. */
  163. #define SD_PRESENT ((uint8_t)0x01)
  164. #define SD_NOT_PRESENT ((uint8_t)0x00)
  165. #define SD_DATATIMEOUT ((uint32_t)100000000)
  166. /**
  167. * @brief SD Card information structure
  168. */
  169. #define BSP_SD_CardInfo SD_CardInfo
  170. /**
  171. * @}
  172. */
  173. /** @defgroup STM32_ADAFRUIT_SD_Exported_Macro
  174. * @{
  175. */
  176. /**
  177. * @}
  178. */
  179. /** @defgroup STM32_ADAFRUIT_SD_Exported_Functions
  180. * @{
  181. */
  182. uint8_t BSP_SD_Init(void);
  183. uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout);
  184. uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout);
  185. uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr);
  186. uint8_t BSP_SD_GetCardState(void);
  187. uint8_t BSP_SD_GetCardInfo(SD_CardInfo *pCardInfo);
  188. /* Link functions for SD Card peripheral*/
  189. void SD_IO_Init(void);
  190. void SD_IO_CSState(uint8_t state);
  191. void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength);
  192. uint8_t SD_IO_WriteByte(uint8_t Data);
  193. /* Link function for HAL delay */
  194. void HAL_Delay(__IO uint32_t Delay);
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. #endif /* __STM32_ADAFRUIT_SD_H */
  199. /**
  200. * @}
  201. */
  202. /**
  203. * @}
  204. */
  205. /**
  206. * @}
  207. */
  208. /**
  209. * @}
  210. */
  211. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/