stm32l4xx_hal_mmc_ex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_mmc_ex.c
  4. * @author MCD Application Team
  5. * @brief MMC card Extended HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Secure Digital (MMC) 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 MMC Extension HAL driver can be used as follows:
  27. (+) Configure Buffer0 and Buffer1 start address and Buffer size using HAL_MMCEx_ConfigDMAMultiBuffer() function.
  28. (+) Start Read and Write for multibuffer mode using HAL_MMCEx_ReadBlocksDMAMultiBuffer() and HAL_MMCEx_WriteBlocksDMAMultiBuffer() functions.
  29. @endverbatim
  30. ******************************************************************************
  31. */
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "stm32l4xx_hal.h"
  34. #if defined(STM32L4P5xx) || defined(STM32L4Q5xx) || defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  35. /** @addtogroup STM32L4xx_HAL_Driver
  36. * @{
  37. */
  38. /** @defgroup MMCEx MMCEx
  39. * @brief MMC Extended HAL module driver
  40. * @{
  41. */
  42. #ifdef HAL_MMC_MODULE_ENABLED
  43. /* Private typedef -----------------------------------------------------------*/
  44. /* Private define ------------------------------------------------------------*/
  45. /* Private macro -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private function prototypes -----------------------------------------------*/
  48. /* Private functions ---------------------------------------------------------*/
  49. /* Exported functions --------------------------------------------------------*/
  50. /** @defgroup MMCEx_Exported_Types MMCEx Exported Types
  51. * @{
  52. */
  53. /** @defgroup MMCEx_Exported_Types_Group1 MMC Internal DMA Buffer structure
  54. * @brief Multibuffer functions
  55. *
  56. @verbatim
  57. ==============================================================================
  58. ##### Multibuffer functions #####
  59. ==============================================================================
  60. [..]
  61. This section provides functions allowing to configure the multibuffer mode and start read and write
  62. multibuffer mode for MMC HAL driver.
  63. @endverbatim
  64. * @{
  65. */
  66. /**
  67. * @brief Configure DMA Dual Buffer mode. The Data transfer is managed by an Internal DMA.
  68. * @param hmmc MMC handle
  69. * @param pDataBuffer0 Pointer to the buffer0 that will contain/receive the transferred data
  70. * @param pDataBuffer1 Pointer to the buffer1 that will contain/receive the transferred data
  71. * @param BufferSize Size of Buffer0 in Blocks. Buffer0 and Buffer1 must have the same size.
  72. * @retval HAL status
  73. */
  74. HAL_StatusTypeDef HAL_MMCEx_ConfigDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t * pDataBuffer0, uint32_t * pDataBuffer1, uint32_t BufferSize)
  75. {
  76. if(hmmc->State == HAL_MMC_STATE_READY)
  77. {
  78. hmmc->Instance->IDMABASE0= (uint32_t) pDataBuffer0 ;
  79. hmmc->Instance->IDMABASE1= (uint32_t) pDataBuffer1 ;
  80. hmmc->Instance->IDMABSIZE= (uint32_t) (MMC_BLOCKSIZE * BufferSize);
  81. return HAL_OK;
  82. }
  83. else
  84. {
  85. return HAL_BUSY;
  86. }
  87. }
  88. /**
  89. * @brief Reads block(s) from a specified address in a card. The received Data will be stored in Buffer0 and Buffer1.
  90. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_MMCEx_ConfigDMAMultiBuffer before call this function.
  91. * @param hmmc MMC handle
  92. * @param BlockAdd Block Address from where data is to be read
  93. * @param NumberOfBlocks Total number of blocks to read
  94. * @retval HAL status
  95. */
  96. HAL_StatusTypeDef HAL_MMCEx_ReadBlocksDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  97. {
  98. SDMMC_DataInitTypeDef config;
  99. uint32_t DmaBase0_reg, DmaBase1_reg;
  100. uint32_t errorstate;
  101. uint32_t add = BlockAdd;
  102. if(hmmc->State == HAL_MMC_STATE_READY)
  103. {
  104. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  105. {
  106. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  107. return HAL_ERROR;
  108. }
  109. DmaBase0_reg = hmmc->Instance->IDMABASE0;
  110. DmaBase1_reg = hmmc->Instance->IDMABASE1;
  111. if ((hmmc->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
  112. {
  113. hmmc->ErrorCode = HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  114. return HAL_ERROR;
  115. }
  116. /* Initialize data control register */
  117. hmmc->Instance->DCTRL = 0;
  118. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  119. hmmc->State = HAL_MMC_STATE_BUSY;
  120. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  121. {
  122. add *= 512U;
  123. }
  124. /* Configure the MMC DPSM (Data Path State Machine) */
  125. config.DataTimeOut = SDMMC_DATATIMEOUT;
  126. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  127. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  128. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  129. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  130. config.DPSM = SDMMC_DPSM_DISABLE;
  131. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  132. hmmc->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
  133. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  134. hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  135. /* Read Blocks in DMA mode */
  136. hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  137. /* Read Multi Block command */
  138. errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
  139. if(errorstate != HAL_MMC_ERROR_NONE)
  140. {
  141. hmmc->State = HAL_MMC_STATE_READY;
  142. hmmc->ErrorCode |= errorstate;
  143. return HAL_ERROR;
  144. }
  145. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_IDMATE | SDMMC_FLAG_IDMABTC));
  146. return HAL_OK;
  147. }
  148. else
  149. {
  150. return HAL_BUSY;
  151. }
  152. }
  153. /**
  154. * @brief Write block(s) to a specified address in a card. The transferred Data are stored in Buffer0 and Buffer1.
  155. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_MMCEx_ConfigDMAMultiBuffer before call this function.
  156. * @param hmmc MMC handle
  157. * @param BlockAdd Block Address from where data is to be read
  158. * @param NumberOfBlocks Total number of blocks to read
  159. * @retval HAL status
  160. */
  161. HAL_StatusTypeDef HAL_MMCEx_WriteBlocksDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  162. {
  163. SDMMC_DataInitTypeDef config;
  164. uint32_t errorstate;
  165. uint32_t DmaBase0_reg, DmaBase1_reg;
  166. uint32_t add = BlockAdd;
  167. if(hmmc->State == HAL_MMC_STATE_READY)
  168. {
  169. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  170. {
  171. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  172. return HAL_ERROR;
  173. }
  174. DmaBase0_reg = hmmc->Instance->IDMABASE0;
  175. DmaBase1_reg = hmmc->Instance->IDMABASE1;
  176. if ((hmmc->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
  177. {
  178. hmmc->ErrorCode = HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  179. return HAL_ERROR;
  180. }
  181. /* Initialize data control register */
  182. hmmc->Instance->DCTRL = 0;
  183. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  184. hmmc->State = HAL_MMC_STATE_BUSY;
  185. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  186. {
  187. add *= 512U;
  188. }
  189. /* Configure the MMC DPSM (Data Path State Machine) */
  190. config.DataTimeOut = SDMMC_DATATIMEOUT;
  191. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  192. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  193. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  194. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  195. config.DPSM = SDMMC_DPSM_DISABLE;
  196. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  197. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  198. hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  199. /* Write Blocks in DMA mode */
  200. hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  201. /* Write Multi Block command */
  202. errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
  203. if(errorstate != HAL_MMC_ERROR_NONE)
  204. {
  205. hmmc->State = HAL_MMC_STATE_READY;
  206. hmmc->ErrorCode |= errorstate;
  207. return HAL_ERROR;
  208. }
  209. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_IDMATE | SDMMC_FLAG_IDMABTC));
  210. return HAL_OK;
  211. }
  212. else
  213. {
  214. return HAL_BUSY;
  215. }
  216. }
  217. /**
  218. * @brief Change the DMA Buffer0 or Buffer1 address on the fly.
  219. * @param hmmc pointer to a MMC_HandleTypeDef structure.
  220. * @param Buffer the buffer to be changed, This parameter can be one of
  221. * the following values: MMC_DMA_BUFFER0 or MMC_DMA_BUFFER1
  222. * @param pDataBuffer The new address
  223. * @note The BUFFER0 address can be changed only when the current transfer use
  224. * BUFFER1 and the BUFFER1 address can be changed only when the current
  225. * transfer use BUFFER0.
  226. * @retval HAL status
  227. */
  228. HAL_StatusTypeDef HAL_MMCEx_ChangeDMABuffer(MMC_HandleTypeDef *hmmc, HAL_MMCEx_DMABuffer_MemoryTypeDef Buffer, uint32_t *pDataBuffer)
  229. {
  230. if(Buffer == MMC_DMA_BUFFER0)
  231. {
  232. /* change the buffer0 address */
  233. hmmc->Instance->IDMABASE0 = (uint32_t)pDataBuffer;
  234. }
  235. else
  236. {
  237. /* change the memory1 address */
  238. hmmc->Instance->IDMABASE1 = (uint32_t)pDataBuffer;
  239. }
  240. return HAL_OK;
  241. }
  242. /**
  243. * @brief Read DMA Buffer 0 Transfer completed callbacks
  244. * @param hmmc MMC handle
  245. * @retval None
  246. */
  247. __weak void HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
  248. {
  249. /* Prevent unused argument(s) compilation warning */
  250. UNUSED(hmmc);
  251. /* NOTE : This function should not be modified, when the callback is needed,
  252. the HAL_MMCEx_Read_DMADoubleBuffer0CpltCallback can be implemented in the user file
  253. */
  254. }
  255. /**
  256. * @brief Read DMA Buffer 1 Transfer completed callbacks
  257. * @param hmmc MMC handle
  258. * @retval None
  259. */
  260. __weak void HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
  261. {
  262. /* Prevent unused argument(s) compilation warning */
  263. UNUSED(hmmc);
  264. /* NOTE : This function should not be modified, when the callback is needed,
  265. the HAL_MMCEx_Read_DMADoubleBuffer1CpltCallback can be implemented in the user file
  266. */
  267. }
  268. /**
  269. * @brief Write DMA Buffer 0 Transfer completed callbacks
  270. * @param hmmc MMC handle
  271. * @retval None
  272. */
  273. __weak void HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback(MMC_HandleTypeDef *hmmc)
  274. {
  275. /* Prevent unused argument(s) compilation warning */
  276. UNUSED(hmmc);
  277. /* NOTE : This function should not be modified, when the callback is needed,
  278. the HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback can be implemented in the user file
  279. */
  280. }
  281. /**
  282. * @brief Write DMA Buffer 1 Transfer completed callbacks
  283. * @param hmmc MMC handle
  284. * @retval None
  285. */
  286. __weak void HAL_MMCEx_Write_DMADoubleBuffer1CpltCallback(MMC_HandleTypeDef *hmmc)
  287. {
  288. /* Prevent unused argument(s) compilation warning */
  289. UNUSED(hmmc);
  290. /* NOTE : This function should not be modified, when the callback is needed,
  291. the HAL_MMCEx_Write_DMADoubleBuffer0CpltCallback can be implemented in the user file
  292. */
  293. }
  294. /**
  295. * @}
  296. */
  297. /**
  298. * @}
  299. */
  300. #endif /* HAL_MMC_MODULE_ENABLED */
  301. /**
  302. * @}
  303. */
  304. /**
  305. * @}
  306. */
  307. #endif /* STM32L4P5xx || STM32L4Q5xx || STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */