stm32l4xx_hal_dma_ex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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) 2017 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 "stm32l4xx_hal.h"
  40. #if defined(DMAMUX1)
  41. /** @addtogroup STM32L4xx_HAL_Driver
  42. * @{
  43. */
  44. /** @defgroup DMAEx DMAEx
  45. * @brief DMA Extended HAL module driver
  46. * @{
  47. */
  48. #ifdef HAL_DMA_MODULE_ENABLED
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. /* Private macro -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private Constants ---------------------------------------------------------*/
  54. /* Private function prototypes -----------------------------------------------*/
  55. /* Exported functions --------------------------------------------------------*/
  56. /** @defgroup DMAEx_Exported_Functions DMAEx Exported Functions
  57. * @{
  58. */
  59. /** @defgroup DMAEx_Exported_Functions_Group1 DMAEx Extended features functions
  60. * @brief Extended features functions
  61. *
  62. @verbatim
  63. ===============================================================================
  64. ##### Extended features functions #####
  65. ===============================================================================
  66. [..] This section provides functions allowing to:
  67. (+) Configure the DMAMUX Synchronization Block using HAL_DMAEx_ConfigMuxSync function.
  68. (+) Configure the DMAMUX Request Generator Block using HAL_DMAEx_ConfigMuxRequestGenerator function.
  69. Functions HAL_DMAEx_EnableMuxRequestGenerator and HAL_DMAEx_DisableMuxRequestGenerator can then be used
  70. to respectively enable/disable the request generator.
  71. (+) Handle DMAMUX interrupts using HAL_DMAEx_MUX_IRQHandler : should be called from
  72. the DMAMUX IRQ handler
  73. @endverbatim
  74. * @{
  75. */
  76. /**
  77. * @brief Configure the DMAMUX synchronization parameters for a given DMA channel (instance).
  78. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  79. * the configuration information for the specified DMA channel.
  80. * @param pSyncConfig Pointer to HAL_DMA_MuxSyncConfigTypeDef contains the DMAMUX synchronization parameters
  81. * @retval HAL status
  82. */
  83. HAL_StatusTypeDef HAL_DMAEx_ConfigMuxSync(DMA_HandleTypeDef *hdma, HAL_DMA_MuxSyncConfigTypeDef *pSyncConfig)
  84. {
  85. /* Check the parameters */
  86. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  87. assert_param(IS_DMAMUX_SYNC_SIGNAL_ID(pSyncConfig->SyncSignalID));
  88. assert_param(IS_DMAMUX_SYNC_POLARITY(pSyncConfig-> SyncPolarity));
  89. assert_param(IS_DMAMUX_SYNC_STATE(pSyncConfig->SyncEnable));
  90. assert_param(IS_DMAMUX_SYNC_EVENT(pSyncConfig->EventEnable));
  91. assert_param(IS_DMAMUX_SYNC_REQUEST_NUMBER(pSyncConfig->RequestNumber));
  92. /*Check if the DMA state is ready */
  93. if (hdma->State == HAL_DMA_STATE_READY)
  94. {
  95. /* Process Locked */
  96. __HAL_LOCK(hdma);
  97. /* Set the new synchronization parameters (and keep the request ID filled during the Init)*/
  98. MODIFY_REG(hdma->DMAmuxChannel->CCR, \
  99. (~DMAMUX_CxCR_DMAREQ_ID), \
  100. (pSyncConfig->SyncSignalID | ((pSyncConfig->RequestNumber - 1U) << DMAMUX_CxCR_NBREQ_Pos) | \
  101. pSyncConfig->SyncPolarity | ((uint32_t)pSyncConfig->SyncEnable << DMAMUX_CxCR_SE_Pos) | \
  102. ((uint32_t)pSyncConfig->EventEnable << DMAMUX_CxCR_EGE_Pos)));
  103. /* Process UnLocked */
  104. __HAL_UNLOCK(hdma);
  105. return HAL_OK;
  106. }
  107. else
  108. {
  109. /* Set the error code to busy */
  110. hdma->ErrorCode = HAL_DMA_ERROR_BUSY;
  111. /* Return error status */
  112. return HAL_ERROR;
  113. }
  114. }
  115. /**
  116. * @brief Configure the DMAMUX request generator block used by the given DMA channel (instance).
  117. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  118. * the configuration information for the specified DMA channel.
  119. * @param pRequestGeneratorConfig Pointer to HAL_DMA_MuxRequestGeneratorConfigTypeDef
  120. * contains the request generator parameters.
  121. *
  122. * @retval HAL status
  123. */
  124. HAL_StatusTypeDef HAL_DMAEx_ConfigMuxRequestGenerator(DMA_HandleTypeDef *hdma,
  125. HAL_DMA_MuxRequestGeneratorConfigTypeDef *pRequestGeneratorConfig)
  126. {
  127. HAL_StatusTypeDef status;
  128. HAL_DMA_StateTypeDef temp_state = hdma->State;
  129. /* Check the parameters */
  130. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  131. assert_param(IS_DMAMUX_REQUEST_GEN_SIGNAL_ID(pRequestGeneratorConfig->SignalID));
  132. assert_param(IS_DMAMUX_REQUEST_GEN_POLARITY(pRequestGeneratorConfig->Polarity));
  133. assert_param(IS_DMAMUX_REQUEST_GEN_REQUEST_NUMBER(pRequestGeneratorConfig->RequestNumber));
  134. /* check if the DMA state is ready
  135. and DMA is using a DMAMUX request generator block
  136. */
  137. if (hdma->DMAmuxRequestGen == 0U)
  138. {
  139. /* Set the error code to busy */
  140. hdma->ErrorCode = HAL_DMA_ERROR_PARAM;
  141. /* error status */
  142. status = HAL_ERROR;
  143. }
  144. else if (((hdma->DMAmuxRequestGen->RGCR & DMAMUX_RGxCR_GE) == 0U) && (temp_state == HAL_DMA_STATE_READY))
  145. {
  146. /* RequestGenerator must be disable prior to the configuration i.e GE bit is 0 */
  147. /* Process Locked */
  148. __HAL_LOCK(hdma);
  149. /* Set the request generator new parameters*/
  150. hdma->DMAmuxRequestGen->RGCR = pRequestGeneratorConfig->SignalID | \
  151. ((pRequestGeneratorConfig->RequestNumber - 1U) << DMAMUX_RGxCR_GNBREQ_Pos) | \
  152. pRequestGeneratorConfig->Polarity;
  153. /* Process UnLocked */
  154. __HAL_UNLOCK(hdma);
  155. return HAL_OK;
  156. }
  157. else
  158. {
  159. /* Set the error code to busy */
  160. hdma->ErrorCode = HAL_DMA_ERROR_BUSY;
  161. /* error status */
  162. status = HAL_ERROR;
  163. }
  164. return status;
  165. }
  166. /**
  167. * @brief Enable the DMAMUX request generator block used by the given DMA channel (instance).
  168. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  169. * the configuration information for the specified DMA channel.
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_DMAEx_EnableMuxRequestGenerator(DMA_HandleTypeDef *hdma)
  173. {
  174. /* Check the parameters */
  175. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  176. /* check if the DMA state is ready
  177. and DMA is using a DMAMUX request generator block
  178. */
  179. if ((hdma->State != HAL_DMA_STATE_RESET) && (hdma->DMAmuxRequestGen != 0))
  180. {
  181. /* Enable the request generator*/
  182. hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_GE;
  183. return HAL_OK;
  184. }
  185. else
  186. {
  187. return HAL_ERROR;
  188. }
  189. }
  190. /**
  191. * @brief Disable the DMAMUX request generator block used by the given DMA channel (instance).
  192. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  193. * the configuration information for the specified DMA channel.
  194. * @retval HAL status
  195. */
  196. HAL_StatusTypeDef HAL_DMAEx_DisableMuxRequestGenerator(DMA_HandleTypeDef *hdma)
  197. {
  198. /* Check the parameters */
  199. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  200. /* check if the DMA state is ready
  201. and DMA is using a DMAMUX request generator block
  202. */
  203. if ((hdma->State != HAL_DMA_STATE_RESET) && (hdma->DMAmuxRequestGen != 0))
  204. {
  205. /* Disable the request generator*/
  206. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_GE;
  207. return HAL_OK;
  208. }
  209. else
  210. {
  211. return HAL_ERROR;
  212. }
  213. }
  214. /**
  215. * @brief Handles DMAMUX interrupt request.
  216. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  217. * the configuration information for the specified DMA channel.
  218. * @retval None
  219. */
  220. void HAL_DMAEx_MUX_IRQHandler(DMA_HandleTypeDef *hdma)
  221. {
  222. /* Check for DMAMUX Synchronization overrun */
  223. if ((hdma->DMAmuxChannelStatus->CSR & hdma->DMAmuxChannelStatusMask) != 0U)
  224. {
  225. /* Disable the synchro overrun interrupt */
  226. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  227. /* Clear the DMAMUX synchro overrun flag */
  228. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  229. /* Update error code */
  230. hdma->ErrorCode |= HAL_DMA_ERROR_SYNC;
  231. if (hdma->XferErrorCallback != NULL)
  232. {
  233. /* Transfer error callback */
  234. hdma->XferErrorCallback(hdma);
  235. }
  236. }
  237. if (hdma->DMAmuxRequestGen != 0)
  238. {
  239. /* if using a DMAMUX request generator block Check for DMAMUX request generator overrun */
  240. if ((hdma->DMAmuxRequestGenStatus->RGSR & hdma->DMAmuxRequestGenStatusMask) != 0U)
  241. {
  242. /* Disable the request gen overrun interrupt */
  243. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  244. /* Clear the DMAMUX request generator overrun flag */
  245. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  246. /* Update error code */
  247. hdma->ErrorCode |= HAL_DMA_ERROR_REQGEN;
  248. if (hdma->XferErrorCallback != NULL)
  249. {
  250. /* Transfer error callback */
  251. hdma->XferErrorCallback(hdma);
  252. }
  253. }
  254. }
  255. }
  256. /**
  257. * @}
  258. */
  259. /**
  260. * @}
  261. */
  262. #endif /* HAL_DMA_MODULE_ENABLED */
  263. /**
  264. * @}
  265. */
  266. /**
  267. * @}
  268. */
  269. #endif /* DMAMUX1 */
  270. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/