1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_ll_comp.h
4   * @author  MCD Application Team
5   * @brief   Header file of COMP LL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2016 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 __STM32F0xx_LL_COMP_H
21 #define __STM32F0xx_LL_COMP_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32f0xx.h"
29 
30 /** @addtogroup STM32F0xx_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 /* Differentiation between COMP instances */
48 /* Note: Value not corresponding to a register offset since both              */
49 /*       COMP instances are sharing the same register) .                      */
50 #define COMPX_BASE  COMP_BASE
51 #define COMPX       (COMP1 - COMP2)
52 
53 /* COMP registers bits positions */
54 #define LL_COMP_OUTPUT_LEVEL_BITOFFSET_POS ((uint32_t)14U) /* Value equivalent to POSITION_VAL(COMP_CSR_COMP1OUT) */
55 
56 /**
57   * @}
58   */
59 
60 /* Private macros ------------------------------------------------------------*/
61 /** @defgroup COMP_LL_Private_Macros COMP Private Macros
62   * @{
63   */
64 
65 /**
66   * @brief  Driver macro reserved for internal use: if COMP instance selected
67   *         is odd (COMP1, COMP3, ...), return value '1', else return '0'.
68   * @param  __COMP_INSTANCE__ COMP instance
69   * @retval If COMP instance is odd, value '1'. Else, value '0'.
70 */
71 #define __COMP_IS_INSTANCE_ODD(__COMP_INSTANCE__)                              \
72   ((~(((uint32_t)(__COMP_INSTANCE__) - COMP_BASE) >> 1U)) & 0x00000001)
73 
74 /**
75   * @brief  Driver macro reserved for internal use: if COMP instance selected
76   *         is even (COMP2, COMP4, ...), return value '1', else return '0'.
77   * @param  __COMP_INSTANCE__ COMP instance
78   * @retval If COMP instance is even, value '1'. Else, value '0'.
79 */
80 #define __COMP_IS_INSTANCE_EVEN(__COMP_INSTANCE__)                             \
81   (((uint32_t)(__COMP_INSTANCE__) - COMP_BASE) >> 1U)
82 
83 /**
84   * @brief  Driver macro reserved for internal use: from COMP instance
85   *         selected, set offset of bits into COMP register.
86   * @note   Since both COMP instances are sharing the same register
87   *         with 2 area of bits with an offset of 16 bits, this function
88   *         returns value "0" if COMP1 is selected and "16" if COMP2 is
89   *         selected.
90   * @param  __COMP_INSTANCE__ COMP instance
91   * @retval Bits offset in register 32 bits
92 */
93 #define __COMP_BITOFFSET_INSTANCE(__COMP_INSTANCE__)                           \
94   (((uint32_t)(__COMP_INSTANCE__) - COMP_BASE) << 3U)
95 
96 /**
97   * @}
98   */
99 
100 /* Exported types ------------------------------------------------------------*/
101 #if defined(USE_FULL_LL_DRIVER)
102 /** @defgroup COMP_LL_ES_INIT COMP Exported Init structure
103   * @{
104   */
105 
106 /**
107   * @brief  Structure definition of some features of COMP instance.
108   */
109 typedef struct
110 {
111   uint32_t PowerMode;                   /*!< Set comparator operating mode to adjust power and speed.
112                                              This parameter can be a value of @ref COMP_LL_EC_POWERMODE
113 
114                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetPowerMode(). */
115 
116   uint32_t InputPlus;                   /*!< Set comparator input plus (non-inverting input).
117                                              This parameter can be a value of @ref COMP_LL_EC_INPUT_PLUS
118 
119                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetInputPlus(). */
120 
121   uint32_t InputMinus;                  /*!< Set comparator input minus (inverting input).
122                                              This parameter can be a value of @ref COMP_LL_EC_INPUT_MINUS
123 
124                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetInputMinus(). */
125 
126   uint32_t InputHysteresis;             /*!< Set comparator hysteresis mode of the input minus.
127                                              This parameter can be a value of @ref COMP_LL_EC_INPUT_HYSTERESIS
128 
129                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetInputHysteresis(). */
130 
131   uint32_t OutputSelection;             /*!< Set comparator output selection.
132                                              This parameter can be a value of @ref COMP_LL_EC_OUTPUT_SELECTION
133 
134                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetOutputSelection(). */
135 
136   uint32_t OutputPolarity;              /*!< Set comparator output polarity.
137                                              This parameter can be a value of @ref COMP_LL_EC_OUTPUT_POLARITY
138 
139                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetOutputPolarity(). */
140 
141 } LL_COMP_InitTypeDef;
142 
143 /**
144   * @}
145   */
146 #endif /* USE_FULL_LL_DRIVER */
147 
148 /* Exported constants --------------------------------------------------------*/
149 /** @defgroup COMP_LL_Exported_Constants COMP Exported Constants
150   * @{
151   */
152 
153 /** @defgroup COMP_LL_EC_COMMON_WINDOWMODE Comparator common modes - Window mode
154   * @{
155   */
156 #define LL_COMP_WINDOWMODE_DISABLE                 ((uint32_t)0x00000000U) /*!< Window mode disable: Comparators 1 and 2 are independent */
157 #define LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON (COMP_CSR_WNDWEN)       /*!< 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). */
158 /**
159   * @}
160   */
161 
162 /** @defgroup COMP_LL_EC_POWERMODE Comparator modes - Power mode
163   * @{
164   */
165 #define LL_COMP_POWERMODE_HIGHSPEED     ((uint32_t)0x00000000U)                       /*!< COMP power mode to high speed */
166 #define LL_COMP_POWERMODE_MEDIUMSPEED   (COMP_CSR_COMP1MODE_0)                        /*!< COMP power mode to medium speed */
167 #define LL_COMP_POWERMODE_LOWPOWER      (COMP_CSR_COMP1MODE_1)                        /*!< COMP power mode to low power */
168 #define LL_COMP_POWERMODE_ULTRALOWPOWER (COMP_CSR_COMP1MODE_1 | COMP_CSR_COMP1MODE_0) /*!< COMP power mode to ultra-low power */
169 /**
170   * @}
171   */
172 
173 /** @defgroup COMP_LL_EC_INPUT_PLUS Comparator inputs - Input plus (input non-inverting) selection
174   * @{
175   */
176 #define LL_COMP_INPUT_PLUS_IO1          ((uint32_t)0x00000000U) /*!< Comparator input plus connected to IO1 (pin PA1 for COMP1, pin PA3 for COMP2) */
177 #define LL_COMP_INPUT_PLUS_DAC1_CH1     (COMP_CSR_COMP1SW1)     /*!< Comparator input plus connected to DAC1 channel 1 (DAC_OUT1), through dedicated switch (Note: this switch is solely intended to redirect signals onto high impedance input, such as COMP1 input plus (highly resistive switch)) (specific to COMP instance: COMP1) */
178 /**
179   * @}
180   */
181 
182 /** @defgroup COMP_LL_EC_INPUT_MINUS Comparator inputs - Input minus (input inverting) selection
183   * @{
184   */
185 #define LL_COMP_INPUT_MINUS_1_4VREFINT  ((uint32_t)0x00000000U)                                                 /*!< Comparator input minus connected to 1/4 VrefInt  */
186 #define LL_COMP_INPUT_MINUS_1_2VREFINT  (                                                COMP_CSR_COMP1INSEL_0) /*!< Comparator input minus connected to 1/2 VrefInt  */
187 #define LL_COMP_INPUT_MINUS_3_4VREFINT  (                        COMP_CSR_COMP1INSEL_1                        ) /*!< Comparator input minus connected to 3/4 VrefInt  */
188 #define LL_COMP_INPUT_MINUS_VREFINT     (                        COMP_CSR_COMP1INSEL_1 | COMP_CSR_COMP1INSEL_0) /*!< Comparator input minus connected to VrefInt */
189 #define LL_COMP_INPUT_MINUS_DAC1_CH1    (COMP_CSR_COMP1INSEL_2                                                ) /*!< Comparator input minus connected to DAC1 channel 1 (DAC_OUT1)  */
190 #define LL_COMP_INPUT_MINUS_DAC1_CH2    (COMP_CSR_COMP1INSEL_2                         | COMP_CSR_COMP1INSEL_0) /*!< Comparator input minus connected to DAC1 channel 2 (DAC_OUT2)  */
191 #define LL_COMP_INPUT_MINUS_IO1         (COMP_CSR_COMP1INSEL_2 | COMP_CSR_COMP1INSEL_1                        ) /*!< Comparator input minus connected to IO1 (pin PA0 for COMP1, pin PA2 for COMP2) */
192 /**
193   * @}
194   */
195 
196 /** @defgroup COMP_LL_EC_INPUT_HYSTERESIS Comparator input - Hysteresis
197   * @{
198   */
199 #define LL_COMP_HYSTERESIS_NONE         ((uint32_t)0x00000000U)                       /*!< No hysteresis */
200 #define LL_COMP_HYSTERESIS_LOW          (                       COMP_CSR_COMP1HYST_0) /*!< Hysteresis level low */
201 #define LL_COMP_HYSTERESIS_MEDIUM       (COMP_CSR_COMP1HYST_1                       ) /*!< Hysteresis level medium */
202 #define LL_COMP_HYSTERESIS_HIGH         (COMP_CSR_COMP1HYST_1 | COMP_CSR_COMP1HYST_0) /*!< Hysteresis level high */
203 /**
204   * @}
205   */
206 
207 /** @defgroup COMP_LL_EC_OUTPUT_SELECTION Comparator output - Output selection
208   * @{
209   */
210 /* Note: Output redirection is common for COMP1 and COMP2 */
211 #define LL_COMP_OUTPUT_NONE             ((uint32_t)0x00000000U)                                                    /*!< COMP output is not connected to other peripherals (except GPIO and EXTI that are always connected to COMP output) */
212 #define LL_COMP_OUTPUT_TIM1_BKIN        (COMP_CSR_COMP1OUTSEL_0)                                                   /*!< COMP output connected to TIM1 break input (BKIN) */
213 #define LL_COMP_OUTPUT_TIM1_IC1         (COMP_CSR_COMP1OUTSEL_1)                                                   /*!< COMP output connected to TIM1 input capture 1 */
214 #define LL_COMP_OUTPUT_TIM1_OCCLR       (COMP_CSR_COMP1OUTSEL_1 | COMP_CSR_COMP1OUTSEL_0)                          /*!< COMP output connected to TIM1 OCREF clear */
215 #define LL_COMP_OUTPUT_TIM2_IC4         (COMP_CSR_COMP1OUTSEL_2)                                                   /*!< COMP output connected to TIM2 input capture 4 */
216 #define LL_COMP_OUTPUT_TIM2_OCCLR       (COMP_CSR_COMP1OUTSEL_2 | COMP_CSR_COMP1OUTSEL_0)                          /*!< COMP output connected to TIM2 OCREF clear */
217 #define LL_COMP_OUTPUT_TIM3_IC1         (COMP_CSR_COMP1OUTSEL_2 | COMP_CSR_COMP1OUTSEL_1)                          /*!< COMP output connected to TIM3 input capture 1 */
218 #define LL_COMP_OUTPUT_TIM3_OCCLR       (COMP_CSR_COMP1OUTSEL_2 | COMP_CSR_COMP1OUTSEL_1 | COMP_CSR_COMP1OUTSEL_0) /*!< COMP output connected to TIM3 OCREF clear */
219 /**
220   * @}
221   */
222 
223 /** @defgroup COMP_LL_EC_OUTPUT_POLARITY Comparator output - Output polarity
224   * @{
225   */
226 #define LL_COMP_OUTPUTPOL_NONINVERTED   ((uint32_t)0x00000000U)  /*!< 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 */
227 #define LL_COMP_OUTPUTPOL_INVERTED      (COMP_CSR_COMP1POL)      /*!< 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 */
228 /**
229   * @}
230   */
231 
232 /** @defgroup COMP_LL_EC_OUTPUT_LEVEL Comparator output - Output level
233   * @{
234   */
235 #define LL_COMP_OUTPUT_LEVEL_LOW        ((uint32_t)0x00000000U) /*!< Comparator output level low (if the polarity is not inverted, otherwise to be complemented) */
236 #define LL_COMP_OUTPUT_LEVEL_HIGH       ((uint32_t)0x00000001U) /*!< Comparator output level high (if the polarity is not inverted, otherwise to be complemented) */
237 /**
238   * @}
239   */
240 
241 /** @defgroup COMP_LL_EC_HW_DELAYS  Definitions of COMP hardware constraints delays
242   * @note   Only COMP IP HW delays are defined in COMP LL driver driver,
243   *         not timeout values.
244   *         For details on delays values, refer to descriptions in source code
245   *         above each literal definition.
246   * @{
247   */
248 
249 /* Delay for comparator startup time.                                         */
250 /* Note: Delay required to reach propagation delay specification.             */
251 /* Literal set to maximum value (refer to device datasheet,                   */
252 /* parameter "tSTART").                                                       */
253 /* Unit: us                                                                   */
254 #define LL_COMP_DELAY_STARTUP_US          ((uint32_t) 60U)  /*!< Delay for COMP startup time */
255 
256 /* Delay for comparator voltage scaler stabilization time                     */
257 /* (voltage from VrefInt, delay based on VrefInt startup time).               */
258 /* Literal set to maximum value (refer to device datasheet,                   */
259 /* parameter "tS_SC").                                                        */
260 /* Unit: us                                                                   */
261 #define LL_COMP_DELAY_VOLTAGE_SCALER_STAB_US ((uint32_t) 200U)  /*!< Delay for COMP voltage scaler stabilization time */
262 
263 
264 /**
265   * @}
266   */
267 
268 /**
269   * @}
270   */
271 
272 /* Exported macro ------------------------------------------------------------*/
273 /** @defgroup COMP_LL_Exported_Macros COMP Exported Macros
274   * @{
275   */
276 /** @defgroup COMP_LL_EM_WRITE_READ Common write and read registers macro
277   * @{
278   */
279 
280 /**
281   * @brief  Write a value in COMP register
282   * @param  __INSTANCE__ comparator instance
283   * @param  __REG__ Register to be written
284   * @param  __VALUE__ Value to be written in the register
285   * @retval None
286   */
287 #define LL_COMP_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG(__INSTANCE__->__REG__, (__VALUE__))
288 
289 /**
290   * @brief  Read a value in COMP register
291   * @param  __INSTANCE__ comparator instance
292   * @param  __REG__ Register to be read
293   * @retval Register value
294   */
295 #define LL_COMP_ReadReg(__INSTANCE__, __REG__) READ_REG(__INSTANCE__->__REG__)
296 /**
297   * @}
298   */
299 
300 /** @defgroup COMP_LL_EM_HELPER_MACRO COMP helper macro
301   * @{
302   */
303 
304 /**
305   * @brief  Helper macro to select the COMP common instance
306   *         to which is belonging the selected COMP instance.
307   * @note   COMP common register instance can be used to
308   *         set parameters common to several COMP instances.
309   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
310   * @param  __COMPx__ COMP instance
311   * @retval COMP common instance or value "0" if there is no COMP common instance.
312   */
313 #define __LL_COMP_COMMON_INSTANCE(__COMPx__)                                   \
314   (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 of COMP hierarchical scope: common to several COMP instances
330   * @{
331   */
332 
333 /**
334   * @brief  Set window mode of a pair of comparators instances
335   *         (2 consecutive COMP instances odd and even COMP<x> and COMP<x+1>).
336   * @rmtoll CSR      WNDWEN         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   * @retval None
343   */
LL_COMP_SetCommonWindowMode(COMP_Common_TypeDef * COMPxy_COMMON,uint32_t WindowMode)344 __STATIC_INLINE void LL_COMP_SetCommonWindowMode(COMP_Common_TypeDef *COMPxy_COMMON, uint32_t WindowMode)
345 {
346   MODIFY_REG(COMPxy_COMMON->CSR, COMP_CSR_WNDWEN, WindowMode);
347 }
348 
349 /**
350   * @brief  Get window mode of a pair of comparators instances
351   *         (2 consecutive COMP instances odd and even COMP<x> and COMP<x+1>).
352   * @rmtoll CSR      WNDWEN         LL_COMP_GetCommonWindowMode
353   * @param  COMPxy_COMMON Comparator common instance
354   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
355   * @retval Returned value can be one of the following values:
356   *         @arg @ref LL_COMP_WINDOWMODE_DISABLE
357   *         @arg @ref LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON
358   */
LL_COMP_GetCommonWindowMode(COMP_Common_TypeDef * COMPxy_COMMON)359 __STATIC_INLINE uint32_t LL_COMP_GetCommonWindowMode(COMP_Common_TypeDef *COMPxy_COMMON)
360 {
361   return (uint32_t)(READ_BIT(COMPxy_COMMON->CSR, COMP_CSR_WNDWEN));
362 }
363 
364 /**
365   * @}
366   */
367 
368 /** @defgroup COMP_LL_EF_Configuration_comparator_modes Configuration of comparator modes
369   * @{
370   */
371 
372 /**
373   * @brief  Set comparator instance operating mode to adjust power and speed.
374   * @rmtoll CSR      COMP1MODE      LL_COMP_SetPowerMode\n
375   *                  COMP2MODE      LL_COMP_SetPowerMode
376   * @param  COMPx Comparator instance
377   * @param  PowerMode This parameter can be one of the following values:
378   *         @arg @ref LL_COMP_POWERMODE_HIGHSPEED
379   *         @arg @ref LL_COMP_POWERMODE_MEDIUMSPEED
380   *         @arg @ref LL_COMP_POWERMODE_LOWPOWER
381   *         @arg @ref LL_COMP_POWERMODE_ULTRALOWPOWER
382   * @retval None
383   */
LL_COMP_SetPowerMode(COMP_TypeDef * COMPx,uint32_t PowerMode)384 __STATIC_INLINE void LL_COMP_SetPowerMode(COMP_TypeDef *COMPx, uint32_t PowerMode)
385 {
386   MODIFY_REG(COMP->CSR,
387              COMP_CSR_COMP1MODE << __COMP_BITOFFSET_INSTANCE(COMPx),
388              PowerMode          << __COMP_BITOFFSET_INSTANCE(COMPx) );
389 }
390 
391 /**
392   * @brief  Get comparator instance operating mode to adjust power and speed.
393   * @rmtoll CSR      COMP1MODE      LL_COMP_GetPowerMode\n
394   *                     COMP2MODE       LL_COMP_GetPowerMode
395   * @param  COMPx Comparator instance
396   * @retval Returned value can be one of the following values:
397   *         @arg @ref LL_COMP_POWERMODE_HIGHSPEED
398   *         @arg @ref LL_COMP_POWERMODE_MEDIUMSPEED
399   *         @arg @ref LL_COMP_POWERMODE_LOWPOWER
400   *         @arg @ref LL_COMP_POWERMODE_ULTRALOWPOWER
401   */
LL_COMP_GetPowerMode(COMP_TypeDef * COMPx)402 __STATIC_INLINE uint32_t LL_COMP_GetPowerMode(COMP_TypeDef *COMPx)
403 {
404   return (uint32_t)(READ_BIT(COMP->CSR,
405                              COMP_CSR_COMP1MODE << __COMP_BITOFFSET_INSTANCE(COMPx))
406                     >> __COMP_BITOFFSET_INSTANCE(COMPx)
407                    );
408 }
409 
410 /**
411   * @}
412   */
413 
414 /** @defgroup COMP_LL_EF_Configuration_comparator_inputs Configuration of comparator inputs
415   * @{
416   */
417 
418 /**
419   * @brief  Set comparator inputs minus (inverting) and plus (non-inverting).
420   * @note   In case of comparator input selected to be connected to IO:
421   *         GPIO pins are specific to each comparator instance.
422   *         Refer to description of parameters or to reference manual.
423   * @rmtoll CSR      COMP1INSEL     LL_COMP_ConfigInputs\n
424   *         CSR      COMP2INSEL     LL_COMP_ConfigInputs\n
425   *         CSR      COMP1SW1       LL_COMP_ConfigInputs
426   * @param  COMPx Comparator instance
427   * @param  InputMinus This parameter can be one of the following values:
428   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
429   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
430   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
431   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
432   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH1
433   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH2
434   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
435   * @param  InputPlus This parameter can be one of the following values:
436   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
437   *         @arg @ref LL_COMP_INPUT_PLUS_DAC1_CH1 (1)
438   *
439   *         (1) Parameter available only on COMP instance: COMP1.
440   * @retval None
441   */
LL_COMP_ConfigInputs(COMP_TypeDef * COMPx,uint32_t InputMinus,uint32_t InputPlus)442 __STATIC_INLINE void LL_COMP_ConfigInputs(COMP_TypeDef *COMPx, uint32_t InputMinus, uint32_t InputPlus)
443 {
444   /* Note: Connection switch is applicable only to COMP instance COMP1,       */
445   /*       therefore if COMP2 is selected the equivalent bit is               */
446   /*       kept unmodified.                                                   */
447   MODIFY_REG(COMP->CSR,
448              (COMP_CSR_COMP1INSEL | (COMP_CSR_COMP1SW1 * __COMP_IS_INSTANCE_ODD(COMPx))) << __COMP_BITOFFSET_INSTANCE(COMPx),
449              (InputMinus | InputPlus)                                        << __COMP_BITOFFSET_INSTANCE(COMPx) );
450 }
451 
452 /**
453   * @brief  Set comparator input plus (non-inverting).
454   * @note   In case of comparator input selected to be connected to IO:
455   *         GPIO pins are specific to each comparator instance.
456   *         Refer to description of parameters or to reference manual.
457   * @rmtoll CSR      COMP1INSEL     LL_COMP_SetInputPlus\n
458   *         CSR      COMP2INSEL     LL_COMP_SetInputPlus
459   * @param  COMPx Comparator instance
460   * @param  InputPlus This parameter can be one of the following values:
461   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
462   *         @arg @ref LL_COMP_INPUT_PLUS_DAC1_CH1 (1)
463   *
464   *         (1) Parameter available only on COMP instance: COMP1.
465   * @retval None
466   */
LL_COMP_SetInputPlus(COMP_TypeDef * COMPx,uint32_t InputPlus)467 __STATIC_INLINE void LL_COMP_SetInputPlus(COMP_TypeDef *COMPx, uint32_t InputPlus)
468 {
469   /* Note: Connection switch is applicable only to COMP instance COMP1,       */
470   /*       therefore if COMP2 is selected the equivalent bit is               */
471   /*       kept unmodified.                                                   */
472   MODIFY_REG(COMP->CSR,
473              (COMP_CSR_COMP1SW1 * __COMP_IS_INSTANCE_ODD(COMPx)) << __COMP_BITOFFSET_INSTANCE(COMPx),
474              InputPlus                                           << __COMP_BITOFFSET_INSTANCE(COMPx) );
475 }
476 
477 /**
478   * @brief  Get comparator input plus (non-inverting).
479   * @note   In case of comparator input selected to be connected to IO:
480   *         GPIO pins are specific to each comparator instance.
481   *         Refer to description of parameters or to reference manual.
482   * @rmtoll CSR      COMP1INSEL     LL_COMP_GetInputPlus\n
483   *         CSR      COMP2INSEL     LL_COMP_GetInputPlus
484   * @param  COMPx Comparator instance
485   * @retval Returned value can be one of the following values:
486   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
487   *         @arg @ref LL_COMP_INPUT_PLUS_DAC1_CH1 (1)
488   *
489   *         (1) Parameter available only on COMP instance: COMP1.
490   */
LL_COMP_GetInputPlus(COMP_TypeDef * COMPx)491 __STATIC_INLINE uint32_t LL_COMP_GetInputPlus(COMP_TypeDef *COMPx)
492 {
493   /* Note: Connection switch is applicable only to COMP instance COMP1,       */
494   /*       therefore is COMP2 is selected the returned value will be null.    */
495   return (uint32_t)(READ_BIT(COMP->CSR,
496                              COMP_CSR_COMP1SW1 << __COMP_BITOFFSET_INSTANCE(COMPx))
497                     >> __COMP_BITOFFSET_INSTANCE(COMPx)
498                    );
499 }
500 
501 /**
502   * @brief  Set comparator input minus (inverting).
503   * @note   In case of comparator input selected to be connected to IO:
504   *         GPIO pins are specific to each comparator instance.
505   *         Refer to description of parameters or to reference manual.
506   * @rmtoll CSR      COMP1SW1       LL_COMP_SetInputMinus
507   * @param  COMPx Comparator instance
508   * @param  InputMinus This parameter can be one of the following values:
509   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
510   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
511   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
512   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
513   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH1
514   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH2
515   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
516   * @retval None
517   */
LL_COMP_SetInputMinus(COMP_TypeDef * COMPx,uint32_t InputMinus)518 __STATIC_INLINE void LL_COMP_SetInputMinus(COMP_TypeDef *COMPx, uint32_t InputMinus)
519 {
520   MODIFY_REG(COMP->CSR,
521              COMP_CSR_COMP1INSEL << __COMP_BITOFFSET_INSTANCE(COMPx),
522              InputMinus          << __COMP_BITOFFSET_INSTANCE(COMPx) );
523 }
524 
525 /**
526   * @brief  Get comparator input minus (inverting).
527   * @note   In case of comparator input selected to be connected to IO:
528   *         GPIO pins are specific to each comparator instance.
529   *         Refer to description of parameters or to reference manual.
530   * @rmtoll CSR      COMP1SW1       LL_COMP_GetInputMinus
531   * @param  COMPx Comparator instance
532   * @retval Returned value can be one of the following values:
533   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
534   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
535   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
536   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
537   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH1
538   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH2
539   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
540   */
LL_COMP_GetInputMinus(COMP_TypeDef * COMPx)541 __STATIC_INLINE uint32_t LL_COMP_GetInputMinus(COMP_TypeDef *COMPx)
542 {
543   return (uint32_t)(READ_BIT(COMP->CSR,
544                              COMP_CSR_COMP1INSEL << __COMP_BITOFFSET_INSTANCE(COMPx))
545                     >> __COMP_BITOFFSET_INSTANCE(COMPx)
546                    );
547 }
548 
549 /**
550   * @brief  Set comparator instance hysteresis mode of the input minus (inverting input).
551   * @rmtoll CSR      COMP1HYST      LL_COMP_SetInputHysteresis\n
552   *                  COMP2HYST      LL_COMP_SetInputHysteresis
553   * @param  COMPx Comparator instance
554   * @param  InputHysteresis This parameter can be one of the following values:
555   *         @arg @ref LL_COMP_HYSTERESIS_NONE
556   *         @arg @ref LL_COMP_HYSTERESIS_LOW
557   *         @arg @ref LL_COMP_HYSTERESIS_MEDIUM
558   *         @arg @ref LL_COMP_HYSTERESIS_HIGH
559   * @retval None
560   */
LL_COMP_SetInputHysteresis(COMP_TypeDef * COMPx,uint32_t InputHysteresis)561 __STATIC_INLINE void LL_COMP_SetInputHysteresis(COMP_TypeDef *COMPx, uint32_t InputHysteresis)
562 {
563   MODIFY_REG(COMP->CSR,
564              COMP_CSR_COMP1HYST << __COMP_BITOFFSET_INSTANCE(COMPx),
565              InputHysteresis    << __COMP_BITOFFSET_INSTANCE(COMPx) );
566 }
567 
568 /**
569   * @brief  Get comparator instance hysteresis mode of the minus (inverting) input.
570   * @rmtoll CSR      COMP1HYST      LL_COMP_GetInputHysteresis\n
571   *                  COMP2HYST      LL_COMP_GetInputHysteresis
572   * @param  COMPx Comparator instance
573   * @retval Returned value can be one of the following values:
574   *         @arg @ref LL_COMP_HYSTERESIS_NONE
575   *         @arg @ref LL_COMP_HYSTERESIS_LOW
576   *         @arg @ref LL_COMP_HYSTERESIS_MEDIUM
577   *         @arg @ref LL_COMP_HYSTERESIS_HIGH
578   */
LL_COMP_GetInputHysteresis(COMP_TypeDef * COMPx)579 __STATIC_INLINE uint32_t LL_COMP_GetInputHysteresis(COMP_TypeDef *COMPx)
580 {
581   return (uint32_t)(READ_BIT(COMP->CSR,
582                              COMP_CSR_COMP1HYST << __COMP_BITOFFSET_INSTANCE(COMPx))
583                     >> __COMP_BITOFFSET_INSTANCE(COMPx)
584                    );
585 
586 }
587 
588 /**
589   * @}
590   */
591 
592 /** @defgroup COMP_LL_EF_Configuration_comparator_output Configuration of comparator output
593   * @{
594   */
595 
596 /**
597   * @brief  Set comparator output selection.
598   * @note   Availability of parameters of output selection to timer
599   *         depends on timers availability on the selected device.
600   * @rmtoll CSR      COMP1OUTSEL    LL_COMP_SetOutputSelection\n
601   *                  COMP2OUTSEL    LL_COMP_SetOutputSelection
602   * @param  COMPx Comparator instance
603   * @param  OutputSelection This parameter can be one of the following values:
604   *         @arg @ref LL_COMP_OUTPUT_NONE
605   *         @arg @ref LL_COMP_OUTPUT_TIM1_BKIN      (1)
606   *         @arg @ref LL_COMP_OUTPUT_TIM1_IC1       (1)
607   *         @arg @ref LL_COMP_OUTPUT_TIM1_OCCLR     (1)
608   *         @arg @ref LL_COMP_OUTPUT_TIM2_IC4       (1)
609   *         @arg @ref LL_COMP_OUTPUT_TIM2_OCCLR     (1)
610   *         @arg @ref LL_COMP_OUTPUT_TIM3_IC1       (1)
611   *         @arg @ref LL_COMP_OUTPUT_TIM3_OCCLR     (1)
612   *
613   *         (1) Parameter availability depending on timer availability
614   *             on the selected device.
615   * @retval None
616   */
LL_COMP_SetOutputSelection(COMP_TypeDef * COMPx,uint32_t OutputSelection)617 __STATIC_INLINE void LL_COMP_SetOutputSelection(COMP_TypeDef *COMPx, uint32_t OutputSelection)
618 {
619   MODIFY_REG(COMP->CSR,
620              COMP_CSR_COMP1OUTSEL << __COMP_BITOFFSET_INSTANCE(COMPx),
621              OutputSelection      << __COMP_BITOFFSET_INSTANCE(COMPx) );
622 }
623 
624 /**
625   * @brief  Get comparator output selection.
626   * @note   Availability of parameters of output selection to timer
627   *         depends on timers availability on the selected device.
628   * @rmtoll CSR      COMP1OUTSEL    LL_COMP_GetOutputSelection\n
629   *                  COMP2OUTSEL    LL_COMP_GetOutputSelection
630   * @param  COMPx Comparator instance
631   * @retval Returned value can be one of the following values:
632   *         @arg @ref LL_COMP_OUTPUT_NONE
633   *         @arg @ref LL_COMP_OUTPUT_TIM1_BKIN      (1)
634   *         @arg @ref LL_COMP_OUTPUT_TIM1_IC1       (1)
635   *         @arg @ref LL_COMP_OUTPUT_TIM1_OCCLR     (1)
636   *         @arg @ref LL_COMP_OUTPUT_TIM2_IC4       (1)
637   *         @arg @ref LL_COMP_OUTPUT_TIM2_OCCLR     (1)
638   *         @arg @ref LL_COMP_OUTPUT_TIM3_IC1       (1)
639   *         @arg @ref LL_COMP_OUTPUT_TIM3_OCCLR     (1)
640   *
641   *         (1) Parameter availability depending on timer availability
642   *             on the selected device.
643   */
LL_COMP_GetOutputSelection(COMP_TypeDef * COMPx)644 __STATIC_INLINE uint32_t LL_COMP_GetOutputSelection(COMP_TypeDef *COMPx)
645 {
646   return (uint32_t)(READ_BIT(COMP->CSR,
647                              COMP_CSR_COMP1OUTSEL << __COMP_BITOFFSET_INSTANCE(COMPx))
648                     >> __COMP_BITOFFSET_INSTANCE(COMPx)
649                    );
650 }
651 
652 /**
653   * @brief  Set comparator instance output polarity.
654   * @rmtoll CSR      COMP1POL       LL_COMP_SetOutputPolarity\n
655   *                  COMP2POL       LL_COMP_SetOutputPolarity
656   * @param  COMPx Comparator instance
657   * @param  OutputPolarity This parameter can be one of the following values:
658   *         @arg @ref LL_COMP_OUTPUTPOL_NONINVERTED
659   *         @arg @ref LL_COMP_OUTPUTPOL_INVERTED
660   * @retval None
661   */
LL_COMP_SetOutputPolarity(COMP_TypeDef * COMPx,uint32_t OutputPolarity)662 __STATIC_INLINE void LL_COMP_SetOutputPolarity(COMP_TypeDef *COMPx, uint32_t OutputPolarity)
663 {
664   MODIFY_REG(COMP->CSR,
665              COMP_CSR_COMP1POL << __COMP_BITOFFSET_INSTANCE(COMPx),
666              OutputPolarity    << __COMP_BITOFFSET_INSTANCE(COMPx) );
667 }
668 
669 /**
670   * @brief  Get comparator instance output polarity.
671   * @rmtoll CSR      COMP1POL       LL_COMP_GetOutputPolarity\n
672   *                  COMP2POL       LL_COMP_GetOutputPolarity
673   * @param  COMPx Comparator instance
674   * @retval Returned value can be one of the following values:
675   *         @arg @ref LL_COMP_OUTPUTPOL_NONINVERTED
676   *         @arg @ref LL_COMP_OUTPUTPOL_INVERTED
677   */
LL_COMP_GetOutputPolarity(COMP_TypeDef * COMPx)678 __STATIC_INLINE uint32_t LL_COMP_GetOutputPolarity(COMP_TypeDef *COMPx)
679 {
680   return (uint32_t)(READ_BIT(COMP->CSR,
681                              COMP_CSR_COMP1POL << __COMP_BITOFFSET_INSTANCE(COMPx))
682                     >> __COMP_BITOFFSET_INSTANCE(COMPx)
683                    );
684 }
685 
686 /**
687   * @}
688   */
689 
690 /** @defgroup COMP_LL_EF_Operation Operation on comparator instance
691   * @{
692   */
693 
694 /**
695   * @brief  Enable comparator instance.
696   * @note   After enable from off state, comparator requires a delay
697   *         to reach reach propagation delay specification.
698   *         Refer to device datasheet, parameter "tSTART".
699   * @rmtoll CSR      COMP1EN        LL_COMP_Enable\n
700   *         CSR      COMP2EN        LL_COMP_Enable
701   * @param  COMPx Comparator instance
702   * @retval None
703   */
LL_COMP_Enable(COMP_TypeDef * COMPx)704 __STATIC_INLINE void LL_COMP_Enable(COMP_TypeDef *COMPx)
705 {
706   SET_BIT(COMP->CSR, COMP_CSR_COMP1EN << __COMP_BITOFFSET_INSTANCE(COMPx));
707 }
708 
709 /**
710   * @brief  Disable comparator instance.
711   * @rmtoll CSR      COMP1EN        LL_COMP_Disable\n
712   *         CSR      COMP2EN        LL_COMP_Disable
713   * @param  COMPx Comparator instance
714   * @retval None
715   */
LL_COMP_Disable(COMP_TypeDef * COMPx)716 __STATIC_INLINE void LL_COMP_Disable(COMP_TypeDef *COMPx)
717 {
718   CLEAR_BIT(COMP->CSR, COMP_CSR_COMP1EN << __COMP_BITOFFSET_INSTANCE(COMPx));
719 }
720 
721 /**
722   * @brief  Get comparator enable state
723   *         (0: COMP is disabled, 1: COMP is enabled)
724   * @rmtoll CSR      COMP1EN        LL_COMP_IsEnabled\n
725   *         CSR      COMP2EN        LL_COMP_IsEnabled
726   * @param  COMPx Comparator instance
727   * @retval State of bit (1 or 0).
728   */
LL_COMP_IsEnabled(COMP_TypeDef * COMPx)729 __STATIC_INLINE uint32_t LL_COMP_IsEnabled(COMP_TypeDef *COMPx)
730 {
731   return (READ_BIT(COMP->CSR, COMP_CSR_COMP1EN << __COMP_BITOFFSET_INSTANCE(COMPx)) == COMP_CSR_COMP1EN << __COMP_BITOFFSET_INSTANCE(COMPx));
732 }
733 
734 /**
735   * @brief  Lock comparator instance.
736   * @note   Once locked, comparator configuration can be accessed in read-only.
737   * @note   The only way to unlock the comparator is a device hardware reset.
738   * @rmtoll CSR      COMP1LOCK      LL_COMP_Lock\n
739   *         CSR      COMP2LOCK      LL_COMP_Lock
740   * @param  COMPx Comparator instance
741   * @retval None
742   */
LL_COMP_Lock(COMP_TypeDef * COMPx)743 __STATIC_INLINE void LL_COMP_Lock(COMP_TypeDef *COMPx)
744 {
745   SET_BIT(COMP->CSR, COMP_CSR_COMP1LOCK << __COMP_BITOFFSET_INSTANCE(COMPx));
746 }
747 
748 /**
749   * @brief  Get comparator lock state
750   *         (0: COMP is unlocked, 1: COMP is locked).
751   * @note   Once locked, comparator configuration can be accessed in read-only.
752   * @note   The only way to unlock the comparator is a device hardware reset.
753   * @rmtoll CSR      COMP1LOCK      LL_COMP_IsLocked\n
754   *         CSR      COMP2LOCK      LL_COMP_IsLocked
755   * @param  COMPx Comparator instance
756   * @retval State of bit (1 or 0).
757   */
LL_COMP_IsLocked(COMP_TypeDef * COMPx)758 __STATIC_INLINE uint32_t LL_COMP_IsLocked(COMP_TypeDef *COMPx)
759 {
760   return (READ_BIT(COMP->CSR, COMP_CSR_COMP1LOCK << __COMP_BITOFFSET_INSTANCE(COMPx)) == COMP_CSR_COMP1LOCK << __COMP_BITOFFSET_INSTANCE(COMPx));
761 }
762 
763 /**
764   * @brief  Read comparator instance output level.
765   * @note   The comparator output level depends on the selected polarity
766   *         (Refer to function @ref LL_COMP_SetOutputPolarity()).
767   *         If the comparator polarity is not inverted:
768   *          - Comparator output is low when the input plus
769   *            is at a lower voltage than the input minus
770   *          - Comparator output is high when the input plus
771   *            is at a higher voltage than the input minus
772   *         If the comparator polarity is inverted:
773   *          - Comparator output is high when the input plus
774   *            is at a lower voltage than the input minus
775   *          - Comparator output is low when the input plus
776   *            is at a higher voltage than the input minus
777   * @rmtoll CSR      COMP1OUT       LL_COMP_ReadOutputLevel\n
778   *         CSR      COMP2OUT       LL_COMP_ReadOutputLevel
779   * @param  COMPx Comparator instance
780   * @retval Returned value can be one of the following values:
781   *         @arg @ref LL_COMP_OUTPUT_LEVEL_LOW
782   *         @arg @ref LL_COMP_OUTPUT_LEVEL_HIGH
783   */
LL_COMP_ReadOutputLevel(COMP_TypeDef * COMPx)784 __STATIC_INLINE uint32_t LL_COMP_ReadOutputLevel(COMP_TypeDef *COMPx)
785 {
786   return (uint32_t)(READ_BIT(COMP->CSR,
787                              COMP_CSR_COMP1OUT << __COMP_BITOFFSET_INSTANCE(COMPx))
788                     >> (__COMP_BITOFFSET_INSTANCE(COMPx) + LL_COMP_OUTPUT_LEVEL_BITOFFSET_POS)
789                    );
790 }
791 
792 /**
793   * @}
794   */
795 
796 #if defined(USE_FULL_LL_DRIVER)
797 /** @defgroup COMP_LL_EF_Init Initialization and de-initialization functions
798   * @{
799   */
800 
801 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx);
802 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct);
803 void        LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct);
804 
805 /**
806   * @}
807   */
808 #endif /* USE_FULL_LL_DRIVER */
809 
810 /**
811   * @}
812   */
813 
814 /**
815   * @}
816   */
817 
818 #endif /* COMP1 || COMP2 */
819 
820 /**
821   * @}
822   */
823 
824 #ifdef __cplusplus
825 }
826 #endif
827 
828 #endif /* __STM32F0xx_LL_COMP_H */
829 
830