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