stm32f4xx_hal_dma_ex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dma_ex.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-2015
  7. * @brief DMA Extension HAL module driver
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the DMA Extension peripheral:
  10. * + Extended features functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The DMA Extension HAL driver can be used as follows:
  18. (#) Start a multi buffer transfer using the HAL_DMA_MultiBufferStart() function
  19. for polling mode or HAL_DMA_MultiBufferStart_IT() for interrupt mode.
  20. -@- In Memory-to-Memory transfer mode, Multi (Double) Buffer mode is not allowed.
  21. -@- When Multi (Double) Buffer mode is enabled the, transfer is circular by default.
  22. -@- In Multi (Double) buffer mode, it is possible to update the base address for
  23. the AHB memory port on the fly (DMA_SxM0AR or DMA_SxM1AR) when the stream is enabled.
  24. @endverbatim
  25. ******************************************************************************
  26. * @attention
  27. *
  28. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  29. *
  30. * Redistribution and use in source and binary forms, with or without modification,
  31. * are permitted provided that the following conditions are met:
  32. * 1. Redistributions of source code must retain the above copyright notice,
  33. * this list of conditions and the following disclaimer.
  34. * 2. Redistributions in binary form must reproduce the above copyright notice,
  35. * this list of conditions and the following disclaimer in the documentation
  36. * and/or other materials provided with the distribution.
  37. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  38. * may be used to endorse or promote products derived from this software
  39. * without specific prior written permission.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  42. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  47. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  48. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  49. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  50. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *
  52. ******************************************************************************
  53. */
  54. /* Includes ------------------------------------------------------------------*/
  55. #include "stm32f4xx_hal.h"
  56. /** @addtogroup STM32F4xx_HAL_Driver
  57. * @{
  58. */
  59. /** @defgroup DMAEx DMAEx
  60. * @brief DMA Extended HAL module driver
  61. * @{
  62. */
  63. #ifdef HAL_DMA_MODULE_ENABLED
  64. /* Private types -------------------------------------------------------------*/
  65. /* Private variables ---------------------------------------------------------*/
  66. /* Private Constants ---------------------------------------------------------*/
  67. /* Private macros ------------------------------------------------------------*/
  68. /* Private functions ---------------------------------------------------------*/
  69. /** @addtogroup DMAEx_Private_Functions
  70. * @{
  71. */
  72. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  73. /**
  74. * @}
  75. */
  76. /* Exported functions ---------------------------------------------------------*/
  77. /** @addtogroup DMAEx_Exported_Functions
  78. * @{
  79. */
  80. /** @addtogroup DMAEx_Exported_Functions_Group1
  81. *
  82. @verbatim
  83. ===============================================================================
  84. ##### Extended features functions #####
  85. ===============================================================================
  86. [..] This section provides functions allowing to:
  87. (+) Configure the source, destination address and data length and
  88. Start MultiBuffer DMA transfer
  89. (+) Configure the source, destination address and data length and
  90. Start MultiBuffer DMA transfer with interrupt
  91. (+) Change on the fly the memory0 or memory1 address.
  92. @endverbatim
  93. * @{
  94. */
  95. /**
  96. * @brief Starts the multi_buffer DMA Transfer.
  97. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  98. * the configuration information for the specified DMA Stream.
  99. * @param SrcAddress: The source memory Buffer address
  100. * @param DstAddress: The destination memory Buffer address
  101. * @param SecondMemAddress: The second memory Buffer address in case of multi buffer Transfer
  102. * @param DataLength: The length of data to be transferred from source to destination
  103. * @retval HAL status
  104. */
  105. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  106. {
  107. /* Process Locked */
  108. __HAL_LOCK(hdma);
  109. /* Current memory buffer used is Memory 0 */
  110. if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
  111. {
  112. hdma->State = HAL_DMA_STATE_BUSY_MEM0;
  113. }
  114. /* Current memory buffer used is Memory 1 */
  115. else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
  116. {
  117. hdma->State = HAL_DMA_STATE_BUSY_MEM1;
  118. }
  119. /* Check the parameters */
  120. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  121. /* Disable the peripheral */
  122. __HAL_DMA_DISABLE(hdma);
  123. /* Enable the double buffer mode */
  124. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  125. /* Configure DMA Stream destination address */
  126. hdma->Instance->M1AR = SecondMemAddress;
  127. /* Configure the source, destination address and the data length */
  128. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  129. /* Enable the peripheral */
  130. __HAL_DMA_ENABLE(hdma);
  131. return HAL_OK;
  132. }
  133. /**
  134. * @brief Starts the multi_buffer DMA Transfer with interrupt enabled.
  135. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  136. * the configuration information for the specified DMA Stream.
  137. * @param SrcAddress: The source memory Buffer address
  138. * @param DstAddress: The destination memory Buffer address
  139. * @param SecondMemAddress: The second memory Buffer address in case of multi buffer Transfer
  140. * @param DataLength: The length of data to be transferred from source to destination
  141. * @retval HAL status
  142. */
  143. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  144. {
  145. /* Process Locked */
  146. __HAL_LOCK(hdma);
  147. /* Current memory buffer used is Memory 0 */
  148. if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
  149. {
  150. hdma->State = HAL_DMA_STATE_BUSY_MEM0;
  151. }
  152. /* Current memory buffer used is Memory 1 */
  153. else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
  154. {
  155. hdma->State = HAL_DMA_STATE_BUSY_MEM1;
  156. }
  157. /* Check the parameters */
  158. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  159. /* Disable the peripheral */
  160. __HAL_DMA_DISABLE(hdma);
  161. /* Enable the Double buffer mode */
  162. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  163. /* Configure DMA Stream destination address */
  164. hdma->Instance->M1AR = SecondMemAddress;
  165. /* Configure the source, destination address and the data length */
  166. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  167. /* Enable the transfer complete interrupt */
  168. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TC);
  169. /* Enable the Half transfer interrupt */
  170. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_HT);
  171. /* Enable the transfer Error interrupt */
  172. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TE);
  173. /* Enable the fifo Error interrupt */
  174. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_FE);
  175. /* Enable the direct mode Error interrupt */
  176. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_DME);
  177. /* Enable the peripheral */
  178. __HAL_DMA_ENABLE(hdma);
  179. return HAL_OK;
  180. }
  181. /**
  182. * @brief Change the memory0 or memory1 address on the fly.
  183. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  184. * the configuration information for the specified DMA Stream.
  185. * @param Address: The new address
  186. * @param memory: the memory to be changed, This parameter can be one of
  187. * the following values:
  188. * MEMORY0 /
  189. * MEMORY1
  190. * @note The MEMORY0 address can be changed only when the current transfer use
  191. * MEMORY1 and the MEMORY1 address can be changed only when the current
  192. * transfer use MEMORY0.
  193. * @retval HAL status
  194. */
  195. HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory)
  196. {
  197. if(memory == MEMORY0)
  198. {
  199. /* change the memory0 address */
  200. hdma->Instance->M0AR = Address;
  201. }
  202. else
  203. {
  204. /* change the memory1 address */
  205. hdma->Instance->M1AR = Address;
  206. }
  207. return HAL_OK;
  208. }
  209. /**
  210. * @}
  211. */
  212. /**
  213. * @}
  214. */
  215. /** @addtogroup DMAEx_Private_Functions
  216. * @{
  217. */
  218. /**
  219. * @brief Set the DMA Transfer parameter.
  220. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  221. * the configuration information for the specified DMA Stream.
  222. * @param SrcAddress: The source memory Buffer address
  223. * @param DstAddress: The destination memory Buffer address
  224. * @param DataLength: The length of data to be transferred from source to destination
  225. * @retval HAL status
  226. */
  227. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  228. {
  229. /* Configure DMA Stream data length */
  230. hdma->Instance->NDTR = DataLength;
  231. /* Peripheral to Memory */
  232. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  233. {
  234. /* Configure DMA Stream destination address */
  235. hdma->Instance->PAR = DstAddress;
  236. /* Configure DMA Stream source address */
  237. hdma->Instance->M0AR = SrcAddress;
  238. }
  239. /* Memory to Peripheral */
  240. else
  241. {
  242. /* Configure DMA Stream source address */
  243. hdma->Instance->PAR = SrcAddress;
  244. /* Configure DMA Stream destination address */
  245. hdma->Instance->M0AR = DstAddress;
  246. }
  247. }
  248. /**
  249. * @}
  250. */
  251. #endif /* HAL_DMA_MODULE_ENABLED */
  252. /**
  253. * @}
  254. */
  255. /**
  256. * @}
  257. */
  258. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/