1 /**
2   ******************************************************************************
3   * @file    stm32wbaxx_ll_comp.h
4   * @author  MCD Application Team
5   * @brief   Header file of COMP LL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2022 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 
19 /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef STM32WBAxx_LL_COMP_H
21 #define STM32WBAxx_LL_COMP_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32wbaxx.h"
29 
30 /** @addtogroup STM32WBAxx_LL_Driver
31   * @{
32   */
33 
34 #if defined (COMP1) || defined (COMP2)
35 
36 /** @defgroup COMP_LL COMP
37   * @{
38   */
39 
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /** @defgroup COMP_LL_Private_Constants COMP Private Constants
44   * @{
45   */
46 
47 /* Internal mask for pair of comparators instances window mode:               */
48 /* To select into literals LL_COMP_WINDOWMODE_COMPx_INPUT_PLUS_COMMON         */
49 /* the relevant bits for:                                                     */
50 /* (concatenation of multiple bits used in different registers)               */
51 /* - Comparator instance selected as master for window mode : register offset */
52 /* - Window mode enable or disable: bit value */
53 #define LL_COMP_WINDOWMODE_COMP_ODD_REGOFFSET_MASK  (0x00000000UL) /* Register of COMP instance odd (COMP1_CSR, ...)
54                                                                       defined as reference register */
55 #define LL_COMP_WINDOWMODE_COMP_EVEN_REGOFFSET_MASK (0x00000001UL) /* Register of COMP instance even (COMP2_CSR, ...)
56                                                                       offset vs register of COMP instance odd */
57 #define LL_COMP_WINDOWMODE_COMPX_REGOFFSET_MASK     (LL_COMP_WINDOWMODE_COMP_ODD_REGOFFSET_MASK  \
58                                                      | LL_COMP_WINDOWMODE_COMP_EVEN_REGOFFSET_MASK)
59 #define LL_COMP_WINDOWMODE_COMPX_SETTING_MASK       (COMP_CSR_WINMODE)
60 #define LL_COMP_WINDOWOUTPUT_COMPX_SETTING_MASK     (COMP_CSR_WINOUT)
61 #define LL_COMP_WINDOWOUTPUT_BOTH_SETTING_MASK      (COMP_CSR_WINOUT << 1UL)
62 #define LL_COMP_WINDOWOUTPUT_BOTH_POS_VS_WINDOW     (1UL)
63 
64 /* COMP registers bits positions */
65 #define LL_COMP_WINDOWMODE_BITOFFSET_POS   (11UL) /* Value equivalent to POSITION_VAL(COMP_CSR_WINMODE) */
66 #define LL_COMP_OUTPUT_LEVEL_BITOFFSET_POS (30UL) /* Value equivalent to POSITION_VAL(COMP_CSR_VALUE) */
67 
68 /**
69   * @}
70   */
71 
72 /* Private macros ------------------------------------------------------------*/
73 /** @defgroup COMP_LL_Private_Macros COMP Private Macros
74   * @{
75   */
76 
77 /**
78   * @brief  Driver macro reserved for internal use: set a pointer to
79   *         a register from a register basis from which an offset
80   *         is applied.
81   * @param  __REG__ Register basis from which the offset is applied.
82   * @param  __REG_OFFFSET__ Offset to be applied (unit: number of registers).
83   * @retval Pointer to register address
84   */
85 #define __COMP_PTR_REG_OFFSET(__REG__, __REG_OFFFSET__)                        \
86   ((__IO uint32_t *)((uint32_t) ((uint32_t)(&(__REG__)) + ((__REG_OFFFSET__) << 2UL))))
87 
88 /**
89   * @}
90   */
91 
92 /* Exported types ------------------------------------------------------------*/
93 #if defined(USE_FULL_LL_DRIVER)
94 /** @defgroup COMP_LL_ES_INIT COMP Exported Init structure
95   * @{
96   */
97 
98 /**
99   * @brief  Structure definition of some features of COMP instance.
100   */
101 typedef struct
102 {
103   uint32_t PowerMode;                   /*!< Set comparator operating mode to adjust power and speed.
104                                              This parameter can be a value of @ref COMP_LL_EC_POWERMODE
105                                              This feature can be modified afterwards using unitary
106                                              function @ref LL_COMP_SetPowerMode(). */
107 
108   uint32_t InputPlus;                   /*!< Set comparator input plus (non-inverting input).
109                                              This parameter can be a value of @ref COMP_LL_EC_INPUT_PLUS
110                                              This feature can be modified afterwards using unitary function
111                                              @ref LL_COMP_SetInputPlus(). */
112 
113   uint32_t InputMinus;                  /*!< Set comparator input minus (inverting input).
114                                              This parameter can be a value of @ref COMP_LL_EC_INPUT_MINUS
115                                              This feature can be modified afterwards using unitary function
116                                               @ref LL_COMP_SetInputMinus(). */
117 
118   uint32_t InputHysteresis;             /*!< Set comparator hysteresis mode of the input minus.
119                                              This parameter can be a value of @ref COMP_LL_EC_INPUT_HYSTERESIS
120                                              This feature can be modified afterwards using unitary function
121                                              @ref LL_COMP_SetInputHysteresis(). */
122 
123   uint32_t OutputPolarity;              /*!< Set comparator output polarity.
124                                              This parameter can be a value of @ref COMP_LL_EC_OUTPUT_POLARITY
125                                              This feature can be modified afterwards using unitary function
126                                              @ref LL_COMP_SetOutputPolarity(). */
127 
128   uint32_t OutputBlankingSource;        /*!< Set comparator blanking source.
129                                              This parameter can be a value of @ref COMP_LL_EC_OUTPUT_BLANKING_SOURCE
130                                              This feature can be modified afterwards using unitary function
131                                              @ref LL_COMP_SetOutputBlankingSource(). */
132 
133 } LL_COMP_InitTypeDef;
134 
135 /**
136   * @}
137   */
138 #endif /* USE_FULL_LL_DRIVER */
139 
140 /* Exported constants --------------------------------------------------------*/
141 /** @defgroup COMP_LL_Exported_Constants COMP Exported Constants
142   * @{
143   */
144 
145 
146 /** @defgroup COMP_LL_EC_COMMON_WINDOWMODE Comparator common modes - Window mode
147   * @{
148   */
149 #define LL_COMP_WINDOWMODE_DISABLE                 (0x00000000UL)                                                   /*!< Window mode disable: Comparators 1 and 2 are independent */
150 #define LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON (COMP_CSR_WINMODE | LL_COMP_WINDOWMODE_COMP_EVEN_REGOFFSET_MASK) /*!< Window mode enable: Comparators instances pair COMP1 and COMP2 have their input plus connected together. The common input is COMP1 input plus (COMP2 input plus is no more accessible). */
151 #define LL_COMP_WINDOWMODE_COMP2_INPUT_PLUS_COMMON (COMP_CSR_WINMODE | LL_COMP_WINDOWMODE_COMP_ODD_REGOFFSET_MASK)  /*!< Window mode enable: if used from COMP1 or COMP2 instance, comparators instances pair COMP1 and COMP2 have their input plus connected together, the common input is COMP2 input plus (COMP1 input plus is no more accessible). */
152 /**
153   * @}
154   */
155 
156 /** @defgroup COMP_LL_EC_COMMON_WINDOWOUTPUT Comparator common modes - Window output
157   * @{
158   */
159 #define LL_COMP_WINDOWOUTPUT_EACH_COMP               (0x00000000UL)                                                  /*!< Window output default mode: Comparators output are indicating each their own state. To know window mode state: each comparator output must be read, if "((COMPx exclusive or COMPy) == 1)" then monitored signal is within comparators window. The same way, if both comparators output are high, then monitored signal is below window. */
160 #define LL_COMP_WINDOWOUTPUT_COMP1                   (COMP_CSR_WINOUT | LL_COMP_WINDOWMODE_COMP_ODD_REGOFFSET_MASK)  /*!< Window output synthesized on COMP1 output: COMP1 output is no more indicating its own state, but global window mode state (logical high means monitored signal is within comparators window). Note: impacts only comparator output signal level (COMPx_OUT propagated to GPIO, EXTI lines, timers, ...), does not impact output digital state of comparator (COMPx_VALUE) always reflecting each comparator output state.*/
161 #define LL_COMP_WINDOWOUTPUT_COMP2                   (COMP_CSR_WINOUT | LL_COMP_WINDOWMODE_COMP_EVEN_REGOFFSET_MASK) /*!< Window output synthesized on COMP2 output: COMP2 output is no more indicating its own state, but global window mode state (logical high means monitored signal is within comparators window). Note: impacts only comparator output signal level (COMPx_OUT propagated to GPIO, EXTI lines, timers, ...), does not impact output digital state of comparator (COMPx_VALUE) always reflecting each comparator output state.*/
162 #define LL_COMP_WINDOWOUTPUT_BOTH                    (COMP_CSR_WINOUT | LL_COMP_WINDOWMODE_COMP_EVEN_REGOFFSET_MASK | LL_COMP_WINDOWOUTPUT_BOTH_SETTING_MASK)      /*!< Window output synthesized on both comparators output of pair of comparator selected (COMP1 and COMP2): both comparators outputs are no more indicating their own state, but global window mode state (logical high means monitored signal is within comparators window). This is a specific configuration (technically possible but not relevant from application point of view: 2 comparators output used for the same signal level), standard configuration for window mode is one of the settings above. */
163 /**
164   * @}
165   */
166 
167 
168 /** @defgroup COMP_LL_EC_POWERMODE Comparator modes - Power mode
169   * @{
170   */
171 #define LL_COMP_POWERMODE_HIGHSPEED     (0x00000000UL)                            /*!< COMP power mode to high speed */
172 #define LL_COMP_POWERMODE_MEDIUMSPEED   (COMP_CSR_PWRMODE_0)                      /*!< COMP power mode to medium speed */
173 /**
174   * @}
175   */
176 
177 /** @defgroup COMP_LL_EC_INPUT_PLUS Comparator inputs - Input plus (input non-inverting) selection
178   * @{
179   */
180 #define LL_COMP_INPUT_PLUS_IO1          (0x00000000UL)                          /*!< Comparator input plus connected to IO1 (pin PA2 for COMP1, pin PA0 for COMP2) */
181 /**
182   * @}
183   */
184 
185 /** @defgroup COMP_LL_EC_INPUT_MINUS Comparator inputs - Input minus (input inverting) selection
186   * @{
187   */
188 #define LL_COMP_INPUT_MINUS_1_4VREFINT  (0x00000000UL)                                                                  /*!< Comparator input minus connected to 1/4 VrefInt  */
189 #define LL_COMP_INPUT_MINUS_1_2VREFINT  (                                                            COMP_CSR_INMSEL_0) /*!< Comparator input minus connected to 1/2 VrefInt  */
190 #define LL_COMP_INPUT_MINUS_3_4VREFINT  (                                        COMP_CSR_INMSEL_1                    ) /*!< Comparator input minus connected to 3/4 VrefInt  */
191 #define LL_COMP_INPUT_MINUS_VREFINT     (                                        COMP_CSR_INMSEL_1 | COMP_CSR_INMSEL_0) /*!< Comparator input minus connected to VrefInt */
192 #define LL_COMP_INPUT_MINUS_DAC1_CH1    (                    COMP_CSR_INMSEL_2                                        ) /*!< Comparator input minus connected to DAC1 channel 1 (DAC_OUT1)  */
193 #define LL_COMP_INPUT_MINUS_DAC1_CH2    (                    COMP_CSR_INMSEL_2                     | COMP_CSR_INMSEL_0) /*!< Comparator input minus connected to DAC1 channel 2 (DAC_OUT2)  */
194 #define LL_COMP_INPUT_MINUS_IO1         (                    COMP_CSR_INMSEL_2 | COMP_CSR_INMSEL_1                    ) /*!< Comparator input minus connected to IO1 (pin PA1 for COMP1, pin PB9 for COMP2) */
195 /**
196   * @}
197   */
198 
199 /** @defgroup COMP_LL_EC_INPUT_HYSTERESIS Comparator input - Hysteresis
200   * @{
201   */
202 #define LL_COMP_HYSTERESIS_NONE         (0x00000000UL)                      /*!< No hysteresis */
203 #define LL_COMP_HYSTERESIS_LOW          (                  COMP_CSR_HYST_0) /*!< Hysteresis level low */
204 #define LL_COMP_HYSTERESIS_MEDIUM       (COMP_CSR_HYST_1                  ) /*!< Hysteresis level medium */
205 #define LL_COMP_HYSTERESIS_HIGH         (COMP_CSR_HYST_1 | COMP_CSR_HYST_0) /*!< Hysteresis level high */
206 /**
207   * @}
208   */
209 
210 /** @defgroup COMP_LL_EC_OUTPUT_POLARITY Comparator output - Output polarity
211   * @{
212   */
213 #define LL_COMP_OUTPUTPOL_NONINVERTED   (0x00000000UL)          /*!< COMP output polarity is not inverted: comparator output is high when the plus (non-inverting) input is at a higher voltage than the minus (inverting) input */
214 #define LL_COMP_OUTPUTPOL_INVERTED      (COMP_CSR_POLARITY)     /*!< COMP output polarity is inverted: comparator output is low when the plus (non-inverting) input is at a lower voltage than the minus (inverting) input */
215 /**
216   * @}
217   */
218 
219 /** @defgroup COMP_LL_EC_OUTPUT_BLANKING_SOURCE Comparator output - Blanking source
220   * @{
221   */
222 #define LL_COMP_BLANKINGSRC_NONE            (0x00000000UL)          /*!<Comparator output without blanking */
223 #define LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1  (COMP_CSR_BLANKSEL_0)   /*!< Comparator output blanking source TIM1 OC5 (specific to COMP instance: COMP1) */
224 #define LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1  (COMP_CSR_BLANKSEL_1)   /*!< Comparator output blanking source TIM2 OC3 (specific to COMP instance: COMP1) */
225 #define LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1  (COMP_CSR_BLANKSEL_2)   /*!< Comparator output blanking source TIM3 OC3 (specific to COMP instance: COMP1) */
226 #define LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2  (COMP_CSR_BLANKSEL_0)   /*!< Comparator output blanking source TIM3 OC4 (specific to COMP instance: COMP2) */
227 /**
228   * @}
229   */
230 
231 /** @defgroup COMP_LL_EC_OUTPUT_LEVEL Comparator output - Output level
232   * @{
233   */
234 #define LL_COMP_OUTPUT_LEVEL_LOW        (0x00000000UL)          /*!< Comparator output level low (if the polarity is not inverted, otherwise to be complemented) */
235 #define LL_COMP_OUTPUT_LEVEL_HIGH       (0x00000001UL)          /*!< Comparator output level high (if the polarity is not inverted, otherwise to be complemented) */
236 /**
237   * @}
238   */
239 
240 /** @defgroup COMP_LL_EC_HW_DELAYS  Definitions of COMP hardware constraints delays
241   * @note   Only COMP peripheral HW delays are defined in COMP LL driver driver,
242   *         not timeout values.
243   *         For details on delays values, refer to descriptions in source code
244   *         above each literal definition.
245   * @{
246   */
247 
248 /* Delay for comparator startup time.                                         */
249 /* Note: Delay required to reach propagation delay specification.             */
250 /* Literal set to maximum value (refer to device datasheet,                   */
251 /* parameter "tSTART").                                                       */
252 /* Unit: us                                                                   */
253 #define LL_COMP_DELAY_STARTUP_US          ( 80UL) /*!< Delay for COMP startup time */
254 
255 /* Delay for comparator voltage scaler stabilization time.                    */
256 /* Note: Voltage scaler is used when selecting comparator input               */
257 /*       based on VrefInt: VrefInt or subdivision of VrefInt.                 */
258 /* Literal set to maximum value (refer to device datasheet,                   */
259 /* parameter "tSTART_SCALER").                                                */
260 /* Unit: us                                                                   */
261 #define LL_COMP_DELAY_VOLTAGE_SCALER_STAB_US ( 200UL) /*!< Delay for COMP voltage scaler stabilization time */
262 
263 /**
264   * @}
265   */
266 
267 /**
268   * @}
269   */
270 
271 /* Exported macro ------------------------------------------------------------*/
272 /** @defgroup COMP_LL_Exported_Macros COMP Exported Macros
273   * @{
274   */
275 /** @defgroup COMP_LL_EM_WRITE_READ Common write and read registers macro
276   * @{
277   */
278 
279 /**
280   * @brief  Write a value in COMP register
281   * @param  __INSTANCE__ comparator instance
282   * @param  __REG__ Register to be written
283   * @param  __VALUE__ Value to be written in the register
284   * @retval None
285   */
286 #define LL_COMP_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG((__INSTANCE__)->__REG__, (__VALUE__))
287 
288 /**
289   * @brief  Read a value in COMP register
290   * @param  __INSTANCE__ comparator instance
291   * @param  __REG__ Register to be read
292   * @retval Register value
293   */
294 #define LL_COMP_ReadReg(__INSTANCE__, __REG__) READ_REG((__INSTANCE__)->__REG__)
295 /**
296   * @}
297   */
298 
299 /** @defgroup COMP_LL_EM_HELPER_MACRO COMP helper macro
300   * @{
301   */
302 
303 /**
304   * @brief  Helper macro to select the COMP common instance
305   *         to which is belonging the selected COMP instance.
306   * @note   COMP common register instance can be used to
307   *         set parameters common to several COMP instances.
308   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
309   * @param  __COMPx__ COMP instance
310   * @retval COMP common instance or value "0" if there is no COMP common instance.
311   */
312 #define __LL_COMP_COMMON_INSTANCE(__COMPx__)          (COMP12_COMMON)
313 
314 /**
315   * @}
316   */
317 
318 /**
319   * @}
320   */
321 
322 /* Exported functions --------------------------------------------------------*/
323 /** @defgroup COMP_LL_Exported_Functions COMP Exported Functions
324   * @{
325   */
326 
327 /** @defgroup COMP_LL_EF_Configuration_comparator_common Configuration of COMP hierarchical scope:
328   *           common to several COMP instances
329   * @{
330   */
331 
332 
333 /**
334   * @brief  Set window mode of a pair of comparators instances
335   *         (2 consecutive COMP instances COMP<x> and COMP<x+1>).
336   * @rmtoll CSR      WINMODE        LL_COMP_SetCommonWindowMode
337   * @param  COMPxy_COMMON Comparator common instance
338   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
339   * @param  WindowMode This parameter can be one of the following values:
340   *         @arg @ref LL_COMP_WINDOWMODE_DISABLE
341   *         @arg @ref LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON
342   *         @arg @ref LL_COMP_WINDOWMODE_COMP2_INPUT_PLUS_COMMON
343   * @retval None
344   */
LL_COMP_SetCommonWindowMode(COMP_Common_TypeDef * COMPxy_COMMON,uint32_t WindowMode)345 __STATIC_INLINE void LL_COMP_SetCommonWindowMode(COMP_Common_TypeDef *COMPxy_COMMON, uint32_t WindowMode)
346 {
347   /* Note: On this STM32 series, window mode can be set from any instance     */
348   /*       of the pair of comparator instances.                               */
349   register __IO uint32_t *preg = __COMP_PTR_REG_OFFSET(COMPxy_COMMON->CSR_ODD,
350                                                        (WindowMode & LL_COMP_WINDOWMODE_COMPX_REGOFFSET_MASK));
351 
352   /* Clear the potential previous setting of window mode */
353   register __IO uint32_t *preg_clear = __COMP_PTR_REG_OFFSET(COMPxy_COMMON->CSR_ODD,
354                                                              (~(WindowMode & LL_COMP_WINDOWMODE_COMPX_REGOFFSET_MASK)
355                                                               & 0x1UL));
356   CLEAR_BIT(*preg_clear,
357             COMP_CSR_WINMODE
358            );
359 
360   /* Set window mode */
361   MODIFY_REG(*preg,
362              COMP_CSR_WINMODE,
363              (WindowMode & LL_COMP_WINDOWMODE_COMPX_SETTING_MASK)
364             );
365 }
366 
367 /**
368   * @brief  Get window mode of a pair of comparators instances
369   *         (2 consecutive COMP instances COMP<x> and COMP<x+1>).
370   * @rmtoll CSR      WINMODE        LL_COMP_GetCommonWindowMode
371   * @param  COMPxy_COMMON Comparator common instance
372   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
373   * @retval Returned value can be one of the following values:
374   *         @arg @ref LL_COMP_WINDOWMODE_DISABLE
375   *         @arg @ref LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON
376   *         @arg @ref LL_COMP_WINDOWMODE_COMP2_INPUT_PLUS_COMMON
377   */
LL_COMP_GetCommonWindowMode(const COMP_Common_TypeDef * COMPxy_COMMON)378 __STATIC_INLINE uint32_t LL_COMP_GetCommonWindowMode(const COMP_Common_TypeDef *COMPxy_COMMON)
379 {
380   /* Note: On this STM32 series, window mode can be set from any instance     */
381   /*       of the pair of comparator instances.                               */
382   register const uint32_t window_mode_comp_odd = (uint32_t)READ_BIT(COMPxy_COMMON->CSR_ODD, COMP_CSR_WINMODE);
383   register const uint32_t window_mode_comp_even = (uint32_t)READ_BIT(COMPxy_COMMON->CSR_EVEN, COMP_CSR_WINMODE);
384 
385   return (uint32_t)(window_mode_comp_odd
386                     | window_mode_comp_even
387                     | ((window_mode_comp_even >> LL_COMP_WINDOWMODE_BITOFFSET_POS)
388                        * LL_COMP_WINDOWMODE_COMP_EVEN_REGOFFSET_MASK)
389                    );
390 }
391 
392 /**
393   * @brief  Set window output of a pair of comparators instances
394   *         (2 consecutive COMP instances COMP<x> and COMP<x+1>).
395   * @rmtoll CSR      WINOUT         LL_COMP_SetCommonWindowOutput
396   * @param  COMPxy_COMMON Comparator common instance
397   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
398   * @param  WindowOutput This parameter can be one of the following values:
399   *         @arg @ref LL_COMP_WINDOWOUTPUT_EACH_COMP
400   *         @arg @ref LL_COMP_WINDOWOUTPUT_COMP1
401   *         @arg @ref LL_COMP_WINDOWOUTPUT_COMP2
402   * @retval None
403   */
LL_COMP_SetCommonWindowOutput(COMP_Common_TypeDef * COMPxy_COMMON,uint32_t WindowOutput)404 __STATIC_INLINE void LL_COMP_SetCommonWindowOutput(COMP_Common_TypeDef *COMPxy_COMMON, uint32_t WindowOutput)
405 {
406   register __IO uint32_t *preg = __COMP_PTR_REG_OFFSET(COMPxy_COMMON->CSR_ODD,
407                                                        (WindowOutput & LL_COMP_WINDOWMODE_COMPX_REGOFFSET_MASK));
408 
409   /* Clear the potential previous setting of window output on the relevant comparator instance */
410   /* (clear bit of window output unless specific case of setting of comparator both output selected) */
411   register __IO uint32_t *preg_clear = __COMP_PTR_REG_OFFSET(COMPxy_COMMON->CSR_ODD,
412                                                              (~(WindowOutput & LL_COMP_WINDOWMODE_COMPX_REGOFFSET_MASK)
413                                                               & 0x1UL));
414   MODIFY_REG(*preg_clear,
415              COMP_CSR_WINOUT,
416              ((WindowOutput & LL_COMP_WINDOWOUTPUT_BOTH_SETTING_MASK) >> LL_COMP_WINDOWOUTPUT_BOTH_POS_VS_WINDOW)
417             );
418 
419   /* Set window output */
420   MODIFY_REG(*preg,
421              COMP_CSR_WINOUT,
422              (WindowOutput & LL_COMP_WINDOWOUTPUT_COMPX_SETTING_MASK)
423             );
424 }
425 
426 /**
427   * @brief  Get window output of a pair of comparators instances
428   *         (2 consecutive COMP instances COMP<x> and COMP<x+1>).
429   * @rmtoll CSR      WINMODE        LL_COMP_GetCommonWindowOutput
430   * @param  COMPxy_COMMON Comparator common instance
431   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
432   * @retval Returned value can be one of the following values:
433   *         @arg @ref LL_COMP_WINDOWOUTPUT_EACH_COMP
434   *         @arg @ref LL_COMP_WINDOWOUTPUT_COMP1
435   *         @arg @ref LL_COMP_WINDOWOUTPUT_COMP2
436   */
LL_COMP_GetCommonWindowOutput(const COMP_Common_TypeDef * COMPxy_COMMON)437 __STATIC_INLINE uint32_t LL_COMP_GetCommonWindowOutput(const COMP_Common_TypeDef *COMPxy_COMMON)
438 {
439   register const uint32_t window_output_comp_odd = (uint32_t)READ_BIT(COMPxy_COMMON->CSR_ODD, COMP_CSR_WINOUT);
440   register const uint32_t window_output_comp_even = (uint32_t)READ_BIT(COMPxy_COMMON->CSR_EVEN, COMP_CSR_WINOUT);
441 
442   /* Construct value corresponding to LL_COMP_WINDOWOUTPUT_xxx */
443   return (uint32_t)(window_output_comp_odd
444                     | window_output_comp_even
445                     | ((window_output_comp_even >> COMP_CSR_WINOUT_Pos) * LL_COMP_WINDOWMODE_COMP_EVEN_REGOFFSET_MASK)
446                     | (window_output_comp_odd + window_output_comp_even));
447 }
448 
449 
450 /**
451   * @}
452   */
453 
454 /** @defgroup COMP_LL_EF_Configuration_comparator_modes Configuration of comparator modes
455   * @{
456   */
457 
458 /**
459   * @brief  Set comparator instance operating mode to adjust power and speed.
460   * @rmtoll CSR      PWRMODE        LL_COMP_SetPowerMode
461   * @param  COMPx Comparator instance
462   * @param  PowerMode This parameter can be one of the following values:
463   *         @arg @ref LL_COMP_POWERMODE_HIGHSPEED
464   *         @arg @ref LL_COMP_POWERMODE_MEDIUMSPEED
465   * @retval None
466   */
LL_COMP_SetPowerMode(COMP_TypeDef * COMPx,uint32_t PowerMode)467 __STATIC_INLINE void LL_COMP_SetPowerMode(COMP_TypeDef *COMPx, uint32_t PowerMode)
468 {
469   MODIFY_REG(COMPx->CSR, COMP_CSR_PWRMODE, PowerMode);
470 }
471 
472 /**
473   * @brief  Get comparator instance operating mode to adjust power and speed.
474   * @rmtoll CSR      PWRMODE        LL_COMP_GetPowerMode
475   * @param  COMPx Comparator instance
476   * @retval Returned value can be one of the following values:
477   *         @arg @ref LL_COMP_POWERMODE_HIGHSPEED
478   *         @arg @ref LL_COMP_POWERMODE_MEDIUMSPEED
479   */
LL_COMP_GetPowerMode(const COMP_TypeDef * COMPx)480 __STATIC_INLINE uint32_t LL_COMP_GetPowerMode(const COMP_TypeDef *COMPx)
481 {
482   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_PWRMODE));
483 }
484 
485 /**
486   * @}
487   */
488 
489 /** @defgroup COMP_LL_EF_Configuration_comparator_inputs Configuration of comparator inputs
490   * @{
491   */
492 
493 /**
494   * @brief  Set comparator inputs minus (inverting) and plus (non-inverting).
495   * @note   In case of comparator input selected to be connected to IO:
496   *         GPIO pins are specific to each comparator instance.
497   *         Refer to description of parameters or to reference manual.
498   * @note   On this STM32 series, a voltage scaler is used
499   *         when COMP input is based on VrefInt (VrefInt or subdivision
500   *         of VrefInt):
501   *         Voltage scaler requires a delay for voltage stabilization.
502   *         Refer to device datasheet, parameter "tSTART_SCALER".
503   * @rmtoll CSR      INMSEL         LL_COMP_ConfigInputs\n
504   *         CSR      INPSEL         LL_COMP_ConfigInputs\n
505   * @param  COMPx Comparator instance
506   * @param  InputMinus This parameter can be one of the following values:
507   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
508   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
509   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
510   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
511   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
512   * @param  InputPlus This parameter can be one of the following values:
513   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
514   * @retval None
515   */
LL_COMP_ConfigInputs(COMP_TypeDef * COMPx,uint32_t InputMinus,uint32_t InputPlus)516 __STATIC_INLINE void LL_COMP_ConfigInputs(COMP_TypeDef *COMPx, uint32_t InputMinus, uint32_t InputPlus)
517 {
518   MODIFY_REG(COMPx->CSR,
519              COMP_CSR_INMSEL | COMP_CSR_INPSEL,
520              InputMinus | InputPlus);
521 }
522 
523 /**
524   * @brief  Set comparator input plus (non-inverting).
525   * @note   In case of comparator input selected to be connected to IO:
526   *         GPIO pins are specific to each comparator instance.
527   *         Refer to description of parameters or to reference manual.
528   * @rmtoll CSR      INPSEL         LL_COMP_SetInputPlus
529   * @param  COMPx Comparator instance
530   * @param  InputPlus This parameter can be one of the following values:
531   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
532   * @retval None
533   */
LL_COMP_SetInputPlus(COMP_TypeDef * COMPx,uint32_t InputPlus)534 __STATIC_INLINE void LL_COMP_SetInputPlus(COMP_TypeDef *COMPx, uint32_t InputPlus)
535 {
536   MODIFY_REG(COMPx->CSR, COMP_CSR_INPSEL, InputPlus);
537 }
538 
539 /**
540   * @brief  Get comparator input plus (non-inverting).
541   * @note   In case of comparator input selected to be connected to IO:
542   *         GPIO pins are specific to each comparator instance.
543   *         Refer to description of parameters or to reference manual.
544   * @rmtoll CSR      INPSEL         LL_COMP_GetInputPlus
545   * @param  COMPx Comparator instance
546   * @retval Returned value can be one of the following values:
547   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
548   */
LL_COMP_GetInputPlus(const COMP_TypeDef * COMPx)549 __STATIC_INLINE uint32_t LL_COMP_GetInputPlus(const COMP_TypeDef *COMPx)
550 {
551   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_INPSEL));
552 }
553 
554 /**
555   * @brief  Set comparator input minus (inverting).
556   * @note   In case of comparator input selected to be connected to IO:
557   *         GPIO pins are specific to each comparator instance.
558   *         Refer to description of parameters or to reference manual.
559   * @note   On this STM32 series, a voltage scaler is used
560   *         when COMP input is based on VrefInt (VrefInt or subdivision
561   *         of VrefInt):
562   *         Voltage scaler requires a delay for voltage stabilization.
563   *         Refer to device datasheet, parameter "tSTART_SCALER".
564   * @rmtoll CSR      INMSEL         LL_COMP_SetInputMinus
565   * @param  COMPx Comparator instance
566   * @param  InputMinus This parameter can be one of the following values:
567   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
568   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
569   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
570   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
571   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
572   * @retval None
573   */
LL_COMP_SetInputMinus(COMP_TypeDef * COMPx,uint32_t InputMinus)574 __STATIC_INLINE void LL_COMP_SetInputMinus(COMP_TypeDef *COMPx, uint32_t InputMinus)
575 {
576   MODIFY_REG(COMPx->CSR, COMP_CSR_INMSEL, InputMinus);
577 }
578 
579 /**
580   * @brief  Get comparator input minus (inverting).
581   * @note   In case of comparator input selected to be connected to IO:
582   *         GPIO pins are specific to each comparator instance.
583   *         Refer to description of parameters or to reference manual.
584   * @rmtoll CSR      INMSEL         LL_COMP_GetInputMinus
585   * @param  COMPx Comparator instance
586   * @retval Returned value can be one of the following values:
587   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
588   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
589   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
590   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
591   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
592   */
LL_COMP_GetInputMinus(const COMP_TypeDef * COMPx)593 __STATIC_INLINE uint32_t LL_COMP_GetInputMinus(const COMP_TypeDef *COMPx)
594 {
595   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_INMSEL));
596 }
597 
598 /**
599   * @brief  Set comparator instance hysteresis mode of the input minus (inverting input).
600   * @rmtoll CSR      HYST           LL_COMP_SetInputHysteresis
601   * @param  COMPx Comparator instance
602   * @param  InputHysteresis This parameter can be one of the following values:
603   *         @arg @ref LL_COMP_HYSTERESIS_NONE
604   *         @arg @ref LL_COMP_HYSTERESIS_LOW
605   *         @arg @ref LL_COMP_HYSTERESIS_MEDIUM
606   *         @arg @ref LL_COMP_HYSTERESIS_HIGH
607   * @retval None
608   */
LL_COMP_SetInputHysteresis(COMP_TypeDef * COMPx,uint32_t InputHysteresis)609 __STATIC_INLINE void LL_COMP_SetInputHysteresis(COMP_TypeDef *COMPx, uint32_t InputHysteresis)
610 {
611   MODIFY_REG(COMPx->CSR, COMP_CSR_HYST, InputHysteresis);
612 }
613 
614 /**
615   * @brief  Get comparator instance hysteresis mode of the minus (inverting) input.
616   * @rmtoll CSR      HYST           LL_COMP_GetInputHysteresis
617   * @param  COMPx Comparator instance
618   * @retval Returned value can be one of the following values:
619   *         @arg @ref LL_COMP_HYSTERESIS_NONE
620   *         @arg @ref LL_COMP_HYSTERESIS_LOW
621   *         @arg @ref LL_COMP_HYSTERESIS_MEDIUM
622   *         @arg @ref LL_COMP_HYSTERESIS_HIGH
623   */
LL_COMP_GetInputHysteresis(const COMP_TypeDef * COMPx)624 __STATIC_INLINE uint32_t LL_COMP_GetInputHysteresis(const COMP_TypeDef *COMPx)
625 {
626   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_HYST));
627 }
628 
629 /**
630   * @}
631   */
632 
633 /** @defgroup COMP_LL_EF_Configuration_comparator_output Configuration of comparator output
634   * @{
635   */
636 
637 /**
638   * @brief  Set comparator instance output polarity.
639   * @rmtoll CSR      POLARITY       LL_COMP_SetOutputPolarity
640   * @param  COMPx Comparator instance
641   * @param  OutputPolarity This parameter can be one of the following values:
642   *         @arg @ref LL_COMP_OUTPUTPOL_NONINVERTED
643   *         @arg @ref LL_COMP_OUTPUTPOL_INVERTED
644   * @retval None
645   */
LL_COMP_SetOutputPolarity(COMP_TypeDef * COMPx,uint32_t OutputPolarity)646 __STATIC_INLINE void LL_COMP_SetOutputPolarity(COMP_TypeDef *COMPx, uint32_t OutputPolarity)
647 {
648   MODIFY_REG(COMPx->CSR, COMP_CSR_POLARITY, OutputPolarity);
649 }
650 
651 /**
652   * @brief  Get comparator instance output polarity.
653   * @rmtoll CSR      POLARITY       LL_COMP_GetOutputPolarity
654   * @param  COMPx Comparator instance
655   * @retval Returned value can be one of the following values:
656   *         @arg @ref LL_COMP_OUTPUTPOL_NONINVERTED
657   *         @arg @ref LL_COMP_OUTPUTPOL_INVERTED
658   */
LL_COMP_GetOutputPolarity(const COMP_TypeDef * COMPx)659 __STATIC_INLINE uint32_t LL_COMP_GetOutputPolarity(const COMP_TypeDef *COMPx)
660 {
661   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_POLARITY));
662 }
663 
664 /**
665   * @brief  Set comparator instance blanking source.
666   * @note   Blanking source may be specific to each comparator instance.
667   *         Refer to description of parameters or to reference manual.
668   * @note   Availability of parameters of blanking source from timer
669   *         depends on timers availability on the selected device.
670   * @rmtoll CSR      BLANKSEL       LL_COMP_SetOutputBlankingSource
671   * @param  COMPx Comparator instance
672   * @param  BlankingSource This parameter can be one of the following values:
673   *         @arg @ref LL_COMP_BLANKINGSRC_NONE
674   *         @arg @ref LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1  (1)
675   *         @arg @ref LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1  (1)
676   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1  (1)
677   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2  (2)
678   *
679   *         (1) Parameter available only on comparator instance: COMP1.
680   *         (2) Parameter available only on comparator instance: COMP2.
681   * @retval None
682   */
LL_COMP_SetOutputBlankingSource(COMP_TypeDef * COMPx,uint32_t BlankingSource)683 __STATIC_INLINE void LL_COMP_SetOutputBlankingSource(COMP_TypeDef *COMPx, uint32_t BlankingSource)
684 {
685   MODIFY_REG(COMPx->CSR, COMP_CSR_BLANKSEL, BlankingSource);
686 }
687 
688 /**
689   * @brief  Get comparator instance blanking source.
690   * @note   Availability of parameters of blanking source from timer
691   *         depends on timers availability on the selected device.
692   * @note   Blanking source may be specific to each comparator instance.
693   *         Refer to description of parameters or to reference manual.
694   * @rmtoll CSR      BLANKSEL       LL_COMP_GetOutputBlankingSource
695   * @param  COMPx Comparator instance
696   * @retval Returned value can be one of the following values:
697   *         @arg @ref LL_COMP_BLANKINGSRC_NONE
698   *         @arg @ref LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1  (1)
699   *         @arg @ref LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1  (1)
700   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1  (1)
701   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2  (2)
702   *
703   *         (1) Parameter available only on comparator instance: COMP1.
704   *         (2) Parameter available only on comparator instance: COMP2.
705   */
LL_COMP_GetOutputBlankingSource(const COMP_TypeDef * COMPx)706 __STATIC_INLINE uint32_t LL_COMP_GetOutputBlankingSource(const COMP_TypeDef *COMPx)
707 {
708   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_BLANKSEL));
709 }
710 
711 /**
712   * @}
713   */
714 
715 /** @defgroup COMP_LL_EF_Operation Operation on comparator instance
716   * @{
717   */
718 
719 /**
720   * @brief  Enable comparator instance.
721   * @note   After enable from off state, comparator requires a delay
722   *         to reach reach propagation delay specification.
723   *         Refer to device datasheet, parameter "tSTART".
724   * @rmtoll CSR      EN             LL_COMP_Enable
725   * @param  COMPx Comparator instance
726   * @retval None
727   */
LL_COMP_Enable(COMP_TypeDef * COMPx)728 __STATIC_INLINE void LL_COMP_Enable(COMP_TypeDef *COMPx)
729 {
730   SET_BIT(COMPx->CSR, COMP_CSR_EN);
731 }
732 
733 /**
734   * @brief  Disable comparator instance.
735   * @rmtoll CSR      EN             LL_COMP_Disable
736   * @param  COMPx Comparator instance
737   * @retval None
738   */
LL_COMP_Disable(COMP_TypeDef * COMPx)739 __STATIC_INLINE void LL_COMP_Disable(COMP_TypeDef *COMPx)
740 {
741   CLEAR_BIT(COMPx->CSR, COMP_CSR_EN);
742 }
743 
744 /**
745   * @brief  Get comparator enable state
746   *         (0: COMP is disabled, 1: COMP is enabled)
747   * @rmtoll CSR      EN             LL_COMP_IsEnabled
748   * @param  COMPx Comparator instance
749   * @retval State of bit (1 or 0).
750   */
LL_COMP_IsEnabled(const COMP_TypeDef * COMPx)751 __STATIC_INLINE uint32_t LL_COMP_IsEnabled(const COMP_TypeDef *COMPx)
752 {
753   return ((READ_BIT(COMPx->CSR, COMP_CSR_EN) == (COMP_CSR_EN)) ? 1UL : 0UL);
754 }
755 
756 /**
757   * @brief  Lock comparator instance.
758   * @note   Once locked, comparator configuration can be accessed in read-only.
759   * @note   The only way to unlock the comparator is a device hardware reset.
760   * @rmtoll CSR      LOCK           LL_COMP_Lock
761   * @param  COMPx Comparator instance
762   * @retval None
763   */
LL_COMP_Lock(COMP_TypeDef * COMPx)764 __STATIC_INLINE void LL_COMP_Lock(COMP_TypeDef *COMPx)
765 {
766   SET_BIT(COMPx->CSR, COMP_CSR_LOCK);
767 }
768 
769 /**
770   * @brief  Get comparator lock state
771   *         (0: COMP is unlocked, 1: COMP is locked).
772   * @note   Once locked, comparator configuration can be accessed in read-only.
773   * @note   The only way to unlock the comparator is a device hardware reset.
774   * @rmtoll CSR      LOCK           LL_COMP_IsLocked
775   * @param  COMPx Comparator instance
776   * @retval State of bit (1 or 0).
777   */
LL_COMP_IsLocked(const COMP_TypeDef * COMPx)778 __STATIC_INLINE uint32_t LL_COMP_IsLocked(const COMP_TypeDef *COMPx)
779 {
780   return ((READ_BIT(COMPx->CSR, COMP_CSR_LOCK) == (COMP_CSR_LOCK)) ? 1UL : 0UL);
781 }
782 
783 /**
784   * @brief  Read comparator instance output level.
785   * @note   The comparator output level depends on the selected polarity
786   *         (Refer to function @ref LL_COMP_SetOutputPolarity()).
787   *         If the comparator polarity is not inverted:
788   *          - Comparator output is low when the input plus
789   *            is at a lower voltage than the input minus
790   *          - Comparator output is high when the input plus
791   *            is at a higher voltage than the input minus
792   *         If the comparator polarity is inverted:
793   *          - Comparator output is high when the input plus
794   *            is at a lower voltage than the input minus
795   *          - Comparator output is low when the input plus
796   *            is at a higher voltage than the input minus
797   * @rmtoll CSR      VALUE          LL_COMP_ReadOutputLevel
798   * @param  COMPx Comparator instance
799   * @retval Returned value can be one of the following values:
800   *         @arg @ref LL_COMP_OUTPUT_LEVEL_LOW
801   *         @arg @ref LL_COMP_OUTPUT_LEVEL_HIGH
802   */
LL_COMP_ReadOutputLevel(const COMP_TypeDef * COMPx)803 __STATIC_INLINE uint32_t LL_COMP_ReadOutputLevel(const COMP_TypeDef *COMPx)
804 {
805   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_VALUE)
806                     >> LL_COMP_OUTPUT_LEVEL_BITOFFSET_POS);
807 }
808 
809 /**
810   * @}
811   */
812 
813 #if defined(USE_FULL_LL_DRIVER)
814 /** @defgroup COMP_LL_EF_Init Initialization and de-initialization functions
815   * @{
816   */
817 
818 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx);
819 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, const LL_COMP_InitTypeDef *COMP_InitStruct);
820 void        LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct);
821 
822 /**
823   * @}
824   */
825 #endif /* USE_FULL_LL_DRIVER */
826 
827 /**
828   * @}
829   */
830 
831 /**
832   * @}
833   */
834 
835 /**
836   * @}
837   */
838 
839 #endif /* COMP1 || COMP2 */
840 
841 /**
842   * @}
843   */
844 
845 #ifdef __cplusplus
846 }
847 #endif
848 
849 #endif /* STM32WBAxx_LL_COMP_H */
850