1 /**
2   ******************************************************************************
3   * @file    tae32f53xx_ll_cmp.c
4   * @author  MCD Application Team
5   * @brief   Source file for CMP Moudle
6   *
7   ******************************************************************************
8   * @attention
9   *
10   * <h2><center>&copy; Copyright (c) 2020 Tai-Action.
11   * All rights reserved.</center></h2>
12   *
13   * This software is licensed by Tai-Action under BSD 3-Clause license,
14   * the "License"; You may not use this file except in compliance with the
15   * License. You may obtain a copy of the License at:
16   *                        opensource.org/licenses/BSD-3-Clause
17   *
18   ******************************************************************************
19   */
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "tae32f53xx_ll.h"
23 
24 
25 #define DBG_TAG             "CMP LL"
26 #define DBG_LVL             DBG_ERROR
27 #include "dbg/tae32f53xx_dbg.h"
28 
29 
30 /** @addtogroup TAE32F53xx_LL_Driver
31   * @{
32   */
33 
34 /** @defgroup CMP_LL CMP LL
35   * @brief    CMP LL module driver.
36   * @{
37   */
38 
39 #ifdef LL_CMP_MODULE_ENABLED
40 
41 /* Private define ------------------------------------------------------------*/
42 /* Private macro -------------------------------------------------------------*/
43 /* Private typedef -----------------------------------------------------------*/
44 /* Private variables ---------------------------------------------------------*/
45 /* Private function prototypes -----------------------------------------------*/
46 /* Exported functions --------------------------------------------------------*/
47 /** @defgroup CMP_LL_Exported_Functions CMP LL Exported Functions
48   * @brief    CMP LL Exported Functions
49   * @{
50   */
51 
52 /** @defgroup CMP_LL_Exported_Functions_Group1 Initialization and De-Initialization functions
53   * @brief    Initialization and De-Initialization functions
54 @verbatim
55   ==============================================================================
56               ##### Initialization and De-Initialization functions #####
57   ==============================================================================
58   [..]
59     This section provides functions allowing to:
60     (+) Initialize the CMP peripheral.
61     (+) De-initialize the CMP peripheral.
62 @endverbatim
63   * @{
64   */
65 
66 /**
67   * @brief  Initialize the CMP peripheral
68   * @param  Instance CMP peripheral instance
69   * @return status of the initialization
70   */
LL_CMP_Init(CMP_TypeDef * Instance)71 LL_StatusETypeDef LL_CMP_Init(CMP_TypeDef *Instance)
72 {
73     /* Check the parameters */
74     assert_param(IS_CMP_ALL_INSTANCE(Instance));
75 
76     /* Init the low level hardware */
77     LL_CMP_MspInit(Instance);
78 
79     /* Clear interrupt pending flags */
80     __LL_CMP_PENDING_FLAG_CLEAR(Instance, CMP_FLAG_ALLIF);
81 
82     /* Return function status */
83     return LL_OK;
84 }
85 
86 /**
87   * @brief  Deinitialize the CMP peripheral
88   * @param  Instance CMP peripheral instance
89   * @return status of the initialization
90   */
LL_CMP_DeInit(CMP_TypeDef * Instance)91 LL_StatusETypeDef LL_CMP_DeInit(CMP_TypeDef *Instance)
92 {
93     /* Check the parameters */
94     assert_param(IS_CMP_ALL_INSTANCE(Instance));
95 
96     for (uint8_t chnnum = 0; chnnum < CMP_CHN_NB; ++chnnum) {
97         /* Disable CMP channel FALIE/RISIE interrupts */
98         __LL_CMP_IT_DISABLE(Instance, chnnum, CMP_IT_FALIE | CMP_IT_RISIE);
99 
100         /* Disable CMP channel */
101         __LL_CMP_DISABLE(Instance, chnnum);
102     }
103 
104     /* DeInit the low level hardware */
105     LL_CMP_MspDeInit(Instance);
106 
107     /* Return function status */
108     return LL_OK;
109 }
110 
111 /**
112   * @brief  Initializes the CMP MSP.
113   * @param  Instance CMP peripheral
114   * @return None
115   */
LL_CMP_MspInit(CMP_TypeDef * Instance)116 __WEAK void LL_CMP_MspInit(CMP_TypeDef *Instance)
117 {
118     /* Prevent unused argument(s) compilation warning */
119     LL_UNUSED(Instance);
120 
121     /* NOTE : This function should not be modified, when the callback is needed,
122               the LL_CMP_MspInit could be implemented in the user file
123      */
124 }
125 
126 /**
127   * @brief  DeInitializes the CMP MSP
128   * @param  Instance CMP peripheral
129   * @return None
130   */
LL_CMP_MspDeInit(CMP_TypeDef * Instance)131 __WEAK void LL_CMP_MspDeInit(CMP_TypeDef *Instance)
132 {
133     /* Prevent unused argument(s) compilation warning */
134     LL_UNUSED(Instance);
135 
136     /* NOTE: This function should not be modified, when the callback is needed,
137              the LL_CMP_MspDeInit could be implemented in the user file
138      */
139 }
140 
141 /**
142   * @}
143   */
144 
145 
146 /** @defgroup CMP_LL_Exported_Functions_Group2 CMP Peripheral Control functions
147   * @brief    CMP Peripheral Control functions
148 @verbatim
149   ===============================================================================
150                        ##### Peripheral Control functions #####
151   ===============================================================================
152   [..] This section provides functions allowing to:
153     (+) Configure CMP channels.
154 
155 @endverbatim
156   * @{
157   */
158 
159 /**
160   * @brief  Configures the selected CMP channel.
161   * @param  Instance CMP peripheral
162   * @param  Channel The selected CMP channel.
163   *         This parameter can be one of the following values:
164   *            @arg CMP_CHANNEL_0: CMP Channel0 selected
165   *            @arg CMP_CHANNEL_1: CMP Channel1 selected
166   *            @arg CMP_CHANNEL_2: CMP Channel2 selected
167   *            @arg CMP_CHANNEL_3: CMP Channel3 selected
168   * @param  sConfig CMP configuration structure.
169   * @return LL Status
170   */
LL_CMP_ChannelConfig(CMP_TypeDef * Instance,uint32_t Channel,CMP_ChannelConfTypeDef * sConfig)171 LL_StatusETypeDef LL_CMP_ChannelConfig(CMP_TypeDef *Instance, uint32_t Channel, CMP_ChannelConfTypeDef *sConfig)
172 {
173     /* Get the channel number */
174     uint8_t chnnum = POSITION_VAL(Channel);
175 
176     /* Check the parameters */
177     assert_param(IS_CMP_ALL_INSTANCE(Instance));
178     assert_param(IS_CMP_CHANNEL(Channel));
179     assert_param(IS_CMP_NUMBER(chnnum));
180     assert_param(sConfig != NULL);
181     assert_param(IS_CMP_INPUT_MINUS(sConfig->InputMinus));
182     assert_param(IS_CMP_HYSTERESIS(sConfig->Hysteresis));
183     assert_param(IS_CMP_BLANKING_SOURCE(Channel, sConfig->BlankingSource));
184     assert_param(IS_CMP_OUTPUT_DEBOUNCE(sConfig->OutputDebounce));
185     assert_param(IS_CMP_OUTPUT_DEBOUNCE_VAL(sConfig->OutputDebounceValue));
186     assert_param(IS_CMP_OUTPUT_POLARITY(sConfig->OutputPolarity));
187     assert_param(IS_CMP_TRIGGER_IT(sConfig->TriggerInterrupt));
188 
189     /* Set Debounce Vaule */
190     if (sConfig->OutputDebounceValue == CMP_OUTPUT_DEBOUNCE_ENABLE) {
191         __LL_CMP_DEBOUNCE_VALUE_SET(Instance, chnnum, sConfig->OutputDebounceValue);
192     }
193 
194     /* Reset and Calculate CR register value depending on sConfig */
195     MODIFY_REG(Instance->CR[chnnum],
196                (CMP_CR_OPOL_Msk
197                 | CMP_CR_ODEB_Msk
198                 | CMP_CR_BLANKING_Msk
199                 | CMP_CR_INM_Msk
200                 | CMP_CR_HYST_Msk
201                 | CMP_CR_FALIE_Msk
202                 | CMP_CR_RISIE_Msk
203                ),
204                (sConfig->OutputPolarity
205                 | sConfig->OutputDebounce
206                 | sConfig->BlankingSource
207                 | sConfig->InputMinus
208                 | sConfig->Hysteresis
209                 | sConfig->TriggerInterrupt));
210 
211     /* Return function status */
212     return LL_OK;
213 }
214 
215 /**
216   * @}
217   */
218 
219 
220 /** @defgroup CMP_LL_Exported_Functions_Group3 CMP IO operation functions
221   * @brief    CMP IO operation functions
222   *
223 @verbatim
224   ==============================================================================
225              ##### IO operation functions #####
226   ==============================================================================
227     [..]  This section provides functions allowing to:
228       (+) Start/Stop CMP.
229 
230 @endverbatim
231   * @{
232   */
233 
234 /**
235   * @brief  Enables CMP Channel conversion
236   * @param  Instance CMP peripheral instance
237   * @param  Channel The selected CMP channel.
238   *         This parameter can be one of the following values:
239   *            @arg CMP_CHANNEL_0: CMP Channel0 selected
240   *            @arg CMP_CHANNEL_1: CMP Channel1 selected
241   *            @arg CMP_CHANNEL_2: CMP Channel2 selected
242   *            @arg CMP_CHANNEL_3: CMP Channel3 selected
243   * @return LL Status
244   */
LL_CMP_Start(CMP_TypeDef * Instance,uint32_t Channel)245 LL_StatusETypeDef LL_CMP_Start(CMP_TypeDef *Instance, uint32_t Channel)
246 {
247     /* Get the channel number */
248     uint8_t chnnum = POSITION_VAL(Channel);
249 
250     /* Check the parameters */
251     assert_param(IS_CMP_ALL_INSTANCE(Instance));
252     assert_param(IS_CMP_CHANNEL(Channel));
253     assert_param(IS_CMP_NUMBER(chnnum));
254 
255     /* Enable the CMP channel */
256     __LL_CMP_ENABLE(Instance, chnnum);
257 
258     /* Return function status */
259     return LL_OK;
260 }
261 
262 /**
263   * @brief  Disable CMP Channel conversion
264   * @param  Instance CMP peripheral instance
265   * @param  Channel The selected CMP channel.
266   *         This parameter can be one of the following values:
267   *            @arg CMP_CHANNEL_0: CMP Channel0 selected
268   *            @arg CMP_CHANNEL_1: CMP Channel1 selected
269   *            @arg CMP_CHANNEL_2: CMP Channel2 selected
270   *            @arg CMP_CHANNEL_3: CMP Channel3 selected
271   * @return LL Status
272   */
LL_CMP_Stop(CMP_TypeDef * Instance,uint32_t Channel)273 LL_StatusETypeDef LL_CMP_Stop(CMP_TypeDef *Instance, uint32_t Channel)
274 {
275     /* Get the channel number */
276     uint8_t chnnum = POSITION_VAL(Channel);
277 
278     /* Check the CMP parameters */
279     assert_param(IS_CMP_ALL_INSTANCE(Instance));
280     assert_param(IS_CMP_CHANNEL(Channel));
281     assert_param(IS_CMP_NUMBER(chnnum));
282 
283     /* Enable CMP Channel */
284     __LL_CMP_DISABLE(Instance, chnnum);
285 
286     /* Return function status */
287     return LL_OK;
288 }
289 
290 /**
291   * @}
292   */
293 
294 
295 /** @defgroup CMP_LL_Exported_Functions_Interrupt CMP Interrupt management
296   * @brief    CMP Interrupt management
297 @verbatim
298   ===============================================================================
299                           ##### Initerrupt management #####
300   ===============================================================================
301   [..]
302       This section provides CMP interrupt handler and callback functions.
303 
304 @endverbatim
305   * @{
306   */
307 
308 /**
309   * @brief   This function handles CMP interrupts requests.
310   * @param   Instance CMP peripheral
311   * @return  None
312   */
LL_CMP_IRQHandler(CMP_TypeDef * Instance)313 void LL_CMP_IRQHandler(CMP_TypeDef *Instance)
314 {
315     uint32_t irqtemp = READ_REG(Instance->SR);
316 
317     /* Check the CMP parameters */
318     assert_param(IS_CMP_ALL_INSTANCE(Instance));
319 
320     for (uint8_t chnnum = 0; chnnum < CMP_CHN_NB; ++chnnum) {
321         if ((__LL_CMP_IT_CHECK_SOURCE(Instance, chnnum, CMP_IT_FALIE) != RESET) &&
322             ((irqtemp & (CMP_SR_FALIF_0 << chnnum)) != RESET)) {
323             /* Chear the CMP_CHx FALIF pending flag */
324             __LL_CMP_PENDING_FLAG_CLEAR(Instance, (CMP_SR_FALIF_0 << chnnum));
325 
326             /* CMP falling edge trigger user callback */
327             LL_CMP_FailingEdgeTrigCallback(Instance, (0x1UL << chnnum));
328         }
329 
330         if ((__LL_CMP_IT_CHECK_SOURCE(Instance, chnnum, CMP_IT_RISIE) != RESET) &&
331             ((irqtemp & (CMP_SR_RISIF_0 << chnnum)) != RESET)) {
332             /* Chear the CMP_CHx RISIF pending flag */
333             __LL_CMP_PENDING_FLAG_CLEAR(Instance, (CMP_SR_RISIF_0 << chnnum));
334 
335             /* CMP rising edge trigger user callback */
336             LL_CMP_RisingEdgeTrigCallback(Instance, (0x1UL << chnnum));
337         }
338     }
339 }
340 
341 /**
342   * @brief  CMP_CHx failing edge interrupt callback function
343   * @param  Instance CMP peripheral
344   * @param  Channel The handling CMP channel
345   * @return None
346   */
LL_CMP_FailingEdgeTrigCallback(CMP_TypeDef * Instance,uint32_t Channel)347 __WEAK void LL_CMP_FailingEdgeTrigCallback(CMP_TypeDef *Instance, uint32_t Channel)
348 {
349     /* Prevent unused argument(s) compilation warning */
350     LL_UNUSED(Instance);
351     LL_UNUSED(Channel);
352 
353     /* NOTE: This function should not be modified, when the callback is needed,
354              the LL_CMP_FailingEdgeTrigCallback could be implemented in the user file
355      */
356 }
357 
358 /**
359   * @brief  CMP_CHx rising edge interrupt callback function
360   * @param  Instance CMP peripheral
361   * @param  Channel The handling CMP channel
362   * @return None
363   */
LL_CMP_RisingEdgeTrigCallback(CMP_TypeDef * Instance,uint32_t Channel)364 __WEAK void LL_CMP_RisingEdgeTrigCallback(CMP_TypeDef *Instance, uint32_t Channel)
365 {
366     /* Prevent unused argument(s) compilation warning */
367     LL_UNUSED(Instance);
368     LL_UNUSED(Channel);
369 
370     /* NOTE: This function should not be modified, when the callback is needed,
371              the LL_CMP_RisingEdgeTrigCallback could be implemented in the user file
372      */
373 }
374 
375 /**
376   * @}
377   */
378 
379 /**
380   * @}
381   */
382 
383 /* Private functions ---------------------------------------------------------*/
384 
385 #endif /* LL_CMP_MODULE_ENABLED */
386 
387 /**
388   * @}
389   */
390 
391 /**
392   * @}
393   */
394 
395 /************************* (C) COPYRIGHT Tai-Action *****END OF FILE***********/
396 
397