stm32f7xx_hal_dma_ex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  27. *
  28. * Redistribution and use in source and binary forms, with or without modification,
  29. * are permitted provided that the following conditions are met:
  30. * 1. Redistributions of source code must retain the above copyright notice,
  31. * this list of conditions and the following disclaimer.
  32. * 2. Redistributions in binary form must reproduce the above copyright notice,
  33. * this list of conditions and the following disclaimer in the documentation
  34. * and/or other materials provided with the distribution.
  35. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  36. * may be used to endorse or promote products derived from this software
  37. * without specific prior written permission.
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  42. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  43. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  45. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. ******************************************************************************
  51. */
  52. /* Includes ------------------------------------------------------------------*/
  53. #include "stm32f7xx_hal.h"
  54. /** @addtogroup STM32F7xx_HAL_Driver
  55. * @{
  56. */
  57. /** @defgroup DMAEx DMAEx
  58. * @brief DMA Extended HAL module driver
  59. * @{
  60. */
  61. #ifdef HAL_DMA_MODULE_ENABLED
  62. /* Private types -------------------------------------------------------------*/
  63. /* Private variables ---------------------------------------------------------*/
  64. /* Private Constants ---------------------------------------------------------*/
  65. /* Private macros ------------------------------------------------------------*/
  66. /* Private functions ---------------------------------------------------------*/
  67. /** @addtogroup DMAEx_Private_Functions
  68. * @{
  69. */
  70. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  71. /**
  72. * @}
  73. */
  74. /* Exported functions ---------------------------------------------------------*/
  75. /** @addtogroup DMAEx_Exported_Functions
  76. * @{
  77. */
  78. /** @addtogroup DMAEx_Exported_Functions_Group1
  79. *
  80. @verbatim
  81. ===============================================================================
  82. ##### Extended features functions #####
  83. ===============================================================================
  84. [..] This section provides functions allowing to:
  85. (+) Configure the source, destination address and data length and
  86. Start MultiBuffer DMA transfer
  87. (+) Configure the source, destination address and data length and
  88. Start MultiBuffer DMA transfer with interrupt
  89. (+) Change on the fly the memory0 or memory1 address.
  90. @endverbatim
  91. * @{
  92. */
  93. /**
  94. * @brief Starts the multi_buffer DMA Transfer.
  95. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  96. * the configuration information for the specified DMA Stream.
  97. * @param SrcAddress The source memory Buffer address
  98. * @param DstAddress The destination memory Buffer address
  99. * @param SecondMemAddress The second memory Buffer address in case of multi buffer Transfer
  100. * @param DataLength The length of data to be transferred from source to destination
  101. * @retval HAL status
  102. */
  103. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  104. {
  105. HAL_StatusTypeDef status = HAL_OK;
  106. /* Check the parameters */
  107. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  108. /* Memory-to-memory transfer not supported in double buffering mode */
  109. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  110. {
  111. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  112. status = HAL_ERROR;
  113. }
  114. else
  115. {
  116. /* Process Locked */
  117. __HAL_LOCK(hdma);
  118. if(HAL_DMA_STATE_READY == hdma->State)
  119. {
  120. /* Change DMA peripheral state */
  121. hdma->State = HAL_DMA_STATE_BUSY;
  122. /* Enable the double buffer mode */
  123. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  124. /* Configure DMA Stream destination address */
  125. hdma->Instance->M1AR = SecondMemAddress;
  126. /* Configure the source, destination address and the data length */
  127. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  128. /* Enable the peripheral */
  129. __HAL_DMA_ENABLE(hdma);
  130. }
  131. else
  132. {
  133. /* Return error status */
  134. status = HAL_BUSY;
  135. }
  136. }
  137. return status;
  138. }
  139. /**
  140. * @brief Starts the multi_buffer DMA Transfer with interrupt enabled.
  141. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  142. * the configuration information for the specified DMA Stream.
  143. * @param SrcAddress The source memory Buffer address
  144. * @param DstAddress The destination memory Buffer address
  145. * @param SecondMemAddress The second memory Buffer address in case of multi buffer Transfer
  146. * @param DataLength The length of data to be transferred from source to destination
  147. * @retval HAL status
  148. */
  149. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  150. {
  151. HAL_StatusTypeDef status = HAL_OK;
  152. /* Check the parameters */
  153. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  154. /* Memory-to-memory transfer not supported in double buffering mode */
  155. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  156. {
  157. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  158. return HAL_ERROR;
  159. }
  160. /* Process locked */
  161. __HAL_LOCK(hdma);
  162. if(HAL_DMA_STATE_READY == hdma->State)
  163. {
  164. /* Change DMA peripheral state */
  165. hdma->State = HAL_DMA_STATE_BUSY;
  166. /* Initialize the error code */
  167. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  168. /* Enable the Double buffer mode */
  169. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  170. /* Configure DMA Stream destination address */
  171. hdma->Instance->M1AR = SecondMemAddress;
  172. /* Configure the source, destination address and the data length */
  173. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  174. /* Clear all flags */
  175. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
  176. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
  177. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
  178. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_DME_FLAG_INDEX(hdma));
  179. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_FE_FLAG_INDEX(hdma));
  180. /* Enable Common interrupts*/
  181. hdma->Instance->CR |= DMA_IT_TC | DMA_IT_TE | DMA_IT_DME;
  182. hdma->Instance->FCR |= DMA_IT_FE;
  183. if((hdma->XferHalfCpltCallback != NULL) || (hdma->XferM1HalfCpltCallback != NULL))
  184. {
  185. hdma->Instance->CR |= DMA_IT_HT;
  186. }
  187. /* Enable the peripheral */
  188. __HAL_DMA_ENABLE(hdma);
  189. }
  190. else
  191. {
  192. /* Process unlocked */
  193. __HAL_UNLOCK(hdma);
  194. /* Return error status */
  195. status = HAL_BUSY;
  196. }
  197. return status;
  198. }
  199. /**
  200. * @brief Change the memory0 or memory1 address on the fly.
  201. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  202. * the configuration information for the specified DMA Stream.
  203. * @param Address The new address
  204. * @param memory the memory to be changed, This parameter can be one of
  205. * the following values:
  206. * MEMORY0 /
  207. * MEMORY1
  208. * @note The MEMORY0 address can be changed only when the current transfer use
  209. * MEMORY1 and the MEMORY1 address can be changed only when the current
  210. * transfer use MEMORY0.
  211. * @retval HAL status
  212. */
  213. HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory)
  214. {
  215. if(memory == MEMORY0)
  216. {
  217. /* change the memory0 address */
  218. hdma->Instance->M0AR = Address;
  219. }
  220. else
  221. {
  222. /* change the memory1 address */
  223. hdma->Instance->M1AR = Address;
  224. }
  225. return HAL_OK;
  226. }
  227. /**
  228. * @}
  229. */
  230. /**
  231. * @}
  232. */
  233. /** @addtogroup DMAEx_Private_Functions
  234. * @{
  235. */
  236. /**
  237. * @brief Set the DMA Transfer parameter.
  238. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  239. * the configuration information for the specified DMA Stream.
  240. * @param SrcAddress The source memory Buffer address
  241. * @param DstAddress The destination memory Buffer address
  242. * @param DataLength The length of data to be transferred from source to destination
  243. * @retval HAL status
  244. */
  245. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  246. {
  247. /* Configure DMA Stream data length */
  248. hdma->Instance->NDTR = DataLength;
  249. /* Peripheral to Memory */
  250. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  251. {
  252. /* Configure DMA Stream destination address */
  253. hdma->Instance->PAR = DstAddress;
  254. /* Configure DMA Stream source address */
  255. hdma->Instance->M0AR = SrcAddress;
  256. }
  257. /* Memory to Peripheral */
  258. else
  259. {
  260. /* Configure DMA Stream source address */
  261. hdma->Instance->PAR = SrcAddress;
  262. /* Configure DMA Stream destination address */
  263. hdma->Instance->M0AR = DstAddress;
  264. }
  265. }
  266. /**
  267. * @}
  268. */
  269. #endif /* HAL_DMA_MODULE_ENABLED */
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/