1 /**
2   ******************************************************************************
3   * @file    stm32u5xx_hal_mdf.c
4   * @author  MCD Application Team
5   * @brief   This file provides firmware functions to manage the following
6   *          functionalities of the Multi-function Digital Filter (MDF)
7   *          peripheral:
8   *           + Initialization and de-initialization
9   *           + Acquisition
10   *           + Clock absence detection
11   *           + Short circuit detection
12   *           + Out-off limit detection
13   *
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2021 STMicroelectronics.
18   * All rights reserved.
19   *
20   * This software is licensed under terms that can be found in the LICENSE file
21   * in the root directory of this software component.
22   * If no LICENSE file comes with this software, it is provided AS-IS.
23   *
24   ******************************************************************************
25   @verbatim
26   ==============================================================================
27                      ##### How to use this driver #####
28   ==============================================================================
29   [..]
30     *** Initialization and de-initialization ***
31     ============================================
32     [..]
33       (#) User has first to initialize MDF or ADF instance.
34       (#) As prerequisite, fill in the HAL_MDF_MspInit() :
35         (++) Enable MDFz or ADFz clock interface with __HAL_RCC_MDFz_CLK_ENABLE()
36              or __HAL_RCC_ADFz_CLK_ENABLE().
37         (++) Enable the clocks for the used GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
38         (++) Configure these pins in alternate mode using HAL_GPIO_Init().
39         (++) If interrupt mode is used, enable and configure MDFz_FLTx or ADFz
40              interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
41         (++) If DMA mode is used, initialize and configure DMA.
42       (#) Configure the common parameters (only for first MDF or ADF instance init),
43           serial interface parameters and filter bitstream selection by calling
44           HAL_MDF_Init() function.
45 
46     [..]
47       (#) User can de-initialize MDF or ADF instance with HAL_MDF_DeInit() function.
48 
49     *** Acquisition ***
50     ===================
51     [..]
52       (#) Configure filter parameters and start acquisition using HAL_MDF_AcqStart(),
53           HAL_MDF_AcqStart_IT() or HAL_MDF_AcqStart_DMA().
54       (#) In polling mode :
55             (++) Use HAL_MDF_PollForAcq() to detect the end of acquisition.
56                  Use HAL_MDF_GetAcqValue to get acquisition value.
57             (++) Only for MDF instance, use HAL_MDF_PollForSnapshotAcq() to detect
58                  the end of snapshot acquisition.
59                  Use HAL_MDF_GetSnapshotAcqValue to get snapshot acquisition value.
60             (++) Only for ADF instance, use HAL_MDF_PollForSndLvl() to detect and get
61                  new sound level value and ambient noise value.
62             (++) Only for ADF instance, use HAL_MDF_PollForSad() to detect sound activity.
63       (#) In interrupt mode :
64             (++) HAL_MDF_AcqCpltCallback() will be called at the end of acquisition.
65                  Use HAL_MDF_GetAcqValue to get acquisition value or use
66                  HAL_MDF_GetSnapshotAcqValue to get snapshot acquisition value.
67             (++) Only for ADF instance, HAL_MDF_SndLvlCallback() will be called when new
68                  sound level and ambient noise values are available.
69             (++) Only for ADF instance, HAL_MDF_SadCallback() will be called when
70                  sound activity detection occurs.
71             (++) HAL_MDF_ErrorCallback() will be called if overflow, filter overrun or
72                  saturation occurs.
73                  Use HAL_MDF_GetErrorCode() to get the corresponding error.
74       (#) In DMA mode :
75             (++) HAL_MDF_AcqHalfCpltCallback() and HAL_MDF_AcqCpltCallback() will be called
76                  respectively at the half acquisition and at the acquisition complete.
77             (++) Only for ADF instance, HAL_MDF_SndLvlCallback() will be called when new
78                  sound level and ambient noise values are available.
79             (++) Only for ADF instance, HAL_MDF_SadCallback() will be called when
80                  sound activity detection occurs.
81             (++) HAL_MDF_ErrorCallback() will be called if overflow, filter overrun,
82                  saturation or DMA error occurs.
83                  Use HAL_MDF_GetErrorCode() to get the corresponding error.
84       (#) Use HAL_MDF_GenerateTrgo() to generate pulse on TRGO signal.
85       (#) During acquisition, use HAL_MDF_SetDelay() and HAL_MDF_GetDelay() to respectively
86           set and get the delay on data source.
87       (#) During acquisition, use HAL_MDF_SetGain() and HAL_MDF_GetGain() to respectively
88           set and get the filter gain.
89       (#) During acquisition, use HAL_MDF_SetOffset() and HAL_MDF_GetOffset() to respectively
90           set and get the filter offset error compensation.
91       (#) During acquisition, only for MDF instance, use HAL_MDF_SetOffset() and HAL_MDF_GetOffset()
92           to respectively set and get the filter offset error compensation.
93       (#) Stop acquisition using HAL_MDF_AcqStop(), HAL_MDF_AcqStop_IT() or HAL_MDF_AcqStop_DMA().
94 
95     *** Clock absence detection ***
96     ===============================
97     [..]
98       (#) Clock absence detection is always enabled so no need to start clock absence detection
99           in polling mode.
100           Use HAL_MDF_CkabStart_IT() to start clock absence detection in interrupt mode.
101       (#) In polling mode, use HAL_MDF_PollForCkab() to detect the clock absence.
102       (#) In interrupt mode, HAL_MDF_ErrorCallback() will be called if clock absence detection
103           occurs.
104           Use HAL_MDF_GetErrorCode() to get the corresponding error.
105       (#) Stop clock absence detection in interrupt mode using HAL_MDF_CkabStop_IT().
106 
107     *** Short circuit detection ***
108     ===============================
109     [..]
110       (#) Only for MDF instance, start short circuit detection using HAL_MDF_ScdStart()
111           or HAL_MDF_ScdStart_IT().
112       (#) In polling mode, use HAL_MDF_PollForScd() to detect short circuit.
113       (#) In interrupt mode, HAL_MDF_ErrorCallback() will be called if short circuit detection
114           occurs.
115           Use HAL_MDF_GetErrorCode() to get the corresponding error.
116       (#) Stop short circuit detection using HAL_MDF_ScdStop() or HAL_MDF_ScdStop_IT().
117 
118     *** Out-off limit detection ***
119     ===============================
120     [..]
121       (#) Only for MDF instance, start out-off limit detection using HAL_MDF_OldStart()
122           or HAL_MDF_OldStart_IT().
123       (#) In polling mode, use HAL_MDF_PollForOld() to detect out-off limit and to get threshold
124           information.
125       (#) In interrupt mode, HAL_MDF_OldCallback() will be called if out-off limit detection occurs.
126       (#) Stop out-off limit detection using HAL_MDF_OldStop() or HAL_MDF_OldStop_IT().
127 
128     *** generic functions ***
129     =========================
130     [..]
131       (#) HAL_MDF_IRQHandler will be called when MDF or ADF interrupt occurs.
132       (#) HAL_MDF_ErrorCallback will be called when MDF or ADF error occurs.
133       (#) Use HAL_MDF_GetState() to get the current MDF or ADF instance state.
134       (#) Use HAL_MDF_GetErrorCode() to get the current MDF or ADF instance error code.
135 
136     *** Callback registration ***
137     =============================
138     [..]
139     The compilation define USE_HAL_MDF_REGISTER_CALLBACKS when set to 1
140     allows the user to configure dynamically the driver callbacks.
141     Use functions HAL_MDF_RegisterCallback(), HAL_MDF_RegisterOldCallback()
142     or HAL_MDF_RegisterSndLvlCallback() to register a user callback.
143 
144     [..]
145     Function HAL_MDF_RegisterCallback() allows to register following callbacks :
146       (+) AcqCpltCallback     : Acquisition complete callback.
147       (+) AcqHalfCpltCallback : Acquisition half complete callback.
148       (+) SadCallback         : Sound activity detection callback (only for ADF instance).
149       (+) ErrorCallback       : Error callback.
150       (+) MspInitCallback     : MSP init callback.
151       (+) MspDeInitCallback   : MSP de-init callback.
152     [..]
153     This function takes as parameters the HAL peripheral handle, the Callback ID
154     and a pointer to the user callback function.
155 
156     [..]
157     For MDF instance and for specific out-off limit detection callback use dedicated
158     register callback :
159     HAL_MDF_RegisterOldCallback().
160 
161     [..]
162     For ADF instance and for specific sound level callback use dedicated register callback :
163     HAL_MDF_RegisterSndLvlCallback().
164 
165     [..]
166     Use function HAL_MDF_UnRegisterCallback() to reset a callback to the default weak function.
167 
168     [..]
169     HAL_MDF_UnRegisterCallback() takes as parameters the HAL peripheral handle and the Callback ID.
170     [..]
171     This function allows to reset following callbacks :
172       (+) AcqCpltCallback     : Acquisition complete callback.
173       (+) AcqHalfCpltCallback : Acquisition half complete callback.
174       (+) SadCallback         : Sound activity detection callback (only for ADF instance).
175       (+) ErrorCallback       : Error callback.
176       (+) MspInitCallback     : MSP init callback.
177       (+) MspDeInitCallback   : MSP de-init callback.
178 
179     [..]
180     For MDF instance and for specific out-off limit detection callback use dedicated
181     unregister callback :
182     HAL_MDF_UnRegisterOldCallback().
183 
184     [..]
185     For ADF instance and for specific sound level callback use dedicated unregister callback :
186     HAL_MDF_UnRegisterSndLvlCallback().
187 
188     [..]
189     By default, after the call of init function and if the state is RESET
190     all callbacks are reset to the corresponding legacy weak functions :
191     examples HAL_MDF_AcqCpltCallback(), HAL_MDF_ErrorCallback().
192     Exception done for MspInit and MspDeInit callbacks that are respectively
193     reset to the legacy weak functions in the init and de-init only when these
194     callbacks are null (not registered beforehand).
195     If not, MspInit or MspDeInit are not null, the init and de-init keep and use
196     the user MspInit/MspDeInit callbacks (registered beforehand).
197 
198     [..]
199     Callbacks can be registered/unregistered in READY state only.
200     Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
201     in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
202     during the init/de-init.
203     In that case first register the MspInit/MspDeInit user callbacks using
204     HAL_MDF_RegisterCallback() before calling init or de-init function.
205 
206     [..]
207     When the compilation define USE_HAL_MDF_REGISTER_CALLBACKS is set to 0 or
208     not defined, the callback registering feature is not available
209     and weak callbacks are used.
210 
211     @endverbatim
212   */
213 
214 /* Includes ------------------------------------------------------------------*/
215 #include "stm32u5xx_hal.h"
216 
217 /** @addtogroup STM32U5xx_HAL_Driver
218   * @{
219   */
220 
221 /** @defgroup MDF MDF
222   * @brief MDF HAL module driver
223   * @{
224   */
225 
226 #ifdef HAL_MDF_MODULE_ENABLED
227 
228 /* Private typedef -----------------------------------------------------------*/
229 /** @defgroup MDF_Private_Typedefs  MDF Private Typedefs
230   * @{
231   */
232 /**
233   * @}
234   */
235 
236 /* Private define ------------------------------------------------------------*/
237 /** @defgroup MDF_Private_Constants  MDF Private Constants
238   * @{
239   */
240 #if defined(STM32U535xx) || defined(STM32U545xx)
241 #define MDF_INSTANCE_NUMBER 3U /* 2 instances for MDF1 and 1 instance for ADF1 */
242 #else /* defined(STM32U535xx) || defined(STM32U545xx) */
243 #define MDF_INSTANCE_NUMBER 7U /* 6 instances for MDF1 and 1 instance for ADF1 */
244 #endif /* defined(STM32U535xx) || defined(STM32U545xx) */
245 /**
246   * @}
247   */
248 
249 /* Private macro -------------------------------------------------------------*/
250 /* Private variables ---------------------------------------------------------*/
251 /** @defgroup MDF_Private_Variables  MDF Private Variables
252   * @{
253   */
254 static uint32_t           v_mdf1InstanceCounter = 0U;
255 static uint32_t           v_adf1InstanceCounter = 0U;
256 static MDF_HandleTypeDef *a_mdfHandle[MDF_INSTANCE_NUMBER] = {NULL};
257 /**
258   * @}
259   */
260 
261 /* Private function prototypes -----------------------------------------------*/
262 /** @defgroup MDF_Private_Functions  MDF Private Functions
263   * @{
264   */
265 static uint32_t MDF_GetHandleNumberFromInstance(const MDF_Filter_TypeDef *const pInstance);
266 static void     MDF_AcqStart(MDF_HandleTypeDef *const hmdf, const MDF_FilterConfigTypeDef *const pFilterConfig);
267 static void     MDF_DmaXferCpltCallback(DMA_HandleTypeDef *hdma);
268 static void     MDF_DmaXferHalfCpltCallback(DMA_HandleTypeDef *hdma);
269 static void     MDF_DmaErrorCallback(DMA_HandleTypeDef *hdma);
270 /**
271   * @}
272   */
273 
274 /* Exported functions ---------------------------------------------------------*/
275 /** @defgroup MDF_Exported_Functions  MDF Exported Functions
276   * @{
277   */
278 
279 /** @defgroup MDF_Exported_Functions_Group1  Initialization and de-initialization functions
280   * @brief    Initialization and de-initialization functions
281   *
282 @verbatim
283   ==============================================================================
284             ##### Initialization and de-initialization functions #####
285   ==============================================================================
286     [..]  This section provides functions allowing to :
287       (+) Initialize the MDF or ADF instance.
288       (+) De-initialize the MDF or ADF instance.
289       (+) Register and unregister callbacks.
290 @endverbatim
291   * @{
292   */
293 
294 /**
295   * @brief  Initialize the MDF instance according to the specified parameters
296   *         in the MDF_InitTypeDef structure and initialize the associated handle.
297   * @param  hmdf MDF handle.
298   * @retval HAL status.
299   */
HAL_MDF_Init(MDF_HandleTypeDef * hmdf)300 HAL_StatusTypeDef HAL_MDF_Init(MDF_HandleTypeDef *hmdf)
301 {
302   HAL_StatusTypeDef status = HAL_OK;
303 
304   /* Check MDF handle */
305   if (hmdf == NULL)
306   {
307     status = HAL_ERROR;
308   }
309   else
310   {
311     /* Check parameters */
312     assert_param(IS_MDF_ALL_INSTANCE(hmdf->Instance));
313     assert_param(IS_MDF_FILTER_BITSTREAM(hmdf->Init.FilterBistream));
314     assert_param(IS_FUNCTIONAL_STATE(hmdf->Init.SerialInterface.Activation));
315 
316     /* Check that instance has not been already initialized */
317     if (a_mdfHandle[MDF_GetHandleNumberFromInstance(hmdf->Instance)] != NULL)
318     {
319       status = HAL_ERROR;
320     }
321     else
322     {
323 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
324       /* Reset callback pointers to the weak predefined callbacks */
325       if (IS_ADF_INSTANCE(hmdf->Instance))
326       {
327         hmdf->OldCallback   = NULL;
328         hmdf->SndLvCallback = HAL_MDF_SndLvlCallback;
329         hmdf->SadCallback   = HAL_MDF_SadCallback;
330       }
331       else /* MDF instance */
332       {
333         hmdf->OldCallback   = HAL_MDF_OldCallback;
334         hmdf->SndLvCallback = NULL;
335         hmdf->SadCallback   = NULL;
336       }
337       hmdf->AcqCpltCallback     = HAL_MDF_AcqCpltCallback;
338       hmdf->AcqHalfCpltCallback = HAL_MDF_AcqHalfCpltCallback;
339       hmdf->ErrorCallback       = HAL_MDF_ErrorCallback;
340 
341       /* Call MDF MSP init function */
342       if (hmdf->MspInitCallback == NULL)
343       {
344         hmdf->MspInitCallback = HAL_MDF_MspInit;
345       }
346       hmdf->MspInitCallback(hmdf);
347 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
348       /* Call MDF MSP init function */
349       HAL_MDF_MspInit(hmdf);
350 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
351 
352       /* Configure common parameters only for first MDF or ADF instance */
353       if (((v_mdf1InstanceCounter == 0U) && IS_MDF_INSTANCE(hmdf->Instance)) ||
354           ((v_adf1InstanceCounter == 0U) && IS_ADF_INSTANCE(hmdf->Instance)))
355       {
356         MDF_TypeDef *mdfBase;
357         /* Get MDF base according instance */
358         mdfBase = (IS_ADF_INSTANCE(hmdf->Instance)) ? ADF1 : MDF1;
359 
360         /* Check clock generator status */
361         if ((mdfBase->CKGCR & MDF_CKGCR_CCKACTIVE) != 0U)
362         {
363           status = HAL_ERROR;
364         }
365         else
366         {
367           /* Configure number of interleaved filters for MDF instance */
368           if (IS_MDF_INSTANCE(hmdf->Instance))
369           {
370             assert_param(IS_MDF_INTERLEAVED_FILTERS(hmdf->Init.CommonParam.InterleavedFilters));
371             mdfBase->GCR &= ~(MDF_GCR_ILVNB);
372             mdfBase->GCR |= (hmdf->Init.CommonParam.InterleavedFilters << MDF_GCR_ILVNB_Pos);
373           }
374 
375           /* Configure processing clock divider, output clock divider,
376              output clock pins and output clock generation trigger */
377           assert_param(IS_MDF_PROC_CLOCK_DIVIDER(hmdf->Init.CommonParam.ProcClockDivider));
378           assert_param(IS_FUNCTIONAL_STATE(hmdf->Init.CommonParam.OutputClock.Activation));
379           mdfBase->CKGCR = 0U;
380           mdfBase->CKGCR |= ((hmdf->Init.CommonParam.ProcClockDivider - 1U) << MDF_CKGCR_PROCDIV_Pos);
381           if (hmdf->Init.CommonParam.OutputClock.Activation == ENABLE)
382           {
383             assert_param(IS_MDF_OUTPUT_CLOCK_PINS(hmdf->Init.CommonParam.OutputClock.Pins));
384             assert_param(IS_MDF_OUTPUT_CLOCK_DIVIDER(hmdf->Init.CommonParam.OutputClock.Divider));
385             assert_param(IS_FUNCTIONAL_STATE(hmdf->Init.CommonParam.OutputClock.Trigger.Activation));
386             mdfBase->CKGCR |= (((hmdf->Init.CommonParam.OutputClock.Divider - 1U) << MDF_CKGCR_CCKDIV_Pos) |
387                                hmdf->Init.CommonParam.OutputClock.Pins |
388                                (hmdf->Init.CommonParam.OutputClock.Pins >> 4U));
389             if (hmdf->Init.CommonParam.OutputClock.Trigger.Activation == ENABLE)
390             {
391               if (IS_MDF_INSTANCE(hmdf->Instance))
392               {
393                 assert_param(IS_MDF_OUTPUT_CLOCK_TRIGGER_SOURCE(hmdf->Init.CommonParam.OutputClock.Trigger.Source));
394               }
395               else /* ADF instance */
396               {
397                 assert_param(IS_ADF_OUTPUT_CLOCK_TRIGGER_SOURCE(hmdf->Init.CommonParam.OutputClock.Trigger.Source));
398               }
399               assert_param(IS_MDF_OUTPUT_CLOCK_TRIGGER_EDGE(hmdf->Init.CommonParam.OutputClock.Trigger.Edge));
400               mdfBase->CKGCR |= (hmdf->Init.CommonParam.OutputClock.Trigger.Source |
401                                  hmdf->Init.CommonParam.OutputClock.Trigger.Edge |
402                                  MDF_CKGCR_CKGMOD);
403             }
404           }
405 
406           /* Activate clock generator */
407           mdfBase->CKGCR |= MDF_CKGCR_CKDEN;
408         }
409       }
410 
411       /* Configure serial interface */
412       if ((status == HAL_OK) && (hmdf->Init.SerialInterface.Activation == ENABLE))
413       {
414         /* Check serial interface status */
415         if ((hmdf->Instance->SITFCR & MDF_SITFCR_SITFACTIVE) != 0U)
416         {
417           status = HAL_ERROR;
418         }
419         else
420         {
421           /* Configure mode, clock source and threshold */
422           assert_param(IS_MDF_SITF_MODE(hmdf->Init.SerialInterface.Mode));
423           assert_param(IS_MDF_SITF_CLOCK_SOURCE(hmdf->Init.SerialInterface.ClockSource));
424           assert_param(IS_MDF_SITF_THRESHOLD(hmdf->Init.SerialInterface.Threshold));
425           hmdf->Instance->SITFCR = 0U;
426           hmdf->Instance->SITFCR |= ((hmdf->Init.SerialInterface.Threshold << MDF_SITFCR_STH_Pos) |
427                                      hmdf->Init.SerialInterface.Mode | hmdf->Init.SerialInterface.ClockSource);
428 
429           /* Activate serial interface */
430           hmdf->Instance->SITFCR |= MDF_SITFCR_SITFEN;
431         }
432       }
433 
434       if (status == HAL_OK)
435       {
436         /* Configure filter bitstream */
437         hmdf->Instance->BSMXCR &= ~(MDF_BSMXCR_BSSEL);
438         hmdf->Instance->BSMXCR |= hmdf->Init.FilterBistream;
439 
440         /* Update instance counter and table */
441         if (IS_ADF_INSTANCE(hmdf->Instance))
442         {
443           v_adf1InstanceCounter++;
444         }
445         else /* MDF instance */
446         {
447           v_mdf1InstanceCounter++;
448         }
449         a_mdfHandle[MDF_GetHandleNumberFromInstance(hmdf->Instance)] = hmdf;
450 
451         /* Update error code and state */
452         hmdf->ErrorCode = MDF_ERROR_NONE;
453         hmdf->State     = HAL_MDF_STATE_READY;
454       }
455     }
456   }
457 
458   /* Return function status */
459   return status;
460 }
461 
462 /**
463   * @brief  De-initialize the MDF instance.
464   * @param  hmdf MDF handle.
465   * @retval HAL status.
466   */
HAL_MDF_DeInit(MDF_HandleTypeDef * hmdf)467 HAL_StatusTypeDef HAL_MDF_DeInit(MDF_HandleTypeDef *hmdf)
468 {
469   HAL_StatusTypeDef status = HAL_OK;
470 
471   /* Check MDF handle */
472   if (hmdf == NULL)
473   {
474     status = HAL_ERROR;
475   }
476   else
477   {
478     /* Check parameters */
479     assert_param(IS_MDF_ALL_INSTANCE(hmdf->Instance));
480 
481     /* Check that instance has not been already deinitialized */
482     if (a_mdfHandle[MDF_GetHandleNumberFromInstance(hmdf->Instance)] == NULL)
483     {
484       status = HAL_ERROR;
485     }
486     else
487     {
488       if (IS_MDF_INSTANCE(hmdf->Instance))
489       {
490         /* Disable short circuit detector if needed */
491         if ((hmdf->Instance->SCDCR & MDF_SCDCR_SCDACTIVE) != 0U)
492         {
493           hmdf->Instance->SCDCR &= ~(MDF_SCDCR_SCDEN);
494         }
495 
496         /* Disable out-off limit detector if needed */
497         if ((hmdf->Instance->OLDCR & MDF_OLDCR_OLDACTIVE) != 0U)
498         {
499           hmdf->Instance->OLDCR &= ~(MDF_OLDCR_OLDEN);
500         }
501       }
502 
503       /* Disable sound activity detector if needed */
504       if (IS_ADF_INSTANCE(hmdf->Instance))
505       {
506         if ((hmdf->Instance->SADCR & MDF_SADCR_SADACTIVE) != 0U)
507         {
508           hmdf->Instance->SADCR &= ~(MDF_SADCR_SADEN);
509         }
510       }
511 
512       /* Disable filter if needed */
513       if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_DFLTACTIVE) != 0U)
514       {
515         hmdf->Instance->DFLTCR &= ~(MDF_DFLTCR_DFLTEN);
516       }
517 
518       /* Disable serial interface if needed */
519       if ((hmdf->Instance->SITFCR & MDF_SITFCR_SITFACTIVE) != 0U)
520       {
521         hmdf->Instance->SITFCR &= ~(MDF_SITFCR_SITFEN);
522       }
523 
524       /* Disable all interrupts and clear all pending flags */
525       hmdf->Instance->DFLTIER = 0U;
526       hmdf->Instance->DFLTISR = 0xFFFFFFFFU;
527 
528       /* Disable clock generator only for last MDF or ADF instance deinitialization */
529       if (((v_mdf1InstanceCounter == 1U) && IS_MDF_INSTANCE(hmdf->Instance)) ||
530           ((v_adf1InstanceCounter == 1U) && IS_ADF_INSTANCE(hmdf->Instance)))
531       {
532         MDF_TypeDef *p_mdf_base;
533         /* Get MDF base according instance */
534         p_mdf_base = (IS_ADF_INSTANCE(hmdf->Instance)) ? ADF1 : MDF1;
535 
536         /* Disable clock generator */
537         p_mdf_base->CKGCR &= ~(MDF_CKGCR_CKDEN);
538       }
539 
540       /* Call MDF MSP deinit function */
541 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
542       if (hmdf->MspDeInitCallback == NULL)
543       {
544         hmdf->MspDeInitCallback = HAL_MDF_MspDeInit;
545       }
546       hmdf->MspDeInitCallback(hmdf);
547 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
548       HAL_MDF_MspDeInit(hmdf);
549 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
550 
551       /* Update instance counter and table */
552       if (IS_ADF_INSTANCE(hmdf->Instance))
553       {
554         v_adf1InstanceCounter--;
555       }
556       else /* MDF instance */
557       {
558         v_mdf1InstanceCounter--;
559       }
560       a_mdfHandle[MDF_GetHandleNumberFromInstance(hmdf->Instance)] = (MDF_HandleTypeDef *) NULL;
561 
562       /* Update state */
563       hmdf->State = HAL_MDF_STATE_RESET;
564     }
565   }
566 
567   /* Return function status */
568   return status;
569 }
570 
571 /**
572   * @brief  Initialize the MDF instance MSP.
573   * @param  hmdf MDF handle.
574   * @retval None.
575   */
HAL_MDF_MspInit(MDF_HandleTypeDef * hmdf)576 __weak void HAL_MDF_MspInit(MDF_HandleTypeDef *hmdf)
577 {
578   /* Prevent unused argument(s) compilation warning */
579   UNUSED(hmdf);
580 
581   /* NOTE : This function should not be modified, when the function is needed,
582             the HAL_MDF_MspInit could be implemented in the user file */
583 }
584 
585 /**
586   * @brief  De-initialize the MDF instance MSP.
587   * @param  hmdf MDF handle.
588   * @retval None.
589   */
HAL_MDF_MspDeInit(MDF_HandleTypeDef * hmdf)590 __weak void HAL_MDF_MspDeInit(MDF_HandleTypeDef *hmdf)
591 {
592   /* Prevent unused argument(s) compilation warning */
593   UNUSED(hmdf);
594 
595   /* NOTE : This function should not be modified, when the function is needed,
596             the HAL_MDF_MspDeInit could be implemented in the user file */
597 }
598 
599 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
600 /**
601   * @brief  Register a user MDF callback to be used instead of the weak predefined callback.
602   * @param  hmdf MDF handle.
603   * @param  CallbackID ID of the callback to be registered.
604   *         This parameter can be one of the following values:
605   *           @arg @ref HAL_MDF_ACQ_COMPLETE_CB_ID acquisition complete callback ID.
606   *           @arg @ref HAL_MDF_ACQ_HALFCOMPLETE_CB_ID acquisition half complete callback ID.
607   *           @arg @ref HAL_MDF_SAD_CB_ID sound activity detector callback ID (only for ADF instance).
608   *           @arg @ref HAL_MDF_ERROR_CB_ID error callback ID.
609   *           @arg @ref HAL_MDF_MSPINIT_CB_ID MSP init callback ID.
610   *           @arg @ref HAL_MDF_MSPDEINIT_CB_ID MSP de-init callback ID.
611   * @param  pCallback pointer to the callback function.
612   * @retval HAL status.
613   */
HAL_MDF_RegisterCallback(MDF_HandleTypeDef * hmdf,HAL_MDF_CallbackIDTypeDef CallbackID,pMDF_CallbackTypeDef pCallback)614 HAL_StatusTypeDef HAL_MDF_RegisterCallback(MDF_HandleTypeDef        *hmdf,
615                                            HAL_MDF_CallbackIDTypeDef CallbackID,
616                                            pMDF_CallbackTypeDef      pCallback)
617 {
618   HAL_StatusTypeDef status = HAL_OK;
619 
620   /* Check parameters */
621   if (pCallback == NULL)
622   {
623     /* Update error code and status */
624     hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
625     status = HAL_ERROR;
626   }
627   else
628   {
629     if (hmdf->State == HAL_MDF_STATE_READY)
630     {
631       switch (CallbackID)
632       {
633         case HAL_MDF_ACQ_COMPLETE_CB_ID :
634           hmdf->AcqCpltCallback = pCallback;
635           break;
636         case HAL_MDF_ACQ_HALFCOMPLETE_CB_ID :
637           hmdf->AcqHalfCpltCallback = pCallback;
638           break;
639         case HAL_MDF_SAD_CB_ID :
640           hmdf->SadCallback = pCallback;
641           break;
642         case HAL_MDF_ERROR_CB_ID :
643           hmdf->ErrorCallback = pCallback;
644           break;
645         case HAL_MDF_MSPINIT_CB_ID :
646           hmdf->MspInitCallback = pCallback;
647           break;
648         case HAL_MDF_MSPDEINIT_CB_ID :
649           hmdf->MspDeInitCallback = pCallback;
650           break;
651         default :
652           /* Update error code and status */
653           hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
654           status = HAL_ERROR;
655           break;
656       }
657     }
658     else if (hmdf->State == HAL_MDF_STATE_RESET)
659     {
660       switch (CallbackID)
661       {
662         case HAL_MDF_MSPINIT_CB_ID :
663           hmdf->MspInitCallback = pCallback;
664           break;
665         case HAL_MDF_MSPDEINIT_CB_ID :
666           hmdf->MspDeInitCallback = pCallback;
667           break;
668         default :
669           /* Update error code and status */
670           hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
671           status = HAL_ERROR;
672           break;
673       }
674     }
675     else
676     {
677       /* Update error code and status */
678       hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
679       status = HAL_ERROR;
680     }
681   }
682 
683   /* Return function status */
684   return status;
685 }
686 
687 /**
688   * @brief  Unregister a user MDF callback.
689   *         MDF callback is redirected to the weak predefined callback.
690   * @param  hmdf MDF handle.
691   * @param  CallbackID ID of the callback to be unregistered.
692   *         This parameter can be one of the following values:
693   *           @arg @ref HAL_MDF_ACQ_COMPLETE_CB_ID acquisition complete callback ID.
694   *           @arg @ref HAL_MDF_ACQ_HALFCOMPLETE_CB_ID acquisition half complete callback ID.
695   *           @arg @ref HAL_MDF_SAD_CB_ID sound activity detector callback ID (only for ADF instance).
696   *           @arg @ref HAL_MDF_ERROR_CB_ID error callback ID.
697   *           @arg @ref HAL_MDF_MSPINIT_CB_ID MSP init callback ID.
698   *           @arg @ref HAL_MDF_MSPDEINIT_CB_ID MSP de-init callback ID.
699   * @retval HAL status.
700   */
HAL_MDF_UnRegisterCallback(MDF_HandleTypeDef * hmdf,HAL_MDF_CallbackIDTypeDef CallbackID)701 HAL_StatusTypeDef HAL_MDF_UnRegisterCallback(MDF_HandleTypeDef        *hmdf,
702                                              HAL_MDF_CallbackIDTypeDef CallbackID)
703 {
704   HAL_StatusTypeDef status = HAL_OK;
705 
706   if (hmdf->State == HAL_MDF_STATE_READY)
707   {
708     switch (CallbackID)
709     {
710       case HAL_MDF_ACQ_COMPLETE_CB_ID :
711         hmdf->AcqCpltCallback = HAL_MDF_AcqCpltCallback;
712         break;
713       case HAL_MDF_ACQ_HALFCOMPLETE_CB_ID :
714         hmdf->AcqHalfCpltCallback = HAL_MDF_AcqHalfCpltCallback;
715         break;
716       case HAL_MDF_SAD_CB_ID :
717         hmdf->SadCallback = HAL_MDF_SadCallback;
718         break;
719       case HAL_MDF_ERROR_CB_ID :
720         hmdf->ErrorCallback = HAL_MDF_ErrorCallback;
721         break;
722       case HAL_MDF_MSPINIT_CB_ID :
723         hmdf->MspInitCallback = HAL_MDF_MspInit;
724         break;
725       case HAL_MDF_MSPDEINIT_CB_ID :
726         hmdf->MspDeInitCallback = HAL_MDF_MspDeInit;
727         break;
728       default :
729         /* Update error code and status */
730         hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
731         status = HAL_ERROR;
732         break;
733     }
734   }
735   else if (hmdf->State == HAL_MDF_STATE_RESET)
736   {
737     switch (CallbackID)
738     {
739       case HAL_MDF_MSPINIT_CB_ID :
740         hmdf->MspInitCallback = HAL_MDF_MspInit;
741         break;
742       case HAL_MDF_MSPDEINIT_CB_ID :
743         hmdf->MspDeInitCallback = HAL_MDF_MspDeInit;
744         break;
745       default :
746         /* Update error code and status */
747         hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
748         status = HAL_ERROR;
749         break;
750     }
751   }
752   else
753   {
754     /* Update error code and status */
755     hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
756     status = HAL_ERROR;
757   }
758 
759   /* Return function status */
760   return status;
761 }
762 
763 /**
764   * @brief  Register specific MDF out-off limit detector callback
765   *         to be used instead of the weak predefined callback.
766   * @param  hmdf MDF handle.
767   * @param  pCallback pointer to the out-off limit detector callback function.
768   * @retval HAL status.
769   * @note   This function must not be used with ADF instance.
770   */
HAL_MDF_RegisterOldCallback(MDF_HandleTypeDef * hmdf,pMDF_OldCallbackTypeDef pCallback)771 HAL_StatusTypeDef HAL_MDF_RegisterOldCallback(MDF_HandleTypeDef      *hmdf,
772                                               pMDF_OldCallbackTypeDef pCallback)
773 {
774   HAL_StatusTypeDef status = HAL_OK;
775 
776   /* Check parameters */
777   if (pCallback == NULL)
778   {
779     /* Update error code and status */
780     hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
781     status = HAL_ERROR;
782   }
783   else
784   {
785     if (hmdf->State == HAL_MDF_STATE_READY)
786     {
787       hmdf->OldCallback = pCallback;
788     }
789     else
790     {
791       /* Update error code and status */
792       hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
793       status = HAL_ERROR;
794     }
795   }
796 
797   /* Return function status */
798   return status;
799 }
800 
801 /**
802   * @brief  Unregister the specific MDF out-off limit detector callback.
803   *         MDF out-off limit detector callback is redirected to the weak predefined callback.
804   * @param  hmdf MDF handle.
805   * @retval HAL status.
806   * @note   This function must not be used with ADF instance.
807   */
HAL_MDF_UnRegisterOldCallback(MDF_HandleTypeDef * hmdf)808 HAL_StatusTypeDef HAL_MDF_UnRegisterOldCallback(MDF_HandleTypeDef *hmdf)
809 {
810   HAL_StatusTypeDef status = HAL_OK;
811 
812   if (hmdf->State == HAL_MDF_STATE_READY)
813   {
814     hmdf->OldCallback = HAL_MDF_OldCallback;
815   }
816   else
817   {
818     /* Update error code and status */
819     hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
820     status = HAL_ERROR;
821   }
822 
823   /* Return function status */
824   return status;
825 }
826 
827 /**
828   * @brief  Register specific MDF sound level callback
829   *         to be used instead of the weak predefined callback.
830   * @param  hmdf MDF handle.
831   * @param  pCallback pointer to the sound level callback function.
832   * @retval HAL status.
833   * @note   This function must not be used with MDF instance.
834   */
HAL_MDF_RegisterSndLvlCallback(MDF_HandleTypeDef * hmdf,pMDF_SndLvlCallbackTypeDef pCallback)835 HAL_StatusTypeDef HAL_MDF_RegisterSndLvlCallback(MDF_HandleTypeDef         *hmdf,
836                                                  pMDF_SndLvlCallbackTypeDef pCallback)
837 {
838   HAL_StatusTypeDef status = HAL_OK;
839 
840   /* Check parameters */
841   if (pCallback == NULL)
842   {
843     /* Update error code and status */
844     hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
845     status = HAL_ERROR;
846   }
847   else
848   {
849     if (hmdf->State == HAL_MDF_STATE_READY)
850     {
851       hmdf->SndLvCallback = pCallback;
852     }
853     else
854     {
855       /* Update error code and status */
856       hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
857       status = HAL_ERROR;
858     }
859   }
860 
861   /* Return function status */
862   return status;
863 }
864 
865 /**
866   * @brief  Unregister the specific MDF sound level callback.
867   *         MDF sound level callback is redirected to the weak predefined callback.
868   * @param  hmdf MDF handle.
869   * @retval HAL status.
870   * @note   This function must not be used with MDF instance.
871   */
HAL_MDF_UnRegisterSndLvlCallback(MDF_HandleTypeDef * hmdf)872 HAL_StatusTypeDef HAL_MDF_UnRegisterSndLvlCallback(MDF_HandleTypeDef *hmdf)
873 {
874   HAL_StatusTypeDef status = HAL_OK;
875 
876   if (hmdf->State == HAL_MDF_STATE_READY)
877   {
878     hmdf->SndLvCallback = HAL_MDF_SndLvlCallback;
879   }
880   else
881   {
882     /* Update error code and status */
883     hmdf->ErrorCode |= MDF_ERROR_INVALID_CALLBACK;
884     status = HAL_ERROR;
885   }
886 
887   /* Return function status */
888   return status;
889 }
890 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
891 
892 /**
893   * @}
894   */
895 
896 /** @defgroup MDF_Exported_Functions_Group2  Acquisition functions
897   * @brief    Acquisition functions
898   *
899 @verbatim
900   ==============================================================================
901                         ##### Acquisition functions #####
902   ==============================================================================
903     [..]  This section provides functions allowing to :
904       (+) Start and stop acquisition in polling, interrupt or DMA mode.
905       (+) Wait and get acquisition values.
906       (+) Generate pulse on TRGO signal.
907       (+) Modify and get some filter parameters during acquisition.
908       (+) Wait and get sound level values for ADF instance.
909       (+) Detect sound activity for ADF instance.
910 @endverbatim
911   * @{
912   */
913 
914 /**
915   * @brief  This function allows to start acquisition in polling mode.
916   * @param  hmdf MDF handle.
917   * @param  pFilterConfig Filter configuration parameters.
918   * @retval HAL status.
919   */
HAL_MDF_AcqStart(MDF_HandleTypeDef * hmdf,const MDF_FilterConfigTypeDef * pFilterConfig)920 HAL_StatusTypeDef HAL_MDF_AcqStart(MDF_HandleTypeDef *hmdf, const MDF_FilterConfigTypeDef *pFilterConfig)
921 {
922   HAL_StatusTypeDef status = HAL_OK;
923 
924   /* Check parameters */
925   if (pFilterConfig == NULL)
926   {
927     status = HAL_ERROR;
928   }
929   else
930   {
931     if (IS_ADF_INSTANCE(hmdf->Instance))
932     {
933       assert_param(IS_ADF_ACQUISITION_MODE(pFilterConfig->AcquisitionMode));
934       assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->SoundActivity.Activation));
935     }
936     else
937     {
938       assert_param(IS_MDF_ACQUISITION_MODE(pFilterConfig->AcquisitionMode));
939     }
940     if ((IS_ADF_INSTANCE(hmdf->Instance)) && (pFilterConfig->SoundActivity.Activation == ENABLE) &&
941         ((pFilterConfig->AcquisitionMode == MDF_MODE_ASYNC_SINGLE) ||
942          (pFilterConfig->AcquisitionMode == MDF_MODE_SYNC_SINGLE) ||
943          (pFilterConfig->AcquisitionMode == MDF_MODE_WINDOW_CONT)))
944     {
945       status = HAL_ERROR;
946     }
947     /* Check state */
948     else if (hmdf->State != HAL_MDF_STATE_READY)
949     {
950       status = HAL_ERROR;
951     }
952     /* Check filter status */
953     else if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_DFLTACTIVE) != 0U)
954     {
955       status = HAL_ERROR;
956     }
957     else
958     {
959       /* For ADF instance, check SAD status */
960       if (IS_ADF_INSTANCE(hmdf->Instance))
961       {
962         if ((hmdf->Instance->SADCR & MDF_SADCR_SADACTIVE) != 0U)
963         {
964           status = HAL_ERROR;
965         }
966       }
967     }
968 
969     if (status == HAL_OK)
970     {
971       /* For MDF instance, check OLD status and main filter order */
972       assert_param(IS_MDF_CIC_MODE(pFilterConfig->CicMode));
973       if (IS_MDF_INSTANCE(hmdf->Instance))
974       {
975         if (((hmdf->Instance->OLDCR & MDF_OLDCR_OLDACTIVE) != 0U) && (pFilterConfig->CicMode >= MDF_ONE_FILTER_SINC4))
976         {
977           status = HAL_ERROR;
978         }
979       }
980 
981       if (status == HAL_OK)
982       {
983         /* Configure filter and start acquisition */
984         hmdf->Instance->DFLTCR = 0U;
985         MDF_AcqStart(hmdf, pFilterConfig);
986       }
987     }
988   }
989 
990   /* Return function status */
991   return status;
992 }
993 
994 /**
995   * @brief  This function allows to poll for available acquisition value.
996   * @param  hmdf MDF handle.
997   * @param  Timeout Timeout value in milliseconds.
998   * @retval HAL status.
999   */
HAL_MDF_PollForAcq(MDF_HandleTypeDef * hmdf,uint32_t Timeout)1000 HAL_StatusTypeDef HAL_MDF_PollForAcq(MDF_HandleTypeDef *hmdf, uint32_t Timeout)
1001 {
1002   HAL_StatusTypeDef status = HAL_OK;
1003 
1004   /* Check state */
1005   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1006   {
1007     status = HAL_ERROR;
1008   }
1009   else
1010   {
1011     uint32_t tickstart = HAL_GetTick();
1012 
1013     /* Wait for available acquisition value */
1014     while (((hmdf->Instance->DFLTISR & MDF_DFLTISR_RXNEF) != MDF_DFLTISR_RXNEF) && (status == HAL_OK))
1015     {
1016       /* Check the timeout */
1017       if (Timeout != HAL_MAX_DELAY)
1018       {
1019         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1020         {
1021           status = HAL_TIMEOUT;
1022         }
1023       }
1024     }
1025 
1026     /* Check if data overflow, saturation or reshape filter occurs */
1027     uint32_t error_flags = (hmdf->Instance->DFLTISR & (MDF_DFLTISR_DOVRF | MDF_DFLTISR_SATF | MDF_DFLTISR_RFOVRF));
1028     if (error_flags != 0U)
1029     {
1030       /* Update error code */
1031       if ((error_flags & MDF_DFLTISR_DOVRF) == MDF_DFLTISR_DOVRF)
1032       {
1033         hmdf->ErrorCode |= MDF_ERROR_ACQUISITION_OVERFLOW;
1034       }
1035       if ((error_flags & MDF_DFLTISR_SATF) == MDF_DFLTISR_SATF)
1036       {
1037         hmdf->ErrorCode |= MDF_ERROR_SATURATION;
1038       }
1039       if ((error_flags & MDF_DFLTISR_RFOVRF) == MDF_DFLTISR_RFOVRF)
1040       {
1041         hmdf->ErrorCode |= MDF_ERROR_RSF_OVERRUN;
1042       }
1043 
1044       /* Clear corresponding flags */
1045       hmdf->Instance->DFLTISR |= error_flags;
1046 
1047       /* Call error callback */
1048 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
1049       hmdf->ErrorCallback(hmdf);
1050 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
1051       HAL_MDF_ErrorCallback(hmdf);
1052 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
1053     }
1054 
1055     if (status == HAL_OK)
1056     {
1057       /* Update state only in asynchronous single shot mode */
1058       if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_ACQMOD) == MDF_MODE_ASYNC_SINGLE)
1059       {
1060         hmdf->State = HAL_MDF_STATE_READY;
1061       }
1062     }
1063   }
1064 
1065   /* Return function status */
1066   return status;
1067 }
1068 
1069 /**
1070   * @brief  This function allows to poll for available snapshot acquisition value.
1071   * @param  hmdf MDF handle.
1072   * @param  Timeout Timeout value in milliseconds.
1073   * @retval HAL status.
1074   * @note   This function must not be used with ADF instance.
1075   */
HAL_MDF_PollForSnapshotAcq(MDF_HandleTypeDef * hmdf,uint32_t Timeout)1076 HAL_StatusTypeDef HAL_MDF_PollForSnapshotAcq(MDF_HandleTypeDef *hmdf, uint32_t Timeout)
1077 {
1078   HAL_StatusTypeDef status = HAL_OK;
1079 
1080   /* Check state */
1081   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1082   {
1083     status = HAL_ERROR;
1084   }
1085   else
1086   {
1087     uint32_t tickstart = HAL_GetTick();
1088 
1089     /* Wait for available snapshot acquisition value */
1090     while (((hmdf->Instance->DFLTISR & MDF_DFLTISR_SSDRF) != MDF_DFLTISR_SSDRF) && (status == HAL_OK))
1091     {
1092       /* Check the timeout */
1093       if (Timeout != HAL_MAX_DELAY)
1094       {
1095         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1096         {
1097           status = HAL_TIMEOUT;
1098         }
1099       }
1100     }
1101 
1102     /* Check if snapshot overrun, saturation or reshape filter occurs */
1103     uint32_t error_flags = (hmdf->Instance->DFLTISR & (MDF_DFLTISR_SSOVRF | MDF_DFLTISR_SATF | MDF_DFLTISR_RFOVRF));
1104     if (error_flags != 0U)
1105     {
1106       /* Update error code */
1107       if ((error_flags & MDF_DFLTISR_SSOVRF) == MDF_DFLTISR_SSOVRF)
1108       {
1109         hmdf->ErrorCode |= MDF_ERROR_ACQUISITION_OVERFLOW;
1110       }
1111       if ((error_flags & MDF_DFLTISR_SATF) == MDF_DFLTISR_SATF)
1112       {
1113         hmdf->ErrorCode |= MDF_ERROR_SATURATION;
1114       }
1115       if ((error_flags & MDF_DFLTISR_RFOVRF) == MDF_DFLTISR_RFOVRF)
1116       {
1117         hmdf->ErrorCode |= MDF_ERROR_RSF_OVERRUN;
1118       }
1119 
1120       /* Clear corresponding flags */
1121       hmdf->Instance->DFLTISR |= error_flags;
1122 
1123       /* Call error callback */
1124 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
1125       hmdf->ErrorCallback(hmdf);
1126 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
1127       HAL_MDF_ErrorCallback(hmdf);
1128 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
1129     }
1130   }
1131 
1132   /* Return function status */
1133   return status;
1134 }
1135 
1136 /**
1137   * @brief  This function allows to get acquisition value.
1138   * @param  hmdf MDF handle.
1139   * @param  pValue Acquisition value on 24 MSB.
1140   * @retval HAL status.
1141   */
HAL_MDF_GetAcqValue(const MDF_HandleTypeDef * hmdf,int32_t * pValue)1142 HAL_StatusTypeDef HAL_MDF_GetAcqValue(const MDF_HandleTypeDef *hmdf, int32_t *pValue)
1143 {
1144   HAL_StatusTypeDef status = HAL_OK;
1145 
1146   /* Check parameters */
1147   if (pValue == NULL)
1148   {
1149     status = HAL_ERROR;
1150   }
1151   else
1152   {
1153     /* Check state */
1154     if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1155     {
1156       if (hmdf->State != HAL_MDF_STATE_READY)
1157       {
1158         status = HAL_ERROR;
1159       }
1160     }
1161   }
1162 
1163   if (status == HAL_OK)
1164   {
1165     /* Get acquisition value */
1166     *pValue = (int32_t) hmdf->Instance->DFLTDR;
1167   }
1168 
1169   /* Return function status */
1170   return status;
1171 }
1172 
1173 /**
1174   * @brief  This function allows to get snapshot acquisition value.
1175   * @param  hmdf MDF handle.
1176   * @param  pSnapshotParam Snapshot parameters.
1177   * @retval HAL status.
1178   * @note   This function must not be used with ADF instance.
1179   */
HAL_MDF_GetSnapshotAcqValue(MDF_HandleTypeDef * hmdf,MDF_SnapshotParamTypeDef * pSnapshotParam)1180 HAL_StatusTypeDef HAL_MDF_GetSnapshotAcqValue(MDF_HandleTypeDef *hmdf, MDF_SnapshotParamTypeDef *pSnapshotParam)
1181 {
1182   HAL_StatusTypeDef status = HAL_OK;
1183 
1184   /* Check parameters */
1185   if (pSnapshotParam == NULL)
1186   {
1187     status = HAL_ERROR;
1188   }
1189   /* Check state */
1190   else if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1191   {
1192     status = HAL_ERROR;
1193   }
1194   else
1195   {
1196     uint32_t snpsdr_value;
1197 
1198     /* Read value of snapshot data register */
1199     snpsdr_value = hmdf->Instance->SNPSDR;
1200 
1201     /* Clear snapshot data ready flag */
1202     hmdf->Instance->DFLTISR |= MDF_DFLTISR_SSDRF;
1203 
1204     /* Store value of decimation counter in snapshot parameter structure */
1205     pSnapshotParam->DecimationCounter = (snpsdr_value & MDF_SNPSDR_MCICDC);
1206 
1207     /* Check snapshot format */
1208     if ((hmdf->Instance->DFLTCR & MDF_SNAPSHOT_16BITS) == MDF_SNAPSHOT_16BITS)
1209     {
1210       /* Store value of integrator counter in snapshot parameter structure */
1211       pSnapshotParam->IntegratorCounter = ((snpsdr_value & MDF_SNPSDR_EXTSDR) >> MDF_SNPSDR_EXTSDR_Pos);
1212 
1213       /* Store snapshot acquisition value (16MSB) in snapshot parameter structure */
1214       snpsdr_value &= 0xFFFF0000U;
1215       pSnapshotParam->Value = (int32_t) snpsdr_value;
1216     }
1217     else
1218     {
1219       /* Store snapshot acquisition value (23MSB) in snapshot parameter structure */
1220       snpsdr_value &= 0xFFFFFE00U;
1221       pSnapshotParam->Value = (int32_t) snpsdr_value;
1222     }
1223   }
1224 
1225   /* Return function status */
1226   return status;
1227 }
1228 
1229 /**
1230   * @brief  This function allows to stop acquisition in polling mode.
1231   * @param  hmdf MDF handle.
1232   * @retval HAL status.
1233   */
HAL_MDF_AcqStop(MDF_HandleTypeDef * hmdf)1234 HAL_StatusTypeDef HAL_MDF_AcqStop(MDF_HandleTypeDef *hmdf)
1235 {
1236   HAL_StatusTypeDef status = HAL_OK;
1237 
1238   /* Check state */
1239   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1240   {
1241     /* Check if state is ready and filter active */
1242     if (hmdf->State == HAL_MDF_STATE_READY)
1243     {
1244       if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_DFLTACTIVE) != MDF_DFLTCR_DFLTACTIVE)
1245       {
1246         status = HAL_ERROR;
1247       }
1248     }
1249     else
1250     {
1251       status = HAL_ERROR;
1252     }
1253   }
1254   else
1255   {
1256     /* Disable sound activity detector if needed for ADF instance */
1257     if (IS_ADF_INSTANCE(hmdf->Instance))
1258     {
1259       if ((hmdf->Instance->SADCR & MDF_SADCR_SADACTIVE) != 0U)
1260       {
1261         hmdf->Instance->SADCR &= ~(MDF_SADCR_SADEN);
1262       }
1263     }
1264   }
1265 
1266   if (status == HAL_OK)
1267   {
1268     /* Disable filter */
1269     hmdf->Instance->DFLTCR &= ~(MDF_DFLTCR_DFLTEN);
1270 
1271     /* Clear all potential pending flags */
1272     if (IS_ADF_INSTANCE(hmdf->Instance))
1273     {
1274       hmdf->Instance->DFLTISR |= (MDF_DFLTISR_DOVRF | MDF_DFLTISR_SATF | MDF_DFLTISR_RFOVRF |
1275                                   MDF_DFLTISR_SDDETF | MDF_DFLTISR_SDLVLF);
1276     }
1277     else
1278     {
1279       hmdf->Instance->DFLTISR |= (MDF_DFLTISR_DOVRF | MDF_DFLTISR_SSDRF | MDF_DFLTISR_SSOVRF |
1280                                   MDF_DFLTISR_SATF | MDF_DFLTISR_RFOVRF);
1281     }
1282 
1283     /* Update state */
1284     hmdf->State = HAL_MDF_STATE_READY;
1285   }
1286 
1287   /* Return function status */
1288   return status;
1289 }
1290 
1291 /**
1292   * @brief  This function allows to start acquisition in interrupt mode.
1293   * @param  hmdf MDF handle.
1294   * @param  pFilterConfig Filter configuration parameters.
1295   * @retval HAL status.
1296   */
HAL_MDF_AcqStart_IT(MDF_HandleTypeDef * hmdf,const MDF_FilterConfigTypeDef * pFilterConfig)1297 HAL_StatusTypeDef HAL_MDF_AcqStart_IT(MDF_HandleTypeDef *hmdf, const MDF_FilterConfigTypeDef *pFilterConfig)
1298 {
1299   HAL_StatusTypeDef status = HAL_OK;
1300 
1301   /* Check parameters */
1302   if (pFilterConfig == NULL)
1303   {
1304     status = HAL_ERROR;
1305   }
1306   else
1307   {
1308     if (IS_ADF_INSTANCE(hmdf->Instance))
1309     {
1310       assert_param(IS_ADF_ACQUISITION_MODE(pFilterConfig->AcquisitionMode));
1311       assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->SoundActivity.Activation));
1312     }
1313     else
1314     {
1315       assert_param(IS_MDF_ACQUISITION_MODE(pFilterConfig->AcquisitionMode));
1316     }
1317     if ((IS_ADF_INSTANCE(hmdf->Instance)) && (pFilterConfig->SoundActivity.Activation == ENABLE) &&
1318         ((pFilterConfig->AcquisitionMode == MDF_MODE_ASYNC_SINGLE) ||
1319          (pFilterConfig->AcquisitionMode == MDF_MODE_SYNC_SINGLE) ||
1320          (pFilterConfig->AcquisitionMode == MDF_MODE_WINDOW_CONT)))
1321     {
1322       status = HAL_ERROR;
1323     }
1324     /* Check state */
1325     else if (hmdf->State != HAL_MDF_STATE_READY)
1326     {
1327       status = HAL_ERROR;
1328     }
1329     /* Check filter status */
1330     else if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_DFLTACTIVE) != 0U)
1331     {
1332       status = HAL_ERROR;
1333     }
1334     else
1335     {
1336       /* For ADF instance, check SAD status */
1337       if (IS_ADF_INSTANCE(hmdf->Instance))
1338       {
1339         if ((hmdf->Instance->SADCR & MDF_SADCR_SADACTIVE) != 0U)
1340         {
1341           status = HAL_ERROR;
1342         }
1343       }
1344     }
1345 
1346     if (status == HAL_OK)
1347     {
1348       /* For MDF instance, check OLD status and main filter order */
1349       assert_param(IS_MDF_CIC_MODE(pFilterConfig->CicMode));
1350       if (IS_MDF_INSTANCE(hmdf->Instance))
1351       {
1352         if (((hmdf->Instance->OLDCR & MDF_OLDCR_OLDACTIVE) != 0U) && (pFilterConfig->CicMode >= MDF_ONE_FILTER_SINC4))
1353         {
1354           status = HAL_ERROR;
1355         }
1356       }
1357 
1358       if (status == HAL_OK)
1359       {
1360         if (pFilterConfig->AcquisitionMode == MDF_MODE_SYNC_SNAPSHOT)
1361         {
1362           /* Enable snapshot overrun and data ready interrupts */
1363           hmdf->Instance->DFLTIER |= (MDF_DFLTIER_SSOVRIE | MDF_DFLTIER_SSDRIE);
1364         }
1365         else
1366         {
1367           if ((IS_MDF_INSTANCE(hmdf->Instance)) || (pFilterConfig->SoundActivity.Activation == DISABLE) ||
1368               (pFilterConfig->SoundActivity.DataMemoryTransfer != MDF_SAD_NO_MEMORY_TRANSFER))
1369           {
1370             /* Enable data overflow and fifo threshold interrupts */
1371             hmdf->Instance->DFLTIER |= (MDF_DFLTIER_DOVRIE | MDF_DFLTIER_FTHIE);
1372           }
1373         }
1374 
1375         if (pFilterConfig->ReshapeFilter.Activation == ENABLE)
1376         {
1377           /* Enable reshape filter overrun interrupt */
1378           hmdf->Instance->DFLTIER |= MDF_DFLTIER_RFOVRIE;
1379         }
1380 
1381         /* Enable saturation interrupt */
1382         hmdf->Instance->DFLTIER |= MDF_DFLTIER_SATIE;
1383 
1384         if ((IS_ADF_INSTANCE(hmdf->Instance)) && (pFilterConfig->SoundActivity.Activation == ENABLE))
1385         {
1386           /* Enable sound level value ready and sound activity detection interrupts */
1387           assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->SoundActivity.SoundLevelInterrupt));
1388           hmdf->Instance->DFLTIER |= (pFilterConfig->SoundActivity.SoundLevelInterrupt == ENABLE) ?
1389                                      (MDF_DFLTIER_SDLVLIE | MDF_DFLTIER_SDDETIE) :
1390                                      MDF_DFLTIER_SDDETIE;
1391         }
1392 
1393         /* Configure filter and start acquisition */
1394         hmdf->Instance->DFLTCR = 0U;
1395         MDF_AcqStart(hmdf, pFilterConfig);
1396       }
1397     }
1398   }
1399 
1400   /* Return function status */
1401   return status;
1402 }
1403 
1404 /**
1405   * @brief  This function allows to stop acquisition in interrupt mode.
1406   * @param  hmdf MDF handle.
1407   * @retval HAL status.
1408   */
HAL_MDF_AcqStop_IT(MDF_HandleTypeDef * hmdf)1409 HAL_StatusTypeDef HAL_MDF_AcqStop_IT(MDF_HandleTypeDef *hmdf)
1410 {
1411   HAL_StatusTypeDef status = HAL_OK;
1412 
1413   /* Check state */
1414   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1415   {
1416     /* Check if state is ready and filter active */
1417     if (hmdf->State == HAL_MDF_STATE_READY)
1418     {
1419       if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_DFLTACTIVE) != MDF_DFLTCR_DFLTACTIVE)
1420       {
1421         status = HAL_ERROR;
1422       }
1423     }
1424     else
1425     {
1426       status = HAL_ERROR;
1427     }
1428   }
1429   else
1430   {
1431     /* Disable sound activity detector if needed for ADF instance */
1432     if (IS_ADF_INSTANCE(hmdf->Instance))
1433     {
1434       if ((hmdf->Instance->SADCR & MDF_SADCR_SADACTIVE) != 0U)
1435       {
1436         hmdf->Instance->SADCR &= ~(MDF_SADCR_SADEN);
1437       }
1438     }
1439   }
1440 
1441   if (status == HAL_OK)
1442   {
1443     /* Disable filter */
1444     hmdf->Instance->DFLTCR &= ~(MDF_DFLTCR_DFLTEN);
1445 
1446     /* Disable interrupts and clear all potential pending flags */
1447     if (IS_ADF_INSTANCE(hmdf->Instance))
1448     {
1449       hmdf->Instance->DFLTIER &= ~(MDF_DFLTIER_FTHIE | MDF_DFLTIER_DOVRIE | MDF_DFLTIER_SATIE |
1450                                    MDF_DFLTIER_RFOVRIE | MDF_DFLTIER_SDDETIE | MDF_DFLTIER_SDLVLIE);
1451       hmdf->Instance->DFLTISR |= (MDF_DFLTISR_DOVRF | MDF_DFLTISR_SATF | MDF_DFLTISR_RFOVRF |
1452                                   MDF_DFLTISR_SDDETF | MDF_DFLTISR_SDLVLF);
1453     }
1454     else
1455     {
1456       hmdf->Instance->DFLTIER &= ~(MDF_DFLTIER_FTHIE | MDF_DFLTIER_DOVRIE | MDF_DFLTIER_SSDRIE |
1457                                    MDF_DFLTIER_SSOVRIE | MDF_DFLTIER_SATIE | MDF_DFLTIER_RFOVRIE);
1458       hmdf->Instance->DFLTISR |= (MDF_DFLTISR_DOVRF | MDF_DFLTISR_SSDRF | MDF_DFLTISR_SSOVRF |
1459                                   MDF_DFLTISR_SATF | MDF_DFLTISR_RFOVRF);
1460     }
1461 
1462     /* Update state */
1463     hmdf->State = HAL_MDF_STATE_READY;
1464   }
1465 
1466   /* Return function status */
1467   return status;
1468 }
1469 
1470 /**
1471   * @brief  This function allows to start acquisition in DMA mode.
1472   * @param  hmdf MDF handle.
1473   * @param  pFilterConfig Filter configuration parameters.
1474   * @param  pDmaConfig DMA configuration parameters.
1475   * @retval HAL status.
1476   */
HAL_MDF_AcqStart_DMA(MDF_HandleTypeDef * hmdf,const MDF_FilterConfigTypeDef * pFilterConfig,const MDF_DmaConfigTypeDef * pDmaConfig)1477 HAL_StatusTypeDef HAL_MDF_AcqStart_DMA(MDF_HandleTypeDef *hmdf, const MDF_FilterConfigTypeDef *pFilterConfig,
1478                                        const MDF_DmaConfigTypeDef *pDmaConfig)
1479 {
1480   HAL_StatusTypeDef status = HAL_OK;
1481 
1482   /* Check parameters */
1483   if ((pFilterConfig == NULL) || (pDmaConfig == NULL))
1484   {
1485     status = HAL_ERROR;
1486   }
1487   else
1488   {
1489     assert_param(IS_FUNCTIONAL_STATE(pDmaConfig->MsbOnly));
1490     if (IS_ADF_INSTANCE(hmdf->Instance))
1491     {
1492       assert_param(IS_ADF_ACQUISITION_MODE(pFilterConfig->AcquisitionMode));
1493       assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->SoundActivity.Activation));
1494     }
1495     else
1496     {
1497       assert_param(IS_MDF_ACQUISITION_MODE(pFilterConfig->AcquisitionMode));
1498     }
1499     if ((IS_ADF_INSTANCE(hmdf->Instance)) && (pFilterConfig->SoundActivity.Activation == ENABLE) &&
1500         ((pFilterConfig->AcquisitionMode == MDF_MODE_ASYNC_SINGLE) ||
1501          (pFilterConfig->AcquisitionMode == MDF_MODE_SYNC_SINGLE) ||
1502          (pFilterConfig->AcquisitionMode == MDF_MODE_WINDOW_CONT)))
1503     {
1504       status = HAL_ERROR;
1505     }
1506     else if (pFilterConfig->AcquisitionMode == MDF_MODE_SYNC_SNAPSHOT)
1507     {
1508       status = HAL_ERROR;
1509     }
1510     /* Check state */
1511     else if (hmdf->State != HAL_MDF_STATE_READY)
1512     {
1513       status = HAL_ERROR;
1514     }
1515     /* Check filter status */
1516     else if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_DFLTACTIVE) != 0U)
1517     {
1518       status = HAL_ERROR;
1519     }
1520     else
1521     {
1522       /* For ADF instance, check SAD status */
1523       if (IS_ADF_INSTANCE(hmdf->Instance))
1524       {
1525         if ((hmdf->Instance->SADCR & MDF_SADCR_SADACTIVE) != 0U)
1526         {
1527           status = HAL_ERROR;
1528         }
1529       }
1530     }
1531 
1532     if (status == HAL_OK)
1533     {
1534       /* For MDF instance, check OLD status and main filter order */
1535       assert_param(IS_MDF_CIC_MODE(pFilterConfig->CicMode));
1536       if (IS_MDF_INSTANCE(hmdf->Instance))
1537       {
1538         if (((hmdf->Instance->OLDCR & MDF_OLDCR_OLDACTIVE) != 0U) && (pFilterConfig->CicMode >= MDF_ONE_FILTER_SINC4))
1539         {
1540           status = HAL_ERROR;
1541         }
1542       }
1543 
1544       if (status == HAL_OK)
1545       {
1546         uint32_t SrcAddress;
1547 
1548         if (pFilterConfig->ReshapeFilter.Activation == ENABLE)
1549         {
1550           /* Enable reshape filter overrun interrupt */
1551           hmdf->Instance->DFLTIER |= MDF_DFLTIER_RFOVRIE;
1552         }
1553 
1554         /* Enable saturation interrupt */
1555         hmdf->Instance->DFLTIER |= MDF_DFLTIER_SATIE;
1556 
1557         if ((IS_ADF_INSTANCE(hmdf->Instance)) && (pFilterConfig->SoundActivity.Activation == ENABLE))
1558         {
1559           /* Enable sound level value ready and sound activity detection interrupts */
1560           assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->SoundActivity.SoundLevelInterrupt));
1561           hmdf->Instance->DFLTIER |= (pFilterConfig->SoundActivity.SoundLevelInterrupt == ENABLE) ?
1562                                      (MDF_DFLTIER_SDLVLIE | MDF_DFLTIER_SDDETIE) :
1563                                      MDF_DFLTIER_SDDETIE;
1564         }
1565 
1566         /* Enable MDF DMA requests */
1567         hmdf->Instance->DFLTCR = MDF_DFLTCR_DMAEN;
1568 
1569         /* Start DMA transfer */
1570         hmdf->hdma->XferCpltCallback     = MDF_DmaXferCpltCallback;
1571         hmdf->hdma->XferHalfCpltCallback = MDF_DmaXferHalfCpltCallback;
1572         hmdf->hdma->XferErrorCallback    = MDF_DmaErrorCallback;
1573         SrcAddress = (pDmaConfig->MsbOnly == ENABLE) ? (((uint32_t) &hmdf->Instance->DFLTDR) + 2U) :
1574                      (uint32_t) &hmdf->Instance->DFLTDR;
1575         if ((hmdf->hdma->Mode & DMA_LINKEDLIST) == DMA_LINKEDLIST)
1576         {
1577           if (hmdf->hdma->LinkedListQueue != NULL)
1578           {
1579             hmdf->hdma->LinkedListQueue->Head->LinkRegisters[NODE_CBR1_DEFAULT_OFFSET] = pDmaConfig->DataLength;
1580             hmdf->hdma->LinkedListQueue->Head->LinkRegisters[NODE_CSAR_DEFAULT_OFFSET] = SrcAddress;
1581             hmdf->hdma->LinkedListQueue->Head->LinkRegisters[NODE_CDAR_DEFAULT_OFFSET] = pDmaConfig->Address;
1582 
1583             status = HAL_DMAEx_List_Start_IT(hmdf->hdma);
1584           }
1585           else
1586           {
1587             status = HAL_ERROR;
1588           }
1589         }
1590         else
1591         {
1592           status = HAL_DMA_Start_IT(hmdf->hdma, SrcAddress, pDmaConfig->Address, pDmaConfig->DataLength);
1593         }
1594         if (status != HAL_OK)
1595         {
1596           /* Update state */
1597           hmdf->State = HAL_MDF_STATE_ERROR;
1598           status = HAL_ERROR;
1599         }
1600         else
1601         {
1602           /* Configure filter and start acquisition */
1603           MDF_AcqStart(hmdf, pFilterConfig);
1604         }
1605       }
1606     }
1607   }
1608 
1609   /* Return function status */
1610   return status;
1611 }
1612 
1613 /**
1614   * @brief  This function allows to stop acquisition in DMA mode.
1615   * @param  hmdf MDF handle.
1616   * @retval HAL status.
1617   */
HAL_MDF_AcqStop_DMA(MDF_HandleTypeDef * hmdf)1618 HAL_StatusTypeDef HAL_MDF_AcqStop_DMA(MDF_HandleTypeDef *hmdf)
1619 {
1620   HAL_StatusTypeDef status = HAL_OK;
1621 
1622   /* Check if state is ready and filter active */
1623   if (hmdf->State == HAL_MDF_STATE_READY)
1624   {
1625     if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_DFLTACTIVE) != MDF_DFLTCR_DFLTACTIVE)
1626     {
1627       status = HAL_ERROR;
1628     }
1629   }
1630   else
1631   {
1632     /* Check state */
1633     if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1634     {
1635       status = HAL_ERROR;
1636     }
1637     else
1638     {
1639       /* Stop the DMA transfer */
1640       if (HAL_DMA_Abort(hmdf->hdma) != HAL_OK)
1641       {
1642         /* Update state */
1643         hmdf->State = HAL_MDF_STATE_ERROR;
1644         status = HAL_ERROR;
1645       }
1646     }
1647   }
1648 
1649   if (status == HAL_OK)
1650   {
1651     /* Disable sound activity detector if needed for ADF instance */
1652     if (IS_ADF_INSTANCE(hmdf->Instance))
1653     {
1654       if ((hmdf->Instance->SADCR & MDF_SADCR_SADACTIVE) != 0U)
1655       {
1656         hmdf->Instance->SADCR &= ~(MDF_SADCR_SADEN);
1657       }
1658     }
1659 
1660     /* Disable filter */
1661     hmdf->Instance->DFLTCR &= ~(MDF_DFLTCR_DFLTEN);
1662 
1663     /* Disable interrupts and clear all potential pending flags */
1664     if (IS_ADF_INSTANCE(hmdf->Instance))
1665     {
1666       hmdf->Instance->DFLTIER &= ~(MDF_DFLTIER_SATIE | MDF_DFLTIER_RFOVRIE | MDF_DFLTIER_SDDETIE |
1667                                    MDF_DFLTIER_SDLVLIE);
1668       hmdf->Instance->DFLTISR |= (MDF_DFLTISR_SATF | MDF_DFLTISR_RFOVRF | MDF_DFLTISR_SDDETF |
1669                                   MDF_DFLTISR_SDLVLF);
1670     }
1671     else
1672     {
1673       hmdf->Instance->DFLTIER &= ~(MDF_DFLTIER_SATIE | MDF_DFLTIER_RFOVRIE);
1674       hmdf->Instance->DFLTISR |= (MDF_DFLTISR_SATF | MDF_DFLTISR_RFOVRF);
1675     }
1676 
1677     /* Disable MDF DMA requests */
1678     hmdf->Instance->DFLTCR &= ~(MDF_DFLTCR_DMAEN);
1679 
1680     /* Update state */
1681     hmdf->State = HAL_MDF_STATE_READY;
1682   }
1683 
1684   /* Return function status */
1685   return status;
1686 }
1687 
1688 /**
1689   * @brief  This function allows to generate pulse on TRGO signal.
1690   * @param  hmdf MDF handle.
1691   * @retval HAL status.
1692   */
HAL_MDF_GenerateTrgo(const MDF_HandleTypeDef * hmdf)1693 HAL_StatusTypeDef HAL_MDF_GenerateTrgo(const MDF_HandleTypeDef *hmdf)
1694 {
1695   HAL_StatusTypeDef status = HAL_OK;
1696 
1697   /* Check state */
1698   if (hmdf->State != HAL_MDF_STATE_READY)
1699   {
1700     if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1701     {
1702       status = HAL_ERROR;
1703     }
1704   }
1705 
1706   if (status == HAL_OK)
1707   {
1708     MDF_TypeDef *p_mdf_base;
1709 
1710     /* Get MDF base according instance */
1711     p_mdf_base = (IS_ADF_INSTANCE(hmdf->Instance)) ? ADF1 : MDF1;
1712 
1713     /* Check if trigger output control is already active */
1714     if ((p_mdf_base->GCR & MDF_GCR_TRGO) == MDF_GCR_TRGO)
1715     {
1716       status = HAL_ERROR;
1717     }
1718     else
1719     {
1720       /* Generate pulse on trigger output control signal */
1721       p_mdf_base->GCR |= MDF_GCR_TRGO;
1722     }
1723   }
1724 
1725   /* Return function status */
1726   return status;
1727 }
1728 
1729 /**
1730   * @brief  This function allows to set delay to apply on data source in number of samples.
1731   * @param  hmdf MDF handle.
1732   * @param  Delay Delay to apply on data source in number of samples.
1733   *         This parameter must be a number between Min_Data = 0 and Max_Data = 127.
1734   * @retval HAL status.
1735   */
HAL_MDF_SetDelay(MDF_HandleTypeDef * hmdf,uint32_t Delay)1736 HAL_StatusTypeDef HAL_MDF_SetDelay(MDF_HandleTypeDef *hmdf, uint32_t Delay)
1737 {
1738   HAL_StatusTypeDef status = HAL_OK;
1739 
1740   /* Check parameters */
1741   assert_param(IS_MDF_DELAY(Delay));
1742 
1743   /* Check state */
1744   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1745   {
1746     status = HAL_ERROR;
1747   }
1748   else
1749   {
1750     /* Check if bitstream delay is already active */
1751     if ((hmdf->Instance->DLYCR & MDF_DLYCR_SKPBF) == MDF_DLYCR_SKPBF)
1752     {
1753       status = HAL_ERROR;
1754     }
1755     else
1756     {
1757       /* Configure bitstream delay */
1758       hmdf->Instance->DLYCR |= Delay;
1759     }
1760   }
1761 
1762   /* Return function status */
1763   return status;
1764 }
1765 
1766 /**
1767   * @brief  This function allows to get current delay applied on data source in number of samples.
1768   * @param  hmdf MDF handle.
1769   * @param  pDelay Current delay applied on data source in number of samples.
1770   *         This value is between Min_Data = 0 and Max_Data = 127.
1771   * @retval HAL status.
1772   */
HAL_MDF_GetDelay(const MDF_HandleTypeDef * hmdf,uint32_t * pDelay)1773 HAL_StatusTypeDef HAL_MDF_GetDelay(const MDF_HandleTypeDef *hmdf, uint32_t *pDelay)
1774 {
1775   HAL_StatusTypeDef status = HAL_OK;
1776 
1777   /* Check parameters */
1778   if (pDelay == NULL)
1779   {
1780     status = HAL_ERROR;
1781   }
1782   /* Check state */
1783   else if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1784   {
1785     status = HAL_ERROR;
1786   }
1787   else
1788   {
1789     /* Get current bitstream delay */
1790     *pDelay = (hmdf->Instance->DLYCR & MDF_DLYCR_SKPDLY);
1791   }
1792 
1793   /* Return function status */
1794   return status;
1795 }
1796 
1797 /**
1798   * @brief  This function allows to set filter gain.
1799   * @param  hmdf MDF handle.
1800   * @param  Gain Filter gain in step of around 3db (from -48db to 72dB).
1801   *         This parameter must be a number between Min_Data = -16 and Max_Data = 24.
1802   * @retval HAL status.
1803   */
HAL_MDF_SetGain(MDF_HandleTypeDef * hmdf,int32_t Gain)1804 HAL_StatusTypeDef HAL_MDF_SetGain(MDF_HandleTypeDef *hmdf, int32_t Gain)
1805 {
1806   HAL_StatusTypeDef status = HAL_OK;
1807 
1808   /* Check parameters */
1809   assert_param(IS_MDF_GAIN(Gain));
1810 
1811   /* Check state */
1812   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1813   {
1814     status = HAL_ERROR;
1815   }
1816   else
1817   {
1818     uint32_t register_gain_value;
1819     uint32_t tmp_register;
1820 
1821     if (Gain < 0)
1822     {
1823       int32_t adjust_gain;
1824 
1825       /* adjust gain value to set on register for negative value (offset of -16) */
1826       adjust_gain = Gain - 16;
1827       register_gain_value = ((uint32_t) adjust_gain & 0x3FU);
1828     }
1829     else
1830     {
1831       /* for positive value, no offset to apply */
1832       register_gain_value = (uint32_t) Gain;
1833     }
1834     /* Set gain */
1835     tmp_register = (hmdf->Instance->DFLTCICR & ~(MDF_DFLTCICR_SCALE));
1836     hmdf->Instance->DFLTCICR = (tmp_register | (register_gain_value << MDF_DFLTCICR_SCALE_Pos));
1837   }
1838 
1839   /* Return function status */
1840   return status;
1841 }
1842 
1843 /**
1844   * @brief  This function allows to get filter gain.
1845   * @param  hmdf MDF handle.
1846   * @param  pGain Filter gain in step of around 3db (from -48db to 72dB).
1847   *         This parameter is between Min_Data = -16 and Max_Data = 24.
1848   * @retval HAL status.
1849   */
HAL_MDF_GetGain(const MDF_HandleTypeDef * hmdf,int32_t * pGain)1850 HAL_StatusTypeDef HAL_MDF_GetGain(const MDF_HandleTypeDef *hmdf, int32_t *pGain)
1851 {
1852   HAL_StatusTypeDef status = HAL_OK;
1853 
1854   /* Check parameters */
1855   if (pGain == NULL)
1856   {
1857     status = HAL_ERROR;
1858   }
1859   /* Check state */
1860   else if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1861   {
1862     status = HAL_ERROR;
1863   }
1864   else
1865   {
1866     uint32_t register_gain_value;
1867 
1868     /* Get current gain */
1869     register_gain_value = ((hmdf->Instance->DFLTCICR & MDF_DFLTCICR_SCALE) >> MDF_DFLTCICR_SCALE_Pos);
1870     if (register_gain_value > 31U)
1871     {
1872       /* adjust gain value to set on register for negative value (offset of +16) */
1873       register_gain_value |= 0xFFFFFFC0U;
1874       *pGain = (int32_t) register_gain_value + 16;
1875     }
1876     else
1877     {
1878       /* for positive value, no offset to apply */
1879       *pGain = (int32_t) register_gain_value;
1880     }
1881   }
1882 
1883   /* Return function status */
1884   return status;
1885 }
1886 
1887 /**
1888   * @brief  This function allows to set filter offset error compensation.
1889   * @param  hmdf MDF handle.
1890   * @param  Offset Filter offset error compensation.
1891   *         This parameter must be a number between Min_Data = -33554432 and Max_Data = 33554431.
1892   * @retval HAL status.
1893   * @note   This function must not be used with ADF instance.
1894   */
HAL_MDF_SetOffset(MDF_HandleTypeDef * hmdf,int32_t Offset)1895 HAL_StatusTypeDef HAL_MDF_SetOffset(MDF_HandleTypeDef *hmdf, int32_t Offset)
1896 {
1897   HAL_StatusTypeDef status = HAL_OK;
1898 
1899   /* Check parameters */
1900   assert_param(IS_MDF_INSTANCE(hmdf->Instance));
1901   assert_param(IS_MDF_OFFSET(Offset));
1902 
1903   /* Check state */
1904   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1905   {
1906     status = HAL_ERROR;
1907   }
1908   else
1909   {
1910     /* Set offset */
1911     hmdf->Instance->OECCR = (uint32_t) Offset;
1912   }
1913 
1914   /* Return function status */
1915   return status;
1916 }
1917 
1918 /**
1919   * @brief  This function allows to get filter offset error compensation.
1920   * @param  hmdf MDF handle.
1921   * @param  pOffset Filter offset error compensation.
1922   *         This value is between Min_Data = -33554432 and Max_Data = 33554431.
1923   * @retval HAL status.
1924   * @note   This function must not be used with ADF instance.
1925   */
HAL_MDF_GetOffset(const MDF_HandleTypeDef * hmdf,int32_t * pOffset)1926 HAL_StatusTypeDef HAL_MDF_GetOffset(const MDF_HandleTypeDef *hmdf, int32_t *pOffset)
1927 {
1928   HAL_StatusTypeDef status = HAL_OK;
1929 
1930   /* Check parameters */
1931   assert_param(IS_MDF_INSTANCE(hmdf->Instance));
1932   if (pOffset == NULL)
1933   {
1934     status = HAL_ERROR;
1935   }
1936   /* Check state */
1937   else if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1938   {
1939     status = HAL_ERROR;
1940   }
1941   else
1942   {
1943     uint32_t register_offset_value;
1944 
1945     /* Get current offset */
1946     register_offset_value = hmdf->Instance->OECCR;
1947     if (register_offset_value > 33554431U)
1948     {
1949       /* Negative value */
1950       register_offset_value |= 0xFC000000U;
1951       *pOffset = (int32_t) register_offset_value;
1952     }
1953     else
1954     {
1955       /* Positive value */
1956       *pOffset = (int32_t) register_offset_value;
1957     }
1958   }
1959 
1960   /* Return function status */
1961   return status;
1962 }
1963 
1964 /**
1965   * @brief  This function allows to poll for sound level data.
1966   * @param  hmdf MDF handle.
1967   * @param  Timeout Timeout value in milliseconds.
1968   * @param  pSoundLevel Sound level.
1969             This parameter can be a value between Min_Data = 0 and Max_Data = 32767.
1970   * @param  pAmbientNoise Ambient noise.
1971             This parameter can be a value between Min_Data = 0 and Max_Data = 32767.
1972   * @retval HAL status.
1973   * @note   This function must not be used with MDF instance.
1974   */
HAL_MDF_PollForSndLvl(MDF_HandleTypeDef * hmdf,uint32_t Timeout,uint32_t * pSoundLevel,uint32_t * pAmbientNoise)1975 HAL_StatusTypeDef HAL_MDF_PollForSndLvl(MDF_HandleTypeDef *hmdf, uint32_t Timeout, uint32_t *pSoundLevel,
1976                                         uint32_t *pAmbientNoise)
1977 {
1978   HAL_StatusTypeDef status = HAL_OK;
1979 
1980   /* Check parameters */
1981   assert_param(IS_ADF_INSTANCE(hmdf->Instance));
1982   if ((pSoundLevel == NULL) || (pAmbientNoise == NULL))
1983   {
1984     status = HAL_ERROR;
1985   }
1986   /* Check state */
1987   else if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
1988   {
1989     status = HAL_ERROR;
1990   }
1991   /* Check SAD status */
1992   else if ((hmdf->Instance->SADCR & MDF_SADCR_SADACTIVE) == 0U)
1993   {
1994     status = HAL_ERROR;
1995   }
1996   else
1997   {
1998     uint32_t tickstart = HAL_GetTick();
1999 
2000     /* Wait for available sound level data */
2001     while (((hmdf->Instance->DFLTISR & MDF_DFLTISR_SDLVLF) != MDF_DFLTISR_SDLVLF) && (status == HAL_OK))
2002     {
2003       /* Check the timeout */
2004       if (Timeout != HAL_MAX_DELAY)
2005       {
2006         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2007         {
2008           status = HAL_TIMEOUT;
2009         }
2010       }
2011     }
2012 
2013     if (status == HAL_OK)
2014     {
2015       /* Get sound level */
2016       *pSoundLevel = hmdf->Instance->SADSDLVR;
2017 
2018       /* Get ambient noise */
2019       *pAmbientNoise = hmdf->Instance->SADANLVR;
2020 
2021       /* Clear sound level ready flag */
2022       hmdf->Instance->DFLTISR |= MDF_DFLTISR_SDLVLF;
2023     }
2024   }
2025 
2026   /* Return function status */
2027   return status;
2028 }
2029 
2030 /**
2031   * @brief  This function allows to poll for sound activity detection.
2032   * @param  hmdf MDF handle.
2033   * @param  Timeout Timeout value in milliseconds.
2034   * @retval HAL status.
2035   * @note   This function must not be used with MDF instance.
2036   */
HAL_MDF_PollForSad(MDF_HandleTypeDef * hmdf,uint32_t Timeout)2037 HAL_StatusTypeDef HAL_MDF_PollForSad(MDF_HandleTypeDef *hmdf, uint32_t Timeout)
2038 {
2039   HAL_StatusTypeDef status = HAL_OK;
2040 
2041   /* Check parameters */
2042   assert_param(IS_ADF_INSTANCE(hmdf->Instance));
2043 
2044   /* Check state */
2045   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2046   {
2047     status = HAL_ERROR;
2048   }
2049   /* Check SAD status */
2050   else if ((hmdf->Instance->SADCR & MDF_SADCR_SADACTIVE) == 0U)
2051   {
2052     status = HAL_ERROR;
2053   }
2054   else
2055   {
2056     uint32_t tickstart = HAL_GetTick();
2057 
2058     /* Wait for sound activity detection */
2059     while (((hmdf->Instance->DFLTISR & MDF_DFLTISR_SDDETF) != MDF_DFLTISR_SDDETF) && (status == HAL_OK))
2060     {
2061       /* Check the timeout */
2062       if (Timeout != HAL_MAX_DELAY)
2063       {
2064         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2065         {
2066           status = HAL_TIMEOUT;
2067         }
2068       }
2069     }
2070 
2071     if (status == HAL_OK)
2072     {
2073       /* Clear sound activity detection flag */
2074       hmdf->Instance->DFLTISR |= MDF_DFLTISR_SDDETF;
2075     }
2076   }
2077 
2078   /* Return function status */
2079   return status;
2080 }
2081 
2082 /**
2083   * @brief  MDF acquisition complete callback.
2084   * @param  hmdf MDF handle.
2085   * @retval None.
2086   */
HAL_MDF_AcqCpltCallback(MDF_HandleTypeDef * hmdf)2087 __weak void HAL_MDF_AcqCpltCallback(MDF_HandleTypeDef *hmdf)
2088 {
2089   /* Prevent unused argument(s) compilation warning */
2090   UNUSED(hmdf);
2091 
2092   /* NOTE : This function should not be modified, when the function is needed,
2093             the HAL_MDF_AcqCpltCallback could be implemented in the user file */
2094 }
2095 
2096 /**
2097   * @brief  MDF acquisition half complete callback.
2098   * @param  hmdf MDF handle.
2099   * @retval None.
2100   */
HAL_MDF_AcqHalfCpltCallback(MDF_HandleTypeDef * hmdf)2101 __weak void HAL_MDF_AcqHalfCpltCallback(MDF_HandleTypeDef *hmdf)
2102 {
2103   /* Prevent unused argument(s) compilation warning */
2104   UNUSED(hmdf);
2105 
2106   /* NOTE : This function should not be modified, when the function is needed,
2107             the HAL_MDF_AcqHalfCpltCallback could be implemented in the user file */
2108 }
2109 
2110 /**
2111   * @brief  MDF sound level callback.
2112   * @param  hmdf MDF handle.
2113   * @param  SoundLevel Sound level value computed by sound activity detector.
2114   *         This parameter can be a value between Min_Data = 0 and Max_Data = 32767.
2115   * @param  AmbientNoise Ambient noise value computed by sound activity detector.
2116   *         This parameter can be a value between Min_Data = 0 and Max_Data = 32767.
2117   * @retval None.
2118   */
HAL_MDF_SndLvlCallback(MDF_HandleTypeDef * hmdf,uint32_t SoundLevel,uint32_t AmbientNoise)2119 __weak void HAL_MDF_SndLvlCallback(MDF_HandleTypeDef *hmdf, uint32_t SoundLevel, uint32_t AmbientNoise)
2120 {
2121   /* Prevent unused argument(s) compilation warning */
2122   UNUSED(hmdf);
2123   UNUSED(SoundLevel);
2124   UNUSED(AmbientNoise);
2125 
2126   /* NOTE : This function should not be modified, when the function is needed,
2127             the HAL_MDF_SndLvlCallback could be implemented in the user file */
2128 }
2129 
2130 /**
2131   * @brief  MDF sound activity detector callback.
2132   * @param  hmdf MDF handle.
2133   * @retval None.
2134   */
HAL_MDF_SadCallback(MDF_HandleTypeDef * hmdf)2135 __weak void HAL_MDF_SadCallback(MDF_HandleTypeDef *hmdf)
2136 {
2137   /* Prevent unused argument(s) compilation warning */
2138   UNUSED(hmdf);
2139 
2140   /* NOTE : This function should not be modified, when the function is needed,
2141             the HAL_MDF_SadCallback could be implemented in the user file */
2142 }
2143 
2144 /**
2145   * @}
2146   */
2147 
2148 /** @defgroup MDF_Exported_Functions_Group3  Clock absence detection functions
2149   * @brief    Clock absence detection functions
2150   *
2151 @verbatim
2152   ==============================================================================
2153                   ##### Clock absence detection functions #####
2154   ==============================================================================
2155     [..]  This section provides functions allowing to :
2156       (+) Start and stop clock absence detection in interrupt mode.
2157       (+) Detect clock absence.
2158 @endverbatim
2159   * @{
2160   */
2161 
2162 /**
2163   * @brief  This function allows to poll for the clock absence detection.
2164   * @param  hmdf MDF handle.
2165   * @param  Timeout Timeout value in milliseconds.
2166   * @retval HAL status.
2167   */
HAL_MDF_PollForCkab(MDF_HandleTypeDef * hmdf,uint32_t Timeout)2168 HAL_StatusTypeDef HAL_MDF_PollForCkab(MDF_HandleTypeDef *hmdf, uint32_t Timeout)
2169 {
2170   HAL_StatusTypeDef status = HAL_OK;
2171 
2172   /* Check state */
2173   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2174   {
2175     if (hmdf->State != HAL_MDF_STATE_READY)
2176     {
2177       status = HAL_ERROR;
2178     }
2179   }
2180 
2181   if (status == HAL_OK)
2182   {
2183     /* Check serial interface status and mode */
2184     if ((hmdf->Instance->SITFCR & MDF_SITFCR_SITFACTIVE) == 0U)
2185     {
2186       status = HAL_ERROR;
2187     }
2188     else
2189     {
2190       if ((hmdf->Instance->SITFCR & MDF_SITFCR_SITFMOD) != MDF_SITF_NORMAL_SPI_MODE)
2191       {
2192         status = HAL_ERROR;
2193       }
2194     }
2195   }
2196 
2197   if (status == HAL_OK)
2198   {
2199     uint32_t tickstart = HAL_GetTick();
2200 
2201     /* Wait for clock absence detection */
2202     while (((hmdf->Instance->DFLTISR & MDF_DFLTISR_CKABF) != MDF_DFLTISR_CKABF) && (status == HAL_OK))
2203     {
2204       /* Check the timeout */
2205       if (Timeout != HAL_MAX_DELAY)
2206       {
2207         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2208         {
2209           status = HAL_TIMEOUT;
2210         }
2211       }
2212     }
2213 
2214     if (status == HAL_OK)
2215     {
2216       /* Clear clock absence detection flag */
2217       hmdf->Instance->DFLTISR |= MDF_DFLTISR_CKABF;
2218     }
2219   }
2220 
2221   /* Return function status */
2222   return status;
2223 }
2224 
2225 /**
2226   * @brief  This function allows to start clock absence detection in interrupt mode.
2227   * @param  hmdf MDF handle.
2228   * @retval HAL status.
2229   */
HAL_MDF_CkabStart_IT(MDF_HandleTypeDef * hmdf)2230 HAL_StatusTypeDef HAL_MDF_CkabStart_IT(MDF_HandleTypeDef *hmdf)
2231 {
2232   HAL_StatusTypeDef status = HAL_OK;
2233 
2234   /* Check state */
2235   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2236   {
2237     if (hmdf->State != HAL_MDF_STATE_READY)
2238     {
2239       status = HAL_ERROR;
2240     }
2241   }
2242 
2243   if (status == HAL_OK)
2244   {
2245     /* Check serial interface status and mode */
2246     if ((hmdf->Instance->SITFCR & MDF_SITFCR_SITFACTIVE) == 0U)
2247     {
2248       status = HAL_ERROR;
2249     }
2250     else
2251     {
2252       if ((hmdf->Instance->SITFCR & MDF_SITFCR_SITFMOD) != MDF_SITF_NORMAL_SPI_MODE)
2253       {
2254         status = HAL_ERROR;
2255       }
2256     }
2257   }
2258 
2259   if (status == HAL_OK)
2260   {
2261     /* Clear clock absence detection flag */
2262     hmdf->Instance->DFLTISR |= MDF_DFLTISR_CKABF;
2263 
2264     /* Check clock absence detection flag */
2265     if ((hmdf->Instance->DFLTISR & MDF_DFLTISR_CKABF) == MDF_DFLTISR_CKABF)
2266     {
2267       status = HAL_ERROR;
2268     }
2269     else
2270     {
2271       /* Enable clock absence detection interrupt */
2272       hmdf->Instance->DFLTIER |= MDF_DFLTIER_CKABIE;
2273     }
2274   }
2275 
2276   /* Return function status */
2277   return status;
2278 }
2279 
2280 /**
2281   * @brief  This function allows to stop clock absence detection in interrupt mode.
2282   * @param  hmdf MDF handle.
2283   * @retval HAL status.
2284   */
HAL_MDF_CkabStop_IT(MDF_HandleTypeDef * hmdf)2285 HAL_StatusTypeDef HAL_MDF_CkabStop_IT(MDF_HandleTypeDef *hmdf)
2286 {
2287   HAL_StatusTypeDef status = HAL_OK;
2288 
2289   /* Check state */
2290   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2291   {
2292     if (hmdf->State != HAL_MDF_STATE_READY)
2293     {
2294       status = HAL_ERROR;
2295     }
2296   }
2297 
2298   if (status == HAL_OK)
2299   {
2300     /* Check serial interface status and mode */
2301     if ((hmdf->Instance->SITFCR & MDF_SITFCR_SITFACTIVE) == 0U)
2302     {
2303       status = HAL_ERROR;
2304     }
2305     else
2306     {
2307       if ((hmdf->Instance->SITFCR & MDF_SITFCR_SITFMOD) != MDF_SITF_NORMAL_SPI_MODE)
2308       {
2309         status = HAL_ERROR;
2310       }
2311     }
2312   }
2313 
2314   if (status == HAL_OK)
2315   {
2316     /* Disable clock absence detection interrupt */
2317     hmdf->Instance->DFLTIER &= ~(MDF_DFLTIER_CKABIE);
2318 
2319     /* Clear potential pending clock absence detection flag */
2320     hmdf->Instance->DFLTISR |= MDF_DFLTISR_CKABF;
2321   }
2322 
2323   /* Return function status */
2324   return status;
2325 }
2326 
2327 /**
2328   * @}
2329   */
2330 
2331 /** @defgroup MDF_Exported_Functions_Group4  Short circuit detection functions
2332   * @brief    Short circuit detection functions
2333   *
2334 @verbatim
2335   ==============================================================================
2336                   ##### Short circuit detection functions #####
2337   ==============================================================================
2338     [..]  This section provides functions available only for MDF instance
2339           allowing to :
2340       (+) Start and stop short circuit detection in polling and interrupt mode.
2341       (+) Detect short circuit.
2342 @endverbatim
2343   * @{
2344   */
2345 
2346 /**
2347   * @brief  This function allows to start short-circuit detection in polling mode.
2348   * @param  hmdf MDF handle.
2349   * @param  pScdConfig Short-circuit detector configuration parameters.
2350   * @retval HAL status.
2351   * @note   This function must not be used with ADF instance.
2352   */
HAL_MDF_ScdStart(MDF_HandleTypeDef * hmdf,const MDF_ScdConfigTypeDef * pScdConfig)2353 HAL_StatusTypeDef HAL_MDF_ScdStart(MDF_HandleTypeDef *hmdf, const MDF_ScdConfigTypeDef *pScdConfig)
2354 {
2355   HAL_StatusTypeDef status = HAL_OK;
2356 
2357   /* Check parameters */
2358   if (pScdConfig == NULL)
2359   {
2360     status = HAL_ERROR;
2361   }
2362   else
2363   {
2364     assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2365     assert_param(IS_MDF_SCD_THRESHOLD(pScdConfig->Threshold));
2366     assert_param(IS_MDF_BREAK_SIGNAL(pScdConfig->BreakSignal));
2367 
2368     /* Check state */
2369     if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2370     {
2371       if (hmdf->State != HAL_MDF_STATE_READY)
2372       {
2373         status = HAL_ERROR;
2374       }
2375     }
2376 
2377     if (status == HAL_OK)
2378     {
2379       /* Check short-circuit detector status */
2380       if ((hmdf->Instance->SCDCR & MDF_SCDCR_SCDACTIVE) == MDF_SCDCR_SCDACTIVE)
2381       {
2382         status = HAL_ERROR;
2383       }
2384       else
2385       {
2386         /* Configure threshold and break signal */
2387         hmdf->Instance->SCDCR = (((pScdConfig->Threshold - 1U) << MDF_SCDCR_SCDT_Pos) |
2388                                  (pScdConfig->BreakSignal << MDF_SCDCR_BKSCD_Pos));
2389 
2390         /* Enable short-circuit detector */
2391         hmdf->Instance->SCDCR |= MDF_SCDCR_SCDEN;
2392       }
2393     }
2394   }
2395 
2396   /* Return function status */
2397   return status;
2398 }
2399 
2400 /**
2401   * @brief  This function allows to poll for the short-circuit detection.
2402   * @param  hmdf MDF handle.
2403   * @param  Timeout Timeout value in milliseconds.
2404   * @retval HAL status.
2405   * @note   This function must not be used with ADF instance.
2406   */
HAL_MDF_PollForScd(MDF_HandleTypeDef * hmdf,uint32_t Timeout)2407 HAL_StatusTypeDef HAL_MDF_PollForScd(MDF_HandleTypeDef *hmdf, uint32_t Timeout)
2408 {
2409   HAL_StatusTypeDef status = HAL_OK;
2410 
2411   /* Check parameters */
2412   assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2413 
2414   /* Check state */
2415   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2416   {
2417     if (hmdf->State != HAL_MDF_STATE_READY)
2418     {
2419       status = HAL_ERROR;
2420     }
2421   }
2422 
2423   if (status == HAL_OK)
2424   {
2425     /* Check short-circuit detector status */
2426     if ((hmdf->Instance->SCDCR & MDF_SCDCR_SCDACTIVE) != MDF_SCDCR_SCDACTIVE)
2427     {
2428       status = HAL_ERROR;
2429     }
2430     else
2431     {
2432       uint32_t tickstart = HAL_GetTick();
2433 
2434       /* Wait for short-circuit detection */
2435       while (((hmdf->Instance->DFLTISR & MDF_DFLTISR_SCDF) != MDF_DFLTISR_SCDF) && (status == HAL_OK))
2436       {
2437         /* Check the timeout */
2438         if (Timeout != HAL_MAX_DELAY)
2439         {
2440           if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2441           {
2442             status = HAL_TIMEOUT;
2443           }
2444         }
2445       }
2446 
2447       if (status == HAL_OK)
2448       {
2449         /* Clear short-circuit detection flag */
2450         hmdf->Instance->DFLTISR |= MDF_DFLTISR_SCDF;
2451       }
2452     }
2453   }
2454 
2455   /* Return function status */
2456   return status;
2457 }
2458 
2459 /**
2460   * @brief  This function allows to stop short-circuit detection in polling mode.
2461   * @param  hmdf MDF handle.
2462   * @retval HAL status.
2463   * @note   This function must not be used with ADF instance.
2464   */
HAL_MDF_ScdStop(MDF_HandleTypeDef * hmdf)2465 HAL_StatusTypeDef HAL_MDF_ScdStop(MDF_HandleTypeDef *hmdf)
2466 {
2467   HAL_StatusTypeDef status = HAL_OK;
2468 
2469   /* Check parameters */
2470   assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2471 
2472   /* Check state */
2473   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2474   {
2475     if (hmdf->State != HAL_MDF_STATE_READY)
2476     {
2477       status = HAL_ERROR;
2478     }
2479   }
2480 
2481   if (status == HAL_OK)
2482   {
2483     /* Check short-circuit detector status */
2484     if ((hmdf->Instance->SCDCR & MDF_SCDCR_SCDACTIVE) != MDF_SCDCR_SCDACTIVE)
2485     {
2486       status = HAL_ERROR;
2487     }
2488     else
2489     {
2490       /* Disable short-circuit detection */
2491       hmdf->Instance->SCDCR &= ~(MDF_SCDCR_SCDEN);
2492 
2493       /* Clear potential pending short-circuit detection flag */
2494       hmdf->Instance->DFLTISR |= MDF_DFLTISR_SCDF;
2495     }
2496   }
2497 
2498   /* Return function status */
2499   return status;
2500 }
2501 
2502 /**
2503   * @brief  This function allows to start short-circuit detection in interrupt mode.
2504   * @param  hmdf MDF handle.
2505   * @param  pScdConfig Short-circuit detector configuration parameters.
2506   * @retval HAL status.
2507   * @note   This function must not be used with ADF instance.
2508   */
HAL_MDF_ScdStart_IT(MDF_HandleTypeDef * hmdf,const MDF_ScdConfigTypeDef * pScdConfig)2509 HAL_StatusTypeDef HAL_MDF_ScdStart_IT(MDF_HandleTypeDef *hmdf, const MDF_ScdConfigTypeDef *pScdConfig)
2510 {
2511   HAL_StatusTypeDef status = HAL_OK;
2512 
2513   /* Check parameters */
2514   if (pScdConfig == NULL)
2515   {
2516     status = HAL_ERROR;
2517   }
2518   else
2519   {
2520     assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2521     assert_param(IS_MDF_SCD_THRESHOLD(pScdConfig->Threshold));
2522     assert_param(IS_MDF_BREAK_SIGNAL(pScdConfig->BreakSignal));
2523 
2524     /* Check state */
2525     if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2526     {
2527       if (hmdf->State != HAL_MDF_STATE_READY)
2528       {
2529         status = HAL_ERROR;
2530       }
2531     }
2532 
2533     if (status == HAL_OK)
2534     {
2535       /* Check short-circuit detector status */
2536       if ((hmdf->Instance->SCDCR & MDF_SCDCR_SCDACTIVE) == MDF_SCDCR_SCDACTIVE)
2537       {
2538         status = HAL_ERROR;
2539       }
2540       else
2541       {
2542         /* Configure threshold and break signal */
2543         hmdf->Instance->SCDCR = (((pScdConfig->Threshold - 1U) << MDF_SCDCR_SCDT_Pos) |
2544                                  (pScdConfig->BreakSignal << MDF_SCDCR_BKSCD_Pos));
2545 
2546         /* Enable short-circuit detector interrupt */
2547         hmdf->Instance->DFLTIER |= MDF_DFLTIER_SCDIE;
2548 
2549         /* Enable short-circuit detector */
2550         hmdf->Instance->SCDCR |= MDF_SCDCR_SCDEN;
2551       }
2552     }
2553   }
2554 
2555   /* Return function status */
2556   return status;
2557 }
2558 
2559 /**
2560   * @brief  This function allows to stop short-circuit detection in interrupt mode.
2561   * @param  hmdf MDF handle.
2562   * @retval HAL status.
2563   * @note   This function must not be used with ADF instance.
2564   */
HAL_MDF_ScdStop_IT(MDF_HandleTypeDef * hmdf)2565 HAL_StatusTypeDef HAL_MDF_ScdStop_IT(MDF_HandleTypeDef *hmdf)
2566 {
2567   HAL_StatusTypeDef status = HAL_OK;
2568 
2569   /* Check parameters */
2570   assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2571 
2572   /* Check state */
2573   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2574   {
2575     if (hmdf->State != HAL_MDF_STATE_READY)
2576     {
2577       status = HAL_ERROR;
2578     }
2579   }
2580 
2581   if (status == HAL_OK)
2582   {
2583     /* Check short-circuit detector status */
2584     if ((hmdf->Instance->SCDCR & MDF_SCDCR_SCDACTIVE) != MDF_SCDCR_SCDACTIVE)
2585     {
2586       status = HAL_ERROR;
2587     }
2588     else
2589     {
2590       /* Disable short-circuit detection */
2591       hmdf->Instance->SCDCR &= ~(MDF_SCDCR_SCDEN);
2592 
2593       /* Disable short-circuit detection interrupt */
2594       hmdf->Instance->DFLTIER &= ~(MDF_DFLTIER_SCDIE);
2595 
2596       /* Clear potential pending short-circuit detection flag */
2597       hmdf->Instance->DFLTISR |= MDF_DFLTISR_SCDF;
2598     }
2599   }
2600 
2601   /* Return function status */
2602   return status;
2603 }
2604 
2605 /**
2606   * @}
2607   */
2608 
2609 /** @defgroup MDF_Exported_Functions_Group5  Out-off limit detection functions
2610   * @brief    Out-off limit detection functions
2611   *
2612 @verbatim
2613   ==============================================================================
2614                   ##### Out-off limit detection functions #####
2615   ==============================================================================
2616     [..]  This section provides functions available only for MDF instance
2617           allowing to :
2618       (+) Start and stop out-off limit detection in polling and interrupt mode.
2619       (+) Detect short circuit and get threshold information.
2620 @endverbatim
2621   * @{
2622   */
2623 
2624 /**
2625   * @brief  This function allows to start out-off limit detection in polling mode.
2626   * @param  hmdf MDF handle.
2627   * @param  pOldConfig Out-off limit detector configuration parameters.
2628   * @retval HAL status.
2629   * @note   This function must not be used with ADF instance.
2630   */
HAL_MDF_OldStart(MDF_HandleTypeDef * hmdf,const MDF_OldConfigTypeDef * pOldConfig)2631 HAL_StatusTypeDef HAL_MDF_OldStart(MDF_HandleTypeDef *hmdf, const MDF_OldConfigTypeDef *pOldConfig)
2632 {
2633   HAL_StatusTypeDef status = HAL_OK;
2634 
2635   /* Check parameters */
2636   if (pOldConfig == NULL)
2637   {
2638     status = HAL_ERROR;
2639   }
2640   else
2641   {
2642     assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2643     assert_param(IS_MDF_OLD_CIC_MODE(pOldConfig->OldCicMode));
2644     assert_param(IS_MDF_OLD_DECIMATION_RATIO(pOldConfig->OldDecimationRatio));
2645     assert_param(IS_MDF_OLD_THRESHOLD(pOldConfig->HighThreshold));
2646     assert_param(IS_MDF_OLD_THRESHOLD(pOldConfig->LowThreshold));
2647     assert_param(IS_MDF_OLD_EVENT_CONFIG(pOldConfig->OldEventConfig));
2648     assert_param(IS_MDF_BREAK_SIGNAL(pOldConfig->BreakSignal));
2649     if (pOldConfig->LowThreshold >= pOldConfig->HighThreshold)
2650     {
2651       status = HAL_ERROR;
2652     }
2653     else
2654     {
2655       /* Check state */
2656       if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2657       {
2658         if (hmdf->State != HAL_MDF_STATE_READY)
2659         {
2660           status = HAL_ERROR;
2661         }
2662       }
2663     }
2664 
2665     if (status == HAL_OK)
2666     {
2667       /* Check out-off limit detector status */
2668       if ((hmdf->Instance->OLDCR & MDF_OLDCR_OLDACTIVE) == MDF_OLDCR_OLDACTIVE)
2669       {
2670         status = HAL_ERROR;
2671       }
2672       else
2673       {
2674         /* Check filter status */
2675         if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_DFLTACTIVE) == MDF_DFLTCR_DFLTACTIVE)
2676         {
2677           /* Check main filter order */
2678           if ((hmdf->Instance->DFLTCICR & MDF_DFLTCICR_CICMOD) >= MDF_ONE_FILTER_SINC4)
2679           {
2680             status = HAL_ERROR;
2681           }
2682         }
2683         else
2684         {
2685           /* Reset main filter order */
2686           hmdf->Instance->DFLTCICR &= ~(MDF_DFLTCICR_CICMOD);
2687         }
2688 
2689         if (status == HAL_OK)
2690         {
2691           /* Configure OLD CIC mode, decimation ratio, event and break signal */
2692           hmdf->Instance->OLDCR = (pOldConfig->OldCicMode | pOldConfig->OldEventConfig |
2693                                    ((pOldConfig->OldDecimationRatio - 1U) << MDF_OLDCR_ACICD_Pos) |
2694                                    (pOldConfig->BreakSignal << MDF_OLDCR_BKOLD_Pos));
2695 
2696           /* Configure low and high thresholds */
2697           hmdf->Instance->OLDTHLR = (uint32_t) pOldConfig->LowThreshold;
2698           hmdf->Instance->OLDTHHR = (uint32_t) pOldConfig->HighThreshold;
2699 
2700           /* Enable out-off limit detector */
2701           hmdf->Instance->OLDCR |= MDF_OLDCR_OLDEN;
2702         }
2703       }
2704     }
2705   }
2706 
2707   /* Return function status */
2708   return status;
2709 }
2710 
2711 /**
2712   * @brief  This function allows to poll for the out-off limit detection.
2713   * @param  hmdf MDF handle.
2714   * @param  Timeout Timeout value in milliseconds.
2715   * @param  pThresholdInfo Threshold information of out-off limit detection.
2716   *         This parameter can be a value of @ref MDF_OldThresholdInfo.
2717   * @retval HAL status.
2718   * @note   This function must not be used with ADF instance.
2719   */
HAL_MDF_PollForOld(MDF_HandleTypeDef * hmdf,uint32_t Timeout,uint32_t * pThresholdInfo)2720 HAL_StatusTypeDef HAL_MDF_PollForOld(MDF_HandleTypeDef *hmdf, uint32_t Timeout, uint32_t *pThresholdInfo)
2721 {
2722   HAL_StatusTypeDef status = HAL_OK;
2723 
2724   /* Check parameters */
2725   assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2726   if (pThresholdInfo == NULL)
2727   {
2728     status = HAL_ERROR;
2729   }
2730   else
2731   {
2732     /* Check state */
2733     if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2734     {
2735       if (hmdf->State != HAL_MDF_STATE_READY)
2736       {
2737         status = HAL_ERROR;
2738       }
2739     }
2740   }
2741 
2742   if (status == HAL_OK)
2743   {
2744     /* Check out-off limit detector status */
2745     if ((hmdf->Instance->OLDCR & MDF_OLDCR_OLDACTIVE) != MDF_OLDCR_OLDACTIVE)
2746     {
2747       status = HAL_ERROR;
2748     }
2749     else
2750     {
2751       uint32_t tickstart = HAL_GetTick();
2752 
2753       /* Wait for out-off limit detection */
2754       while (((hmdf->Instance->DFLTISR & MDF_DFLTISR_OLDF) != MDF_DFLTISR_OLDF) && (status == HAL_OK))
2755       {
2756         /* Check the timeout */
2757         if (Timeout != HAL_MAX_DELAY)
2758         {
2759           if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2760           {
2761             status = HAL_TIMEOUT;
2762           }
2763         }
2764       }
2765 
2766       if (status == HAL_OK)
2767       {
2768         /* Get threshold information */
2769         if ((hmdf->Instance->DFLTISR & (MDF_DFLTISR_THLF | MDF_DFLTISR_THHF)) == 0U)
2770         {
2771           *pThresholdInfo = MDF_OLD_IN_THRESHOLDS;
2772         }
2773         else if ((hmdf->Instance->DFLTISR & MDF_DFLTISR_THLF) == MDF_DFLTISR_THLF)
2774         {
2775           *pThresholdInfo = MDF_OLD_LOW_THRESHOLD;
2776         }
2777         else
2778         {
2779           *pThresholdInfo = MDF_OLD_HIGH_THRESHOLD;
2780         }
2781 
2782         /* Clear out-off limit detection flags */
2783         hmdf->Instance->DFLTISR |= MDF_DFLTISR_OLDF;
2784       }
2785     }
2786   }
2787 
2788   /* Return function status */
2789   return status;
2790 }
2791 
2792 /**
2793   * @brief  This function allows to stop out-off limit detection in polling mode.
2794   * @param  hmdf MDF handle.
2795   * @retval HAL status.
2796   * @note   This function must not be used with ADF instance.
2797   */
HAL_MDF_OldStop(MDF_HandleTypeDef * hmdf)2798 HAL_StatusTypeDef HAL_MDF_OldStop(MDF_HandleTypeDef *hmdf)
2799 {
2800   HAL_StatusTypeDef status = HAL_OK;
2801 
2802   /* Check parameters */
2803   assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2804 
2805   /* Check state */
2806   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2807   {
2808     if (hmdf->State != HAL_MDF_STATE_READY)
2809     {
2810       status = HAL_ERROR;
2811     }
2812   }
2813 
2814   if (status == HAL_OK)
2815   {
2816     /* Check out-off limit detector status */
2817     if ((hmdf->Instance->OLDCR & MDF_OLDCR_OLDACTIVE) != MDF_OLDCR_OLDACTIVE)
2818     {
2819       status = HAL_ERROR;
2820     }
2821     else
2822     {
2823       /* Disable out-off limit detection */
2824       hmdf->Instance->OLDCR &= ~(MDF_OLDCR_OLDEN);
2825 
2826       /* Clear potential pending out-off limit detection flags */
2827       hmdf->Instance->DFLTISR |= MDF_DFLTISR_OLDF;
2828     }
2829   }
2830 
2831   /* Return function status */
2832   return status;
2833 }
2834 
2835 /**
2836   * @brief  This function allows to start out-off limit detection in interrupt mode.
2837   * @param  hmdf MDF handle.
2838   * @param  pOldConfig Out-off limit detector configuration parameters.
2839   * @retval HAL status.
2840   * @note   This function must not be used with ADF instance.
2841   */
HAL_MDF_OldStart_IT(MDF_HandleTypeDef * hmdf,const MDF_OldConfigTypeDef * pOldConfig)2842 HAL_StatusTypeDef HAL_MDF_OldStart_IT(MDF_HandleTypeDef *hmdf, const MDF_OldConfigTypeDef *pOldConfig)
2843 {
2844   HAL_StatusTypeDef status = HAL_OK;
2845 
2846   /* Check parameters */
2847   if (pOldConfig == NULL)
2848   {
2849     status = HAL_ERROR;
2850   }
2851   else
2852   {
2853     assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2854     assert_param(IS_MDF_OLD_CIC_MODE(pOldConfig->OldCicMode));
2855     assert_param(IS_MDF_OLD_DECIMATION_RATIO(pOldConfig->OldDecimationRatio));
2856     assert_param(IS_MDF_OLD_THRESHOLD(pOldConfig->HighThreshold));
2857     assert_param(IS_MDF_OLD_THRESHOLD(pOldConfig->LowThreshold));
2858     assert_param(IS_MDF_OLD_EVENT_CONFIG(pOldConfig->OldEventConfig));
2859     assert_param(IS_MDF_BREAK_SIGNAL(pOldConfig->BreakSignal));
2860     if (pOldConfig->LowThreshold >= pOldConfig->HighThreshold)
2861     {
2862       status = HAL_ERROR;
2863     }
2864     else
2865     {
2866       /* Check state */
2867       if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2868       {
2869         if (hmdf->State != HAL_MDF_STATE_READY)
2870         {
2871           status = HAL_ERROR;
2872         }
2873       }
2874     }
2875 
2876     if (status == HAL_OK)
2877     {
2878       /* Check out-off limit detector status */
2879       if ((hmdf->Instance->OLDCR & MDF_OLDCR_OLDACTIVE) == MDF_OLDCR_OLDACTIVE)
2880       {
2881         status = HAL_ERROR;
2882       }
2883       else
2884       {
2885         /* Check filter status */
2886         if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_DFLTACTIVE) == MDF_DFLTCR_DFLTACTIVE)
2887         {
2888           /* Check main filter order */
2889           if ((hmdf->Instance->DFLTCICR & MDF_DFLTCICR_CICMOD) >= MDF_ONE_FILTER_SINC4)
2890           {
2891             status = HAL_ERROR;
2892           }
2893         }
2894         else
2895         {
2896           /* Reset main filter order */
2897           hmdf->Instance->DFLTCICR &= ~(MDF_DFLTCICR_CICMOD);
2898         }
2899 
2900         if (status == HAL_OK)
2901         {
2902           /* Configure OLD CIC mode, decimation ratio, event and break signal */
2903           hmdf->Instance->OLDCR = (pOldConfig->OldCicMode | pOldConfig->OldEventConfig |
2904                                    ((pOldConfig->OldDecimationRatio - 1U) << MDF_OLDCR_ACICD_Pos) |
2905                                    (pOldConfig->BreakSignal << MDF_OLDCR_BKOLD_Pos));
2906 
2907           /* Configure low and high thresholds */
2908           hmdf->Instance->OLDTHLR = (uint32_t) pOldConfig->LowThreshold;
2909           hmdf->Instance->OLDTHHR = (uint32_t) pOldConfig->HighThreshold;
2910 
2911           /* Enable out-off limit detector interrupt */
2912           hmdf->Instance->DFLTIER |= MDF_DFLTIER_OLDIE;
2913 
2914           /* Enable out-off limit detector */
2915           hmdf->Instance->OLDCR |= MDF_OLDCR_OLDEN;
2916         }
2917       }
2918     }
2919   }
2920 
2921   /* Return function status */
2922   return status;
2923 }
2924 
2925 /**
2926   * @brief  This function allows to stop out-off limit detection in interrupt mode.
2927   * @param  hmdf MDF handle.
2928   * @retval HAL status.
2929   * @note   This function must not be used with ADF instance.
2930   */
HAL_MDF_OldStop_IT(MDF_HandleTypeDef * hmdf)2931 HAL_StatusTypeDef HAL_MDF_OldStop_IT(MDF_HandleTypeDef *hmdf)
2932 {
2933   HAL_StatusTypeDef status = HAL_OK;
2934 
2935   /* Check parameters */
2936   assert_param(IS_MDF_INSTANCE(hmdf->Instance));
2937 
2938   /* Check state */
2939   if (hmdf->State != HAL_MDF_STATE_ACQUISITION)
2940   {
2941     if (hmdf->State != HAL_MDF_STATE_READY)
2942     {
2943       status = HAL_ERROR;
2944     }
2945   }
2946 
2947   if (status == HAL_OK)
2948   {
2949     /* Check out-off limit detector status */
2950     if ((hmdf->Instance->OLDCR & MDF_OLDCR_OLDACTIVE) != MDF_OLDCR_OLDACTIVE)
2951     {
2952       status = HAL_ERROR;
2953     }
2954     else
2955     {
2956       /* Disable out-off limit detection */
2957       hmdf->Instance->OLDCR &= ~(MDF_OLDCR_OLDEN);
2958 
2959       /* Disable out-off limit detector interrupt */
2960       hmdf->Instance->DFLTIER &= ~(MDF_DFLTIER_OLDIE);
2961 
2962       /* Clear potential pending out-off limit detection flags */
2963       hmdf->Instance->DFLTISR |= MDF_DFLTISR_OLDF;
2964     }
2965   }
2966 
2967   /* Return function status */
2968   return status;
2969 }
2970 
2971 /**
2972   * @brief  MDF out-off limit detector callback.
2973   * @param  hmdf MDF handle.
2974   * @param  ThresholdInfo Threshold information of out-off limit detection.
2975   *         This parameter can be a value of @ref MDF_OldThresholdInfo.
2976   * @retval None.
2977   */
HAL_MDF_OldCallback(MDF_HandleTypeDef * hmdf,uint32_t ThresholdInfo)2978 __weak void HAL_MDF_OldCallback(MDF_HandleTypeDef *hmdf, uint32_t ThresholdInfo)
2979 {
2980   /* Prevent unused argument(s) compilation warning */
2981   UNUSED(hmdf);
2982   UNUSED(ThresholdInfo);
2983 
2984   /* NOTE : This function should not be modified, when the function is needed,
2985             the HAL_MDF_OldCallback could be implemented in the user file */
2986 }
2987 
2988 /**
2989   * @}
2990   */
2991 
2992 /** @defgroup MDF_Exported_Functions_Group6  Generic functions
2993   * @brief    Generic functions
2994   *
2995 @verbatim
2996   ==============================================================================
2997                           ##### Generic functions #####
2998   ==============================================================================
2999     [..]  This section provides functions allowing to :
3000       (+) Handle MDF interrupt.
3001       (+) Inform user that error occurs.
3002       (+) Get the current MDF instance state.
3003       (+) Get the current MDF instance error code.
3004 @endverbatim
3005   * @{
3006   */
3007 
3008 /**
3009   * @brief  This function handles the MDF interrupts.
3010   * @param  hmdf MDF handle.
3011   * @retval None.
3012   */
HAL_MDF_IRQHandler(MDF_HandleTypeDef * hmdf)3013 void HAL_MDF_IRQHandler(MDF_HandleTypeDef *hmdf)
3014 {
3015   uint32_t tmp_reg1;
3016   uint32_t tmp_reg2;
3017   uint32_t interrupts;
3018 
3019   /* Read current flags and interrupts and determine which ones occur */
3020   tmp_reg1 = hmdf->Instance->DFLTIER;
3021   tmp_reg2 = hmdf->Instance->DFLTISR;
3022   interrupts = (tmp_reg1 & tmp_reg2);
3023 
3024   /* Check if data overflow occurs */
3025   if ((interrupts & MDF_DFLTISR_DOVRF) == MDF_DFLTISR_DOVRF)
3026   {
3027     /* Clear data overflow flag */
3028     hmdf->Instance->DFLTISR |= MDF_DFLTISR_DOVRF;
3029 
3030     /* Update error code */
3031     hmdf->ErrorCode |= MDF_ERROR_ACQUISITION_OVERFLOW;
3032 
3033     /* Call error callback */
3034 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3035     hmdf->ErrorCallback(hmdf);
3036 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3037     HAL_MDF_ErrorCallback(hmdf);
3038 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3039   }
3040   /* Check if snapshot overrun occurs */
3041   else if ((interrupts & MDF_DFLTISR_SSOVRF) == MDF_DFLTISR_SSOVRF)
3042   {
3043     /* Clear snapshot overrun flag */
3044     hmdf->Instance->DFLTISR |= MDF_DFLTISR_SSOVRF;
3045 
3046     /* Update error code */
3047     hmdf->ErrorCode |= MDF_ERROR_ACQUISITION_OVERFLOW;
3048 
3049     /* Call error callback */
3050 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3051     hmdf->ErrorCallback(hmdf);
3052 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3053     HAL_MDF_ErrorCallback(hmdf);
3054 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3055   }
3056   /* Check if RXFIFO threshold occurs */
3057   else if ((interrupts & MDF_DFLTISR_FTHF) == MDF_DFLTISR_FTHF)
3058   {
3059     /* Call acquisition complete callback */
3060 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3061     hmdf->AcqCpltCallback(hmdf);
3062 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3063     HAL_MDF_AcqCpltCallback(hmdf);
3064 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3065 
3066     /* Update state only in asynchronous single shot mode */
3067     if ((hmdf->Instance->DFLTCR & MDF_DFLTCR_ACQMOD) == MDF_MODE_ASYNC_SINGLE)
3068     {
3069       hmdf->State = HAL_MDF_STATE_READY;
3070     }
3071   }
3072   /* Check if snapshot data ready occurs */
3073   else if ((interrupts & MDF_DFLTISR_SSDRF) == MDF_DFLTISR_SSDRF)
3074   {
3075     /* Clear snapshot data ready flag */
3076     hmdf->Instance->DFLTISR |= MDF_DFLTISR_SSDRF;
3077 
3078     /* Call acquisition complete callback */
3079 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3080     hmdf->AcqCpltCallback(hmdf);
3081 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3082     HAL_MDF_AcqCpltCallback(hmdf);
3083 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3084   }
3085   /* Check if reshape filter overrun occurs */
3086   else if ((interrupts & MDF_DFLTISR_RFOVRF) == MDF_DFLTISR_RFOVRF)
3087   {
3088     /* Clear reshape filter overrun flag */
3089     hmdf->Instance->DFLTISR |= MDF_DFLTISR_RFOVRF;
3090 
3091     /* Update error code */
3092     hmdf->ErrorCode |= MDF_ERROR_RSF_OVERRUN;
3093 
3094     /* Call error callback */
3095 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3096     hmdf->ErrorCallback(hmdf);
3097 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3098     HAL_MDF_ErrorCallback(hmdf);
3099 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3100   }
3101   /* Check if clock absence detection occurs */
3102   else if ((interrupts & MDF_DFLTISR_CKABF) == MDF_DFLTISR_CKABF)
3103   {
3104     /* Clear clock absence detection flag */
3105     hmdf->Instance->DFLTISR |= MDF_DFLTISR_CKABF;
3106 
3107     /* Update error code */
3108     hmdf->ErrorCode |= MDF_ERROR_CLOCK_ABSENCE;
3109 
3110     /* Call error callback */
3111 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3112     hmdf->ErrorCallback(hmdf);
3113 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3114     HAL_MDF_ErrorCallback(hmdf);
3115 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3116   }
3117   /* Check if saturation occurs */
3118   else if ((interrupts & MDF_DFLTISR_SATF) == MDF_DFLTISR_SATF)
3119   {
3120     /* Clear saturation flag */
3121     hmdf->Instance->DFLTISR |= MDF_DFLTISR_SATF;
3122 
3123     /* Update error code */
3124     hmdf->ErrorCode |= MDF_ERROR_SATURATION;
3125 
3126     /* Call error callback */
3127 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3128     hmdf->ErrorCallback(hmdf);
3129 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3130     HAL_MDF_ErrorCallback(hmdf);
3131 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3132   }
3133   /* Check if short-circuit detection occurs */
3134   else if ((interrupts & MDF_DFLTISR_SCDF) == MDF_DFLTISR_SCDF)
3135   {
3136     /* Clear short-circuit detection flag */
3137     hmdf->Instance->DFLTISR |= MDF_DFLTISR_SCDF;
3138 
3139     /* Update error code */
3140     hmdf->ErrorCode |= MDF_ERROR_SHORT_CIRCUIT;
3141 
3142     /* Call error callback */
3143 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3144     hmdf->ErrorCallback(hmdf);
3145 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3146     HAL_MDF_ErrorCallback(hmdf);
3147 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3148   }
3149   /* Check if out-off limit detection occurs */
3150   else if ((interrupts & MDF_DFLTISR_OLDF) == MDF_DFLTISR_OLDF)
3151   {
3152     uint32_t threshold_info;
3153 
3154     /* Get threshold information */
3155     if ((hmdf->Instance->DFLTISR & (MDF_DFLTISR_THLF | MDF_DFLTISR_THHF)) == 0U)
3156     {
3157       threshold_info = MDF_OLD_IN_THRESHOLDS;
3158     }
3159     else if ((hmdf->Instance->DFLTISR & MDF_DFLTISR_THLF) == MDF_DFLTISR_THLF)
3160     {
3161       threshold_info = MDF_OLD_LOW_THRESHOLD;
3162     }
3163     else
3164     {
3165       threshold_info = MDF_OLD_HIGH_THRESHOLD;
3166     }
3167 
3168     /* Clear out-off limit detection flag */
3169     hmdf->Instance->DFLTISR |= MDF_DFLTISR_OLDF;
3170 
3171     /* Update error code */
3172     hmdf->ErrorCode |= MDF_ERROR_OUT_OFF_LIMIT;
3173 
3174     /* Call out-off limit detection callback */
3175 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3176     hmdf->OldCallback(hmdf, threshold_info);
3177 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3178     HAL_MDF_OldCallback(hmdf, threshold_info);
3179 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3180   }
3181   /* Check if sound activity detection occurs */
3182   else if ((interrupts & MDF_DFLTISR_SDDETF) == MDF_DFLTISR_SDDETF)
3183   {
3184     /* Clear sound activity detection flag */
3185     hmdf->Instance->DFLTISR |= MDF_DFLTISR_SDDETF;
3186 
3187     /* Call sound activity detection callback */
3188 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3189     hmdf->SadCallback(hmdf);
3190 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3191     HAL_MDF_SadCallback(hmdf);
3192 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3193   }
3194   else
3195   {
3196     /* Check if sound level ready occurs */
3197     if ((interrupts & MDF_DFLTISR_SDLVLF) == MDF_DFLTISR_SDLVLF)
3198     {
3199       uint32_t sound_level;
3200       uint32_t ambient_noise;
3201 
3202       /* Get sound level */
3203       sound_level = hmdf->Instance->SADSDLVR;
3204 
3205       /* Get ambient noise */
3206       ambient_noise = hmdf->Instance->SADANLVR;
3207 
3208       /* Clear sound level ready flag */
3209       hmdf->Instance->DFLTISR |= MDF_DFLTISR_SDLVLF;
3210 
3211       /* Call sound level callback */
3212 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3213       hmdf->SndLvCallback(hmdf, sound_level, ambient_noise);
3214 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3215       HAL_MDF_SndLvlCallback(hmdf, sound_level, ambient_noise);
3216 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3217     }
3218   }
3219 }
3220 
3221 /**
3222   * @brief  MDF error callback.
3223   * @param  hmdf MDF handle.
3224   * @retval None.
3225   */
HAL_MDF_ErrorCallback(MDF_HandleTypeDef * hmdf)3226 __weak void HAL_MDF_ErrorCallback(MDF_HandleTypeDef *hmdf)
3227 {
3228   /* Prevent unused argument(s) compilation warning */
3229   UNUSED(hmdf);
3230 
3231   /* NOTE : This function should not be modified, when the function is needed,
3232             the HAL_MDF_ErrorCallback could be implemented in the user file */
3233 }
3234 
3235 /**
3236   * @brief  This function allows to get the current MDF state.
3237   * @param  hmdf MDF handle.
3238   * @retval MDF state.
3239   */
HAL_MDF_GetState(const MDF_HandleTypeDef * hmdf)3240 HAL_MDF_StateTypeDef HAL_MDF_GetState(const MDF_HandleTypeDef *hmdf)
3241 {
3242   /* Return MDF state */
3243   return hmdf->State;
3244 }
3245 
3246 /**
3247   * @brief  This function allows to get the current MDF error.
3248   * @param  hmdf MDF handle.
3249   * @retval MDF error code.
3250   */
HAL_MDF_GetError(const MDF_HandleTypeDef * hmdf)3251 uint32_t HAL_MDF_GetError(const MDF_HandleTypeDef *hmdf)
3252 {
3253   /* Return MDF error code */
3254   return hmdf->ErrorCode;
3255 }
3256 
3257 /**
3258   * @}
3259   */
3260 
3261 /**
3262   * @}
3263   */
3264 
3265 /** @addtogroup MDF_Private_Functions
3266   * @brief      Private functions
3267   * @{
3268   */
3269 
3270 /**
3271   * @brief  This function allows to get the handle number from instance.
3272   * @param  pInstance MDF instance.
3273   * @retval Instance number.
3274   */
MDF_GetHandleNumberFromInstance(const MDF_Filter_TypeDef * const pInstance)3275 static uint32_t MDF_GetHandleNumberFromInstance(const MDF_Filter_TypeDef *const pInstance)
3276 {
3277   uint32_t handle_number;
3278 
3279   /* Get handle number from instance */
3280   if (pInstance == MDF1_Filter0)
3281   {
3282     handle_number = 0U;
3283   }
3284   else if (pInstance == MDF1_Filter1)
3285   {
3286     handle_number = 1U;
3287   }
3288 #if !defined(STM32U535xx) && !defined(STM32U545xx)
3289   else if (pInstance == MDF1_Filter2)
3290   {
3291     handle_number = 2U;
3292   }
3293   else if (pInstance == MDF1_Filter3)
3294   {
3295     handle_number = 3U;
3296   }
3297   else if (pInstance == MDF1_Filter4)
3298   {
3299     handle_number = 4U;
3300   }
3301   else if (pInstance == MDF1_Filter5)
3302   {
3303     handle_number = 5U;
3304   }
3305   else /* ADF1_Filter0 */
3306   {
3307     handle_number = 6U;
3308   }
3309 #else /* !defined(STM32U535xx) && !defined(STM32U545xx) */
3310   else /* ADF1_Filter0 */
3311   {
3312     handle_number = 2U;
3313   }
3314 #endif /* !defined(STM32U535xx) && !defined(STM32U545xx) */
3315 
3316   return handle_number;
3317 }
3318 
3319 /**
3320   * @brief  This function allows to configure filter and start acquisition.
3321   * @param  hmdf MDF handle.
3322   * @param  pFilterConfig Filter configuration parameters.
3323   * @retval None.
3324   */
MDF_AcqStart(MDF_HandleTypeDef * const hmdf,const MDF_FilterConfigTypeDef * const pFilterConfig)3325 static void MDF_AcqStart(MDF_HandleTypeDef *const hmdf, const MDF_FilterConfigTypeDef *const pFilterConfig)
3326 {
3327   uint32_t register_gain_value;
3328 
3329   /* Configure acquisition mode, discard samples, trigger and fifo threshold */
3330   assert_param(IS_MDF_DISCARD_SAMPLES(pFilterConfig->DiscardSamples));
3331   assert_param(IS_MDF_FIFO_THRESHOLD(pFilterConfig->FifoThreshold));
3332   if ((pFilterConfig->AcquisitionMode == MDF_MODE_ASYNC_CONT) ||
3333       (pFilterConfig->AcquisitionMode == MDF_MODE_ASYNC_SINGLE))
3334   {
3335     /* Trigger parameters are not used */
3336     hmdf->Instance->DFLTCR |= (pFilterConfig->AcquisitionMode | pFilterConfig->FifoThreshold |
3337                                (pFilterConfig->DiscardSamples << MDF_DFLTCR_NBDIS_Pos));
3338   }
3339   else
3340   {
3341     /* Trigger parameters are used */
3342     if (IS_ADF_INSTANCE(hmdf->Instance))
3343     {
3344       assert_param(IS_ADF_TRIGGER_SOURCE(pFilterConfig->Trigger.Source));
3345     }
3346     else
3347     {
3348       assert_param(IS_MDF_TRIGGER_SOURCE(pFilterConfig->Trigger.Source));
3349     }
3350     assert_param(IS_MDF_TRIGGER_EDGE(pFilterConfig->Trigger.Edge));
3351     hmdf->Instance->DFLTCR |= (pFilterConfig->AcquisitionMode | pFilterConfig->FifoThreshold |
3352                                pFilterConfig->Trigger.Source | pFilterConfig->Trigger.Edge |
3353                                (pFilterConfig->DiscardSamples << MDF_DFLTCR_NBDIS_Pos));
3354   }
3355 
3356   /* Configure if needed snapshot format only for MDF instance */
3357   if (IS_MDF_INSTANCE(hmdf->Instance) && (pFilterConfig->AcquisitionMode == MDF_MODE_SYNC_SNAPSHOT))
3358   {
3359     assert_param(IS_MDF_SNAPSHOT_FORMAT(pFilterConfig->SnapshotFormat));
3360     hmdf->Instance->DFLTCR |= pFilterConfig->SnapshotFormat;
3361   }
3362 
3363   /* Configure data source, CIC mode, decimation ratio and gain */
3364   if (IS_ADF_INSTANCE(hmdf->Instance))
3365   {
3366     assert_param(IS_ADF_DATA_SOURCE(pFilterConfig->DataSource));
3367     assert_param(IS_ADF_CIC_MODE(pFilterConfig->CicMode));
3368   }
3369   else
3370   {
3371     assert_param(IS_MDF_DATA_SOURCE(pFilterConfig->DataSource));
3372   }
3373   assert_param(IS_MDF_DECIMATION_RATIO(pFilterConfig->DecimationRatio));
3374   assert_param(IS_MDF_GAIN(pFilterConfig->Gain));
3375   if (pFilterConfig->Gain < 0)
3376   {
3377     int32_t adjust_gain;
3378 
3379     /* adjust gain value to set on register for negative value (offset of -16) */
3380     adjust_gain = pFilterConfig->Gain - 16;
3381     register_gain_value = ((uint32_t) adjust_gain & 0x3FU);
3382   }
3383   else
3384   {
3385     /* for positive value, no offset to apply */
3386     register_gain_value = (uint32_t) pFilterConfig->Gain;
3387   }
3388   hmdf->Instance->DFLTCICR = (pFilterConfig->DataSource | pFilterConfig->CicMode |
3389                               ((pFilterConfig->DecimationRatio - 1U) << MDF_DFLTCICR_MCICD_Pos) |
3390                               (register_gain_value << MDF_DFLTCICR_SCALE_Pos));
3391 
3392   /* Configure bitstream delay */
3393   assert_param(IS_MDF_DELAY(pFilterConfig->Delay));
3394   hmdf->Instance->DLYCR = pFilterConfig->Delay;
3395 
3396   /* Configure offset compensation only for MDF instance */
3397   if (IS_MDF_INSTANCE(hmdf->Instance))
3398   {
3399     assert_param(IS_MDF_OFFSET(pFilterConfig->Offset));
3400     hmdf->Instance->OECCR = (uint32_t) pFilterConfig->Offset;
3401   }
3402 
3403   /* Configure reshape filter */
3404   assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->ReshapeFilter.Activation));
3405   hmdf->Instance->DFLTRSFR = 0U;
3406   if (pFilterConfig->ReshapeFilter.Activation == ENABLE)
3407   {
3408     /* Configure reshape filter decimation ratio */
3409     assert_param(IS_MDF_RSF_DECIMATION_RATIO(pFilterConfig->ReshapeFilter.DecimationRatio));
3410     hmdf->Instance->DFLTRSFR |= pFilterConfig->ReshapeFilter.DecimationRatio;
3411   }
3412   else
3413   {
3414     /* Bypass reshape filter */
3415     hmdf->Instance->DFLTRSFR |= MDF_DFLTRSFR_RSFLTBYP;
3416   }
3417 
3418   /* Configure high-pass filter */
3419   assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->HighPassFilter.Activation));
3420   if (pFilterConfig->HighPassFilter.Activation == ENABLE)
3421   {
3422     /* Configure high-pass filter cut-off frequency */
3423     assert_param(IS_MDF_HPF_CUTOFF_FREQ(pFilterConfig->HighPassFilter.CutOffFrequency));
3424     hmdf->Instance->DFLTRSFR |= pFilterConfig->HighPassFilter.CutOffFrequency;
3425   }
3426   else
3427   {
3428     /* Bypass high-pass filter */
3429     hmdf->Instance->DFLTRSFR |= MDF_DFLTRSFR_HPFBYP;
3430   }
3431 
3432   /* Configure integrator only for MDF instance */
3433   if (IS_MDF_INSTANCE(hmdf->Instance))
3434   {
3435     assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->Integrator.Activation));
3436     if (pFilterConfig->Integrator.Activation == ENABLE)
3437     {
3438       /* Configure integrator value and output division */
3439       assert_param(IS_MDF_INTEGRATOR_VALUE(pFilterConfig->Integrator.Value));
3440       assert_param(IS_MDF_INTEGRATOR_OUTPUT_DIV(pFilterConfig->Integrator.OutputDivision));
3441       hmdf->Instance->DFLTINTR = (((pFilterConfig->Integrator.Value - 1U) << MDF_DFLTINTR_INTVAL_Pos) |
3442                                   pFilterConfig->Integrator.OutputDivision);
3443     }
3444     else
3445     {
3446       /* Bypass integrator */
3447       hmdf->Instance->DFLTINTR = 0U;
3448     }
3449   }
3450 
3451   if (IS_ADF_INSTANCE(hmdf->Instance))
3452   {
3453     assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->SoundActivity.Activation));
3454     if (pFilterConfig->SoundActivity.Activation == ENABLE)
3455     {
3456       /* Configure SAD mode, frame size, hysteresis, sound trigger event
3457          and data memory transfer only for ADF instance */
3458       assert_param(IS_MDF_SAD_MODE(pFilterConfig->SoundActivity.Mode));
3459       assert_param(IS_MDF_SAD_FRAME_SIZE(pFilterConfig->SoundActivity.FrameSize));
3460       if (pFilterConfig->SoundActivity.Mode != MDF_SAD_AMBIENT_NOISE_DETECTOR)
3461       {
3462         assert_param(IS_FUNCTIONAL_STATE(pFilterConfig->SoundActivity.Hysteresis));
3463       }
3464       assert_param(IS_MDF_SAD_SOUND_TRIGGER(pFilterConfig->SoundActivity.SoundTriggerEvent));
3465       assert_param(IS_MDF_SAD_DATA_MEMORY_TRANSFER(pFilterConfig->SoundActivity.DataMemoryTransfer));
3466       if ((pFilterConfig->SoundActivity.Mode != MDF_SAD_AMBIENT_NOISE_DETECTOR) &&
3467           (pFilterConfig->SoundActivity.Hysteresis == ENABLE))
3468       {
3469         hmdf->Instance->SADCR = (pFilterConfig->SoundActivity.Mode | pFilterConfig->SoundActivity.FrameSize |
3470                                  MDF_SADCR_HYSTEN | pFilterConfig->SoundActivity.SoundTriggerEvent |
3471                                  pFilterConfig->SoundActivity.DataMemoryTransfer);
3472       }
3473       else
3474       {
3475         hmdf->Instance->SADCR = (pFilterConfig->SoundActivity.Mode | pFilterConfig->SoundActivity.FrameSize |
3476                                  pFilterConfig->SoundActivity.SoundTriggerEvent |
3477                                  pFilterConfig->SoundActivity.DataMemoryTransfer);
3478       }
3479 
3480       /* Configure SAD minimum noise level, hangover window, learning frames,
3481          ambient noise slope control and signal noise threshold only for ADF instance */
3482       assert_param(IS_MDF_SAD_MIN_NOISE_LEVEL(pFilterConfig->SoundActivity.MinNoiseLevel));
3483       assert_param(IS_MDF_SAD_HANGOVER_WINDOW(pFilterConfig->SoundActivity.HangoverWindow));
3484       assert_param(IS_MDF_SAD_LEARNING_FRAMES(pFilterConfig->SoundActivity.LearningFrames));
3485       assert_param(IS_MDF_SAD_SIGNAL_NOISE_THRESHOLD(pFilterConfig->SoundActivity.SignalNoiseThreshold));
3486       if (pFilterConfig->SoundActivity.Mode != MDF_SAD_SOUND_DETECTOR)
3487       {
3488         assert_param(IS_MDF_SAD_AMBIENT_NOISE_SLOPE(pFilterConfig->SoundActivity.AmbientNoiseSlope));
3489         hmdf->Instance->SADCFGR = ((pFilterConfig->SoundActivity.MinNoiseLevel << MDF_SADCFGR_ANMIN_Pos) |
3490                                    pFilterConfig->SoundActivity.HangoverWindow |
3491                                    pFilterConfig->SoundActivity.LearningFrames |
3492                                    (pFilterConfig->SoundActivity.AmbientNoiseSlope << MDF_SADCFGR_ANSLP_Pos) |
3493                                    pFilterConfig->SoundActivity.SignalNoiseThreshold);
3494       }
3495       else
3496       {
3497         hmdf->Instance->SADCFGR = ((pFilterConfig->SoundActivity.MinNoiseLevel << MDF_SADCFGR_ANMIN_Pos) |
3498                                    pFilterConfig->SoundActivity.HangoverWindow |
3499                                    pFilterConfig->SoundActivity.LearningFrames |
3500                                    pFilterConfig->SoundActivity.SignalNoiseThreshold);
3501       }
3502     }
3503     else
3504     {
3505       /* SAD is not used */
3506       hmdf->Instance->SADCR = 0U;
3507       hmdf->Instance->SADCFGR = 0U;
3508     }
3509   }
3510 
3511   /* Update instance state */
3512   hmdf->State = HAL_MDF_STATE_ACQUISITION;
3513 
3514   /* Enable sound activity detector if needed only for ADF instance */
3515   if ((IS_ADF_INSTANCE(hmdf->Instance)) && (pFilterConfig->SoundActivity.Activation == ENABLE))
3516   {
3517     hmdf->Instance->SADCR |= MDF_SADCR_SADEN;
3518   }
3519 
3520   /* Enable filter */
3521   hmdf->Instance->DFLTCR |= MDF_DFLTCR_DFLTEN;
3522 }
3523 
3524 /**
3525   * @brief  This function handles DMA transfer complete callback.
3526   * @param  hdma DMA handle.
3527   * @retval None.
3528   */
MDF_DmaXferCpltCallback(DMA_HandleTypeDef * hdma)3529 static void MDF_DmaXferCpltCallback(DMA_HandleTypeDef *hdma)
3530 {
3531   MDF_HandleTypeDef *hmdf = (MDF_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3532 
3533   /* Check if DMA in circular mode */
3534   if (hdma->Mode != DMA_LINKEDLIST_CIRCULAR)
3535   {
3536     hmdf->State = HAL_MDF_STATE_READY;
3537   }
3538 
3539 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3540   hmdf->AcqCpltCallback(hmdf);
3541 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3542   HAL_MDF_AcqCpltCallback(hmdf);
3543 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3544 }
3545 
3546 /**
3547   * @brief  This function handles DMA half transfer complete callback.
3548   * @param  hdma DMA handle.
3549   * @retval None.
3550   */
MDF_DmaXferHalfCpltCallback(DMA_HandleTypeDef * hdma)3551 static void MDF_DmaXferHalfCpltCallback(DMA_HandleTypeDef *hdma)
3552 {
3553   MDF_HandleTypeDef *hmdf = (MDF_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3554 
3555 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3556   hmdf->AcqHalfCpltCallback(hmdf);
3557 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3558   HAL_MDF_AcqHalfCpltCallback(hmdf);
3559 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3560 }
3561 
3562 /**
3563   * @brief  This function handles DMA error callback.
3564   * @param  hdma DMA handle.
3565   * @retval None.
3566   */
MDF_DmaErrorCallback(DMA_HandleTypeDef * hdma)3567 static void MDF_DmaErrorCallback(DMA_HandleTypeDef *hdma)
3568 {
3569   MDF_HandleTypeDef *hmdf = (MDF_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3570 
3571   /* Update error code */
3572   hmdf->ErrorCode |= MDF_ERROR_DMA;
3573 
3574 #if (USE_HAL_MDF_REGISTER_CALLBACKS == 1)
3575   hmdf->ErrorCallback(hmdf);
3576 #else /* USE_HAL_MDF_REGISTER_CALLBACKS */
3577   HAL_MDF_ErrorCallback(hmdf);
3578 #endif /* USE_HAL_MDF_REGISTER_CALLBACKS */
3579 }
3580 
3581 /**
3582   * @}
3583   */
3584 
3585 #endif /* HAL_MDF_MODULE_ENABLED */
3586 
3587 /**
3588   * @}
3589   */
3590 
3591 /**
3592   * @}
3593   */
3594