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.

stm32f4xx_hal_dma_ex.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  27. * All rights reserved.</center></h2>
  28. *
  29. * This software component is licensed by ST under BSD 3-Clause license,
  30. * the "License"; You may not use this file except in compliance with the
  31. * License. You may obtain a copy of the License at:
  32. * opensource.org/licenses/BSD-3-Clause
  33. *
  34. ******************************************************************************
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32f4xx_hal.h"
  38. /** @addtogroup STM32F4xx_HAL_Driver
  39. * @{
  40. */
  41. /** @defgroup DMAEx DMAEx
  42. * @brief DMA Extended HAL module driver
  43. * @{
  44. */
  45. #ifdef HAL_DMA_MODULE_ENABLED
  46. /* Private types -------------------------------------------------------------*/
  47. /* Private variables ---------------------------------------------------------*/
  48. /* Private Constants ---------------------------------------------------------*/
  49. /* Private macros ------------------------------------------------------------*/
  50. /* Private functions ---------------------------------------------------------*/
  51. /** @addtogroup DMAEx_Private_Functions
  52. * @{
  53. */
  54. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  55. /**
  56. * @}
  57. */
  58. /* Exported functions ---------------------------------------------------------*/
  59. /** @addtogroup DMAEx_Exported_Functions
  60. * @{
  61. */
  62. /** @addtogroup DMAEx_Exported_Functions_Group1
  63. *
  64. @verbatim
  65. ===============================================================================
  66. ##### Extended features functions #####
  67. ===============================================================================
  68. [..] This section provides functions allowing to:
  69. (+) Configure the source, destination address and data length and
  70. Start MultiBuffer DMA transfer
  71. (+) Configure the source, destination address and data length and
  72. Start MultiBuffer DMA transfer with interrupt
  73. (+) Change on the fly the memory0 or memory1 address.
  74. @endverbatim
  75. * @{
  76. */
  77. /**
  78. * @brief Starts the multi_buffer DMA Transfer.
  79. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  80. * the configuration information for the specified DMA Stream.
  81. * @param SrcAddress The source memory Buffer address
  82. * @param DstAddress The destination memory Buffer address
  83. * @param SecondMemAddress The second memory Buffer address in case of multi buffer Transfer
  84. * @param DataLength The length of data to be transferred from source to destination
  85. * @retval HAL status
  86. */
  87. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  88. {
  89. HAL_StatusTypeDef status = HAL_OK;
  90. /* Check the parameters */
  91. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  92. /* Memory-to-memory transfer not supported in double buffering mode */
  93. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  94. {
  95. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  96. status = HAL_ERROR;
  97. }
  98. else
  99. {
  100. /* Process Locked */
  101. __HAL_LOCK(hdma);
  102. if(HAL_DMA_STATE_READY == hdma->State)
  103. {
  104. /* Change DMA peripheral state */
  105. hdma->State = HAL_DMA_STATE_BUSY;
  106. /* Enable the double buffer mode */
  107. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  108. /* Configure DMA Stream destination address */
  109. hdma->Instance->M1AR = SecondMemAddress;
  110. /* Configure the source, destination address and the data length */
  111. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  112. /* Enable the peripheral */
  113. __HAL_DMA_ENABLE(hdma);
  114. }
  115. else
  116. {
  117. /* Return error status */
  118. status = HAL_BUSY;
  119. }
  120. }
  121. return status;
  122. }
  123. /**
  124. * @brief Starts the multi_buffer DMA Transfer with interrupt enabled.
  125. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  126. * the configuration information for the specified DMA Stream.
  127. * @param SrcAddress The source memory Buffer address
  128. * @param DstAddress The destination memory Buffer address
  129. * @param SecondMemAddress The second memory Buffer address in case of multi buffer Transfer
  130. * @param DataLength The length of data to be transferred from source to destination
  131. * @retval HAL status
  132. */
  133. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  134. {
  135. HAL_StatusTypeDef status = HAL_OK;
  136. /* Check the parameters */
  137. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  138. /* Memory-to-memory transfer not supported in double buffering mode */
  139. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  140. {
  141. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  142. return HAL_ERROR;
  143. }
  144. /* Check callback functions */
  145. if ((NULL == hdma->XferCpltCallback) || (NULL == hdma->XferM1CpltCallback) || (NULL == hdma->XferErrorCallback))
  146. {
  147. hdma->ErrorCode = HAL_DMA_ERROR_PARAM;
  148. return HAL_ERROR;
  149. }
  150. /* Process locked */
  151. __HAL_LOCK(hdma);
  152. if(HAL_DMA_STATE_READY == hdma->State)
  153. {
  154. /* Change DMA peripheral state */
  155. hdma->State = HAL_DMA_STATE_BUSY;
  156. /* Initialize the error code */
  157. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  158. /* Enable the Double buffer mode */
  159. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  160. /* Configure DMA Stream destination address */
  161. hdma->Instance->M1AR = SecondMemAddress;
  162. /* Configure the source, destination address and the data length */
  163. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  164. /* Clear all flags */
  165. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
  166. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
  167. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
  168. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_DME_FLAG_INDEX(hdma));
  169. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_FE_FLAG_INDEX(hdma));
  170. /* Enable Common interrupts*/
  171. hdma->Instance->CR |= DMA_IT_TC | DMA_IT_TE | DMA_IT_DME;
  172. hdma->Instance->FCR |= DMA_IT_FE;
  173. if((hdma->XferHalfCpltCallback != NULL) || (hdma->XferM1HalfCpltCallback != NULL))
  174. {
  175. hdma->Instance->CR |= DMA_IT_HT;
  176. }
  177. /* Enable the peripheral */
  178. __HAL_DMA_ENABLE(hdma);
  179. }
  180. else
  181. {
  182. /* Process unlocked */
  183. __HAL_UNLOCK(hdma);
  184. /* Return error status */
  185. status = HAL_BUSY;
  186. }
  187. return status;
  188. }
  189. /**
  190. * @brief Change the memory0 or memory1 address on the fly.
  191. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  192. * the configuration information for the specified DMA Stream.
  193. * @param Address The new address
  194. * @param memory the memory to be changed, This parameter can be one of
  195. * the following values:
  196. * MEMORY0 /
  197. * MEMORY1
  198. * @note The MEMORY0 address can be changed only when the current transfer use
  199. * MEMORY1 and the MEMORY1 address can be changed only when the current
  200. * transfer use MEMORY0.
  201. * @retval HAL status
  202. */
  203. HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory)
  204. {
  205. if(memory == MEMORY0)
  206. {
  207. /* change the memory0 address */
  208. hdma->Instance->M0AR = Address;
  209. }
  210. else
  211. {
  212. /* change the memory1 address */
  213. hdma->Instance->M1AR = Address;
  214. }
  215. return HAL_OK;
  216. }
  217. /**
  218. * @}
  219. */
  220. /**
  221. * @}
  222. */
  223. /** @addtogroup DMAEx_Private_Functions
  224. * @{
  225. */
  226. /**
  227. * @brief Set the DMA Transfer parameter.
  228. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  229. * the configuration information for the specified DMA Stream.
  230. * @param SrcAddress The source memory Buffer address
  231. * @param DstAddress The destination memory Buffer address
  232. * @param DataLength The length of data to be transferred from source to destination
  233. * @retval HAL status
  234. */
  235. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  236. {
  237. /* Configure DMA Stream data length */
  238. hdma->Instance->NDTR = DataLength;
  239. /* Peripheral to Memory */
  240. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  241. {
  242. /* Configure DMA Stream destination address */
  243. hdma->Instance->PAR = DstAddress;
  244. /* Configure DMA Stream source address */
  245. hdma->Instance->M0AR = SrcAddress;
  246. }
  247. /* Memory to Peripheral */
  248. else
  249. {
  250. /* Configure DMA Stream source address */
  251. hdma->Instance->PAR = SrcAddress;
  252. /* Configure DMA Stream destination address */
  253. hdma->Instance->M0AR = DstAddress;
  254. }
  255. }
  256. /**
  257. * @}
  258. */
  259. #endif /* HAL_DMA_MODULE_ENABLED */
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */
  266. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/