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_INTERMSPEED   (COMP_CSR_PWRMODE_0)                      /*!< COMP power mode to intermediate speed */
173 #define LL_COMP_POWERMODE_MEDIUMSPEED   (COMP_CSR_PWRMODE_1)                      /*!< COMP power mode to medium speed */
174 #define LL_COMP_POWERMODE_ULTRALOWPOWER (COMP_CSR_PWRMODE_1 | COMP_CSR_PWRMODE_0) /*!< COMP power mode to ultra-low power */
175 /**
176   * @}
177   */
178 
179 /** @defgroup COMP_LL_EC_INPUT_PLUS Comparator inputs - Input plus (input non-inverting) selection
180   * @{
181   */
182 #define LL_COMP_INPUT_PLUS_IO1          (0x00000000UL)                          /*!< Comparator input plus connected to IO1 (pin PA2 for COMP1, pin PA0 for COMP2) */
183 /**
184   * @}
185   */
186 
187 /** @defgroup COMP_LL_EC_INPUT_MINUS Comparator inputs - Input minus (input inverting) selection
188   * @{
189   */
190 #define LL_COMP_INPUT_MINUS_1_4VREFINT  (0x00000000UL)                                                                  /*!< Comparator input minus connected to 1/4 VrefInt  */
191 #define LL_COMP_INPUT_MINUS_1_2VREFINT  (                                                            COMP_CSR_INMSEL_0) /*!< Comparator input minus connected to 1/2 VrefInt  */
192 #define LL_COMP_INPUT_MINUS_3_4VREFINT  (                                        COMP_CSR_INMSEL_1                    ) /*!< Comparator input minus connected to 3/4 VrefInt  */
193 #define LL_COMP_INPUT_MINUS_VREFINT     (                                        COMP_CSR_INMSEL_1 | COMP_CSR_INMSEL_0) /*!< Comparator input minus connected to VrefInt */
194 #define LL_COMP_INPUT_MINUS_DAC1_CH1    (                    COMP_CSR_INMSEL_2                                        ) /*!< Comparator input minus connected to DAC1 channel 1 (DAC_OUT1)  */
195 #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)  */
196 #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) */
197 /**
198   * @}
199   */
200 
201 /** @defgroup COMP_LL_EC_INPUT_HYSTERESIS Comparator input - Hysteresis
202   * @{
203   */
204 #define LL_COMP_HYSTERESIS_NONE         (0x00000000UL)                      /*!< No hysteresis */
205 #define LL_COMP_HYSTERESIS_LOW          (                  COMP_CSR_HYST_0) /*!< Hysteresis level low */
206 #define LL_COMP_HYSTERESIS_MEDIUM       (COMP_CSR_HYST_1                  ) /*!< Hysteresis level medium */
207 #define LL_COMP_HYSTERESIS_HIGH         (COMP_CSR_HYST_1 | COMP_CSR_HYST_0) /*!< Hysteresis level high */
208 /**
209   * @}
210   */
211 
212 /** @defgroup COMP_LL_EC_OUTPUT_POLARITY Comparator output - Output polarity
213   * @{
214   */
215 #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 */
216 #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 */
217 /**
218   * @}
219   */
220 
221 /** @defgroup COMP_LL_EC_OUTPUT_BLANKING_SOURCE Comparator output - Blanking source
222   * @{
223   */
224 #define LL_COMP_BLANKINGSRC_NONE            (0x00000000UL)          /*!<Comparator output without blanking */
225 #define LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1  (COMP_CSR_BLANKSEL_0)   /*!< Comparator output blanking source TIM1 OC5 (specific to COMP instance: COMP1) */
226 #define LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1  (COMP_CSR_BLANKSEL_1)   /*!< Comparator output blanking source TIM2 OC3 (specific to COMP instance: COMP1) */
227 #define LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1  (COMP_CSR_BLANKSEL_2)   /*!< Comparator output blanking source TIM3 OC3 (specific to COMP instance: COMP1) */
228 #define LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2  (COMP_CSR_BLANKSEL_0)   /*!< Comparator output blanking source TIM3 OC4 (specific to COMP instance: COMP2) */
229 /**
230   * @}
231   */
232 
233 /** @defgroup COMP_LL_EC_OUTPUT_LEVEL Comparator output - Output level
234   * @{
235   */
236 #define LL_COMP_OUTPUT_LEVEL_LOW        (0x00000000UL)          /*!< Comparator output level low (if the polarity is not inverted, otherwise to be complemented) */
237 #define LL_COMP_OUTPUT_LEVEL_HIGH       (0x00000001UL)          /*!< Comparator output level high (if the polarity is not inverted, otherwise to be complemented) */
238 /**
239   * @}
240   */
241 
242 /** @defgroup COMP_LL_EC_HW_DELAYS  Definitions of COMP hardware constraints delays
243   * @note   Only COMP peripheral HW delays are defined in COMP LL driver driver,
244   *         not timeout values.
245   *         For details on delays values, refer to descriptions in source code
246   *         above each literal definition.
247   * @{
248   */
249 
250 /* Delay for comparator startup time.                                         */
251 /* Note: Delay required to reach propagation delay specification.             */
252 /* Literal set to maximum value (refer to device datasheet,                   */
253 /* parameter "tSTART").                                                       */
254 /* Unit: us                                                                   */
255 #define LL_COMP_DELAY_STARTUP_US          ( 80UL) /*!< Delay for COMP startup time */
256 
257 /* Delay for comparator voltage scaler stabilization time.                    */
258 /* Note: Voltage scaler is used when selecting comparator input               */
259 /*       based on VrefInt: VrefInt or subdivision of VrefInt.                 */
260 /* Literal set to maximum value (refer to device datasheet,                   */
261 /* parameter "tSTART_SCALER").                                                */
262 /* Unit: us                                                                   */
263 #define LL_COMP_DELAY_VOLTAGE_SCALER_STAB_US ( 200UL) /*!< Delay for COMP voltage scaler stabilization time */
264 
265 /**
266   * @}
267   */
268 
269 /**
270   * @}
271   */
272 
273 /* Exported macro ------------------------------------------------------------*/
274 /** @defgroup COMP_LL_Exported_Macros COMP Exported Macros
275   * @{
276   */
277 /** @defgroup COMP_LL_EM_WRITE_READ Common write and read registers macro
278   * @{
279   */
280 
281 /**
282   * @brief  Write a value in COMP register
283   * @param  __INSTANCE__ comparator instance
284   * @param  __REG__ Register to be written
285   * @param  __VALUE__ Value to be written in the register
286   * @retval None
287   */
288 #define LL_COMP_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG((__INSTANCE__)->__REG__, (__VALUE__))
289 
290 /**
291   * @brief  Read a value in COMP register
292   * @param  __INSTANCE__ comparator instance
293   * @param  __REG__ Register to be read
294   * @retval Register value
295   */
296 #define LL_COMP_ReadReg(__INSTANCE__, __REG__) READ_REG((__INSTANCE__)->__REG__)
297 /**
298   * @}
299   */
300 
301 /** @defgroup COMP_LL_EM_HELPER_MACRO COMP helper macro
302   * @{
303   */
304 
305 /**
306   * @brief  Helper macro to select the COMP common instance
307   *         to which is belonging the selected COMP instance.
308   * @note   COMP common register instance can be used to
309   *         set parameters common to several COMP instances.
310   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
311   * @param  __COMPx__ COMP instance
312   * @retval COMP common instance or value "0" if there is no COMP common instance.
313   */
314 #define __LL_COMP_COMMON_INSTANCE(__COMPx__)          (COMP12_COMMON)
315 
316 /**
317   * @}
318   */
319 
320 /**
321   * @}
322   */
323 
324 /* Exported functions --------------------------------------------------------*/
325 /** @defgroup COMP_LL_Exported_Functions COMP Exported Functions
326   * @{
327   */
328 
329 /** @defgroup COMP_LL_EF_Configuration_comparator_common Configuration
330   *           of COMP hierarchical scope: common to several COMP instances
331   * @{
332   */
333 
334 
335 /**
336   * @brief  Set window mode of a pair of comparators instances
337   *         (2 consecutive COMP instances COMP<x> and COMP<x+1>).
338   * @rmtoll CSR      WINMODE        LL_COMP_SetCommonWindowMode
339   * @param  COMPxy_COMMON Comparator common instance
340   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
341   * @param  WindowMode This parameter can be one of the following values:
342   *         @arg @ref LL_COMP_WINDOWMODE_DISABLE
343   *         @arg @ref LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON
344   *         @arg @ref LL_COMP_WINDOWMODE_COMP2_INPUT_PLUS_COMMON
345   * @retval None
346   */
LL_COMP_SetCommonWindowMode(COMP_Common_TypeDef * COMPxy_COMMON,uint32_t WindowMode)347 __STATIC_INLINE void LL_COMP_SetCommonWindowMode(COMP_Common_TypeDef *COMPxy_COMMON, uint32_t WindowMode)
348 {
349   /* Note: On this STM32 series, window mode can be set from any instance     */
350   /*       of the pair of comparator instances.                               */
351   register __IO uint32_t *preg = __COMP_PTR_REG_OFFSET(COMPxy_COMMON->CSR_ODD,
352                                                        (WindowMode & LL_COMP_WINDOWMODE_COMPX_REGOFFSET_MASK));
353 
354   /* Clear the potential previous setting of window mode */
355   register __IO uint32_t *preg_clear = __COMP_PTR_REG_OFFSET(COMPxy_COMMON->CSR_ODD,
356                                                              (~(WindowMode & LL_COMP_WINDOWMODE_COMPX_REGOFFSET_MASK)
357                                                               & 0x1UL));
358   CLEAR_BIT(*preg_clear,
359             COMP_CSR_WINMODE
360            );
361 
362   /* Set window mode */
363   MODIFY_REG(*preg,
364              COMP_CSR_WINMODE,
365              (WindowMode & LL_COMP_WINDOWMODE_COMPX_SETTING_MASK)
366             );
367 }
368 
369 /**
370   * @brief  Get window mode of a pair of comparators instances
371   *         (2 consecutive COMP instances COMP<x> and COMP<x+1>).
372   * @rmtoll CSR      WINMODE        LL_COMP_GetCommonWindowMode
373   * @param  COMPxy_COMMON Comparator common instance
374   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
375   * @retval Returned value can be one of the following values:
376   *         @arg @ref LL_COMP_WINDOWMODE_DISABLE
377   *         @arg @ref LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON
378   *         @arg @ref LL_COMP_WINDOWMODE_COMP2_INPUT_PLUS_COMMON
379   */
LL_COMP_GetCommonWindowMode(const COMP_Common_TypeDef * COMPxy_COMMON)380 __STATIC_INLINE uint32_t LL_COMP_GetCommonWindowMode(const COMP_Common_TypeDef *COMPxy_COMMON)
381 {
382   /* Note: On this STM32 series, window mode can be set from any instance     */
383   /*       of the pair of comparator instances.                               */
384   register const uint32_t window_mode_comp_odd = (uint32_t)READ_BIT(COMPxy_COMMON->CSR_ODD, COMP_CSR_WINMODE);
385   register const uint32_t window_mode_comp_even = (uint32_t)READ_BIT(COMPxy_COMMON->CSR_EVEN, COMP_CSR_WINMODE);
386 
387   return (uint32_t)(window_mode_comp_odd
388                     | window_mode_comp_even
389                     | ((window_mode_comp_even >> LL_COMP_WINDOWMODE_BITOFFSET_POS)
390                        * LL_COMP_WINDOWMODE_COMP_EVEN_REGOFFSET_MASK)
391                    );
392 }
393 
394 /**
395   * @brief  Set window output of a pair of comparators instances
396   *         (2 consecutive COMP instances COMP<x> and COMP<x+1>).
397   * @rmtoll CSR      WINOUT         LL_COMP_SetCommonWindowOutput
398   * @param  COMPxy_COMMON Comparator common instance
399   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
400   * @param  WindowOutput This parameter can be one of the following values:
401   *         @arg @ref LL_COMP_WINDOWOUTPUT_EACH_COMP
402   *         @arg @ref LL_COMP_WINDOWOUTPUT_COMP1
403   *         @arg @ref LL_COMP_WINDOWOUTPUT_COMP2
404   * @retval None
405   */
LL_COMP_SetCommonWindowOutput(COMP_Common_TypeDef * COMPxy_COMMON,uint32_t WindowOutput)406 __STATIC_INLINE void LL_COMP_SetCommonWindowOutput(COMP_Common_TypeDef *COMPxy_COMMON, uint32_t WindowOutput)
407 {
408   register __IO uint32_t *preg = __COMP_PTR_REG_OFFSET(COMPxy_COMMON->CSR_ODD,
409                                                        (WindowOutput & LL_COMP_WINDOWMODE_COMPX_REGOFFSET_MASK));
410 
411   /* Clear the potential previous setting of window output on the relevant comparator instance */
412   /* (clear bit of window output unless specific case of setting of comparator both output selected) */
413   register __IO uint32_t *preg_clear = __COMP_PTR_REG_OFFSET(COMPxy_COMMON->CSR_ODD,
414                                                              (~(WindowOutput & LL_COMP_WINDOWMODE_COMPX_REGOFFSET_MASK)
415                                                               & 0x1UL));
416   MODIFY_REG(*preg_clear,
417              COMP_CSR_WINOUT,
418              ((WindowOutput & LL_COMP_WINDOWOUTPUT_BOTH_SETTING_MASK) >> LL_COMP_WINDOWOUTPUT_BOTH_POS_VS_WINDOW)
419             );
420 
421   /* Set window output */
422   MODIFY_REG(*preg,
423              COMP_CSR_WINOUT,
424              (WindowOutput & LL_COMP_WINDOWOUTPUT_COMPX_SETTING_MASK)
425             );
426 }
427 
428 /**
429   * @brief  Get window output of a pair of comparators instances
430   *         (2 consecutive COMP instances COMP<x> and COMP<x+1>).
431   * @rmtoll CSR      WINMODE        LL_COMP_GetCommonWindowOutput
432   * @param  COMPxy_COMMON Comparator common instance
433   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
434   * @retval Returned value can be one of the following values:
435   *         @arg @ref LL_COMP_WINDOWOUTPUT_EACH_COMP
436   *         @arg @ref LL_COMP_WINDOWOUTPUT_COMP1
437   *         @arg @ref LL_COMP_WINDOWOUTPUT_COMP2
438   */
LL_COMP_GetCommonWindowOutput(const COMP_Common_TypeDef * COMPxy_COMMON)439 __STATIC_INLINE uint32_t LL_COMP_GetCommonWindowOutput(const COMP_Common_TypeDef *COMPxy_COMMON)
440 {
441   register const uint32_t window_output_comp_odd = (uint32_t)READ_BIT(COMPxy_COMMON->CSR_ODD, COMP_CSR_WINOUT);
442   register const uint32_t window_output_comp_even = (uint32_t)READ_BIT(COMPxy_COMMON->CSR_EVEN, COMP_CSR_WINOUT);
443 
444   /* Construct value corresponding to LL_COMP_WINDOWOUTPUT_xxx */
445   return (uint32_t)(window_output_comp_odd
446                     | window_output_comp_even
447                     | ((window_output_comp_even >> COMP_CSR_WINOUT_Pos) * LL_COMP_WINDOWMODE_COMP_EVEN_REGOFFSET_MASK)
448                     | (window_output_comp_odd + window_output_comp_even));
449 }
450 
451 
452 /**
453   * @}
454   */
455 
456 /** @defgroup COMP_LL_EF_Configuration_comparator_modes Configuration of comparator modes
457   * @{
458   */
459 
460 /**
461   * @brief  Set comparator instance operating mode to adjust power and speed.
462   * @rmtoll CSR      PWRMODE        LL_COMP_SetPowerMode
463   * @param  COMPx Comparator instance
464   * @param  PowerMode This parameter can be one of the following values:
465   *         @arg @ref LL_COMP_POWERMODE_HIGHSPEED
466   *         @arg @ref LL_COMP_POWERMODE_INTERMSPEED
467   *         @arg @ref LL_COMP_POWERMODE_MEDIUMSPEED
468   *         @arg @ref LL_COMP_POWERMODE_ULTRALOWPOWER
469   * @retval None
470   */
LL_COMP_SetPowerMode(COMP_TypeDef * COMPx,uint32_t PowerMode)471 __STATIC_INLINE void LL_COMP_SetPowerMode(COMP_TypeDef *COMPx, uint32_t PowerMode)
472 {
473   MODIFY_REG(COMPx->CSR, COMP_CSR_PWRMODE, PowerMode);
474 }
475 
476 /**
477   * @brief  Get comparator instance operating mode to adjust power and speed.
478   * @rmtoll CSR      PWRMODE        LL_COMP_GetPowerMode
479   * @param  COMPx Comparator instance
480   * @retval Returned value can be one of the following values:
481   *         @arg @ref LL_COMP_POWERMODE_HIGHSPEED
482   *         @arg @ref LL_COMP_POWERMODE_INTERMSPEED
483   *         @arg @ref LL_COMP_POWERMODE_MEDIUMSPEED
484   *         @arg @ref LL_COMP_POWERMODE_ULTRALOWPOWER
485   */
LL_COMP_GetPowerMode(const COMP_TypeDef * COMPx)486 __STATIC_INLINE uint32_t LL_COMP_GetPowerMode(const COMP_TypeDef *COMPx)
487 {
488   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_PWRMODE));
489 }
490 
491 /**
492   * @}
493   */
494 
495 /** @defgroup COMP_LL_EF_Configuration_comparator_inputs Configuration of comparator inputs
496   * @{
497   */
498 
499 /**
500   * @brief  Set comparator inputs minus (inverting) and plus (non-inverting).
501   * @note   In case of comparator input selected to be connected to IO:
502   *         GPIO pins are specific to each comparator instance.
503   *         Refer to description of parameters or to reference manual.
504   * @note   On this STM32 series, a voltage scaler is used
505   *         when COMP input is based on VrefInt (VrefInt or subdivision
506   *         of VrefInt):
507   *         Voltage scaler requires a delay for voltage stabilization.
508   *         Refer to device datasheet, parameter "tSTART_SCALER".
509   * @rmtoll CSR      INMSEL         LL_COMP_ConfigInputs\n
510   *         CSR      INPSEL         LL_COMP_ConfigInputs\n
511   * @param  COMPx Comparator instance
512   * @param  InputMinus This parameter can be one of the following values:
513   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
514   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
515   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
516   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
517   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
518   * @param  InputPlus This parameter can be one of the following values:
519   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
520   * @retval None
521   */
LL_COMP_ConfigInputs(COMP_TypeDef * COMPx,uint32_t InputMinus,uint32_t InputPlus)522 __STATIC_INLINE void LL_COMP_ConfigInputs(COMP_TypeDef *COMPx, uint32_t InputMinus, uint32_t InputPlus)
523 {
524   MODIFY_REG(COMPx->CSR,
525              COMP_CSR_INMSEL | COMP_CSR_INPSEL,
526              InputMinus | InputPlus);
527 }
528 
529 /**
530   * @brief  Set comparator input plus (non-inverting).
531   * @note   In case of comparator input selected to be connected to IO:
532   *         GPIO pins are specific to each comparator instance.
533   *         Refer to description of parameters or to reference manual.
534   * @rmtoll CSR      INPSEL         LL_COMP_SetInputPlus
535   * @param  COMPx Comparator instance
536   * @param  InputPlus This parameter can be one of the following values:
537   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
538   * @retval None
539   */
LL_COMP_SetInputPlus(COMP_TypeDef * COMPx,uint32_t InputPlus)540 __STATIC_INLINE void LL_COMP_SetInputPlus(COMP_TypeDef *COMPx, uint32_t InputPlus)
541 {
542   MODIFY_REG(COMPx->CSR, COMP_CSR_INPSEL, InputPlus);
543 }
544 
545 /**
546   * @brief  Get comparator input plus (non-inverting).
547   * @note   In case of comparator input selected to be connected to IO:
548   *         GPIO pins are specific to each comparator instance.
549   *         Refer to description of parameters or to reference manual.
550   * @rmtoll CSR      INPSEL         LL_COMP_GetInputPlus
551   * @param  COMPx Comparator instance
552   * @retval Returned value can be one of the following values:
553   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
554   */
LL_COMP_GetInputPlus(const COMP_TypeDef * COMPx)555 __STATIC_INLINE uint32_t LL_COMP_GetInputPlus(const COMP_TypeDef *COMPx)
556 {
557   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_INPSEL));
558 }
559 
560 /**
561   * @brief  Set comparator input minus (inverting).
562   * @note   In case of comparator input selected to be connected to IO:
563   *         GPIO pins are specific to each comparator instance.
564   *         Refer to description of parameters or to reference manual.
565   * @note   On this STM32 series, a voltage scaler is used
566   *         when COMP input is based on VrefInt (VrefInt or subdivision
567   *         of VrefInt):
568   *         Voltage scaler requires a delay for voltage stabilization.
569   *         Refer to device datasheet, parameter "tSTART_SCALER".
570   * @rmtoll CSR      INMSEL         LL_COMP_SetInputMinus
571   * @param  COMPx Comparator instance
572   * @param  InputMinus This parameter can be one of the following values:
573   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
574   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
575   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
576   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
577   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
578   * @retval None
579   */
LL_COMP_SetInputMinus(COMP_TypeDef * COMPx,uint32_t InputMinus)580 __STATIC_INLINE void LL_COMP_SetInputMinus(COMP_TypeDef *COMPx, uint32_t InputMinus)
581 {
582   MODIFY_REG(COMPx->CSR, COMP_CSR_INMSEL, InputMinus);
583 }
584 
585 /**
586   * @brief  Get comparator input minus (inverting).
587   * @note   In case of comparator input selected to be connected to IO:
588   *         GPIO pins are specific to each comparator instance.
589   *         Refer to description of parameters or to reference manual.
590   * @rmtoll CSR      INMSEL         LL_COMP_GetInputMinus
591   * @param  COMPx Comparator instance
592   * @retval Returned value can be one of the following values:
593   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
594   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
595   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
596   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
597   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
598   */
LL_COMP_GetInputMinus(const COMP_TypeDef * COMPx)599 __STATIC_INLINE uint32_t LL_COMP_GetInputMinus(const COMP_TypeDef *COMPx)
600 {
601   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_INMSEL));
602 }
603 
604 /**
605   * @brief  Set comparator instance hysteresis mode of the input minus (inverting input).
606   * @rmtoll CSR      HYST           LL_COMP_SetInputHysteresis
607   * @param  COMPx Comparator instance
608   * @param  InputHysteresis This parameter can be one of the following values:
609   *         @arg @ref LL_COMP_HYSTERESIS_NONE
610   *         @arg @ref LL_COMP_HYSTERESIS_LOW
611   *         @arg @ref LL_COMP_HYSTERESIS_MEDIUM
612   *         @arg @ref LL_COMP_HYSTERESIS_HIGH
613   * @retval None
614   */
LL_COMP_SetInputHysteresis(COMP_TypeDef * COMPx,uint32_t InputHysteresis)615 __STATIC_INLINE void LL_COMP_SetInputHysteresis(COMP_TypeDef *COMPx, uint32_t InputHysteresis)
616 {
617   MODIFY_REG(COMPx->CSR, COMP_CSR_HYST, InputHysteresis);
618 }
619 
620 /**
621   * @brief  Get comparator instance hysteresis mode of the minus (inverting) input.
622   * @rmtoll CSR      HYST           LL_COMP_GetInputHysteresis
623   * @param  COMPx Comparator instance
624   * @retval Returned value can be one of the following values:
625   *         @arg @ref LL_COMP_HYSTERESIS_NONE
626   *         @arg @ref LL_COMP_HYSTERESIS_LOW
627   *         @arg @ref LL_COMP_HYSTERESIS_MEDIUM
628   *         @arg @ref LL_COMP_HYSTERESIS_HIGH
629   */
LL_COMP_GetInputHysteresis(const COMP_TypeDef * COMPx)630 __STATIC_INLINE uint32_t LL_COMP_GetInputHysteresis(const COMP_TypeDef *COMPx)
631 {
632   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_HYST));
633 }
634 
635 /**
636   * @}
637   */
638 
639 /** @defgroup COMP_LL_EF_Configuration_comparator_output Configuration of comparator output
640   * @{
641   */
642 
643 /**
644   * @brief  Set comparator instance output polarity.
645   * @rmtoll CSR      POLARITY       LL_COMP_SetOutputPolarity
646   * @param  COMPx Comparator instance
647   * @param  OutputPolarity This parameter can be one of the following values:
648   *         @arg @ref LL_COMP_OUTPUTPOL_NONINVERTED
649   *         @arg @ref LL_COMP_OUTPUTPOL_INVERTED
650   * @retval None
651   */
LL_COMP_SetOutputPolarity(COMP_TypeDef * COMPx,uint32_t OutputPolarity)652 __STATIC_INLINE void LL_COMP_SetOutputPolarity(COMP_TypeDef *COMPx, uint32_t OutputPolarity)
653 {
654   MODIFY_REG(COMPx->CSR, COMP_CSR_POLARITY, OutputPolarity);
655 }
656 
657 /**
658   * @brief  Get comparator instance output polarity.
659   * @rmtoll CSR      POLARITY       LL_COMP_GetOutputPolarity
660   * @param  COMPx Comparator instance
661   * @retval Returned value can be one of the following values:
662   *         @arg @ref LL_COMP_OUTPUTPOL_NONINVERTED
663   *         @arg @ref LL_COMP_OUTPUTPOL_INVERTED
664   */
LL_COMP_GetOutputPolarity(const COMP_TypeDef * COMPx)665 __STATIC_INLINE uint32_t LL_COMP_GetOutputPolarity(const COMP_TypeDef *COMPx)
666 {
667   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_POLARITY));
668 }
669 
670 /**
671   * @brief  Set comparator instance blanking source.
672   * @note   Blanking source may be specific to each comparator instance.
673   *         Refer to description of parameters or to reference manual.
674   * @note   Availability of parameters of blanking source from timer
675   *         depends on timers availability on the selected device.
676   * @rmtoll CSR      BLANKSEL       LL_COMP_SetOutputBlankingSource
677   * @param  COMPx Comparator instance
678   * @param  BlankingSource This parameter can be one of the following values:
679   *         @arg @ref LL_COMP_BLANKINGSRC_NONE
680   *         @arg @ref LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1  (1)
681   *         @arg @ref LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1  (1)
682   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1  (1)
683   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2  (2)
684   *
685   *         (1) Parameter available only on comparator instance: COMP1.
686   *         (2) Parameter available only on comparator instance: COMP2.
687   * @retval None
688   */
LL_COMP_SetOutputBlankingSource(COMP_TypeDef * COMPx,uint32_t BlankingSource)689 __STATIC_INLINE void LL_COMP_SetOutputBlankingSource(COMP_TypeDef *COMPx, uint32_t BlankingSource)
690 {
691   MODIFY_REG(COMPx->CSR, COMP_CSR_BLANKSEL, BlankingSource);
692 }
693 
694 /**
695   * @brief  Get comparator instance blanking source.
696   * @note   Availability of parameters of blanking source from timer
697   *         depends on timers availability on the selected device.
698   * @note   Blanking source may be specific to each comparator instance.
699   *         Refer to description of parameters or to reference manual.
700   * @rmtoll CSR      BLANKSEL       LL_COMP_GetOutputBlankingSource
701   * @param  COMPx Comparator instance
702   * @retval Returned value can be one of the following values:
703   *         @arg @ref LL_COMP_BLANKINGSRC_NONE
704   *         @arg @ref LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1  (1)
705   *         @arg @ref LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1  (1)
706   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1  (1)
707   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2  (2)
708   *
709   *         (1) Parameter available only on comparator instance: COMP1.
710   *         (2) Parameter available only on comparator instance: COMP2.
711   */
LL_COMP_GetOutputBlankingSource(const COMP_TypeDef * COMPx)712 __STATIC_INLINE uint32_t LL_COMP_GetOutputBlankingSource(const COMP_TypeDef *COMPx)
713 {
714   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_BLANKSEL));
715 }
716 
717 /**
718   * @}
719   */
720 
721 /** @defgroup COMP_LL_EF_Operation Operation on comparator instance
722   * @{
723   */
724 
725 /**
726   * @brief  Enable comparator instance.
727   * @note   After enable from off state, comparator requires a delay
728   *         to reach reach propagation delay specification.
729   *         Refer to device datasheet, parameter "tSTART".
730   * @rmtoll CSR      EN             LL_COMP_Enable
731   * @param  COMPx Comparator instance
732   * @retval None
733   */
LL_COMP_Enable(COMP_TypeDef * COMPx)734 __STATIC_INLINE void LL_COMP_Enable(COMP_TypeDef *COMPx)
735 {
736   SET_BIT(COMPx->CSR, COMP_CSR_EN);
737 }
738 
739 /**
740   * @brief  Disable comparator instance.
741   * @rmtoll CSR      EN             LL_COMP_Disable
742   * @param  COMPx Comparator instance
743   * @retval None
744   */
LL_COMP_Disable(COMP_TypeDef * COMPx)745 __STATIC_INLINE void LL_COMP_Disable(COMP_TypeDef *COMPx)
746 {
747   CLEAR_BIT(COMPx->CSR, COMP_CSR_EN);
748 }
749 
750 /**
751   * @brief  Get comparator enable state
752   *         (0: COMP is disabled, 1: COMP is enabled)
753   * @rmtoll CSR      EN             LL_COMP_IsEnabled
754   * @param  COMPx Comparator instance
755   * @retval State of bit (1 or 0).
756   */
LL_COMP_IsEnabled(const COMP_TypeDef * COMPx)757 __STATIC_INLINE uint32_t LL_COMP_IsEnabled(const COMP_TypeDef *COMPx)
758 {
759   return ((READ_BIT(COMPx->CSR, COMP_CSR_EN) == (COMP_CSR_EN)) ? 1UL : 0UL);
760 }
761 
762 /**
763   * @brief  Lock comparator instance.
764   * @note   Once locked, comparator configuration can be accessed in read-only.
765   * @note   The only way to unlock the comparator is a device hardware reset.
766   * @rmtoll CSR      LOCK           LL_COMP_Lock
767   * @param  COMPx Comparator instance
768   * @retval None
769   */
LL_COMP_Lock(COMP_TypeDef * COMPx)770 __STATIC_INLINE void LL_COMP_Lock(COMP_TypeDef *COMPx)
771 {
772   SET_BIT(COMPx->CSR, COMP_CSR_LOCK);
773 }
774 
775 /**
776   * @brief  Get comparator lock state
777   *         (0: COMP is unlocked, 1: COMP is locked).
778   * @note   Once locked, comparator configuration can be accessed in read-only.
779   * @note   The only way to unlock the comparator is a device hardware reset.
780   * @rmtoll CSR      LOCK           LL_COMP_IsLocked
781   * @param  COMPx Comparator instance
782   * @retval State of bit (1 or 0).
783   */
LL_COMP_IsLocked(const COMP_TypeDef * COMPx)784 __STATIC_INLINE uint32_t LL_COMP_IsLocked(const COMP_TypeDef *COMPx)
785 {
786   return ((READ_BIT(COMPx->CSR, COMP_CSR_LOCK) == (COMP_CSR_LOCK)) ? 1UL : 0UL);
787 }
788 
789 /**
790   * @brief  Read comparator instance output level.
791   * @note   The comparator output level depends on the selected polarity
792   *         (Refer to function @ref LL_COMP_SetOutputPolarity()).
793   *         If the comparator polarity is not inverted:
794   *          - Comparator output is low when the input plus
795   *            is at a lower voltage than the input minus
796   *          - Comparator output is high when the input plus
797   *            is at a higher voltage than the input minus
798   *         If the comparator polarity is inverted:
799   *          - Comparator output is high when the input plus
800   *            is at a lower voltage than the input minus
801   *          - Comparator output is low when the input plus
802   *            is at a higher voltage than the input minus
803   * @rmtoll CSR      VALUE          LL_COMP_ReadOutputLevel
804   * @param  COMPx Comparator instance
805   * @retval Returned value can be one of the following values:
806   *         @arg @ref LL_COMP_OUTPUT_LEVEL_LOW
807   *         @arg @ref LL_COMP_OUTPUT_LEVEL_HIGH
808   */
LL_COMP_ReadOutputLevel(const COMP_TypeDef * COMPx)809 __STATIC_INLINE uint32_t LL_COMP_ReadOutputLevel(const COMP_TypeDef *COMPx)
810 {
811   return (uint32_t)(READ_BIT(COMPx->CSR, COMP_CSR_VALUE)
812                     >> LL_COMP_OUTPUT_LEVEL_BITOFFSET_POS);
813 }
814 
815 /**
816   * @}
817   */
818 
819 #if defined(USE_FULL_LL_DRIVER)
820 /** @defgroup COMP_LL_EF_Init Initialization and de-initialization functions
821   * @{
822   */
823 
824 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx);
825 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, const LL_COMP_InitTypeDef *COMP_InitStruct);
826 void        LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct);
827 
828 /**
829   * @}
830   */
831 #endif /* USE_FULL_LL_DRIVER */
832 
833 /**
834   * @}
835   */
836 
837 /**
838   * @}
839   */
840 
841 /**
842   * @}
843   */
844 
845 #endif /* COMP1 || COMP2 */
846 
847 /**
848   * @}
849   */
850 
851 #ifdef __cplusplus
852 }
853 #endif
854 
855 #endif /* STM32WBAxx_LL_COMP_H */
856