1 /**
2   ******************************************************************************
3   * @file    tae32f53xx_ll_iir.c
4   * @author  MCD Application Team
5   * @brief   IIR LL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the IIR peripheral:
8   *           + Initialization and De-Initialization functions
9   *           + Start and Stop functions
10   *           + Get output data function
11   *           + Reset internal buffer function
12   *           + Interrupt and Callback functions
13 
14   ******************************************************************************
15   * @attention
16   *
17   * <h2><center>&copy; Copyright (c) 2020 Tai-Action.
18   * All rights reserved.</center></h2>
19   *
20   * This software is licensed by Tai-Action under BSD 3-Clause license,
21   * the "License"; You may not use this file except in compliance with the
22   * License. You may obtain a copy of the License at:
23   *                        opensource.org/licenses/BSD-3-Clause
24   *
25   ******************************************************************************
26   */
27 
28 /* Includes ------------------------------------------------------------------*/
29 #include "tae32f53xx_ll.h"
30 
31 
32 #define DBG_TAG             "IIR LL"
33 #define DBG_LVL             DBG_ERROR
34 #include "dbg/tae32f53xx_dbg.h"
35 
36 
37 /** @addtogroup TAE32F53xx_LL_Driver
38   * @{
39   */
40 
41 /** @defgroup IIR_LL IIR LL
42   * @brief    IIR LL module driver
43   * @{
44   */
45 
46 #ifdef LL_IIR_MODULE_ENABLED
47 
48 /* Private typedef -----------------------------------------------------------*/
49 /* Private define ------------------------------------------------------------*/
50 /* Private macro -------------------------------------------------------------*/
51 /* Private variables ---------------------------------------------------------*/
52 /* Private function prototypes -----------------------------------------------*/
53 /* Exported functions --------------------------------------------------------*/
54 /** @defgroup IIR_LL_Exported_functions IIR LL Exported functions
55   * @brief    IIR LL Exported functions
56   * @{
57   */
58 
59 /** @defgroup IIR_LL_Exported_Functions_Group1 Initialization and De-Initialization functions
60   * @brief    Initialization and De-Initialization functions
61 @verbatim
62   ===============================================================================
63             ##### Initialization and De-Initialization functions #####
64   ===============================================================================
65   [..]  This section provides a set of functions allowing to initialize and
66         deinitialize the IIR peripheral
67 
68 @endverbatim
69   * @{
70   */
71 
72 /**
73   * @brief  Initializes the IIR peripheral.
74   * @param  Instance Specifies IIR peripheral.
75   * @param  Init pointer to a IIR_InitTypeDef structure that contains the
76   *         configuration information for the specified IIR peripheral.
77   * @return status of the initialization
78   */
LL_IIR_Init(IIR_TypeDef * Instance,IIR_InitTypeDef * Init)79 LL_StatusETypeDef LL_IIR_Init(IIR_TypeDef *Instance, IIR_InitTypeDef *Init)
80 {
81     /* Check the parameters */
82     assert_param(IS_IIR_ALL_INSTANCE(Instance));
83 
84     /* Handle Something */
85     LL_IIR_MspInit(Instance);
86 
87     /* IIRx_CR0 Configure : IIR Order select, Internal buffer reset and
88        enable IIR peripheral */
89     WRITE_REG(Instance->CR0, (Init->Order | Init->BufferReset));
90 
91     /* Enable the IIR peripheral */
92     __LL_IIR_ENABLE(Instance);
93 
94     /* Return function status */
95     return LL_OK;
96 }
97 
98 /**
99   * @brief  De-initializes the IIR peripheral registers to their default reset values.
100   * @param  IIRx where x can be (0, 1, ...) to select the IIR peripheral.
101   * @retval status of the de-initialization
102   */
LL_IIR_DeInit(IIR_TypeDef * Instance)103 LL_StatusETypeDef LL_IIR_DeInit(IIR_TypeDef *Instance)
104 {
105     /* Check the parameters */
106     assert_param(IS_IIR_ALL_INSTANCE(Instance));
107 
108     /* Disable IIR */
109     __LL_IIR_DISABLE(Instance);
110 
111     /* Handle Something */
112     LL_IIR_MspDeInit(Instance);
113 
114     /* Return function status */
115     return LL_OK;
116 }
117 
118 /**
119   * @brief  Initializes the IIR MSP.
120   * @param  Instance IIR peripheral
121   * @return None
122   */
LL_IIR_MspInit(IIR_TypeDef * Instance)123 __WEAK void LL_IIR_MspInit(IIR_TypeDef *Instance)
124 {
125     /* Prevent unused argument(s) compilation warning */
126     LL_UNUSED(Instance);
127     /* NOTE: This function should not be modified, when the callback is needed,
128              the LL_IIR_MspInit could be implemented in the user file
129      */
130 }
131 
132 /**
133   * @brief  DeInitializes the IIR MSP
134   * @param  Instance IIR peripheral
135   * @return None
136   */
LL_IIR_MspDeInit(IIR_TypeDef * Instance)137 __WEAK void LL_IIR_MspDeInit(IIR_TypeDef *Instance)
138 {
139     /* Prevent unused argument(s) compilation warning */
140     LL_UNUSED(Instance);
141     /* NOTE: This function should not be modified, when the callback is needed,
142              the LL_IIR_MspDeInit could be implemented in the user file
143      */
144 }
145 
146 /**
147   * @}
148   */
149 
150 
151 /** @defgroup IIR_LL_Exported_Functions_Group2 IIR Peripheral Control functions
152   * @brief    IIR Peripheral Control functions
153 @verbatim
154   ===============================================================================
155                   ##### Peripheral Control functions #####
156   ===============================================================================
157     [..]  This section provides functions allowing to:
158       (+) Configure the IIR control registers
159       (+) Configure the IIR preload control registers
160 
161 @endverbatim
162   * @{
163   */
164 
165 /**
166   * @brief  Configures the IIR filter
167   * @param  Instance IIR peripheral.
168   * @param  Config   pointer to a IIR_ConfigTypeDef structure that contains
169   *         the configuration information for the specified IIR peripheral.
170   * @return status of the configuration
171   */
LL_IIR_FilterConfig(IIR_TypeDef * Instance,IIR_ConfigTypeDef * Config)172 LL_StatusETypeDef LL_IIR_FilterConfig(IIR_TypeDef *Instance, IIR_ConfigTypeDef *Config)
173 {
174     /* Check the parameters */
175     assert_param(IS_IIR_ALL_INSTANCE(Instance));
176     assert_param(IS_IIR_INPUT_DATA_SCALE(Config->InDataScale));
177     assert_param(IS_IIR_OUTPUT_DATA_SCALE(Config->OutDataScale));
178     assert_param(IS_IIR_FEEDBACK_SCALE(Config->FeedBackScale));
179 
180     /* IIR Scale configure : Output data scale, Input data scale and Feedback scale */
181     WRITE_REG(Instance->SCALR, (((uint32_t)(Config->InDataScale     << IIR_SCALR_DISCAL_Pos))  |
182                                 ((uint32_t)(Config->FeedBackScale   << IIR_SCALR_FBSCAL_Pos))  |
183                                 ((uint32_t)(Config->OutDataScale    << IIR_SCALR_DOSCAL_Pos))));
184 
185     /* IIR Data Input Address */
186     WRITE_REG(Instance->DIAR, Config->InDataAddress);
187 
188     /* fill the coefficients */
189     for (int idx = 0;  idx < ARRAY_SIZE(Config->AxCOEF); idx++) {
190         WRITE_REG(Instance->AxCOEFR[idx], ((uint32_t)(Config->AxCOEF[idx]) & IIR_AxCOEFR_AxCOEF_Msk));
191     }
192 
193     for (int idx = 0;  idx < ARRAY_SIZE(Config->BxCOEF); idx++) {
194         WRITE_REG(Instance->BxCOEFR[idx], ((uint32_t)(Config->BxCOEF[idx]) & IIR_BxCOEFR_BxCOEF_Msk));
195     }
196 
197     /* Return function status */
198     return LL_OK;
199 }
200 
201 /**
202   * @brief  Configures the IIR filter preload registers
203   * @note   This function is related to IIR Auto-Reload feature.
204   *         Using __LL_IIR_AUTORELAOD_ENABLE() will auto-reload this configures.
205   *         Using LL_IIR_FilterStart[_IT]() will auto-reload this configures(with AutoReload enabled) then start an IIR filter.
206   * @param  Instance IIR peripheral.
207   * @param  Config   pointer to a IIR_ConfigTypeDef structure that contains
208   *         the configuration information for the specified IIR peripheral.
209   * @return status of the configuration
210   */
LL_IIR_FilterConfig_Preload(IIR_TypeDef * Instance,IIR_ConfigTypeDef * Config)211 LL_StatusETypeDef LL_IIR_FilterConfig_Preload(IIR_TypeDef *Instance, IIR_ConfigTypeDef *Config)
212 {
213     /* Check the parameters */
214     assert_param(IS_IIR_ALL_INSTANCE(Instance));
215     assert_param(IS_IIR_INPUT_DATA_SCALE(Config->InDataScale));
216     assert_param(IS_IIR_OUTPUT_DATA_SCALE(Config->OutDataScale));
217     assert_param(IS_IIR_FEEDBACK_SCALE(Config->FeedBackScale));
218 
219     /* IIR Scale configure : Output data scale, Input data scale and Feedback scale */
220     WRITE_REG(Instance->SCALSR, (((uint32_t)(Config->InDataScale     << IIR_SCALSR_DISCALS_Pos))  |
221                                  ((uint32_t)(Config->FeedBackScale   << IIR_SCALSR_FBSCALS_Pos))  |
222                                  ((uint32_t)(Config->OutDataScale    << IIR_SCALSR_DOSCALS_Pos))));
223 
224     /* IIR Data Input Address */
225     WRITE_REG(Instance->DIASR, Config->InDataAddress);
226 
227     /* fill the coefficients */
228     for (int idx = 0;  idx < ARRAY_SIZE(Config->AxCOEF); idx++) {
229         WRITE_REG(Instance->AxCOEFSR[idx], ((uint32_t)(Config->AxCOEF[idx]) & IIR_AxCOEFSR_AxCOEFS_Msk));
230     }
231 
232     for (int idx = 0;  idx < ARRAY_SIZE(Config->BxCOEF); idx++) {
233         WRITE_REG(Instance->BxCOEFSR[idx], ((uint32_t)(Config->BxCOEF[idx]) & IIR_BxCOEFSR_BxCOEFS_Msk));
234     }
235 
236     /* Return function status */
237     return LL_OK;
238 }
239 
240 /**
241   * @}
242   */
243 
244 
245 /** @defgroup IIR_LL_Exported_Functions_Group3 IIR Input and Output operation functions
246   * @brief    IIR Input and Output operation functions
247 @verbatim
248   ===============================================================================
249                   ##### Input and Output operation functions #####
250   ===============================================================================
251   [..]
252     This section provides functions allowing to:
253     (+) start and stop functions.
254     (+) start and stop with interrupt functions.
255     (+) get output data function.
256     (+) reset internal buffer function.
257 
258 @endverbatim
259   * @{
260   */
261 
262 /**
263   * @brief  Start an IIR Filter for current input data
264   * @param  Instance IIR peripheral.
265   * @param  AutoReload specifies IIR Auto-Reload the preloaded configures or not.
266   *         This parameter can be one of the following values:
267   *           @arg IIR_AUTORELOAD_DISABLE: Disable Auto-Reload feature.
268   *           @arg IIR_AUTORELOAD_ENABLE: Auto-Reload the preload configures before starting IIR filter.
269   * @note   The preloaded configures should be configured by calling LL_IIR_FilterConfig_Preload() function,
270   *         before enable Auto-Reload feature.
271   * @return LL status
272   */
LL_IIR_FilterStart(IIR_TypeDef * Instance,IIR_ATReloadETypeDef AutoReload)273 LL_StatusETypeDef LL_IIR_FilterStart(IIR_TypeDef *Instance, IIR_ATReloadETypeDef AutoReload)
274 {
275     /* Check the parameters */
276     assert_param(IS_IIR_ALL_INSTANCE(Instance));
277 
278     /* Configure the Auto-Reload feature before start the IIR filter */
279     if (AutoReload != IIR_AUTORELOAD_DISABLE) {
280         __LL_IIR_AUTORELOAD_ENABLE(Instance);
281     }
282 
283     /* Start the IIR filter */
284     __LL_IIR_FILTER_START(Instance);
285 
286     /* Return function status */
287     return LL_OK;
288 }
289 
290 /**
291   * @brief  Start an IIR Filter for current input data with interrupt
292   * @param  Instance IIR peripheral.
293   * @param  AutoReload specifies IIR Auto-Reload the preloaded configures or not.
294   *         This parameter can be one of the following values:
295   *           @arg IIR_AUTORELOAD_DISABLE: Disable Auto-Reload feature.
296   *           @arg IIR_AUTORELOAD_ENABLE: Auto-Reload the preload configures before starting IIR filter.
297   * @note   The preloaded configures should be configured by calling LL_IIR_FilterConfig_Preload() function,
298   *         before enable Auto-Reload feature
299   * @return LL status
300   */
LL_IIR_FilterStart_IT(IIR_TypeDef * Instance,IIR_ATReloadETypeDef AutoReload)301 LL_StatusETypeDef LL_IIR_FilterStart_IT(IIR_TypeDef *Instance, IIR_ATReloadETypeDef AutoReload)
302 {
303     /* Check the parameters */
304     assert_param(IS_IIR_ALL_INSTANCE(Instance));
305 
306     /* Enable IIR interrupt */
307     __LL_IIR_ENABLE_IT(Instance, IIR_IT_FDIE);
308 
309     /* Configure the Auto-Reload feature before start the IIR filter */
310     if (AutoReload != IIR_AUTORELOAD_DISABLE) {
311         __LL_IIR_AUTORELOAD_ENABLE(Instance);
312     }
313 
314     /* Start the IIR filter */
315     __LL_IIR_FILTER_START(Instance);
316 
317     /* Return function status */
318     return LL_OK;
319 }
320 
321 /**
322   * @brief  Get the latest IIR filter output data
323   * @param  Instance IIR peripheral.
324   * @return IIR filter output data
325   */
LL_IIR_FilterDataGet(IIR_TypeDef * Instance)326 int16_t LL_IIR_FilterDataGet(IIR_TypeDef *Instance)
327 {
328     /* Check the parameters */
329     assert_param(IS_IIR_ALL_INSTANCE(Instance));
330 
331     /* Return the IIR filter output data */
332     return (int16_t)(READ_REG(Instance->DOR) & 0xFFFFUL);
333 }
334 
335 /**
336   * @brief  Reset the IIR internal data buffer.
337   * @param  Instance IIR peripheral.
338   * @return LL status
339   */
LL_IIR_FilterBufferReset(IIR_TypeDef * Instance)340 LL_StatusETypeDef LL_IIR_FilterBufferReset(IIR_TypeDef *Instance)
341 {
342     /* Check the parameters */
343     assert_param(IS_IIR_ALL_INSTANCE(Instance));
344 
345     /* IIR Internal Buffer Reset control */
346     __LL_IIR_FILTER_BUFFER_RESET(Instance);
347 
348     /* Return function status */
349     return LL_OK;
350 }
351 
352 /**
353   * @}
354   */
355 
356 
357 /** @defgroup IIR_LL_Exported_Functions_Interrupt IIR Initerrupt management
358   * @brief    IIR Initerrupt management
359 @verbatim
360   ===============================================================================
361                         ##### Initerrupt management #####
362   ===============================================================================
363   [..]
364       This section provides IIR interrupt handler and callback functions.
365 
366 @endverbatim
367   * @{
368   */
369 
370 /**
371   * @brief  This function handles IIR interrupts requests.
372   * @param  Instance IIR peripheral
373   * @return None
374   */
LL_IIR_IRQHandler(IIR_TypeDef * Instance)375 void LL_IIR_IRQHandler(IIR_TypeDef *Instance)
376 {
377     /* Check the parameters */
378     assert_param(IS_IIR_ALL_INSTANCE(Instance));
379 
380     if ((__LL_IIR_IT_CHECK_SOURCE(Instance, IIR_IT_FDIE) != RESET) && (__LL_IIR_GET_FLAG(Instance, IIR_FLAG_FDIF) != RESET)) {
381         /* Clear the FDIF interrupt flag */
382         __LL_IIR_CLEAR_FLAG(Instance, IIR_FLAG_FDIF);
383 
384         /* IIR Fliter done callback */
385         LL_IIR_FilterDoneCallBack(Instance);
386     }
387 }
388 /**
389   * @brief  IIR Fliter done callback
390   * @param  Instance IIR peripheral
391   * @return None
392   */
LL_IIR_FilterDoneCallBack(IIR_TypeDef * Instance)393 __WEAK void LL_IIR_FilterDoneCallBack(IIR_TypeDef *Instance)
394 {
395     /* Prevent unused argument(s) compilation warning */
396     LL_UNUSED(Instance);
397 
398     /* NOTE: This function should not be modified, when the callback is needed,
399              the LL_IIR_FilterDoneCallBack could be implemented in the user file
400      */
401 }
402 
403 /**
404   * @}
405   */
406 
407 /**
408   * @}
409   */
410 
411 
412 /* Private functions ---------------------------------------------------------*/
413 
414 
415 #endif /* LL_GPIO_MODULE_ENABLE */
416 
417 
418 /**
419   * @}
420   */
421 
422 /**
423   * @}
424   */
425 
426 
427 /************************* (C) COPYRIGHT Tai-Action *****END OF FILE***********/
428 
429