bsp_driver_sd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. ******************************************************************************
  3. * @file bsp_driver_sd.c for F4 (based on stm324x9i_eval_sd.c)
  4. * @brief This file includes a generic uSD card driver.
  5. ******************************************************************************
  6. * This notice applies to any and all portions of this file
  7. * that are not between comment pairs USER CODE BEGIN and
  8. * USER CODE END. Other portions of this file, whether
  9. * inserted by the user or by software development tools
  10. * are owned by their respective copyright owners.
  11. *
  12. * Copyright (c) 2018 STMicroelectronics International N.V.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted, provided that the following conditions are met:
  17. *
  18. * 1. Redistribution of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. Neither the name of STMicroelectronics nor the names of other
  24. * contributors to this software may be used to endorse or promote products
  25. * derived from this software without specific written permission.
  26. * 4. This software, including modifications and/or derivative works of this
  27. * software, must execute solely and exclusively on microcontroller or
  28. * microprocessor devices manufactured by or for STMicroelectronics.
  29. * 5. Redistribution and use of this software other than as permitted under
  30. * this license is void and will automatically terminate your rights under
  31. * this license.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  34. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  35. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  36. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  37. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  38. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  41. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  42. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  44. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. ******************************************************************************
  47. */
  48. #ifdef OLD_API
  49. /* kept to avoid issue when migrating old projects. */
  50. /* USER CODE BEGIN 0 */
  51. /* USER CODE END 0 */
  52. #else
  53. /* USER CODE BEGIN FirstSection */
  54. /* can be used to modify / undefine following code or add new definitions */
  55. /* USER CODE END FirstSection */
  56. /* Includes ------------------------------------------------------------------*/
  57. #include "bsp_driver_sd.h"
  58. /* Extern variables ---------------------------------------------------------*/
  59. extern SD_HandleTypeDef hsd;
  60. /* USER CODE BEGIN BeforeInitSection */
  61. /* can be used to modify / undefine following code or add code */
  62. /* USER CODE END BeforeInitSection */
  63. /**
  64. * @brief Initializes the SD card device.
  65. * @retval SD status
  66. */
  67. uint8_t BSP_SD_Init(void)
  68. {
  69. uint8_t sd_state = MSD_OK;
  70. /* Check if the SD card is plugged in the slot */
  71. if (BSP_SD_IsDetected() != SD_PRESENT)
  72. {
  73. return MSD_ERROR;
  74. }
  75. /* HAL SD initialization */
  76. sd_state = HAL_SD_Init(&hsd);
  77. /* Configure SD Bus width (4 bits mode selected) */
  78. if (sd_state == MSD_OK)
  79. {
  80. /* Enable wide operation */
  81. if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK)
  82. {
  83. sd_state = MSD_ERROR;
  84. }
  85. }
  86. return sd_state;
  87. }
  88. /* USER CODE BEGIN AfterInitSection */
  89. /* can be used to modify previous code / undefine following code / add code */
  90. /* USER CODE END AfterInitSection */
  91. /**
  92. * @brief Configures Interrupt mode for SD detection pin.
  93. * @retval Returns 0 in success otherwise 1.
  94. */
  95. uint8_t BSP_SD_ITConfig(void)
  96. {
  97. /* TBI: add user code here depending on the hardware configuration used */
  98. return (uint8_t)0;
  99. }
  100. /** @brief SD detect IT treatment
  101. */
  102. void BSP_SD_DetectIT(void)
  103. {
  104. /* TBI: add user code here depending on the hardware configuration used */
  105. }
  106. /** @brief SD detect IT detection callback
  107. */
  108. __weak void BSP_SD_DetectCallback(void)
  109. {
  110. /* NOTE: This function Should not be modified, when the callback is needed,
  111. the BSP_SD_DetectCallback could be implemented in the user file
  112. */
  113. }
  114. /* USER CODE BEGIN BeforeReadBlocksSection */
  115. /* can be used to modify previous code / undefine following code / add code */
  116. /* USER CODE END BeforeReadBlocksSection */
  117. /**
  118. * @brief Reads block(s) from a specified address in an SD card, in polling mode.
  119. * @param pData: Pointer to the buffer that will contain the data to transmit
  120. * @param ReadAddr: Address from where data is to be read
  121. * @param NumOfBlocks: Number of SD blocks to read
  122. * @param Timeout: Timeout for read operation
  123. * @retval SD status
  124. */
  125. uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
  126. {
  127. uint8_t sd_state = MSD_OK;
  128. if (HAL_SD_ReadBlocks(&hsd, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) != HAL_OK)
  129. {
  130. sd_state = MSD_ERROR;
  131. }
  132. return sd_state;
  133. }
  134. /* USER CODE BEGIN BeforeWriteBlocksSection */
  135. /* can be used to modify previous code / undefine following code / add code */
  136. /* USER CODE END BeforeWriteBlocksSection */
  137. /**
  138. * @brief Writes block(s) to a specified address in an SD card, in polling mode.
  139. * @param pData: Pointer to the buffer that will contain the data to transmit
  140. * @param WriteAddr: Address from where data is to be written
  141. * @param NumOfBlocks: Number of SD blocks to write
  142. * @param Timeout: Timeout for write operation
  143. * @retval SD status
  144. */
  145. uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
  146. {
  147. uint8_t sd_state = MSD_OK;
  148. if (HAL_SD_WriteBlocks(&hsd, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) != HAL_OK)
  149. {
  150. sd_state = MSD_ERROR;
  151. }
  152. return sd_state;
  153. }
  154. /* USER CODE BEGIN BeforeReadDMABlocksSection */
  155. /* can be used to modify previous code / undefine following code / add code */
  156. /* USER CODE END BeforeReadDMABlocksSection */
  157. /**
  158. * @brief Reads block(s) from a specified address in an SD card, in DMA mode.
  159. * @param pData: Pointer to the buffer that will contain the data to transmit
  160. * @param ReadAddr: Address from where data is to be read
  161. * @param NumOfBlocks: Number of SD blocks to read
  162. * @retval SD status
  163. */
  164. uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks)
  165. {
  166. uint8_t sd_state = MSD_OK;
  167. /* Read block(s) in DMA transfer mode */
  168. if (HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t *)pData, ReadAddr, NumOfBlocks) != HAL_OK)
  169. {
  170. sd_state = MSD_ERROR;
  171. }
  172. return sd_state;
  173. }
  174. /* USER CODE BEGIN BeforeWriteDMABlocksSection */
  175. /* can be used to modify previous code / undefine following code / add code */
  176. /* USER CODE END BeforeWriteDMABlocksSection */
  177. /**
  178. * @brief Writes block(s) to a specified address in an SD card, in DMA mode.
  179. * @param pData: Pointer to the buffer that will contain the data to transmit
  180. * @param WriteAddr: Address from where data is to be written
  181. * @param NumOfBlocks: Number of SD blocks to write
  182. * @retval SD status
  183. */
  184. uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks)
  185. {
  186. uint8_t sd_state = MSD_OK;
  187. /* Write block(s) in DMA transfer mode */
  188. if (HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t *)pData, WriteAddr, NumOfBlocks) != HAL_OK)
  189. {
  190. sd_state = MSD_ERROR;
  191. }
  192. return sd_state;
  193. }
  194. /* USER CODE BEGIN BeforeEraseSection */
  195. /* can be used to modify previous code / undefine following code / add code */
  196. /* USER CODE END BeforeEraseSection */
  197. /**
  198. * @brief Erases the specified memory area of the given SD card.
  199. * @param StartAddr: Start byte address
  200. * @param EndAddr: End byte address
  201. * @retval SD status
  202. */
  203. uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr)
  204. {
  205. uint8_t sd_state = MSD_OK;
  206. if (HAL_SD_Erase(&hsd, StartAddr, EndAddr) != HAL_OK)
  207. {
  208. sd_state = MSD_ERROR;
  209. }
  210. return sd_state;
  211. }
  212. /* USER CODE BEGIN BeforeHandlersSection */
  213. /* can be used to modify previous code / undefine following code / add code */
  214. /* USER CODE END BeforeHandlersSection */
  215. /**
  216. * @brief Handles SD card interrupt request.
  217. */
  218. void BSP_SD_IRQHandler(void)
  219. {
  220. HAL_SD_IRQHandler(&hsd);
  221. }
  222. /**
  223. * @brief Handles SD DMA Tx transfer interrupt request.
  224. */
  225. void BSP_SD_DMA_Tx_IRQHandler(void)
  226. {
  227. HAL_DMA_IRQHandler(hsd.hdmatx);
  228. }
  229. /**
  230. * @brief Handles SD DMA Rx transfer interrupt request.
  231. */
  232. void BSP_SD_DMA_Rx_IRQHandler(void)
  233. {
  234. HAL_DMA_IRQHandler(hsd.hdmarx);
  235. }
  236. /**
  237. * @brief Gets the current SD card data status.
  238. * @param None
  239. * @retval Data transfer state.
  240. * This value can be one of the following values:
  241. * @arg SD_TRANSFER_OK: No data transfer is acting
  242. * @arg SD_TRANSFER_BUSY: Data transfer is acting
  243. */
  244. uint8_t BSP_SD_GetCardState(void)
  245. {
  246. return ((HAL_SD_GetCardState(&hsd) == HAL_SD_CARD_TRANSFER ) ? SD_TRANSFER_OK : SD_TRANSFER_BUSY);
  247. }
  248. /**
  249. * @brief Get SD information about specific SD card.
  250. * @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure
  251. * @retval None
  252. */
  253. void BSP_SD_GetCardInfo(HAL_SD_CardInfoTypeDef *CardInfo)
  254. {
  255. /* Get SD card Information */
  256. HAL_SD_GetCardInfo(&hsd, CardInfo);
  257. }
  258. /* USER CODE BEGIN BeforeCallBacksSection */
  259. /* can be used to modify previous code / undefine following code / add code */
  260. /* USER CODE END BeforeCallBacksSection */
  261. /**
  262. * @brief SD Abort callbacks
  263. * @param hsd: SD handle
  264. * @retval None
  265. */
  266. void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
  267. {
  268. BSP_SD_AbortCallback();
  269. }
  270. /**
  271. * @brief Tx Transfer completed callback
  272. * @param hsd: SD handle
  273. * @retval None
  274. */
  275. void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
  276. {
  277. BSP_SD_WriteCpltCallback();
  278. }
  279. /**
  280. * @brief Rx Transfer completed callback
  281. * @param hsd: SD handle
  282. * @retval None
  283. */
  284. void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
  285. {
  286. BSP_SD_ReadCpltCallback();
  287. }
  288. /* USER CODE BEGIN CallBacksSection_C */
  289. /**
  290. * @brief BSP SD Abort callback
  291. * @retval None
  292. */
  293. __weak void BSP_SD_AbortCallback(void)
  294. {
  295. }
  296. /**
  297. * @brief BSP Tx Transfer completed callback
  298. * @retval None
  299. */
  300. __weak void BSP_SD_WriteCpltCallback(void)
  301. {
  302. }
  303. /**
  304. * @brief BSP Rx Transfer completed callback
  305. * @retval None
  306. */
  307. __weak void BSP_SD_ReadCpltCallback(void)
  308. {
  309. }
  310. /* USER CODE END CallBacksSection_C */
  311. #endif
  312. /**
  313. * @brief Detects if SD card is correctly plugged in the memory slot or not.
  314. * @param None
  315. * @retval Returns if SD is detected or not
  316. */
  317. uint8_t BSP_SD_IsDetected(void)
  318. {
  319. __IO uint8_t status = SD_PRESENT;
  320. /* USER CODE BEGIN 1 */
  321. /* user code can be inserted here */
  322. /* USER CODE END 1 */
  323. return status;
  324. }
  325. /* USER CODE BEGIN AdditionalCode */
  326. /* user code can be inserted here */
  327. /* USER CODE END AdditionalCode */
  328. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/