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 
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) 2017 STMicroelectronics</center></h2>
33   *
34   * Redistribution and use in source and binary forms, with or without modification,
35   * are permitted provided that the following conditions are met:
36   *   1. Redistributions of source code must retain the above copyright notice,
37   *      this list of conditions and the following disclaimer.
38   *   2. Redistributions in binary form must reproduce the above copyright notice,
39   *      this list of conditions and the following disclaimer in the documentation
40   *      and/or other materials provided with the distribution.
41   *   3. Neither the name of STMicroelectronics nor the names of its contributors
42   *      may be used to endorse or promote products derived from this software
43   *      without specific prior written permission.
44   *
45   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
49   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
52   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55   *
56   ******************************************************************************
57   */
58 
59 /* Includes ------------------------------------------------------------------*/
60 #include "stm32l4xx_hal.h"
61 
62 #if defined(DMAMUX1)
63 
64 /** @addtogroup STM32L4xx_HAL_Driver
65   * @{
66   */
67 
68 /** @defgroup DMAEx DMAEx
69   * @brief DMA Extended HAL module driver
70   * @{
71   */
72 
73 #ifdef HAL_DMA_MODULE_ENABLED
74 
75 /* Private typedef -----------------------------------------------------------*/
76 /* Private define ------------------------------------------------------------*/
77 /* Private macro -------------------------------------------------------------*/
78 /* Private variables ---------------------------------------------------------*/
79 /* Private Constants ---------------------------------------------------------*/
80 /* Private function prototypes -----------------------------------------------*/
81 /* Private functions ---------------------------------------------------------*/
82 
83 
84 /** @defgroup DMAEx_Exported_Functions DMAEx Exported Functions
85   * @{
86   */
87 
88 /** @defgroup DMAEx_Exported_Functions_Group1 DMAEx Extended features functions
89  *  @brief   Extended features functions
90  *
91 @verbatim
92  ===============================================================================
93                 #####  Extended features functions  #####
94  ===============================================================================
95     [..]  This section provides functions allowing to:
96 
97     (+) Configure the DMAMUX Synchronization Block using HAL_DMAEx_ConfigMuxSync function.
98     (+) Configure the DMAMUX Request Generator Block using HAL_DMAEx_ConfigMuxRequestGenerator function.
99        Functions HAL_DMAEx_EnableMuxRequestGenerator and HAL_DMAEx_DisableMuxRequestGenerator can then be used
100        to respectively enable/disable the request generator.
101 
102 @endverbatim
103   * @{
104   */
105 
106 
107 /**
108   * @brief  Configure the DMAMUX synchronization parameters for a given DMA channel (instance).
109   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains
110   *                     the configuration information for the specified DMA channel.
111   * @param  pSyncConfig : pointer to HAL_DMA_MuxSyncConfigTypeDef : contains the DMAMUX synchronization parameters
112   * @retval HAL status
113   */
HAL_DMAEx_ConfigMuxSync(DMA_HandleTypeDef * hdma,HAL_DMA_MuxSyncConfigTypeDef * pSyncConfig)114 HAL_StatusTypeDef HAL_DMAEx_ConfigMuxSync(DMA_HandleTypeDef *hdma, HAL_DMA_MuxSyncConfigTypeDef *pSyncConfig)
115 {
116   /* Check the parameters */
117   assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
118 
119   assert_param(IS_DMAMUX_SYNC_SIGNAL_ID(pSyncConfig->SyncSignalID));
120 
121   assert_param(IS_DMAMUX_SYNC_POLARITY(pSyncConfig-> SyncPolarity));
122   assert_param(IS_DMAMUX_SYNC_STATE(pSyncConfig->SyncEnable));
123   assert_param(IS_DMAMUX_SYNC_EVENT(pSyncConfig->EventEnable));
124   assert_param(IS_DMAMUX_SYNC_REQUEST_NUMBER(pSyncConfig->RequestNumber));
125 
126   /*Check if the DMA state is ready */
127   if(hdma->State == HAL_DMA_STATE_READY)
128   {
129     /* Process Locked */
130     __HAL_LOCK(hdma);
131 
132     /* Set the new synchronization parameters (and keep the request ID filled during the Init)*/
133     MODIFY_REG( hdma->DMAmuxChannel->CCR, \
134                (~DMAMUX_CxCR_DMAREQ_ID) , \
135                ((pSyncConfig->SyncSignalID) << DMAMUX_CxCR_SYNC_ID_Pos) | ((pSyncConfig->RequestNumber - 1U) << DMAMUX_CxCR_NBREQ_Pos) | \
136                pSyncConfig->SyncPolarity | ((uint32_t)pSyncConfig->SyncEnable << DMAMUX_CxCR_SE_Pos) | \
137                  ((uint32_t)pSyncConfig->EventEnable << DMAMUX_CxCR_EGE_Pos));
138 
139     /* Process UnLocked */
140     __HAL_UNLOCK(hdma);
141 
142     return HAL_OK;
143   }
144   else
145   {
146     /*DMA State not Ready*/
147     return HAL_ERROR;
148   }
149 }
150 
151 /**
152   * @brief  Configure the DMAMUX request generator block used by the given DMA channel (instance).
153   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains
154   *                     the configuration information for the specified DMA channel.
155   * @param  pRequestGeneratorConfig : pointer to HAL_DMA_MuxRequestGeneratorConfigTypeDef :
156   *         contains the request generator parameters.
157   *
158   * @retval HAL status
159   */
HAL_DMAEx_ConfigMuxRequestGenerator(DMA_HandleTypeDef * hdma,HAL_DMA_MuxRequestGeneratorConfigTypeDef * pRequestGeneratorConfig)160 HAL_StatusTypeDef HAL_DMAEx_ConfigMuxRequestGenerator (DMA_HandleTypeDef *hdma, HAL_DMA_MuxRequestGeneratorConfigTypeDef *pRequestGeneratorConfig)
161 {
162   /* Check the parameters */
163   assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
164 
165   assert_param(IS_DMAMUX_REQUEST_GEN_SIGNAL_ID(pRequestGeneratorConfig->SignalID));
166 
167   assert_param(IS_DMAMUX_REQUEST_GEN_POLARITY(pRequestGeneratorConfig->Polarity));
168   assert_param(IS_DMAMUX_REQUEST_GEN_REQUEST_NUMBER(pRequestGeneratorConfig->RequestNumber));
169 
170   /* check if the DMA state is ready
171      and DMA is using a DMAMUX request generator block
172   */
173   if((hdma->State == HAL_DMA_STATE_READY) && (hdma->DMAmuxRequestGen != 0U))
174   {
175     /* Process Locked */
176     __HAL_LOCK(hdma);
177 
178     /* Set the request generator new parameters */
179     hdma->DMAmuxRequestGen->RGCR = pRequestGeneratorConfig->SignalID | \
180                                   ((pRequestGeneratorConfig->RequestNumber - 1U) << DMAMUX_RGxCR_GNBREQ_Pos)| \
181                                   pRequestGeneratorConfig->Polarity;
182    /* Process UnLocked */
183    __HAL_UNLOCK(hdma);
184 
185    return HAL_OK;
186  }
187  else
188  {
189    return HAL_ERROR;
190  }
191 }
192 
193 /**
194   * @brief  Enable the DMAMUX request generator block used by the given DMA channel (instance).
195   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains
196   *                     the configuration information for the specified DMA channel.
197   * @retval HAL status
198   */
HAL_DMAEx_EnableMuxRequestGenerator(DMA_HandleTypeDef * hdma)199 HAL_StatusTypeDef HAL_DMAEx_EnableMuxRequestGenerator (DMA_HandleTypeDef *hdma)
200 {
201   /* Check the parameters */
202   assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
203 
204   /* check if the DMA state is ready
205      and DMA is using a DMAMUX request generator block
206   */
207   if((hdma->State != HAL_DMA_STATE_RESET) && (hdma->DMAmuxRequestGen != 0))
208   {
209 
210     /* Enable the request generator*/
211     hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_GE;
212 
213    return HAL_OK;
214  }
215  else
216  {
217    return HAL_ERROR;
218  }
219 }
220 
221 /**
222   * @brief  Disable the DMAMUX request generator block used by the given DMA channel (instance).
223   * @param  hdma:       pointer to a DMA_HandleTypeDef structure that contains
224   *                     the configuration information for the specified DMA channel.
225   * @retval HAL status
226   */
HAL_DMAEx_DisableMuxRequestGenerator(DMA_HandleTypeDef * hdma)227 HAL_StatusTypeDef HAL_DMAEx_DisableMuxRequestGenerator (DMA_HandleTypeDef *hdma)
228 {
229   /* Check the parameters */
230   assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
231 
232   /* check if the DMA state is ready
233      and DMA is using a DMAMUX request generator block
234   */
235   if((hdma->State != HAL_DMA_STATE_RESET) && (hdma->DMAmuxRequestGen != 0))
236   {
237 
238     /* Disable the request generator*/
239     hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_GE;
240 
241     return HAL_OK;
242   }
243   else
244   {
245     return HAL_ERROR;
246   }
247 }
248 
249 /**
250   * @brief  Handles DMAMUX interrupt request.
251   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
252   *               the configuration information for the specified DMA channel.
253   * @retval None
254   */
HAL_DMAEx_MUX_IRQHandler(DMA_HandleTypeDef * hdma)255 void HAL_DMAEx_MUX_IRQHandler(DMA_HandleTypeDef *hdma)
256 {
257   /* Check for DMAMUX Synchronization overrun */
258   if((hdma->DMAmuxChannelStatus->CSR & hdma->DMAmuxChannelStatusMask) != 0U)
259   {
260     /* Disable the synchro overrun interrupt */
261     hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
262 
263     /* Clear the DMAMUX synchro overrun flag */
264     hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
265 
266     /* Update error code */
267     hdma->ErrorCode |= HAL_DMA_ERROR_SYNC;
268 
269     if(hdma->XferErrorCallback != NULL)
270     {
271       /* Transfer error callback */
272       hdma->XferErrorCallback(hdma);
273     }
274   }
275 
276   if(hdma->DMAmuxRequestGen != 0)
277   {
278    /* if using a DMAMUX request generator block Check for DMAMUX request generator overrun */
279     if((hdma->DMAmuxRequestGenStatus->RGSR & hdma->DMAmuxRequestGenStatusMask) != 0U)
280     {
281       /* Disable the request gen overrun interrupt */
282       hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
283 
284       /* Clear the DMAMUX request generator overrun flag */
285       hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
286 
287       /* Update error code */
288       hdma->ErrorCode |= HAL_DMA_ERROR_REQGEN;
289 
290       if(hdma->XferErrorCallback != NULL)
291       {
292         /* Transfer error callback */
293         hdma->XferErrorCallback(hdma);
294       }
295     }
296   }
297 }
298 
299 /**
300   * @}
301   */
302 
303 /**
304   * @}
305   */
306 
307 #endif /* HAL_DMA_MODULE_ENABLED */
308 
309 /**
310   * @}
311   */
312 
313 /**
314   * @}
315   */
316 
317 #endif /* DMAMUX1 */
318 
319 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
320