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