stm32f7xx_hal_dma_ex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_dma_ex.c
  4. * @author MCD Application Team
  5. * @brief DMA Extension HAL module driver
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the DMA Extension peripheral:
  8. * + Extended features functions
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### How to use this driver #####
  13. ==============================================================================
  14. [..]
  15. The DMA Extension HAL driver can be used as follows:
  16. (+) Start a multi buffer transfer using the HAL_DMA_MultiBufferStart() function
  17. for polling mode or HAL_DMA_MultiBufferStart_IT() for interrupt mode.
  18. -@- In Memory-to-Memory transfer mode, Multi (Double) Buffer mode is not allowed.
  19. -@- When Multi (Double) Buffer mode is enabled, the transfer is circular by default.
  20. -@- In Multi (Double) buffer mode, it is possible to update the base address for
  21. the AHB memory port on the fly (DMA_SxM0AR or DMA_SxM1AR) when the stream is enabled.
  22. @endverbatim
  23. ******************************************************************************
  24. * @attention
  25. *
  26. * Copyright (c) 2017 STMicroelectronics.
  27. * All rights reserved.
  28. *
  29. * This software is licensed under terms that can be found in the LICENSE file in
  30. * the root directory of this software component.
  31. * If no LICENSE file comes with this software, it is provided AS-IS.
  32. *
  33. ******************************************************************************
  34. */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "stm32f7xx_hal.h"
  37. /** @addtogroup STM32F7xx_HAL_Driver
  38. * @{
  39. */
  40. /** @defgroup DMAEx DMAEx
  41. * @brief DMA Extended HAL module driver
  42. * @{
  43. */
  44. #ifdef HAL_DMA_MODULE_ENABLED
  45. /* Private types -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private Constants ---------------------------------------------------------*/
  48. /* Private macros ------------------------------------------------------------*/
  49. /* Private functions ---------------------------------------------------------*/
  50. /** @addtogroup DMAEx_Private_Functions
  51. * @{
  52. */
  53. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  54. /**
  55. * @}
  56. */
  57. /* Exported functions ---------------------------------------------------------*/
  58. /** @addtogroup DMAEx_Exported_Functions
  59. * @{
  60. */
  61. /** @addtogroup DMAEx_Exported_Functions_Group1
  62. *
  63. @verbatim
  64. ===============================================================================
  65. ##### Extended features functions #####
  66. ===============================================================================
  67. [..] This section provides functions allowing to:
  68. (+) Configure the source, destination address and data length and
  69. Start MultiBuffer DMA transfer
  70. (+) Configure the source, destination address and data length and
  71. Start MultiBuffer DMA transfer with interrupt
  72. (+) Change on the fly the memory0 or memory1 address.
  73. @endverbatim
  74. * @{
  75. */
  76. /**
  77. * @brief Starts the multi_buffer DMA Transfer.
  78. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  79. * the configuration information for the specified DMA Stream.
  80. * @param SrcAddress The source memory Buffer address
  81. * @param DstAddress The destination memory Buffer address
  82. * @param SecondMemAddress The second memory Buffer address in case of multi buffer Transfer
  83. * @param DataLength The length of data to be transferred from source to destination
  84. * @retval HAL status
  85. */
  86. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  87. {
  88. HAL_StatusTypeDef status = HAL_OK;
  89. /* Check the parameters */
  90. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  91. /* Memory-to-memory transfer not supported in double buffering mode */
  92. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  93. {
  94. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  95. status = HAL_ERROR;
  96. }
  97. else
  98. {
  99. /* Process Locked */
  100. __HAL_LOCK(hdma);
  101. if(HAL_DMA_STATE_READY == hdma->State)
  102. {
  103. /* Change DMA peripheral state */
  104. hdma->State = HAL_DMA_STATE_BUSY;
  105. /* Enable the double buffer mode */
  106. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  107. /* Configure DMA Stream destination address */
  108. hdma->Instance->M1AR = SecondMemAddress;
  109. /* Configure the source, destination address and the data length */
  110. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  111. /* Enable the peripheral */
  112. __HAL_DMA_ENABLE(hdma);
  113. }
  114. else
  115. {
  116. /* Return error status */
  117. status = HAL_BUSY;
  118. }
  119. }
  120. return status;
  121. }
  122. /**
  123. * @brief Starts the multi_buffer DMA Transfer with interrupt enabled.
  124. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  125. * the configuration information for the specified DMA Stream.
  126. * @param SrcAddress The source memory Buffer address
  127. * @param DstAddress The destination memory Buffer address
  128. * @param SecondMemAddress The second memory Buffer address in case of multi buffer Transfer
  129. * @param DataLength The length of data to be transferred from source to destination
  130. * @retval HAL status
  131. */
  132. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  133. {
  134. HAL_StatusTypeDef status = HAL_OK;
  135. /* Check the parameters */
  136. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  137. /* Memory-to-memory transfer not supported in double buffering mode */
  138. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  139. {
  140. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  141. return HAL_ERROR;
  142. }
  143. /* Process locked */
  144. __HAL_LOCK(hdma);
  145. if(HAL_DMA_STATE_READY == hdma->State)
  146. {
  147. /* Change DMA peripheral state */
  148. hdma->State = HAL_DMA_STATE_BUSY;
  149. /* Initialize the error code */
  150. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  151. /* Enable the Double buffer mode */
  152. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  153. /* Configure DMA Stream destination address */
  154. hdma->Instance->M1AR = SecondMemAddress;
  155. /* Configure the source, destination address and the data length */
  156. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  157. /* Clear all flags */
  158. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
  159. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
  160. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
  161. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_DME_FLAG_INDEX(hdma));
  162. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_FE_FLAG_INDEX(hdma));
  163. /* Enable Common interrupts*/
  164. hdma->Instance->CR |= DMA_IT_TC | DMA_IT_TE | DMA_IT_DME;
  165. hdma->Instance->FCR |= DMA_IT_FE;
  166. if((hdma->XferHalfCpltCallback != NULL) || (hdma->XferM1HalfCpltCallback != NULL))
  167. {
  168. hdma->Instance->CR |= DMA_IT_HT;
  169. }
  170. /* Enable the peripheral */
  171. __HAL_DMA_ENABLE(hdma);
  172. }
  173. else
  174. {
  175. /* Process unlocked */
  176. __HAL_UNLOCK(hdma);
  177. /* Return error status */
  178. status = HAL_BUSY;
  179. }
  180. return status;
  181. }
  182. /**
  183. * @brief Change the memory0 or memory1 address on the fly.
  184. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  185. * the configuration information for the specified DMA Stream.
  186. * @param Address The new address
  187. * @param memory the memory to be changed, This parameter can be one of
  188. * the following values:
  189. * MEMORY0 /
  190. * MEMORY1
  191. * @note The MEMORY0 address can be changed only when the current transfer use
  192. * MEMORY1 and the MEMORY1 address can be changed only when the current
  193. * transfer use MEMORY0.
  194. * @retval HAL status
  195. */
  196. HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory)
  197. {
  198. if(memory == MEMORY0)
  199. {
  200. /* change the memory0 address */
  201. hdma->Instance->M0AR = Address;
  202. }
  203. else
  204. {
  205. /* change the memory1 address */
  206. hdma->Instance->M1AR = Address;
  207. }
  208. return HAL_OK;
  209. }
  210. /**
  211. * @}
  212. */
  213. /**
  214. * @}
  215. */
  216. /** @addtogroup DMAEx_Private_Functions
  217. * @{
  218. */
  219. /**
  220. * @brief Set the DMA Transfer parameter.
  221. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  222. * the configuration information for the specified DMA Stream.
  223. * @param SrcAddress The source memory Buffer address
  224. * @param DstAddress The destination memory Buffer address
  225. * @param DataLength The length of data to be transferred from source to destination
  226. * @retval HAL status
  227. */
  228. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  229. {
  230. /* Configure DMA Stream data length */
  231. hdma->Instance->NDTR = DataLength;
  232. /* Peripheral to Memory */
  233. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  234. {
  235. /* Configure DMA Stream destination address */
  236. hdma->Instance->PAR = DstAddress;
  237. /* Configure DMA Stream source address */
  238. hdma->Instance->M0AR = SrcAddress;
  239. }
  240. /* Memory to Peripheral */
  241. else
  242. {
  243. /* Configure DMA Stream source address */
  244. hdma->Instance->PAR = SrcAddress;
  245. /* Configure DMA Stream destination address */
  246. hdma->Instance->M0AR = DstAddress;
  247. }
  248. }
  249. /**
  250. * @}
  251. */
  252. #endif /* HAL_DMA_MODULE_ENABLED */
  253. /**
  254. * @}
  255. */
  256. /**
  257. * @}
  258. */