bsp_driver_sd.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. ******************************************************************************
  3. * @file bsp_driver_sd.c (based on stm324x9i_eval_sd.c)
  4. * @brief This file includes a generic uSD card driver.
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2016 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. #define BUS_4BITS 1
  34. /* USER CODE BEGIN 0 */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "bsp_driver_sd.h"
  37. /* Extern variables ---------------------------------------------------------*/
  38. extern SD_HandleTypeDef hsd;
  39. extern HAL_SD_CardInfoTypedef SDCardInfo;
  40. /**
  41. * @brief Initializes the SD card device.
  42. * @param None
  43. * @retval SD status
  44. */
  45. uint8_t BSP_SD_Init(void)
  46. {
  47. uint8_t SD_state = MSD_OK;
  48. /* Check if the SD card is plugged in the slot */
  49. if (BSP_SD_IsDetected() != SD_PRESENT)
  50. {
  51. return MSD_ERROR;
  52. }
  53. SD_state = HAL_SD_Init(&hsd, &SDCardInfo);
  54. #ifdef BUS_4BITS
  55. if (SD_state == MSD_OK)
  56. {
  57. if (HAL_SD_WideBusOperation_Config(&hsd, SDIO_BUS_WIDE_4B) != SD_OK)
  58. {
  59. SD_state = MSD_ERROR;
  60. }
  61. else
  62. {
  63. SD_state = MSD_OK;
  64. }
  65. }
  66. #endif
  67. return SD_state;
  68. }
  69. /**
  70. * @brief Configures Interrupt mode for SD detection pin.
  71. * @param None
  72. * @retval Returns 0 in success otherwise 1.
  73. */
  74. uint8_t BSP_SD_ITConfig(void)
  75. {
  76. /* TBI: add user code here depending on the hardware configuration used */
  77. return 0;
  78. }
  79. /** @brief SD detect IT treatment
  80. * @param None
  81. * @retval None
  82. */
  83. void BSP_SD_DetectIT(void)
  84. {
  85. /* TBI: add user code here depending on the hardware configuration used */
  86. }
  87. /** @brief SD detect IT detection callback
  88. * @param None
  89. * @retval None
  90. */
  91. __weak void BSP_SD_DetectCallback(void)
  92. {
  93. /* NOTE: This function Should not be modified, when the callback is needed,
  94. the SD_DetectCallback could be implemented in the user file
  95. */
  96. }
  97. /**
  98. * @brief Reads block(s) from a specified address in an SD card, in polling mode.
  99. * @param pData: Pointer to the buffer that will contain the data to transmit
  100. * @param ReadAddr: Address from where data is to be read
  101. * @param BlockSize: SD card data block size, that should be 512
  102. * @param NumOfBlocks: Number of SD blocks to read
  103. * @retval SD status
  104. */
  105. uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
  106. {
  107. if(HAL_SD_ReadBlocks_DMA(&hsd, pData, ReadAddr, BlockSize, NumOfBlocks) != SD_OK)
  108. {
  109. return MSD_ERROR;
  110. }
  111. else
  112. {
  113. return MSD_OK;
  114. }
  115. }
  116. /**
  117. * @brief Writes block(s) to a specified address in an SD card, in polling mode.
  118. * @param pData: Pointer to the buffer that will contain the data to transmit
  119. * @param WriteAddr: Address from where data is to be written
  120. * @param BlockSize: SD card data block size, that should be 512
  121. * @param NumOfBlocks: Number of SD blocks to write
  122. * @retval SD status
  123. */
  124. uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
  125. {
  126. if(HAL_SD_WriteBlocks_DMA(&hsd, pData, WriteAddr, BlockSize, NumOfBlocks) != SD_OK)
  127. {
  128. return MSD_ERROR;
  129. }
  130. else
  131. {
  132. return MSD_OK;
  133. }
  134. }
  135. /**
  136. * @brief Reads block(s) from a specified address in an SD card, in DMA mode.
  137. * @param pData: Pointer to the buffer that will contain the data to transmit
  138. * @param ReadAddr: Address from where data is to be read
  139. * @param BlockSize: SD card data block size, that should be 512
  140. * @param NumOfBlocks: Number of SD blocks to read
  141. * @retval SD status
  142. */
  143. uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
  144. {
  145. uint8_t SD_state = MSD_OK;
  146. /* Read block(s) in DMA transfer mode */
  147. if(HAL_SD_ReadBlocks_DMA(&hsd, pData, ReadAddr, BlockSize, NumOfBlocks) != SD_OK)
  148. {
  149. SD_state = MSD_ERROR;
  150. }
  151. /* Wait until transfer is complete */
  152. if(SD_state == MSD_OK)
  153. {
  154. if(HAL_SD_CheckReadOperation(&hsd, (uint32_t)SD_DATATIMEOUT) != SD_OK)
  155. {
  156. SD_state = MSD_ERROR;
  157. }
  158. else
  159. {
  160. SD_state = MSD_OK;
  161. }
  162. }
  163. return SD_state;
  164. }
  165. /**
  166. * @brief Writes block(s) to a specified address in an SD card, in DMA mode.
  167. * @param pData: Pointer to the buffer that will contain the data to transmit
  168. * @param WriteAddr: Address from where data is to be written
  169. * @param BlockSize: SD card data block size, that should be 512
  170. * @param NumOfBlocks: Number of SD blocks to write
  171. * @retval SD status
  172. */
  173. uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
  174. {
  175. uint8_t SD_state = SD_OK;
  176. /* Write block(s) in DMA transfer mode */
  177. if(HAL_SD_WriteBlocks_DMA(&hsd, pData, WriteAddr, BlockSize, NumOfBlocks) != SD_OK)
  178. {
  179. SD_state = MSD_ERROR;
  180. }
  181. /* Wait until transfer is complete */
  182. if(SD_state == MSD_OK)
  183. {
  184. if(HAL_SD_CheckWriteOperation(&hsd, (uint32_t)SD_DATATIMEOUT) != SD_OK)
  185. {
  186. SD_state = MSD_ERROR;
  187. }
  188. else
  189. {
  190. SD_state = MSD_OK;
  191. }
  192. }
  193. return SD_state;
  194. }
  195. /**
  196. * @brief Erases the specified memory area of the given SD card.
  197. * @param StartAddr: Start byte address
  198. * @param EndAddr: End byte address
  199. * @retval SD status
  200. */
  201. uint8_t BSP_SD_Erase(uint64_t StartAddr, uint64_t EndAddr)
  202. {
  203. if(HAL_SD_Erase(&hsd, StartAddr, EndAddr) != SD_OK)
  204. {
  205. return MSD_ERROR;
  206. }
  207. else
  208. {
  209. return MSD_OK;
  210. }
  211. }
  212. /**
  213. * @brief Handles SD card interrupt request.
  214. * @param None
  215. * @retval None
  216. */
  217. void BSP_SD_IRQHandler(void)
  218. {
  219. HAL_SD_IRQHandler(&hsd);
  220. }
  221. /**
  222. * @brief Handles SD DMA Tx transfer interrupt request.
  223. * @param None
  224. * @retval None
  225. */
  226. void BSP_SD_DMA_Tx_IRQHandler(void)
  227. {
  228. HAL_DMA_IRQHandler(hsd.hdmatx);
  229. }
  230. /**
  231. * @brief Handles SD DMA Rx transfer interrupt request.
  232. * @param None
  233. * @retval None
  234. */
  235. void BSP_SD_DMA_Rx_IRQHandler(void)
  236. {
  237. HAL_DMA_IRQHandler(hsd.hdmarx);
  238. }
  239. /**
  240. * @brief Gets the current SD card data status.
  241. * @param None
  242. * @retval Data transfer state.
  243. * This value can be one of the following values:
  244. * @arg SD_TRANSFER_OK: No data transfer is acting
  245. * @arg SD_TRANSFER_BUSY: Data transfer is acting
  246. * @arg SD_TRANSFER_ERROR: Data transfer error
  247. */
  248. HAL_SD_TransferStateTypedef BSP_SD_GetStatus(void)
  249. {
  250. return(HAL_SD_GetStatus(&hsd));
  251. }
  252. /**
  253. * @brief Get SD information about specific SD card.
  254. * @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure
  255. * @retval None
  256. */
  257. void BSP_SD_GetCardInfo(HAL_SD_CardInfoTypedef* CardInfo)
  258. {
  259. /* Get SD card Information */
  260. HAL_SD_Get_CardInfo(&hsd, CardInfo);
  261. }
  262. /* USER CODE END 0 */
  263. /**
  264. * @brief Detects if SD card is correctly plugged in the memory slot or not.
  265. * @param None
  266. * @retval Returns if SD is detected or not
  267. */
  268. uint8_t BSP_SD_IsDetected(void)
  269. {
  270. __IO uint8_t status = SD_PRESENT;
  271. /* USER CODE BEGIN 1 */
  272. /* user code can be inserted here */
  273. /* USER CODE END 1 */
  274. return status;
  275. }
  276. /* USER CODE BEGIN AdditionalCode */
  277. /* user code can be inserted here */
  278. /* USER CODE END AdditionalCode */
  279. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/