You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

305 lines
9.8 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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. @verbatim
  11. ==============================================================================
  12. ##### How to use this driver #####
  13. ==============================================================================
  14. [..]
  15. The MMC Extension HAL driver can be used as follows:
  16. (+) Configure Buffer0 and Buffer1 start address and Buffer size using HAL_MMCEx_ConfigDMAMultiBuffer() function.
  17. (+) Start Read and Write for multibuffer mode using HAL_MMCEx_ReadBlocksDMAMultiBuffer() and HAL_MMCEx_WriteBlocksDMAMultiBuffer() functions.
  18. @endverbatim
  19. ******************************************************************************
  20. * @attention
  21. *
  22. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  23. * All rights reserved.</center></h2>
  24. *
  25. * This software component is licensed by ST under BSD 3-Clause license,
  26. * the "License"; You may not use this file except in compliance with the
  27. * License. You may obtain a copy of the License at:
  28. * opensource.org/licenses/BSD-3-Clause
  29. *
  30. ******************************************************************************
  31. */
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "stm32h7xx_hal.h"
  34. /** @addtogroup STM32H7xx_HAL_Driver
  35. * @{
  36. */
  37. /** @defgroup MMCEx MMCEx
  38. * @brief MMC Extended HAL module driver
  39. * @{
  40. */
  41. #ifdef HAL_MMC_MODULE_ENABLED
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* Private define ------------------------------------------------------------*/
  44. /* Private macro -------------------------------------------------------------*/
  45. /* Private variables ---------------------------------------------------------*/
  46. /* Private function prototypes -----------------------------------------------*/
  47. /* Private functions ---------------------------------------------------------*/
  48. /* Exported functions --------------------------------------------------------*/
  49. /** @addtogroup MMCEx_Exported_Functions
  50. * @{
  51. */
  52. /** @addtogroup MMCEx_Exported_Functions_Group1
  53. * @brief Multibuffer functions
  54. *
  55. @verbatim
  56. ==============================================================================
  57. ##### Multibuffer functions #####
  58. ==============================================================================
  59. [..]
  60. This section provides functions allowing to configure the multibuffer mode and start read and write
  61. multibuffer mode for MMC HAL driver.
  62. @endverbatim
  63. * @{
  64. */
  65. /**
  66. * @brief Configure DMA Dual Buffer mode. The Data transfer is managed by an Internal DMA.
  67. * @param hmmc: MMC handle
  68. * @param pDataBuffer0: Pointer to the buffer0 that will contain/receive the transfered data
  69. * @param pDataBuffer1: Pointer to the buffer1 that will contain/receive the transfered data
  70. * @param BufferSize: Size of Buffer0 in Blocks. Buffer0 and Buffer1 must have the same size.
  71. * @retval HAL status
  72. */
  73. HAL_StatusTypeDef HAL_MMCEx_ConfigDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t * pDataBuffer0, uint32_t * pDataBuffer1, uint32_t BufferSize)
  74. {
  75. if(hmmc->State == HAL_MMC_STATE_READY)
  76. {
  77. hmmc->Instance->IDMABASE0= (uint32_t) pDataBuffer0 ;
  78. hmmc->Instance->IDMABASE1= (uint32_t) pDataBuffer1 ;
  79. hmmc->Instance->IDMABSIZE= (uint32_t) (MMC_BLOCKSIZE * BufferSize);
  80. return HAL_OK;
  81. }
  82. else
  83. {
  84. return HAL_BUSY;
  85. }
  86. }
  87. /**
  88. * @brief Reads block(s) from a specified address in a card. The received Data will be stored in Buffer0 and Buffer1.
  89. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_MMCEx_ConfigDMAMultiBuffer before call this function.
  90. * @param hmmc: MMC handle
  91. * @param BlockAdd: Block Address from where data is to be read
  92. * @param NumberOfBlocks: Total number of blocks to read
  93. * @retval HAL status
  94. */
  95. HAL_StatusTypeDef HAL_MMCEx_ReadBlocksDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  96. {
  97. SDMMC_DataInitTypeDef config;
  98. uint32_t DmaBase0_reg, DmaBase1_reg;
  99. uint32_t errorstate;
  100. uint32_t add = BlockAdd;
  101. if(hmmc->State == HAL_MMC_STATE_READY)
  102. {
  103. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  104. {
  105. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  106. return HAL_ERROR;
  107. }
  108. DmaBase0_reg = hmmc->Instance->IDMABASE0;
  109. DmaBase1_reg = hmmc->Instance->IDMABASE1;
  110. if ((hmmc->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
  111. {
  112. hmmc->ErrorCode = HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  113. return HAL_ERROR;
  114. }
  115. /* Initialize data control register */
  116. hmmc->Instance->DCTRL = 0;
  117. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  118. hmmc->State = HAL_MMC_STATE_BUSY;
  119. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  120. {
  121. add *= 512U;
  122. }
  123. /* Configure the MMC DPSM (Data Path State Machine) */
  124. config.DataTimeOut = SDMMC_DATATIMEOUT;
  125. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  126. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  127. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  128. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  129. config.DPSM = SDMMC_DPSM_DISABLE;
  130. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  131. hmmc->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
  132. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  133. hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  134. /* Read Blocks in DMA mode */
  135. hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  136. /* Read Multi Block command */
  137. errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, add);
  138. if(errorstate != HAL_MMC_ERROR_NONE)
  139. {
  140. hmmc->State = HAL_MMC_STATE_READY;
  141. hmmc->ErrorCode |= errorstate;
  142. return HAL_ERROR;
  143. }
  144. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_IDMATE | SDMMC_FLAG_IDMABTC));
  145. return HAL_OK;
  146. }
  147. else
  148. {
  149. return HAL_BUSY;
  150. }
  151. }
  152. /**
  153. * @brief Write block(s) to a specified address in a card. The transfered Data are stored in Buffer0 and Buffer1.
  154. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_MMCEx_ConfigDMAMultiBuffer before call this function.
  155. * @param hmmc: MMC handle
  156. * @param BlockAdd: Block Address from where data is to be read
  157. * @param NumberOfBlocks: Total number of blocks to read
  158. * @retval HAL status
  159. */
  160. HAL_StatusTypeDef HAL_MMCEx_WriteBlocksDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  161. {
  162. SDMMC_DataInitTypeDef config;
  163. uint32_t errorstate;
  164. uint32_t DmaBase0_reg, DmaBase1_reg;
  165. uint32_t add = BlockAdd;
  166. if(hmmc->State == HAL_MMC_STATE_READY)
  167. {
  168. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  169. {
  170. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  171. return HAL_ERROR;
  172. }
  173. DmaBase0_reg = hmmc->Instance->IDMABASE0;
  174. DmaBase1_reg = hmmc->Instance->IDMABASE1;
  175. if ((hmmc->Instance->IDMABSIZE == 0U) || (DmaBase0_reg == 0U) || (DmaBase1_reg == 0U))
  176. {
  177. hmmc->ErrorCode = HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  178. return HAL_ERROR;
  179. }
  180. /* Initialize data control register */
  181. hmmc->Instance->DCTRL = 0;
  182. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  183. hmmc->State = HAL_MMC_STATE_BUSY;
  184. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  185. {
  186. add *= 512U;
  187. }
  188. /* Configure the MMC DPSM (Data Path State Machine) */
  189. config.DataTimeOut = SDMMC_DATATIMEOUT;
  190. config.DataLength = MMC_BLOCKSIZE * NumberOfBlocks;
  191. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  192. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  193. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  194. config.DPSM = SDMMC_DPSM_DISABLE;
  195. (void)SDMMC_ConfigData(hmmc->Instance, &config);
  196. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  197. hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  198. /* Write Blocks in DMA mode */
  199. hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  200. /* Write Multi Block command */
  201. errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, add);
  202. if(errorstate != HAL_MMC_ERROR_NONE)
  203. {
  204. hmmc->State = HAL_MMC_STATE_READY;
  205. hmmc->ErrorCode |= errorstate;
  206. return HAL_ERROR;
  207. }
  208. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_IDMATE | SDMMC_FLAG_IDMABTC));
  209. return HAL_OK;
  210. }
  211. else
  212. {
  213. return HAL_BUSY;
  214. }
  215. }
  216. /**
  217. * @brief Change the DMA Buffer0 or Buffer1 address on the fly.
  218. * @param hmmc: pointer to a MMC_HandleTypeDef structure.
  219. * @param Buffer: the buffer to be changed, This parameter can be one of
  220. * the following values: MMC_DMA_BUFFER0 or MMC_DMA_BUFFER1
  221. * @param pDataBuffer: The new address
  222. * @note The BUFFER0 address can be changed only when the current transfer use
  223. * BUFFER1 and the BUFFER1 address can be changed only when the current
  224. * transfer use BUFFER0.
  225. * @retval HAL status
  226. */
  227. HAL_StatusTypeDef HAL_MMCEx_ChangeDMABuffer(MMC_HandleTypeDef *hmmc, HAL_MMCEx_DMABuffer_MemoryTypeDef Buffer, uint32_t *pDataBuffer)
  228. {
  229. if(Buffer == MMC_DMA_BUFFER0)
  230. {
  231. /* change the buffer0 address */
  232. hmmc->Instance->IDMABASE0 = (uint32_t)pDataBuffer;
  233. }
  234. else
  235. {
  236. /* change the memory1 address */
  237. hmmc->Instance->IDMABASE1 = (uint32_t)pDataBuffer;
  238. }
  239. return HAL_OK;
  240. }
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. #endif /* HAL_MMC_MODULE_ENABLED */
  248. /**
  249. * @}
  250. */
  251. /**
  252. * @}
  253. */
  254. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/