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