stm32g4xx_hal_dma_ex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_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. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2019 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. @verbatim
  22. ==============================================================================
  23. ##### How to use this driver #####
  24. ==============================================================================
  25. [..]
  26. The DMA Extension HAL driver can be used as follows:
  27. (+) Configure the DMA_MUX Synchronization Block using HAL_DMAEx_ConfigMuxSync function.
  28. (+) Configure the DMA_MUX Request Generator Block using HAL_DMAEx_ConfigMuxRequestGenerator function.
  29. Functions HAL_DMAEx_EnableMuxRequestGenerator and HAL_DMAEx_DisableMuxRequestGenerator can then be used
  30. to respectively enable/disable the request generator.
  31. (+) To handle the DMAMUX Interrupts, the function HAL_DMAEx_MUX_IRQHandler should be called from
  32. the DMAMUX IRQ handler i.e DMAMUX1_OVR_IRQHandler.
  33. As only one interrupt line is available for all DMAMUX channels and request generators , HAL_DMAEx_MUX_IRQHandler should be
  34. called with, as parameter, the appropriate DMA handle as many as used DMAs in the user project
  35. (exception done if a given DMA is not using the DMAMUX SYNC block neither a request generator)
  36. @endverbatim
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stm32g4xx_hal.h"
  40. /** @addtogroup STM32G4xx_HAL_Driver
  41. * @{
  42. */
  43. /** @defgroup DMAEx DMAEx
  44. * @brief DMA Extended HAL module driver
  45. * @{
  46. */
  47. #ifdef HAL_DMA_MODULE_ENABLED
  48. /* Private typedef -----------------------------------------------------------*/
  49. /* Private define ------------------------------------------------------------*/
  50. /* Private macro -------------------------------------------------------------*/
  51. /* Private variables ---------------------------------------------------------*/
  52. /* Private Constants ---------------------------------------------------------*/
  53. /* Private function prototypes -----------------------------------------------*/
  54. /* Private functions ---------------------------------------------------------*/
  55. /** @defgroup DMAEx_Exported_Functions DMAEx Exported Functions
  56. * @{
  57. */
  58. /** @defgroup DMAEx_Exported_Functions_Group1 DMAEx Extended features functions
  59. * @brief Extended features functions
  60. *
  61. @verbatim
  62. ===============================================================================
  63. ##### Extended features functions #####
  64. ===============================================================================
  65. [..] This section provides functions allowing to:
  66. (+) Configure the DMAMUX Synchronization Block using HAL_DMAEx_ConfigMuxSync function.
  67. (+) Configure the DMAMUX Request Generator Block using HAL_DMAEx_ConfigMuxRequestGenerator function.
  68. Functions HAL_DMAEx_EnableMuxRequestGenerator and HAL_DMAEx_DisableMuxRequestGenerator can then be used
  69. to respectively enable/disable the request generator.
  70. @endverbatim
  71. * @{
  72. */
  73. /**
  74. * @brief Configure the DMAMUX synchronization parameters for a given DMA channel (instance).
  75. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  76. * the configuration information for the specified DMA channel.
  77. * @param pSyncConfig : pointer to HAL_DMA_MuxSyncConfigTypeDef : contains the DMAMUX synchronization parameters
  78. * @retval HAL status
  79. */
  80. HAL_StatusTypeDef HAL_DMAEx_ConfigMuxSync(DMA_HandleTypeDef *hdma, HAL_DMA_MuxSyncConfigTypeDef *pSyncConfig)
  81. {
  82. /* Check the parameters */
  83. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  84. assert_param(IS_DMAMUX_SYNC_SIGNAL_ID(pSyncConfig->SyncSignalID));
  85. assert_param(IS_DMAMUX_SYNC_POLARITY(pSyncConfig-> SyncPolarity));
  86. assert_param(IS_DMAMUX_SYNC_STATE(pSyncConfig->SyncEnable));
  87. assert_param(IS_DMAMUX_SYNC_EVENT(pSyncConfig->EventEnable));
  88. assert_param(IS_DMAMUX_SYNC_REQUEST_NUMBER(pSyncConfig->RequestNumber));
  89. /*Check if the DMA state is ready */
  90. if (hdma->State == HAL_DMA_STATE_READY)
  91. {
  92. /* Process Locked */
  93. __HAL_LOCK(hdma);
  94. /* Set the new synchronization parameters (and keep the request ID filled during the Init)*/
  95. MODIFY_REG(hdma->DMAmuxChannel->CCR, \
  96. (~DMAMUX_CxCR_DMAREQ_ID), \
  97. ((pSyncConfig->SyncSignalID) << DMAMUX_CxCR_SYNC_ID_Pos) | ((pSyncConfig->RequestNumber - 1U) << DMAMUX_CxCR_NBREQ_Pos) | \
  98. pSyncConfig->SyncPolarity | ((uint32_t)pSyncConfig->SyncEnable << DMAMUX_CxCR_SE_Pos) | \
  99. ((uint32_t)pSyncConfig->EventEnable << DMAMUX_CxCR_EGE_Pos));
  100. /* Process UnLocked */
  101. __HAL_UNLOCK(hdma);
  102. return HAL_OK;
  103. }
  104. else
  105. {
  106. /*DMA State not Ready*/
  107. return HAL_ERROR;
  108. }
  109. }
  110. /**
  111. * @brief Configure the DMAMUX request generator block used by the given DMA channel (instance).
  112. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  113. * the configuration information for the specified DMA channel.
  114. * @param pRequestGeneratorConfig : pointer to HAL_DMA_MuxRequestGeneratorConfigTypeDef :
  115. * contains the request generator parameters.
  116. *
  117. * @retval HAL status
  118. */
  119. HAL_StatusTypeDef HAL_DMAEx_ConfigMuxRequestGenerator(DMA_HandleTypeDef *hdma,
  120. HAL_DMA_MuxRequestGeneratorConfigTypeDef *pRequestGeneratorConfig)
  121. {
  122. /* Check the parameters */
  123. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  124. assert_param(IS_DMAMUX_REQUEST_GEN_SIGNAL_ID(pRequestGeneratorConfig->SignalID));
  125. assert_param(IS_DMAMUX_REQUEST_GEN_POLARITY(pRequestGeneratorConfig->Polarity));
  126. assert_param(IS_DMAMUX_REQUEST_GEN_REQUEST_NUMBER(pRequestGeneratorConfig->RequestNumber));
  127. /* check if the DMA state is ready
  128. and DMA is using a DMAMUX request generator block
  129. */
  130. if ((hdma->State == HAL_DMA_STATE_READY) && (hdma->DMAmuxRequestGen != 0U))
  131. {
  132. /* Process Locked */
  133. __HAL_LOCK(hdma);
  134. /* Set the request generator new parameters */
  135. hdma->DMAmuxRequestGen->RGCR = pRequestGeneratorConfig->SignalID | \
  136. ((pRequestGeneratorConfig->RequestNumber - 1U) << (POSITION_VAL(DMAMUX_RGxCR_GNBREQ) & 0x1FU)) | \
  137. pRequestGeneratorConfig->Polarity;
  138. /* Process UnLocked */
  139. __HAL_UNLOCK(hdma);
  140. return HAL_OK;
  141. }
  142. else
  143. {
  144. return HAL_ERROR;
  145. }
  146. }
  147. /**
  148. * @brief Enable the DMAMUX request generator block used by the given DMA channel (instance).
  149. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  150. * the configuration information for the specified DMA channel.
  151. * @retval HAL status
  152. */
  153. HAL_StatusTypeDef HAL_DMAEx_EnableMuxRequestGenerator(DMA_HandleTypeDef *hdma)
  154. {
  155. /* Check the parameters */
  156. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  157. /* check if the DMA state is ready
  158. and DMA is using a DMAMUX request generator block
  159. */
  160. if ((hdma->State != HAL_DMA_STATE_RESET) && (hdma->DMAmuxRequestGen != 0))
  161. {
  162. /* Enable the request generator*/
  163. hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_GE;
  164. return HAL_OK;
  165. }
  166. else
  167. {
  168. return HAL_ERROR;
  169. }
  170. }
  171. /**
  172. * @brief Disable the DMAMUX request generator block used by the given DMA channel (instance).
  173. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  174. * the configuration information for the specified DMA channel.
  175. * @retval HAL status
  176. */
  177. HAL_StatusTypeDef HAL_DMAEx_DisableMuxRequestGenerator(DMA_HandleTypeDef *hdma)
  178. {
  179. /* Check the parameters */
  180. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  181. /* check if the DMA state is ready
  182. and DMA is using a DMAMUX request generator block
  183. */
  184. if ((hdma->State != HAL_DMA_STATE_RESET) && (hdma->DMAmuxRequestGen != 0))
  185. {
  186. /* Disable the request generator*/
  187. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_GE;
  188. return HAL_OK;
  189. }
  190. else
  191. {
  192. return HAL_ERROR;
  193. }
  194. }
  195. /**
  196. * @brief Handles DMAMUX interrupt request.
  197. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  198. * the configuration information for the specified DMA channel.
  199. * @retval None
  200. */
  201. void HAL_DMAEx_MUX_IRQHandler(DMA_HandleTypeDef *hdma)
  202. {
  203. /* Check for DMAMUX Synchronization overrun */
  204. if ((hdma->DMAmuxChannelStatus->CSR & hdma->DMAmuxChannelStatusMask) != 0U)
  205. {
  206. /* Disable the synchro overrun interrupt */
  207. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  208. /* Clear the DMAMUX synchro overrun flag */
  209. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  210. /* Update error code */
  211. hdma->ErrorCode |= HAL_DMA_ERROR_SYNC;
  212. if (hdma->XferErrorCallback != NULL)
  213. {
  214. /* Transfer error callback */
  215. hdma->XferErrorCallback(hdma);
  216. }
  217. }
  218. if (hdma->DMAmuxRequestGen != 0)
  219. {
  220. /* if using a DMAMUX request generator block Check for DMAMUX request generator overrun */
  221. if ((hdma->DMAmuxRequestGenStatus->RGSR & hdma->DMAmuxRequestGenStatusMask) != 0U)
  222. {
  223. /* Disable the request gen overrun interrupt */
  224. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  225. /* Clear the DMAMUX request generator overrun flag */
  226. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  227. /* Update error code */
  228. hdma->ErrorCode |= HAL_DMA_ERROR_REQGEN;
  229. if (hdma->XferErrorCallback != NULL)
  230. {
  231. /* Transfer error callback */
  232. hdma->XferErrorCallback(hdma);
  233. }
  234. }
  235. }
  236. }
  237. /**
  238. * @}
  239. */
  240. /**
  241. * @}
  242. */
  243. #endif /* HAL_DMA_MODULE_ENABLED */
  244. /**
  245. * @}
  246. */
  247. /**
  248. * @}
  249. */