stm32_adafruit_sd.h 7.6 KB

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