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.
 
 
 

295 lines
10 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dma_ex.c
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 19-June-2014
  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) 2014 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
  60. * @brief DMA Extended HAL module driver
  61. * @{
  62. */
  63. #ifdef HAL_DMA_MODULE_ENABLED
  64. /* Private typedef -----------------------------------------------------------*/
  65. /* Private define ------------------------------------------------------------*/
  66. /* Private macro -------------------------------------------------------------*/
  67. /* Private variables ---------------------------------------------------------*/
  68. /* Private function prototypes -----------------------------------------------*/
  69. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  70. /* Private functions ---------------------------------------------------------*/
  71. /** @defgroup DMAEx_Private_Functions
  72. * @{
  73. */
  74. /** @defgroup DMAEx_Group1 Extended features functions
  75. * @brief Extended features functions
  76. *
  77. @verbatim
  78. ===============================================================================
  79. ##### Extended features functions #####
  80. ===============================================================================
  81. [..] This section provides functions allowing to:
  82. (+) Configure the source, destination address and data length and
  83. Start MultiBuffer DMA transfer
  84. (+) Configure the source, destination address and data length and
  85. Start MultiBuffer DMA transfer with interrupt
  86. (+) Change on the fly the memory0 or memory1 address.
  87. @endverbatim
  88. * @{
  89. */
  90. /**
  91. * @brief Starts the multi_buffer DMA Transfer.
  92. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  93. * the configuration information for the specified DMA Stream.
  94. * @param SrcAddress: The source memory Buffer address
  95. * @param DstAddress: The destination memory Buffer address
  96. * @param SecondMemAddress: The second memory Buffer address in case of multi buffer Transfer
  97. * @param DataLength: The length of data to be transferred from source to destination
  98. * @retval HAL status
  99. */
  100. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  101. {
  102. /* Process Locked */
  103. __HAL_LOCK(hdma);
  104. /* Current memory buffer used is Memory 0 */
  105. if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
  106. {
  107. hdma->State = HAL_DMA_STATE_BUSY_MEM0;
  108. }
  109. /* Current memory buffer used is Memory 1 */
  110. else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
  111. {
  112. hdma->State = HAL_DMA_STATE_BUSY_MEM1;
  113. }
  114. /* Check the parameters */
  115. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  116. /* Disable the peripheral */
  117. __HAL_DMA_DISABLE(hdma);
  118. /* Enable the double buffer mode */
  119. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  120. /* Configure DMA Stream destination address */
  121. hdma->Instance->M1AR = SecondMemAddress;
  122. /* Configure the source, destination address and the data length */
  123. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  124. /* Enable the peripheral */
  125. __HAL_DMA_ENABLE(hdma);
  126. return HAL_OK;
  127. }
  128. /**
  129. * @brief Starts the multi_buffer DMA Transfer with interrupt enabled.
  130. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  131. * the configuration information for the specified DMA Stream.
  132. * @param SrcAddress: The source memory Buffer address
  133. * @param DstAddress: The destination memory Buffer address
  134. * @param SecondMemAddress: The second memory Buffer address in case of multi buffer Transfer
  135. * @param DataLength: The length of data to be transferred from source to destination
  136. * @retval HAL status
  137. */
  138. HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength)
  139. {
  140. /* Process Locked */
  141. __HAL_LOCK(hdma);
  142. /* Current memory buffer used is Memory 0 */
  143. if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
  144. {
  145. hdma->State = HAL_DMA_STATE_BUSY_MEM0;
  146. }
  147. /* Current memory buffer used is Memory 1 */
  148. else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
  149. {
  150. hdma->State = HAL_DMA_STATE_BUSY_MEM1;
  151. }
  152. /* Check the parameters */
  153. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  154. /* Disable the peripheral */
  155. __HAL_DMA_DISABLE(hdma);
  156. /* Enable the Double buffer mode */
  157. hdma->Instance->CR |= (uint32_t)DMA_SxCR_DBM;
  158. /* Configure DMA Stream destination address */
  159. hdma->Instance->M1AR = SecondMemAddress;
  160. /* Configure the source, destination address and the data length */
  161. DMA_MultiBufferSetConfig(hdma, SrcAddress, DstAddress, DataLength);
  162. /* Enable the transfer complete interrupt */
  163. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TC);
  164. /* Enable the Half transfer interrupt */
  165. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_HT);
  166. /* Enable the transfer Error interrupt */
  167. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TE);
  168. /* Enable the fifo Error interrupt */
  169. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_FE);
  170. /* Enable the direct mode Error interrupt */
  171. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_DME);
  172. /* Enable the peripheral */
  173. __HAL_DMA_ENABLE(hdma);
  174. return HAL_OK;
  175. }
  176. /**
  177. * @brief Change the memory0 or memory1 address on the fly.
  178. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  179. * the configuration information for the specified DMA Stream.
  180. * @param Address: The new address
  181. * @param memory: the memory to be changed, This parameter can be one of
  182. * the following values:
  183. * MEMORY0 /
  184. * MEMORY1
  185. * @note The MEMORY0 address can be changed only when the current transfer use
  186. * MEMORY1 and the MEMORY1 address can be changed only when the current
  187. * transfer use MEMORY0.
  188. * @retval HAL status
  189. */
  190. HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory)
  191. {
  192. if(memory == MEMORY0)
  193. {
  194. /* change the memory0 address */
  195. hdma->Instance->M0AR = Address;
  196. }
  197. else
  198. {
  199. /* change the memory1 address */
  200. hdma->Instance->M1AR = Address;
  201. }
  202. return HAL_OK;
  203. }
  204. /**
  205. * @}
  206. */
  207. /**
  208. * @brief Set the DMA Transfer parameter.
  209. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  210. * the configuration information for the specified DMA Stream.
  211. * @param SrcAddress: The source memory Buffer address
  212. * @param DstAddress: The destination memory Buffer address
  213. * @param DataLength: The length of data to be transferred from source to destination
  214. * @retval HAL status
  215. */
  216. static void DMA_MultiBufferSetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  217. {
  218. /* Configure DMA Stream data length */
  219. hdma->Instance->NDTR = DataLength;
  220. /* Peripheral to Memory */
  221. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  222. {
  223. /* Configure DMA Stream destination address */
  224. hdma->Instance->PAR = DstAddress;
  225. /* Configure DMA Stream source address */
  226. hdma->Instance->M0AR = SrcAddress;
  227. }
  228. /* Memory to Peripheral */
  229. else
  230. {
  231. /* Configure DMA Stream source address */
  232. hdma->Instance->PAR = SrcAddress;
  233. /* Configure DMA Stream destination address */
  234. hdma->Instance->M0AR = DstAddress;
  235. }
  236. }
  237. /**
  238. * @}
  239. */
  240. #endif /* HAL_DMA_MODULE_ENABLED */
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/