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.
 
 
 

314 lines
11 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_mmc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.2.0
  6. * @date 29-December-2017
  7. * @brief MMC card Extended HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Secure Digital (MMC) peripheral:
  10. * + Extended features functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The MMC Extension HAL driver can be used as follows:
  18. (+) Configure Buffer0 and Buffer1 start address and Buffer size using HAL_MMCEx_ConfigDMAMultiBuffer() function.
  19. (+) Start Read and Write for multibuffer mode using HAL_MMCEx_ReadBlocksDMAMultiBuffer() and HAL_MMCEx_WriteBlocksDMAMultiBuffer() functions.
  20. @endverbatim
  21. ******************************************************************************
  22. * @attention
  23. *
  24. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  25. *
  26. * Redistribution and use in source and binary forms, with or without modification,
  27. * are permitted provided that the following conditions are met:
  28. * 1. Redistributions of source code must retain the above copyright notice,
  29. * this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright notice,
  31. * this list of conditions and the following disclaimer in the documentation
  32. * and/or other materials provided with the distribution.
  33. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  34. * may be used to endorse or promote products derived from this software
  35. * without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  38. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  41. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  43. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. ******************************************************************************
  49. */
  50. /* Includes ------------------------------------------------------------------*/
  51. #include "stm32h7xx_hal.h"
  52. /** @addtogroup STM32H7xx_HAL_Driver
  53. * @{
  54. */
  55. /** @defgroup MMCEx MMCEx
  56. * @brief MMC Extended HAL module driver
  57. * @{
  58. */
  59. #ifdef HAL_MMC_MODULE_ENABLED
  60. /* Private typedef -----------------------------------------------------------*/
  61. /* Private define ------------------------------------------------------------*/
  62. /* Private macro -------------------------------------------------------------*/
  63. /* Private variables ---------------------------------------------------------*/
  64. /* Private function prototypes -----------------------------------------------*/
  65. /* Private functions ---------------------------------------------------------*/
  66. /* Exported functions --------------------------------------------------------*/
  67. /** @addtogroup MMCEx_Exported_Functions
  68. * @{
  69. */
  70. /** @addtogroup MMCEx_Exported_Functions_Group1
  71. * @brief Multibuffer functions
  72. *
  73. @verbatim
  74. ==============================================================================
  75. ##### Multibuffer functions #####
  76. ==============================================================================
  77. [..]
  78. This section provides functions allowing to configure the multibuffer mode and start read and write
  79. multibuffer mode for MMC HAL driver.
  80. @endverbatim
  81. * @{
  82. */
  83. /**
  84. * @brief Configure DMA Dual Buffer mode. The Data transfer is managed by an Internal DMA.
  85. * @param hmmc: MMC handle
  86. * @param pDataBuffer0: Pointer to the buffer0 that will contain/receive the transfered data
  87. * @param pDataBuffer1: Pointer to the buffer1 that will contain/receive the transfered data
  88. * @param BufferSize: Size of Buffer0 in Blocks. Buffer0 and Buffer1 must have the same size.
  89. * @retval HAL status
  90. */
  91. HAL_StatusTypeDef HAL_MMCEx_ConfigDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t * pDataBuffer0, uint32_t * pDataBuffer1, uint32_t BufferSize)
  92. {
  93. if(hmmc->State == HAL_MMC_STATE_READY)
  94. {
  95. hmmc->Instance->IDMABASE0= (uint32_t) pDataBuffer0 ;
  96. hmmc->Instance->IDMABASE1= (uint32_t) pDataBuffer1 ;
  97. hmmc->Instance->IDMABSIZE= (uint32_t) BufferSize;
  98. return HAL_OK;
  99. }
  100. else
  101. {
  102. return HAL_BUSY;
  103. }
  104. }
  105. /**
  106. * @brief Reads block(s) from a specified address in a card. The received Data will be stored in Buffer0 and Buffer1.
  107. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_MMCEx_ConfigDMAMultiBuffer before call this function.
  108. * @param hmmc: MMC handle
  109. * @param BlockAdd: Block Address from where data is to be read
  110. * @param NumberOfBlocks: Total number of blocks to read
  111. * @retval HAL status
  112. */
  113. HAL_StatusTypeDef HAL_MMCEx_ReadBlocksDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  114. {
  115. SDMMC_DataInitTypeDef config;
  116. uint32_t errorstate = HAL_MMC_ERROR_NONE;
  117. if(hmmc->State == HAL_MMC_STATE_READY)
  118. {
  119. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  120. {
  121. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  122. return HAL_ERROR;
  123. }
  124. if ((hmmc->Instance->IDMABASE0 == 0) || (hmmc->Instance->IDMABASE1 == 0) || (hmmc->Instance->IDMABSIZE == 0))
  125. {
  126. hmmc->ErrorCode = HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  127. return HAL_ERROR;
  128. }
  129. /* Initialize data control register */
  130. hmmc->Instance->DCTRL = 0;
  131. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  132. hmmc->State = HAL_MMC_STATE_BUSY;
  133. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  134. {
  135. BlockAdd *= 512;
  136. }
  137. /* Configure the MMC DPSM (Data Path State Machine) */
  138. config.DataTimeOut = SDMMC_DATATIMEOUT;
  139. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  140. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  141. config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
  142. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  143. config.DPSM = SDMMC_DPSM_DISABLE;
  144. SDMMC_ConfigData(hmmc->Instance, &config);
  145. hmmc->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
  146. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  147. hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  148. __HAL_MMC_ENABLE_IT(hmmc, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_IDMATE | SDMMC_FLAG_IDMABTC));
  149. /* Read Blocks in DMA mode */
  150. hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  151. /* Read Multi Block command */
  152. errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, BlockAdd);
  153. if(errorstate != HAL_MMC_ERROR_NONE)
  154. {
  155. hmmc->State = HAL_MMC_STATE_READY;
  156. hmmc->ErrorCode |= errorstate;
  157. return HAL_ERROR;
  158. }
  159. return HAL_OK;
  160. }
  161. else
  162. {
  163. return HAL_BUSY;
  164. }
  165. }
  166. /**
  167. * @brief Write block(s) to a specified address in a card. The transfered Data are stored in Buffer0 and Buffer1.
  168. * Buffer0, Buffer1 and BufferSize need to be configured by function HAL_MMCEx_ConfigDMAMultiBuffer before call this function.
  169. * @param hmmc: MMC handle
  170. * @param BlockAdd: Block Address from where data is to be read
  171. * @param NumberOfBlocks: Total number of blocks to read
  172. * @retval HAL status
  173. */
  174. HAL_StatusTypeDef HAL_MMCEx_WriteBlocksDMAMultiBuffer(MMC_HandleTypeDef *hmmc, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  175. {
  176. SDMMC_DataInitTypeDef config;
  177. uint32_t errorstate = HAL_MMC_ERROR_NONE;
  178. if(hmmc->State == HAL_MMC_STATE_READY)
  179. {
  180. if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
  181. {
  182. hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  183. return HAL_ERROR;
  184. }
  185. if ((hmmc->Instance->IDMABASE0 == 0) || (hmmc->Instance->IDMABASE1 == 0) || (hmmc->Instance->IDMABSIZE == 0))
  186. {
  187. hmmc->ErrorCode = HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
  188. return HAL_ERROR;
  189. }
  190. /* Initialize data control register */
  191. hmmc->Instance->DCTRL = 0;
  192. hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
  193. hmmc->State = HAL_MMC_STATE_BUSY;
  194. if ((hmmc->MmcCard.CardType) != MMC_HIGH_CAPACITY_CARD)
  195. {
  196. BlockAdd *= 512;
  197. }
  198. /* Configure the MMC DPSM (Data Path State Machine) */
  199. config.DataTimeOut = SDMMC_DATATIMEOUT;
  200. config.DataLength = BLOCKSIZE * NumberOfBlocks;
  201. config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
  202. config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
  203. config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
  204. config.DPSM = SDMMC_DPSM_DISABLE;
  205. SDMMC_ConfigData(hmmc->Instance, &config);
  206. __SDMMC_CMDTRANS_ENABLE( hmmc->Instance);
  207. hmmc->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_DOUBLE_BUFF0;
  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. /* Write Blocks in DMA mode */
  210. hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
  211. /* Write Multi Block command */
  212. errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, BlockAdd);
  213. if(errorstate != HAL_MMC_ERROR_NONE)
  214. {
  215. hmmc->State = HAL_MMC_STATE_READY;
  216. hmmc->ErrorCode |= errorstate;
  217. return HAL_ERROR;
  218. }
  219. return HAL_OK;
  220. }
  221. else
  222. {
  223. return HAL_BUSY;
  224. }
  225. }
  226. /**
  227. * @brief Change the DMA Buffer0 or Buffer1 address on the fly.
  228. * @param hmmc: pointer to a MMC_HandleTypeDef structure.
  229. * @param Buffer: the buffer to be changed, This parameter can be one of
  230. * the following values: MMC_DMA_BUFFER0 or MMC_DMA_BUFFER1
  231. * @param pDataBuffer: The new address
  232. * @note The BUFFER0 address can be changed only when the current transfer use
  233. * BUFFER1 and the BUFFER1 address can be changed only when the current
  234. * transfer use BUFFER0.
  235. * @retval HAL status
  236. */
  237. HAL_StatusTypeDef HAL_MMCEx_ChangeDMABuffer(MMC_HandleTypeDef *hmmc, HAL_MMCEx_DMABuffer_MemoryTypeDef Buffer, uint32_t *pDataBuffer)
  238. {
  239. if(Buffer == MMC_DMA_BUFFER0)
  240. {
  241. /* change the buffer0 address */
  242. hmmc->Instance->IDMABASE0 = (uint32_t)pDataBuffer;
  243. }
  244. else
  245. {
  246. /* change the memory1 address */
  247. hmmc->Instance->IDMABASE1 = (uint32_t)pDataBuffer;
  248. }
  249. return HAL_OK;
  250. }
  251. /**
  252. * @}
  253. */
  254. /**
  255. * @}
  256. */
  257. #endif /* HAL_MMC_MODULE_ENABLED */
  258. /**
  259. * @}
  260. */
  261. /**
  262. * @}
  263. */
  264. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/