stm32l4xx_hal_sd_ex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_sd_ex.c
  4. * @author MCD Application Team
  5. * @brief SD card Extended HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Secure Digital (SD) peripheral:
  8. * + Extended features functions
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2017 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### How to use this driver #####
  24. ==============================================================================
  25. [..]
  26. The SD Extension HAL driver can be used as follows:
  27. (+) Set card in High Speed mode using HAL_SDEx_HighSpeed() function.
  28. (+) Configure Buffer0 and Buffer1 start address and Buffer size using HAL_SDEx_ConfigDMAMultiBuffer() function.
  29. (+) Start Read and Write for multibuffer mode using HAL_SDEx_ReadBlocksDMAMultiBuffer() and HAL_SDEx_WriteBlocksDMAMultiBuffer() functions.
  30. @endverbatim
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32l4xx_hal.h"
  35. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  36. /** @addtogroup STM32L4xx_HAL_Driver
  37. * @{
  38. */
  39. /** @defgroup SDEx SDEx
  40. * @brief SD Extended HAL module driver
  41. * @{
  42. */
  43. #ifdef HAL_SD_MODULE_ENABLED
  44. /* Private typedef -----------------------------------------------------------*/
  45. /* Private define ------------------------------------------------------------*/
  46. /* Private macro -------------------------------------------------------------*/
  47. /* Private variables ---------------------------------------------------------*/
  48. /* Private function prototypes -----------------------------------------------*/
  49. /* Private functions ---------------------------------------------------------*/
  50. extern uint32_t SD_HighSpeed(SD_HandleTypeDef *hsd);
  51. /* Exported functions --------------------------------------------------------*/
  52. /** @addtogroup SDEx_Exported_Functions
  53. * @{
  54. */
  55. /** @addtogroup SDEx_Exported_Functions_Group1
  56. * @brief High Speed function
  57. *
  58. @verbatim
  59. ==============================================================================
  60. ##### High Speed function #####
  61. ==============================================================================
  62. [..]
  63. This section provides function allowing to configure the card in High Speed mode.
  64. @endverbatim
  65. * @{
  66. */
  67. /**
  68. * @brief Switches the SD card to High Speed mode.
  69. * This API must be used after "Transfer State"
  70. * @note This operation should be followed by the configuration
  71. * of PLL to have SDMMCCK clock between 50 and 120 MHz
  72. * @param hsd SD handle
  73. * @retval SD Card error state
  74. */
  75. uint32_t HAL_SDEx_HighSpeed(SD_HandleTypeDef *hsd)
  76. {
  77. return SD_HighSpeed (hsd);
  78. }
  79. /**
  80. * @brief Enable/Disable the SD Transceiver 1.8V Mode Callback.
  81. * @param status Voltage Switch State
  82. * @retval None
  83. */
  84. __weak void HAL_SDEx_DriveTransceiver_1_8V_Callback(FlagStatus status)
  85. {
  86. /* Prevent unused argument(s) compilation warning */
  87. UNUSED(status);
  88. /* NOTE : This function Should not be modified, when the callback is needed,
  89. the HAL_SD_EnableTransciver could be implemented in the user file
  90. */
  91. }
  92. /**
  93. * @}
  94. */
  95. /** @addtogroup SDEx_Exported_Functions_Group2
  96. * @brief Multibuffer functions
  97. *
  98. @verbatim
  99. ==============================================================================
  100. ##### Multibuffer functions #####
  101. ==============================================================================
  102. [..]
  103. This section provides functions allowing to configure the multibuffer mode and start read and write
  104. multibuffer mode for SD HAL driver.
  105. @endverbatim
  106. * @{
  107. */
  108. /**
  109. * @brief Configure DMA Dual Buffer mode. The Data transfer is managed by an Internal DMA.
  110. * @param hsd SD handle
  111. * @param pDataBuffer0 Pointer to the buffer0 that will contain/receive the transferred data
  112. * @param pDataBuffer1 Pointer to the buffer1 that will contain/receive the transferred data
  113. * @param BufferSize Size of Buffer0 in Blocks. Buffer0 and Buffer1 must have the same size.
  114. * @retval HAL status
  115. */
  116. HAL_StatusTypeDef HAL_SDEx_ConfigDMAMultiBuffer(SD_HandleTypeDef *hsd, uint32_t *pDataBuffer0, uint32_t *pDataBuffer1, uint32_t BufferSize)
  117. {
  118. if(hsd->State == HAL_SD_STATE_READY)
  119. {
  120. hsd->Instance->IDMABASE0= (uint32_t) pDataBuffer0;
  121. hsd->Instance->IDMABASE1= (uint32_t) pDataBuffer1;
  122. hsd->Instance->IDMABSIZE= (uint32_t) (BLOCKSIZE * BufferSize);
  123. return HAL_OK;
  124. }
  125. else
  126. {
  127. return HAL_BUSY;
  128. }
  129. }
  130. /**
  131. * @brief Reads block(s) from a specified address in a card. The received Data will be stored in Buffer0 and Buffer1.
  132. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_SDEx_ConfigDMAMultiBuffer before call this function.
  133. * @param hsd SD handle
  134. * @param BlockAdd Block Address from where data is to be read
  135. * @param NumberOfBlocks Total number of blocks to read
  136. * @retval HAL status
  137. */
  138. HAL_StatusTypeDef HAL_SDEx_ReadBlocksDMAMultiBuffer(SD_HandleTypeDef *hsd, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  139. {
  140. SDMMC_DataInitTypeDef config;
  141. uint32_t errorstate;
  142. uint32_t DmaBase0_reg, DmaBase1_reg;
  143. uint32_t add = BlockAdd;
  144. if(hsd->State == HAL_SD_STATE_READY)
  145. {
  146. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  147. {
  148. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  149. return HAL_ERROR;
  150. }
  151. DmaBase0_reg = hsd->Instance->IDMABASE0;
  152. DmaBase1_reg = hsd->Instance->IDMABASE1;
  153. if ((hsd->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
  154. {
  155. hsd->ErrorCode = HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  156. return HAL_ERROR;
  157. }
  158. /* Initialize data control register */
  159. hsd->Instance->DCTRL = 0;
  160. /* Clear old Flags*/
  161. __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
  162. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  163. hsd->State = HAL_SD_STATE_BUSY;
  164. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  165. {
  166. add *= 512U;
  167. }
  168. /* Configure the SD DPSM (Data Path State Machine) */
  169. config.DataTimeOut = SDMMC_DATATIMEOUT;
  170. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  171. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  172. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  173. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  174. config.DPSM = SDMMC_DPSM_DISABLE;
  175. (void)SDMMC_ConfigData(hsd->Instance, &config);
  176. hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
  177. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  178. hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  179. /* Read Blocks in DMA mode */
  180. hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  181. /* Read Multi Block command */
  182. errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
  183. if(errorstate != HAL_SD_ERROR_NONE)
  184. {
  185. hsd->State = HAL_SD_STATE_READY;
  186. hsd->ErrorCode |= errorstate;
  187. return HAL_ERROR;
  188. }
  189. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_IT_IDMABTC));
  190. return HAL_OK;
  191. }
  192. else
  193. {
  194. return HAL_BUSY;
  195. }
  196. }
  197. /**
  198. * @brief Write block(s) to a specified address in a card. The transferred Data are stored in Buffer0 and Buffer1.
  199. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_SDEx_ConfigDMAMultiBuffer before call this function.
  200. * @param hsd SD handle
  201. * @param BlockAdd Block Address from where data is to be read
  202. * @param NumberOfBlocks Total number of blocks to read
  203. * @retval HAL status
  204. */
  205. HAL_StatusTypeDef HAL_SDEx_WriteBlocksDMAMultiBuffer(SD_HandleTypeDef *hsd, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  206. {
  207. SDMMC_DataInitTypeDef config;
  208. uint32_t errorstate;
  209. uint32_t DmaBase0_reg, DmaBase1_reg;
  210. uint32_t add = BlockAdd;
  211. if(hsd->State == HAL_SD_STATE_READY)
  212. {
  213. if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  214. {
  215. hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  216. return HAL_ERROR;
  217. }
  218. DmaBase0_reg = hsd->Instance->IDMABASE0;
  219. DmaBase1_reg = hsd->Instance->IDMABASE1;
  220. if ((hsd->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
  221. {
  222. hsd->ErrorCode = HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  223. return HAL_ERROR;
  224. }
  225. /* Initialize data control register */
  226. hsd->Instance->DCTRL = 0;
  227. hsd->ErrorCode = HAL_SD_ERROR_NONE;
  228. hsd->State = HAL_SD_STATE_BUSY;
  229. if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  230. {
  231. add *= 512U;
  232. }
  233. /* Configure the SD DPSM (Data Path State Machine) */
  234. config.DataTimeOut = SDMMC_DATATIMEOUT;
  235. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  236. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  237. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  238. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  239. config.DPSM = SDMMC_DPSM_DISABLE;
  240. (void)SDMMC_ConfigData(hsd->Instance, &config);
  241. __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
  242. hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  243. /* Write Blocks in DMA mode */
  244. hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  245. /* Write Multi Block command */
  246. errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
  247. if(errorstate != HAL_SD_ERROR_NONE)
  248. {
  249. hsd->State = HAL_SD_STATE_READY;
  250. hsd->ErrorCode |= errorstate;
  251. return HAL_ERROR;
  252. }
  253. __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_IT_IDMABTC));
  254. return HAL_OK;
  255. }
  256. else
  257. {
  258. return HAL_BUSY;
  259. }
  260. }
  261. /**
  262. * @brief Change the DMA Buffer0 or Buffer1 address on the fly.
  263. * @param hsd pointer to a SD_HandleTypeDef structure.
  264. * @param Buffer the buffer to be changed, This parameter can be one of
  265. * the following values: SD_DMA_BUFFER0 or SD_DMA_BUFFER1
  266. * @param pDataBuffer The new address
  267. * @note The BUFFER0 address can be changed only when the current transfer use
  268. * BUFFER1 and the BUFFER1 address can be changed only when the current
  269. * transfer use BUFFER0.
  270. * @retval HAL status
  271. */
  272. HAL_StatusTypeDef HAL_SDEx_ChangeDMABuffer(SD_HandleTypeDef *hsd, HAL_SDEx_DMABuffer_MemoryTypeDef Buffer, uint32_t *pDataBuffer)
  273. {
  274. if(Buffer == SD_DMA_BUFFER0)
  275. {
  276. /* change the buffer0 address */
  277. hsd->Instance->IDMABASE0 = (uint32_t)pDataBuffer;
  278. }
  279. else
  280. {
  281. /* change the memory1 address */
  282. hsd->Instance->IDMABASE1 = (uint32_t)pDataBuffer;
  283. }
  284. return HAL_OK;
  285. }
  286. /**
  287. * @brief Read DMA Buffer 0 Transfer completed callbacks
  288. * @param hsd: SD handle
  289. * @retval None
  290. */
  291. __weak void HAL_SDEx_Read_DMADoubleBuffer0CpltCallback(SD_HandleTypeDef *hsd)
  292. {
  293. /* Prevent unused argument(s) compilation warning */
  294. UNUSED(hsd);
  295. /* NOTE : This function should not be modified, when the callback is needed,
  296. the HAL_SDEx_Read_DMADoubleBuffer0CpltCallback can be implemented in the user file
  297. */
  298. }
  299. /**
  300. * @brief Read DMA Buffer 1 Transfer completed callbacks
  301. * @param hsd: SD handle
  302. * @retval None
  303. */
  304. __weak void HAL_SDEx_Read_DMADoubleBuffer1CpltCallback(SD_HandleTypeDef *hsd)
  305. {
  306. /* Prevent unused argument(s) compilation warning */
  307. UNUSED(hsd);
  308. /* NOTE : This function should not be modified, when the callback is needed,
  309. the HAL_SDEx_Read_DMADoubleBuffer1CpltCallback can be implemented in the user file
  310. */
  311. }
  312. /**
  313. * @brief Write DMA Buffer 0 Transfer completed callbacks
  314. * @param hsd: SD handle
  315. * @retval None
  316. */
  317. __weak void HAL_SDEx_Write_DMADoubleBuffer0CpltCallback(SD_HandleTypeDef *hsd)
  318. {
  319. /* Prevent unused argument(s) compilation warning */
  320. UNUSED(hsd);
  321. /* NOTE : This function should not be modified, when the callback is needed,
  322. the HAL_SDEx_Write_DMADoubleBuffer0CpltCallback can be implemented in the user file
  323. */
  324. }
  325. /**
  326. * @brief Write DMA Buffer 1 Transfer completed callbacks
  327. * @param hsd: SD handle
  328. * @retval None
  329. */
  330. __weak void HAL_SDEx_Write_DMADoubleBuffer1CpltCallback(SD_HandleTypeDef *hsd)
  331. {
  332. /* Prevent unused argument(s) compilation warning */
  333. UNUSED(hsd);
  334. /* NOTE : This function should not be modified, when the callback is needed,
  335. the HAL_SDEx_Write_DMADoubleBuffer0CpltCallback can be implemented in the user file
  336. */
  337. }
  338. /**
  339. * @}
  340. */
  341. /**
  342. * @}
  343. */
  344. #endif /* HAL_SD_MODULE_ENABLED */
  345. /**
  346. * @}
  347. */
  348. /**
  349. * @}
  350. */
  351. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */