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.
 
 
 

329 lines
11 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_dma_ex.c
  4. * @author MCD Application Team
  5. * @version V1.1.2
  6. * @date 23-September-2016
  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) 2016 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 "stm32f7xx_hal.h"
  56. /** @addtogroup STM32F7xx_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. HAL_StatusTypeDef status = HAL_OK;
  108. /* Check the parameters */
  109. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  110. /* Memory-to-memory transfer not supported in double buffering mode */
  111. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  112. {
  113. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  114. status = HAL_ERROR;
  115. }
  116. else
  117. {
  118. /* Process Locked */
  119. __HAL_LOCK(hdma);
  120. if(HAL_DMA_STATE_READY == hdma->State)
  121. {
  122. /* Change DMA peripheral state */
  123. hdma->State = HAL_DMA_STATE_BUSY;
  124. /* Enable the double buffer mode */
  125. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  126. /* Configure DMA Stream destination address */
  127. hdma->Instance->M1AR = SecondMemAddress;
  128. /* Configure the source, destination address and the data length */
  129. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  130. /* Enable the peripheral */
  131. __HAL_DMA_ENABLE(hdma);
  132. }
  133. else
  134. {
  135. /* Return error status */
  136. status = HAL_BUSY;
  137. }
  138. }
  139. return status;
  140. }
  141. /**
  142. * @brief Starts the multi_buffer DMA Transfer with interrupt enabled.
  143. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  144. * the configuration information for the specified DMA Stream.
  145. * @param SrcAddress: The source memory Buffer address
  146. * @param DstAddress: The destination memory Buffer address
  147. * @param SecondMemAddress: The second memory Buffer address in case of multi buffer Transfer
  148. * @param DataLength: The length of data to be transferred from source to destination
  149. * @retval HAL status
  150. */
  151. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  152. {
  153. HAL_StatusTypeDef status = HAL_OK;
  154. /* Check the parameters */
  155. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  156. /* Memory-to-memory transfer not supported in double buffering mode */
  157. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  158. {
  159. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  160. return HAL_ERROR;
  161. }
  162. /* Process locked */
  163. __HAL_LOCK(hdma);
  164. if(HAL_DMA_STATE_READY == hdma->State)
  165. {
  166. /* Change DMA peripheral state */
  167. hdma->State = HAL_DMA_STATE_BUSY;
  168. /* Initialize the error code */
  169. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  170. /* Enable the Double buffer mode */
  171. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  172. /* Configure DMA Stream destination address */
  173. hdma->Instance->M1AR = SecondMemAddress;
  174. /* Configure the source, destination address and the data length */
  175. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  176. /* Clear all flags */
  177. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
  178. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
  179. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
  180. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_DME_FLAG_INDEX(hdma));
  181. __HAL_DMA_CLEAR_FLAG (hdma, __HAL_DMA_GET_FE_FLAG_INDEX(hdma));
  182. /* Enable Common interrupts*/
  183. hdma->Instance->CR |= DMA_IT_TC | DMA_IT_TE | DMA_IT_DME;
  184. hdma->Instance->FCR |= DMA_IT_FE;
  185. if((hdma->XferHalfCpltCallback != NULL) || (hdma->XferM1HalfCpltCallback != NULL))
  186. {
  187. hdma->Instance->CR |= DMA_IT_HT;
  188. }
  189. /* Enable the peripheral */
  190. __HAL_DMA_ENABLE(hdma);
  191. }
  192. else
  193. {
  194. /* Process unlocked */
  195. __HAL_UNLOCK(hdma);
  196. /* Return error status */
  197. status = HAL_BUSY;
  198. }
  199. return status;
  200. }
  201. /**
  202. * @brief Change the memory0 or memory1 address on the fly.
  203. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  204. * the configuration information for the specified DMA Stream.
  205. * @param Address: The new address
  206. * @param memory: the memory to be changed, This parameter can be one of
  207. * the following values:
  208. * MEMORY0 /
  209. * MEMORY1
  210. * @note The MEMORY0 address can be changed only when the current transfer use
  211. * MEMORY1 and the MEMORY1 address can be changed only when the current
  212. * transfer use MEMORY0.
  213. * @retval HAL status
  214. */
  215. HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory)
  216. {
  217. if(memory == MEMORY0)
  218. {
  219. /* change the memory0 address */
  220. hdma->Instance->M0AR = Address;
  221. }
  222. else
  223. {
  224. /* change the memory1 address */
  225. hdma->Instance->M1AR = Address;
  226. }
  227. return HAL_OK;
  228. }
  229. /**
  230. * @}
  231. */
  232. /**
  233. * @}
  234. */
  235. /** @addtogroup DMAEx_Private_Functions
  236. * @{
  237. */
  238. /**
  239. * @brief Set the DMA Transfer parameter.
  240. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  241. * the configuration information for the specified DMA Stream.
  242. * @param SrcAddress: The source memory Buffer address
  243. * @param DstAddress: The destination memory Buffer address
  244. * @param DataLength: The length of data to be transferred from source to destination
  245. * @retval HAL status
  246. */
  247. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  248. {
  249. /* Configure DMA Stream data length */
  250. hdma->Instance->NDTR = DataLength;
  251. /* Peripheral to Memory */
  252. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  253. {
  254. /* Configure DMA Stream destination address */
  255. hdma->Instance->PAR = DstAddress;
  256. /* Configure DMA Stream source address */
  257. hdma->Instance->M0AR = SrcAddress;
  258. }
  259. /* Memory to Peripheral */
  260. else
  261. {
  262. /* Configure DMA Stream source address */
  263. hdma->Instance->PAR = SrcAddress;
  264. /* Configure DMA Stream destination address */
  265. hdma->Instance->M0AR = DstAddress;
  266. }
  267. }
  268. /**
  269. * @}
  270. */
  271. #endif /* HAL_DMA_MODULE_ENABLED */
  272. /**
  273. * @}
  274. */
  275. /**
  276. * @}
  277. */
  278. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/