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.
 
 
 

334 lines
11 KiB

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