stm32f4xx_hal_dma_ex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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</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 "stm32f4xx_hal.h"
  54. /** @addtogroup STM32F4xx_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. /* Check callback functions */
  161. if ((NULL == hdma->XferCpltCallback) || (NULL == hdma->XferM1CpltCallback) || (NULL == hdma->XferErrorCallback))
  162. {
  163. hdma->ErrorCode = HAL_DMA_ERROR_PARAM;
  164. return HAL_ERROR;
  165. }
  166. /* Process locked */
  167. __HAL_LOCK(hdma);
  168. if(HAL_DMA_STATE_READY == hdma->State)
  169. {
  170. /* Change DMA peripheral state */
  171. hdma->State = HAL_DMA_STATE_BUSY;
  172. /* Initialize the error code */
  173. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  174. /* Enable the Double buffer mode */
  175. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  176. /* Configure DMA Stream destination address */
  177. hdma->Instance->M1AR = SecondMemAddress;
  178. /* Configure the source, destination address and the data length */
  179. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  180. /* Clear all flags */
  181. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
  182. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
  183. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
  184. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_DME_FLAG_INDEX(hdma));
  185. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_FE_FLAG_INDEX(hdma));
  186. /* Enable Common interrupts*/
  187. hdma->Instance->CR |= DMA_IT_TC | DMA_IT_TE | DMA_IT_DME;
  188. hdma->Instance->FCR |= DMA_IT_FE;
  189. if((hdma->XferHalfCpltCallback != NULL) || (hdma->XferM1HalfCpltCallback != NULL))
  190. {
  191. hdma->Instance->CR |= DMA_IT_HT;
  192. }
  193. /* Enable the peripheral */
  194. __HAL_DMA_ENABLE(hdma);
  195. }
  196. else
  197. {
  198. /* Process unlocked */
  199. __HAL_UNLOCK(hdma);
  200. /* Return error status */
  201. status = HAL_BUSY;
  202. }
  203. return status;
  204. }
  205. /**
  206. * @brief Change the memory0 or memory1 address on the fly.
  207. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  208. * the configuration information for the specified DMA Stream.
  209. * @param Address The new address
  210. * @param memory the memory to be changed, This parameter can be one of
  211. * the following values:
  212. * MEMORY0 /
  213. * MEMORY1
  214. * @note The MEMORY0 address can be changed only when the current transfer use
  215. * MEMORY1 and the MEMORY1 address can be changed only when the current
  216. * transfer use MEMORY0.
  217. * @retval HAL status
  218. */
  219. HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory)
  220. {
  221. if(memory == MEMORY0)
  222. {
  223. /* change the memory0 address */
  224. hdma->Instance->M0AR = Address;
  225. }
  226. else
  227. {
  228. /* change the memory1 address */
  229. hdma->Instance->M1AR = Address;
  230. }
  231. return HAL_OK;
  232. }
  233. /**
  234. * @}
  235. */
  236. /**
  237. * @}
  238. */
  239. /** @addtogroup DMAEx_Private_Functions
  240. * @{
  241. */
  242. /**
  243. * @brief Set the DMA Transfer parameter.
  244. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  245. * the configuration information for the specified DMA Stream.
  246. * @param SrcAddress The source memory Buffer address
  247. * @param DstAddress The destination memory Buffer address
  248. * @param DataLength The length of data to be transferred from source to destination
  249. * @retval HAL status
  250. */
  251. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  252. {
  253. /* Configure DMA Stream data length */
  254. hdma->Instance->NDTR = DataLength;
  255. /* Peripheral to Memory */
  256. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  257. {
  258. /* Configure DMA Stream destination address */
  259. hdma->Instance->PAR = DstAddress;
  260. /* Configure DMA Stream source address */
  261. hdma->Instance->M0AR = SrcAddress;
  262. }
  263. /* Memory to Peripheral */
  264. else
  265. {
  266. /* Configure DMA Stream source address */
  267. hdma->Instance->PAR = SrcAddress;
  268. /* Configure DMA Stream destination address */
  269. hdma->Instance->M0AR = DstAddress;
  270. }
  271. }
  272. /**
  273. * @}
  274. */
  275. #endif /* HAL_DMA_MODULE_ENABLED */
  276. /**
  277. * @}
  278. */
  279. /**
  280. * @}
  281. */
  282. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/