stm32g0xx_hal_dma_ex.c 11 KB

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