1 /**
2   ******************************************************************************
3   * @file    stm32h7xx_ll_comp.h
4   * @author  MCD Application Team
5   * @brief   Header file of COMP LL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2017 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 __STM32H7xx_LL_COMP_H
21 #define __STM32H7xx_LL_COMP_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32h7xx.h"
29 
30 /** @addtogroup STM32H7xx_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 /* COMP registers bits positions */
48 #define LL_COMP_OUTPUT_LEVEL_BITOFFSET_POS (30U) /* Value equivalent to POSITION_VAL(COMP_CSR_VALUE) */
49 
50 /**
51   * @}
52   */
53 
54 /* Private macros ------------------------------------------------------------*/
55 /** @defgroup COMP_LL_Private_Macros COMP Private Macros
56   * @{
57   */
58 /**
59   * @}
60   */
61 /* Exported types ------------------------------------------------------------*/
62 #if defined(USE_FULL_LL_DRIVER)
63 /** @defgroup COMP_LL_ES_INIT COMP Exported Init structure
64   * @{
65   */
66 
67 /**
68   * @brief  Structure definition of some features of COMP instance.
69   */
70 typedef struct
71 {
72   uint32_t PowerMode;                   /*!< Set comparator operating mode to adjust power and speed.
73                                              This parameter can be a value of @ref COMP_LL_EC_POWERMODE
74 
75                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetPowerMode(). */
76 
77   uint32_t InputPlus;                   /*!< Set comparator input plus (non-inverting input).
78                                              This parameter can be a value of @ref COMP_LL_EC_INPUT_PLUS
79 
80                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetInputPlus(). */
81 
82   uint32_t InputMinus;                  /*!< Set comparator input minus (inverting input).
83                                              This parameter can be a value of @ref COMP_LL_EC_INPUT_MINUS
84 
85                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetInputMinus(). */
86 
87   uint32_t InputHysteresis;             /*!< Set comparator hysteresis mode of the input minus.
88                                              This parameter can be a value of @ref COMP_LL_EC_INPUT_HYSTERESIS
89 
90                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetInputHysteresis(). */
91 
92   uint32_t OutputPolarity;              /*!< Set comparator output polarity.
93                                              This parameter can be a value of @ref COMP_LL_EC_OUTPUT_POLARITY
94 
95                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetOutputPolarity(). */
96 
97   uint32_t OutputBlankingSource;        /*!< Set comparator blanking source.
98                                              This parameter can be a value of @ref COMP_LL_EC_OUTPUT_BLANKING_SOURCE
99 
100                                              This feature can be modified afterwards using unitary function @ref LL_COMP_SetOutputBlankingSource(). */
101 
102 } LL_COMP_InitTypeDef;
103 
104 /**
105   * @}
106   */
107 #endif /* USE_FULL_LL_DRIVER */
108 
109 /* Exported constants --------------------------------------------------------*/
110 /** @defgroup COMP_LL_Exported_Constants COMP Exported Constants
111   * @{
112   */
113 
114 /** @defgroup COMP_LL_EC_COMMON_WINDOWMODE Comparator common modes - Window mode
115   * @{
116   */
117 #define LL_COMP_WINDOWMODE_DISABLE                 (0x00000000UL)          /*!< Window mode disable: Comparators 1 and 2 are independent */
118 #define LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON (COMP_CFGRx_WINMODE)    /*!< 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). */
119 /**
120   * @}
121   */
122 
123 /** @defgroup COMP_LL_EC_POWERMODE Comparator modes - Power mode
124   * @{
125   */
126 #define LL_COMP_POWERMODE_HIGHSPEED     (0x00000000UL)                                /*!< COMP power mode to high speed */
127 #define LL_COMP_POWERMODE_MEDIUMSPEED   (COMP_CFGRx_PWRMODE_0)                        /*!< COMP power mode to medium speed */
128 #define LL_COMP_POWERMODE_ULTRALOWPOWER (COMP_CFGRx_PWRMODE_1 | COMP_CFGRx_PWRMODE_0) /*!< COMP power mode to ultra-low power */
129 /**
130   * @}
131   */
132 
133 /** @defgroup COMP_LL_EC_INPUT_PLUS Comparator inputs - Input plus (input non-inverting) selection
134   * @{
135   */
136 #define LL_COMP_INPUT_PLUS_IO1          (0x00000000UL)                           /*!< Comparator input plus connected to IO1 (pin PB0 for COMP1, pin PE9 for COMP2) */
137 #define LL_COMP_INPUT_PLUS_IO2          (COMP_CFGRx_INPSEL)                      /*!< Comparator input plus connected to IO2 (pin PB2 for COMP1, pin PE11 for COMP2) */
138 #if defined (COMP_CFGRx_INP2SEL)
139 #define LL_COMP_INPUT_PLUS_DAC2_CH1     (COMP_CFGRx_INP2SEL)                     /*!< Comparator input plus 2 connected to (DAC2_CH1 for COMP1)                      */
140 #endif
141 /**
142   * @}
143   */
144 
145 /** @defgroup COMP_LL_EC_INPUT_MINUS Comparator inputs - Input minus (input inverting) selection
146   * @{
147   */
148 #define LL_COMP_INPUT_MINUS_1_4VREFINT      (                                                                                        COMP_CFGRx_SCALEN | COMP_CFGRx_BRGEN)   /*!< Comparator input minus connected to 1/4 VrefInt  */
149 #define LL_COMP_INPUT_MINUS_1_2VREFINT      (                                                                  COMP_CFGRx_INMSEL_0 | COMP_CFGRx_SCALEN | COMP_CFGRx_BRGEN)   /*!< Comparator input minus connected to 1/2 VrefInt  */
150 #define LL_COMP_INPUT_MINUS_3_4VREFINT      (                                            COMP_CFGRx_INMSEL_1                       | COMP_CFGRx_SCALEN | COMP_CFGRx_BRGEN)   /*!< Comparator input minus connected to 3/4 VrefInt  */
151 #define LL_COMP_INPUT_MINUS_VREFINT         (                                            COMP_CFGRx_INMSEL_1 | COMP_CFGRx_INMSEL_0 | COMP_CFGRx_SCALEN                   )   /*!< Comparator input minus connected to VrefInt */
152 #define LL_COMP_INPUT_MINUS_DAC1_CH1        (                      COMP_CFGRx_INMSEL_2                                                                                   )   /*!< Comparator input minus connected to DAC1 channel 1 (DAC_OUT1)  */
153 #define LL_COMP_INPUT_MINUS_DAC1_CH2        (                      COMP_CFGRx_INMSEL_2                       | COMP_CFGRx_INMSEL_0                                       )   /*!< Comparator input minus connected to DAC1 channel 2 (DAC_OUT2)  */
154 #define LL_COMP_INPUT_MINUS_IO1             (                      COMP_CFGRx_INMSEL_2 | COMP_CFGRx_INMSEL_1                                                             )   /*!< Comparator input minus connected to IO1 (pin PB1 for COMP1, pin PE10 for COMP2) */
155 #define LL_COMP_INPUT_MINUS_IO2             (                      COMP_CFGRx_INMSEL_2 | COMP_CFGRx_INMSEL_1 | COMP_CFGRx_INMSEL_0                                       )   /*!< Comparator input minus connected to IO2 (pin PC4 for COMP1, pin PE7 for COMP2) */
156 #if defined (COMP_CFGRx_INMSEL_3)
157 #define LL_COMP_INPUT_MINUS_TPSENS_DAC2CH1  (COMP_CFGRx_INMSEL_3                                                                                                         )   /*!< Comparator input minus connected to  (temp sensor which is exist in ADC for COMP1, DAC2_CH1 for COMP2) */
158 #define LL_COMP_INPUT_MINUS_VBAT_VDDAP      (COMP_CFGRx_INMSEL_3                                             | COMP_CFGRx_INMSEL_0                                       )   /*!< Comparator input minus connected to  (VBAT/4 for COMP1, VDDAP for COMP2) */
159 #endif
160 /**
161   * @}
162   */
163 
164 /** @defgroup COMP_LL_EC_INPUT_HYSTERESIS Comparator input - Hysteresis
165   * @{
166   */
167 #define LL_COMP_HYSTERESIS_NONE         (0x00000000UL)                            /*!< No hysteresis */
168 #define LL_COMP_HYSTERESIS_LOW          (                    COMP_CFGRx_HYST_0)   /*!< Hysteresis level low */
169 #define LL_COMP_HYSTERESIS_MEDIUM       (COMP_CFGRx_HYST_1                    )   /*!< Hysteresis level medium */
170 #define LL_COMP_HYSTERESIS_HIGH         (COMP_CFGRx_HYST_1 | COMP_CFGRx_HYST_0)   /*!< Hysteresis level high */
171 /**
172   * @}
173   */
174 
175 /** @defgroup COMP_LL_EC_OUTPUT_POLARITY Comparator output - Output polarity
176   * @{
177   */
178 #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 */
179 #define LL_COMP_OUTPUTPOL_INVERTED      (COMP_CFGRx_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 */
180 /**
181   * @}
182   */
183 
184 /** @defgroup COMP_LL_EC_OUTPUT_BLANKING_SOURCE Comparator output - Blanking source
185   * @{
186   */
187 #define LL_COMP_BLANKINGSRC_NONE      (0x00000000UL)                                  /*!<Comparator output without blanking */
188 #define LL_COMP_BLANKINGSRC_TIM1_OC5  (COMP_CFGRx_BLANKING_0)                         /*!< Comparator output blanking source TIM1 OC5 (common to all COMP instances: COMP1, COMP2) */
189 #define LL_COMP_BLANKINGSRC_TIM2_OC3  (COMP_CFGRx_BLANKING_1)                         /*!< Comparator output blanking source TIM2 OC3 (common to all COMP instances: COMP1, COMP2) */
190 #define LL_COMP_BLANKINGSRC_TIM3_OC3  (COMP_CFGRx_BLANKING_0 |COMP_CFGRx_BLANKING_1)  /*!< Comparator output blanking source TIM3 OC3 (common to all COMP instances: COMP1, COMP2) */
191 #define LL_COMP_BLANKINGSRC_TIM3_OC4  (COMP_CFGRx_BLANKING_2)                         /*!< Comparator output blanking source TIM3 OC4 (common to all COMP instances: COMP1, COMP2) */
192 #define LL_COMP_BLANKINGSRC_TIM8_OC5  (COMP_CFGRx_BLANKING_2|COMP_CFGRx_BLANKING_0)   /*!< Comparator output blanking source TIM8 OC5 (common to all COMP instances: COMP1, COMP2) */
193 #define LL_COMP_BLANKINGSRC_TIM15_OC1 (COMP_CFGRx_BLANKING_2|COMP_CFGRx_BLANKING_1)   /*!< Comparator output blanking source TIM15 OC1 (common to all COMP instances: COMP1, COMP2) */
194 /**
195   * @}
196   */
197 
198 /** @defgroup COMP_LL_EC_OUTPUT_LEVEL Comparator output - Output level
199   * @{
200   */
201 #define LL_COMP_OUTPUT_LEVEL_LOW        (0x00000000UL)           /*!< Comparator output level low (if the polarity is not inverted, otherwise to be complemented) */
202 #define LL_COMP_OUTPUT_LEVEL_HIGH       (0x00000001UL)           /*!< Comparator output level high (if the polarity is not inverted, otherwise to be complemented) */
203 /**
204   * @}
205   */
206 /** @defgroup COMP_LL_EC_OUTPUT_BKIN_TIMER Comparator output - Output to BKIN timer
207   * @{
208   */
209 #define LL_COMP_AF_PA6          COMP_OR_AFOPA6       /*!< Comparator Alternate Function PA6 source selected to timer BKIN input  */
210 #define LL_COMP_AF_PA8          COMP_OR_AFOPA8       /*!< Comparator Alternate Function PA8 source selected to timer BKIN input  */
211 #define LL_COMP_AF_PB12         COMP_OR_AFOPB12      /*!< Comparator Alternate Function PB12 source selected to timer BKIN input */
212 #define LL_COMP_AF_PE6          COMP_OR_AFOPE6       /*!< Comparator Alternate Function PE6 source selected to timer BKIN input  */
213 #define LL_COMP_AF_PE15         COMP_OR_AFOPE15      /*!< Comparator Alternate Function PE15 source selected to timer BKIN input */
214 #define LL_COMP_AF_PG2          COMP_OR_AFOPG2       /*!< Comparator Alternate Function PG2 source selected to timer BKIN input  */
215 #define LL_COMP_AF_PG3          COMP_OR_AFOPG3       /*!< Comparator Alternate Function PG3 source selected to timer BKIN input  */
216 #define LL_COMP_AF_PG4          COMP_OR_AFOPG4       /*!< Comparator Alternate Function PG4 source selected to timer BKIN input  */
217 #define LL_COMP_AF_PI1          COMP_OR_AFOPI1       /*!< Comparator Alternate Function PI1 source selected to timer BKIN input  */
218 #define LL_COMP_AF_PI4          COMP_OR_AFOPI4       /*!< Comparator Alternate Function PI4 source selected to timer BKIN input  */
219 #define LL_COMP_AF_PK2          COMP_OR_AFOPK2       /*!< Comparator Alternate Function PK2 source selected to timer BKIN input  */
220 /**
221   * @}
222   */
223 
224 /** @defgroup COMP_LL_EC_HW_DELAYS  Definitions of COMP hardware constraints delays
225   * @note   Only COMP IP HW delays are defined in COMP LL driver driver,
226   *         not timeout values.
227   *         For details on delays values, refer to descriptions in source code
228   *         above each literal definition.
229   * @{
230   */
231 
232 /* Delay for comparator startup time.                                         */
233 /* Note: Delay required to reach propagation delay specification.             */
234 /* Literal set to maximum value (refer to device datasheet,                   */
235 /* parameter "tSTART").                                                       */
236 /* Unit: us                                                                   */
237 #define LL_COMP_DELAY_STARTUP_US          ( 80UL)  /*!< Delay for COMP startup time */
238 
239 /* Delay for comparator voltage scaler stabilization time.                    */
240 /* Note: Voltage scaler is used when selecting comparator input               */
241 /*       based on VrefInt: VrefInt or subdivision of VrefInt.                 */
242 /* Literal set to maximum value (refer to device datasheet,                   */
243 /* parameter "tSTART_SCALER").                                                */
244 /* Unit: us                                                                   */
245 #define LL_COMP_DELAY_VOLTAGE_SCALER_STAB_US ( 200UL)  /*!< Delay for COMP voltage scaler stabilization time */
246 
247 /**
248   * @}
249   */
250 
251 /**
252   * @}
253   */
254 
255 /* Exported macro ------------------------------------------------------------*/
256 /** @defgroup COMP_LL_Exported_Macros COMP Exported Macros
257   * @{
258   */
259 /** @defgroup COMP_LL_EM_WRITE_READ Common write and read registers macro
260   * @{
261   */
262 
263 /**
264   * @brief  Write a value in COMP register
265   * @param  __INSTANCE__ comparator instance
266   * @param  __REG__ Register to be written
267   * @param  __VALUE__ Value to be written in the register
268   * @retval None
269   */
270 #define LL_COMP_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG(__INSTANCE__->__REG__, (__VALUE__))
271 
272 /**
273   * @brief  Read a value in COMP register
274   * @param  __INSTANCE__ comparator instance
275   * @param  __REG__ Register to be read
276   * @retval Register value
277   */
278 #define LL_COMP_ReadReg(__INSTANCE__, __REG__) READ_REG(__INSTANCE__->__REG__)
279 /**
280   * @}
281   */
282 
283 /** @defgroup COMP_LL_EM_HELPER_MACRO COMP helper macro
284   * @{
285   */
286 
287 /**
288   * @brief  Helper macro to select the COMP common instance
289   *         to which is belonging the selected COMP instance.
290   * @note   COMP common register instance can be used to
291   *         set parameters common to several COMP instances.
292   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
293   * @param  __COMPx__ COMP instance
294   * @retval COMP common instance or value "0" if there is no COMP common instance.
295   */
296 #define __LL_COMP_COMMON_INSTANCE(__COMPx__)                                   \
297   (COMP12_COMMON)
298 
299 /**
300   * @}
301   */
302 
303 /**
304   * @}
305   */
306 
307 /* Exported functions --------------------------------------------------------*/
308 /** @defgroup COMP_LL_Exported_Functions COMP Exported Functions
309   * @{
310   */
311 
312 /** @defgroup COMP_LL_EF_Configuration_comparator_common Configuration of COMP hierarchical scope: common to several COMP instances
313   * @{
314   */
315 
316 /**
317   * @brief  Set window mode of a pair of comparators instances
318   *         (2 consecutive COMP instances odd and even COMP<x> and COMP<x+1>).
319   * @rmtoll CFGRx    WINMODE        LL_COMP_SetCommonWindowMode
320   * @param  COMPxy_COMMON Comparator common instance
321   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
322   * @param  WindowMode This parameter can be one of the following values:
323   *         @arg @ref LL_COMP_WINDOWMODE_DISABLE
324   *         @arg @ref LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON
325   * @retval None
326   */
LL_COMP_SetCommonWindowMode(COMP_Common_TypeDef * COMPxy_COMMON,uint32_t WindowMode)327 __STATIC_INLINE void LL_COMP_SetCommonWindowMode(COMP_Common_TypeDef *COMPxy_COMMON, uint32_t WindowMode)
328 {
329   /* Note: On this STM32 series, window mode can be set only                   */
330   /*       from COMP instance: COMP2.                                         */
331   MODIFY_REG(COMPxy_COMMON->CFGR, COMP_CFGRx_WINMODE, WindowMode);
332 }
333 
334 /**
335   * @brief  Get window mode of a pair of comparators instances
336   *         (2 consecutive COMP instances odd and even COMP<x> and COMP<x+1>).
337   * @rmtoll CFGRx    WINMODE        LL_COMP_GetCommonWindowMode
338   * @param  COMPxy_COMMON Comparator common instance
339   *         (can be set directly from CMSIS definition or by using helper macro @ref __LL_COMP_COMMON_INSTANCE() )
340   * @retval Returned value can be one of the following values:
341   *         @arg @ref LL_COMP_WINDOWMODE_DISABLE
342   *         @arg @ref LL_COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON
343   */
LL_COMP_GetCommonWindowMode(COMP_Common_TypeDef * COMPxy_COMMON)344 __STATIC_INLINE uint32_t LL_COMP_GetCommonWindowMode(COMP_Common_TypeDef *COMPxy_COMMON)
345 {
346   return (uint32_t)(READ_BIT(COMPxy_COMMON->CFGR, COMP_CFGRx_WINMODE));
347 }
348 
349 /**
350   * @}
351   */
352 
353 /** @defgroup COMP_LL_EF_Configuration_comparator_modes Configuration of comparator modes
354   * @{
355   */
356 
357 /**
358   * @brief  Set comparator instance operating mode to adjust power and speed.
359   * @rmtoll CFGRx    PWRMODE        LL_COMP_SetPowerMode
360   * @param  COMPx Comparator instance
361   * @param  PowerMode This parameter can be one of the following values:
362   *         @arg @ref LL_COMP_POWERMODE_HIGHSPEED
363   *         @arg @ref LL_COMP_POWERMODE_MEDIUMSPEED
364   *         @arg @ref LL_COMP_POWERMODE_ULTRALOWPOWER
365   * @retval None
366   */
LL_COMP_SetPowerMode(COMP_TypeDef * COMPx,uint32_t PowerMode)367 __STATIC_INLINE void LL_COMP_SetPowerMode(COMP_TypeDef *COMPx, uint32_t PowerMode)
368 {
369   MODIFY_REG(COMPx->CFGR, COMP_CFGRx_PWRMODE, PowerMode);
370 }
371 
372 /**
373   * @brief  Get comparator instance operating mode to adjust power and speed.
374   * @rmtoll CFGRx    PWRMODE        LL_COMP_GetPowerMode
375   * @param  COMPx Comparator instance
376   * @retval Returned value can be one of the following values:
377   *         @arg @ref LL_COMP_POWERMODE_HIGHSPEED
378   *         @arg @ref LL_COMP_POWERMODE_MEDIUMSPEED
379   *         @arg @ref LL_COMP_POWERMODE_ULTRALOWPOWER
380   */
LL_COMP_GetPowerMode(COMP_TypeDef * COMPx)381 __STATIC_INLINE uint32_t LL_COMP_GetPowerMode(COMP_TypeDef *COMPx)
382 {
383   return (uint32_t)(READ_BIT(COMPx->CFGR, COMP_CFGRx_PWRMODE));
384 }
385 
386 /**
387   * @}
388   */
389 
390 /** @defgroup COMP_LL_EF_Configuration_comparator_inputs Configuration of comparator inputs
391   * @{
392   */
393 
394 /**
395   * @brief  Set comparator inputs minus (inverting) and plus (non-inverting).
396   * @note   In case of comparator input selected to be connected to IO:
397   *         GPIO pins are specific to each comparator instance.
398   *         Refer to description of parameters or to reference manual.
399   * @note   On this STM32 series, scaler bridge is configurable:
400   *         to optimize power consumption, this function enables the
401   *         voltage scaler bridge only when required
402   *         (when selecting comparator input based on VrefInt: VrefInt or
403   *         subdivision of VrefInt).
404   *         - For scaler bridge power consumption values,
405   *           refer to device datasheet, parameter "IDDA(SCALER)".
406   *         - Voltage scaler requires a delay for voltage stabilization.
407   *           Refer to device datasheet, parameter "tSTART_SCALER".
408   *         - Scaler bridge is common for all comparator instances,
409   *           therefore if at least one of the comparator instance
410   *           is requiring the scaler bridge, it remains enabled.
411   * @rmtoll CFGRx      INMSEL         LL_COMP_ConfigInputs\n
412   *         CFGRx      INPSEL         LL_COMP_ConfigInputs\n
413   *         CFGRx      BRGEN          LL_COMP_ConfigInputs\n
414   *         CFGRx      SCALEN         LL_COMP_ConfigInputs
415   * @param  COMPx Comparator instance
416   * @param  InputMinus This parameter can be one of the following values:
417   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
418   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
419   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
420   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
421   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH1
422   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH2
423   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
424   *         @arg @ref LL_COMP_INPUT_MINUS_IO2
425   *         @arg @ref LL_COMP_INPUT_MINUS_TPSENS_DAC2CH1
426   *         @arg @ref LL_COMP_INPUT_MINUS_VBAT_VDDAP
427   * @param  InputPlus This parameter can be one of the following values:
428   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
429   *         @arg @ref LL_COMP_INPUT_PLUS_IO2
430   *         @arg @ref LL_COMP_INPUT_PLUS_DAC2_CH1
431   * @retval None
432   */
LL_COMP_ConfigInputs(COMP_TypeDef * COMPx,uint32_t InputMinus,uint32_t InputPlus)433 __STATIC_INLINE void LL_COMP_ConfigInputs(COMP_TypeDef *COMPx, uint32_t InputMinus, uint32_t InputPlus)
434 {
435 #if defined (COMP_CFGRx_INP2SEL)
436   MODIFY_REG(COMPx->CFGR,
437              COMP_CFGRx_INMSEL | COMP_CFGRx_INPSEL |
438              COMP_CFGRx_INP2SEL |
439              COMP_CFGRx_SCALEN | COMP_CFGRx_BRGEN,
440              InputMinus | InputPlus);
441 #else
442   MODIFY_REG(COMPx->CFGR,
443              COMP_CFGRx_INMSEL | COMP_CFGRx_INPSEL |
444              COMP_CFGRx_SCALEN | COMP_CFGRx_BRGEN,
445              InputMinus | InputPlus);
446 #endif
447 }
448 
449 /**
450   * @brief  Set comparator input plus (non-inverting).
451   * @note   In case of comparator input selected to be connected to IO:
452   *         GPIO pins are specific to each comparator instance.
453   *         Refer to description of parameters or to reference manual.
454   * @rmtoll CFGRx    INPSEL         LL_COMP_SetInputPlus
455   * @param  COMPx Comparator instance
456   * @param  InputPlus This parameter can be one of the following values:
457   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
458   *         @arg @ref LL_COMP_INPUT_PLUS_IO2
459   *         @arg @ref LL_COMP_INPUT_PLUS_DAC2_CH1
460   * @retval None
461   */
LL_COMP_SetInputPlus(COMP_TypeDef * COMPx,uint32_t InputPlus)462 __STATIC_INLINE void LL_COMP_SetInputPlus(COMP_TypeDef *COMPx, uint32_t InputPlus)
463 {
464 #if defined (COMP_CFGRx_INP2SEL)
465   MODIFY_REG(COMPx->CFGR, COMP_CFGRx_INPSEL | COMP_CFGRx_INP2SEL , InputPlus);
466 #else
467   MODIFY_REG(COMPx->CFGR, COMP_CFGRx_INPSEL , InputPlus);
468 #endif
469 }
470 
471 /**
472   * @brief  Get comparator input plus (non-inverting).
473   * @note   In case of comparator input selected to be connected to IO:
474   *         GPIO pins are specific to each comparator instance.
475   *         Refer to description of parameters or to reference manual.
476   * @rmtoll CFGRx    INPSEL         LL_COMP_GetInputPlus
477   * @param  COMPx Comparator instance
478   * @retval Returned value can be one of the following values:
479   *         @arg @ref LL_COMP_INPUT_PLUS_IO1
480   *         @arg @ref LL_COMP_INPUT_PLUS_IO2
481   *         @arg @ref LL_COMP_INPUT_PLUS_DAC2_CH1
482   */
LL_COMP_GetInputPlus(COMP_TypeDef * COMPx)483 __STATIC_INLINE uint32_t LL_COMP_GetInputPlus(COMP_TypeDef *COMPx)
484 {
485 #if defined (COMP_CFGRx_INP2SEL)
486   return (uint32_t)(READ_BIT(COMPx->CFGR, COMP_CFGRx_INPSEL | COMP_CFGRx_INP2SEL));
487 #else
488   return (uint32_t)(READ_BIT(COMPx->CFGR, COMP_CFGRx_INPSEL));
489 #endif
490 }
491 
492 /**
493   * @brief  Set comparator input minus (inverting).
494   * @note   In case of comparator input selected to be connected to IO:
495   *         GPIO pins are specific to each comparator instance.
496   *         Refer to description of parameters or to reference manual.
497   * @note   On this STM32 series, scaler bridge is configurable:
498   *         to optimize power consumption, this function enables the
499   *         voltage scaler bridge only when required
500   *         (when selecting comparator input based on VrefInt: VrefInt or
501   *         subdivision of VrefInt).
502   *         - For scaler bridge power consumption values,
503   *           refer to device datasheet, parameter "IDDA(SCALER)".
504   *         - Voltage scaler requires a delay for voltage stabilization.
505   *           Refer to device datasheet, parameter "tSTART_SCALER".
506   *         - Scaler bridge is common for all comparator instances,
507   *           therefore if at least one of the comparator instance
508   *           is requiring the scaler bridge, it remains enabled.
509   * @rmtoll CFGRx     INMSEL         LL_COMP_SetInputMinus\n
510   *         CFGRx     BRGEN          LL_COMP_SetInputMinus\n
511   *         CFGRx     SCALEN         LL_COMP_SetInputMinus
512   * @param  COMPx Comparator instance
513   * @param  InputMinus This parameter can be one of the following values:
514   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
515   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
516   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
517   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
518   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH1
519   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH2
520   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
521   *         @arg @ref LL_COMP_INPUT_MINUS_IO2
522   *         @arg @ref LL_COMP_INPUT_MINUS_TPSENS_DAC2CH1
523   *         @arg @ref LL_COMP_INPUT_MINUS_VBAT_VDDAP
524   * @retval None
525   */
LL_COMP_SetInputMinus(COMP_TypeDef * COMPx,uint32_t InputMinus)526 __STATIC_INLINE void LL_COMP_SetInputMinus(COMP_TypeDef *COMPx, uint32_t InputMinus)
527 {
528   MODIFY_REG(COMPx->CFGR, COMP_CFGRx_INMSEL | COMP_CFGRx_SCALEN | COMP_CFGRx_BRGEN, InputMinus);
529 }
530 
531 /**
532   * @brief  Get comparator input minus (inverting).
533   * @note   In case of comparator input selected to be connected to IO:
534   *         GPIO pins are specific to each comparator instance.
535   *         Refer to description of parameters or to reference manual.
536   * @rmtoll CFGRx     INMSEL         LL_COMP_GetInputMinus\n
537   *         CFGRx     BRGEN          LL_COMP_GetInputMinus\n
538   *         CFGRx     SCALEN         LL_COMP_GetInputMinus
539   * @param  COMPx Comparator instance
540   * @retval Returned value can be one of the following values:
541   *         @arg @ref LL_COMP_INPUT_MINUS_1_4VREFINT
542   *         @arg @ref LL_COMP_INPUT_MINUS_1_2VREFINT
543   *         @arg @ref LL_COMP_INPUT_MINUS_3_4VREFINT
544   *         @arg @ref LL_COMP_INPUT_MINUS_VREFINT
545   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH1
546   *         @arg @ref LL_COMP_INPUT_MINUS_DAC1_CH2
547   *         @arg @ref LL_COMP_INPUT_MINUS_IO1
548   *         @arg @ref LL_COMP_INPUT_MINUS_IO2
549   *         @arg @ref LL_COMP_INPUT_MINUS_TPSENS_DAC2CH1
550   *         @arg @ref LL_COMP_INPUT_MINUS_VBAT_VDDAP
551   */
LL_COMP_GetInputMinus(COMP_TypeDef * COMPx)552 __STATIC_INLINE uint32_t LL_COMP_GetInputMinus(COMP_TypeDef *COMPx)
553 {
554   return (uint32_t)(READ_BIT(COMPx->CFGR, COMP_CFGRx_INMSEL | COMP_CFGRx_SCALEN | COMP_CFGRx_BRGEN));
555 }
556 
557 /**
558   * @brief  Set comparator instance hysteresis mode of the input minus (inverting input).
559   * @rmtoll CFGRx    HYST           LL_COMP_SetInputHysteresis
560   * @param  COMPx Comparator instance
561   * @param  InputHysteresis This parameter can be one of the following values:
562   *         @arg @ref LL_COMP_HYSTERESIS_NONE
563   *         @arg @ref LL_COMP_HYSTERESIS_LOW
564   *         @arg @ref LL_COMP_HYSTERESIS_MEDIUM
565   *         @arg @ref LL_COMP_HYSTERESIS_HIGH
566   * @retval None
567   */
LL_COMP_SetInputHysteresis(COMP_TypeDef * COMPx,uint32_t InputHysteresis)568 __STATIC_INLINE void LL_COMP_SetInputHysteresis(COMP_TypeDef *COMPx, uint32_t InputHysteresis)
569 {
570   MODIFY_REG(COMPx->CFGR, COMP_CFGRx_HYST, InputHysteresis);
571 }
572 
573 /**
574   * @brief  Get comparator instance hysteresis mode of the minus (inverting) input.
575   * @rmtoll CSR      HYST           LL_COMP_GetInputHysteresis
576   * @param  COMPx Comparator instance
577   * @retval Returned value can be one of the following values:
578   *         @arg @ref LL_COMP_HYSTERESIS_NONE
579   *         @arg @ref LL_COMP_HYSTERESIS_LOW
580   *         @arg @ref LL_COMP_HYSTERESIS_MEDIUM
581   *         @arg @ref LL_COMP_HYSTERESIS_HIGH
582   */
LL_COMP_GetInputHysteresis(COMP_TypeDef * COMPx)583 __STATIC_INLINE uint32_t LL_COMP_GetInputHysteresis(COMP_TypeDef *COMPx)
584 {
585   return (uint32_t)(READ_BIT(COMPx->CFGR, COMP_CFGRx_HYST));
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 instance output polarity.
598   * @rmtoll CFGRx    POLARITY       LL_COMP_SetOutputPolarity
599   * @param  COMPx Comparator instance
600   * @param  OutputPolarity This parameter can be one of the following values:
601   *         @arg @ref LL_COMP_OUTPUTPOL_NONINVERTED
602   *         @arg @ref LL_COMP_OUTPUTPOL_INVERTED
603   * @retval None
604   */
LL_COMP_SetOutputPolarity(COMP_TypeDef * COMPx,uint32_t OutputPolarity)605 __STATIC_INLINE void LL_COMP_SetOutputPolarity(COMP_TypeDef *COMPx, uint32_t OutputPolarity)
606 {
607   MODIFY_REG(COMPx->CFGR, COMP_CFGRx_POLARITY, OutputPolarity);
608 }
609 
610 /**
611   * @brief  Get comparator instance output polarity.
612   * @rmtoll CFGRx    POLARITY       LL_COMP_GetOutputPolarity
613   * @param  COMPx Comparator instance
614   * @retval Returned value can be one of the following values:
615   *         @arg @ref LL_COMP_OUTPUTPOL_NONINVERTED
616   *         @arg @ref LL_COMP_OUTPUTPOL_INVERTED
617   */
LL_COMP_GetOutputPolarity(COMP_TypeDef * COMPx)618 __STATIC_INLINE uint32_t LL_COMP_GetOutputPolarity(COMP_TypeDef *COMPx)
619 {
620   return (uint32_t)(READ_BIT(COMPx->CFGR, COMP_CFGRx_POLARITY));
621 }
622 
623 /**
624   * @brief  Set comparator instance blanking source.
625   * @note   Blanking source may be specific to each comparator instance.
626   *         Refer to description of parameters or to reference manual.
627   * @note   Availability of parameters of blanking source from timer
628   *         depends on timers availability on the selected device.
629   * @rmtoll CFGR     BLANKING       LL_COMP_SetOutputBlankingSource
630   * @param  COMPx Comparator instance
631   * @param  BlankingSource This parameter can be one of the following values:
632   *         @arg @ref LL_COMP_BLANKINGSRC_NONE
633   *         @arg @ref LL_COMP_BLANKINGSRC_TIM1_OC5
634   *         @arg @ref LL_COMP_BLANKINGSRC_TIM2_OC3
635   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC3
636   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC4
637   *         @arg @ref LL_COMP_BLANKINGSRC_TIM8_OC5
638   *         @arg @ref LL_COMP_BLANKINGSRC_TIM15_OC1
639   * @retval None
640   */
LL_COMP_SetOutputBlankingSource(COMP_TypeDef * COMPx,uint32_t BlankingSource)641 __STATIC_INLINE void LL_COMP_SetOutputBlankingSource(COMP_TypeDef *COMPx, uint32_t BlankingSource)
642 {
643   MODIFY_REG(COMPx->CFGR, COMP_CFGRx_BLANKING, BlankingSource);
644 }
645 
646 /**
647   * @brief  Get comparator instance blanking source.
648   * @note   Availability of parameters of blanking source from timer
649   *         depends on timers availability on the selected device.
650   * @note   Blanking source may be specific to each comparator instance.
651   *         Refer to description of parameters or to reference manual.
652   * @rmtoll CFGR     BLANKING       LL_COMP_GetOutputBlankingSource
653   * @param  COMPx Comparator instance
654   * @retval Returned value can be one of the following values:
655   *         @arg @ref LL_COMP_BLANKINGSRC_NONE
656   *         @arg @ref LL_COMP_BLANKINGSRC_TIM1_OC5
657   *         @arg @ref LL_COMP_BLANKINGSRC_TIM2_OC3
658   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC3
659   *         @arg @ref LL_COMP_BLANKINGSRC_TIM3_OC4
660   *         @arg @ref LL_COMP_BLANKINGSRC_TIM8_OC5
661   *         @arg @ref LL_COMP_BLANKINGSRC_TIM15_OC1
662   */
LL_COMP_GetOutputBlankingSource(COMP_TypeDef * COMPx)663 __STATIC_INLINE uint32_t LL_COMP_GetOutputBlankingSource(COMP_TypeDef *COMPx)
664 {
665   return (uint32_t)(READ_BIT(COMPx->CFGR, COMP_CFGRx_BLANKING));
666 }
667 
668 /** @brief  Set the output alternate function in the Option register
669   *         in order to be used with the alternate function
670   *         of the timer break input.
671   * @rmtoll OR    AFOP       COMP_LL_EC_OUTPUT_BKIN_TIMER
672   * @param  COMPx specifies the instance.
673   * @param  CompAFx specifies the Alternate Function source selection.
674   *          This parameter can be one of the following values:
675   *            @arg @ref LL_COMP_AF_PA6
676   *            @arg @ref LL_COMP_AF_PA8
677   *            @arg @ref LL_COMP_AF_PB12
678   *            @arg @ref LL_COMP_AF_PE6
679   *            @arg @ref LL_COMP_AF_PE15
680   *            @arg @ref LL_COMP_AF_PG2
681   *            @arg @ref LL_COMP_AF_PG3
682   *            @arg @ref LL_COMP_AF_PG4
683   *            @arg @ref LL_COMP_AF_PI1
684   *            @arg @ref LL_COMP_AF_PI4
685   *            @arg @ref LL_COMP_AF_PK2
686   * @retval None
687   */
LL_COMP_SetOutputAlternateFunction(COMP_TypeDef * COMPx,uint32_t CompAFx)688 __STATIC_INLINE void LL_COMP_SetOutputAlternateFunction(COMP_TypeDef *COMPx, uint32_t CompAFx)
689 {
690   MODIFY_REG(COMP12->OR, 0x7FFUL, (COMPx == COMP1) ? ((~CompAFx) & 0x7FFUL) : CompAFx);
691 }
692 
693 /** @brief  Get the output alternate function from the Option register.
694   * @rmtoll OR     AFOP       COMP_LL_EC_OUTPUT_BKIN_TIMER
695   * @param  COMPx specifies the Comparator instance.
696   * @retval Returned value can be one of the following values:
697   *            @arg @ref LL_COMP_AF_PA6
698   *            @arg @ref LL_COMP_AF_PA8
699   *            @arg @ref LL_COMP_AF_PB12
700   *            @arg @ref LL_COMP_AF_PE6
701   *            @arg @ref LL_COMP_AF_PE15
702   *            @arg @ref LL_COMP_AF_PG2
703   *            @arg @ref LL_COMP_AF_PG3
704   *            @arg @ref LL_COMP_AF_PG4
705   *            @arg @ref LL_COMP_AF_PI1
706   *            @arg @ref LL_COMP_AF_PI4
707   *            @arg @ref LL_COMP_AF_PK2
708   */
LL_COMP_GetOutputAlternateFunction(COMP_TypeDef * COMPx)709 __STATIC_INLINE uint32_t LL_COMP_GetOutputAlternateFunction(COMP_TypeDef *COMPx )
710 {
711   return (uint32_t) ((COMPx == COMP1) ? ((~COMP12->OR) & 0x7FFUL) : (COMP12->OR &  0x7FFUL));
712 }
713 
714 /**
715   * @}
716   */
717 
718 /** @defgroup COMP_LL_EF_Operation Operation on comparator instance
719   * @{
720   */
721 
722 /**
723   * @brief  Enable comparator instance.
724   * @note   After enable from off state, comparator requires a delay
725   *         to reach reach propagation delay specification.
726   *         Refer to device datasheet, parameter "tSTART".
727   * @rmtoll CFGR     EN             LL_COMP_Enable
728   * @param  COMPx Comparator instance
729   * @retval None
730   */
LL_COMP_Enable(COMP_TypeDef * COMPx)731 __STATIC_INLINE void LL_COMP_Enable(COMP_TypeDef *COMPx)
732 {
733   SET_BIT(COMPx->CFGR, COMP_CFGRx_EN);
734 }
735 
736 /**
737   * @brief  Disable comparator instance.
738   * @rmtoll CFGR     EN             LL_COMP_Disable
739   * @param  COMPx Comparator instance
740   * @retval None
741   */
LL_COMP_Disable(COMP_TypeDef * COMPx)742 __STATIC_INLINE void LL_COMP_Disable(COMP_TypeDef *COMPx)
743 {
744   CLEAR_BIT(COMPx->CFGR, COMP_CFGRx_EN);
745 }
746 
747 /**
748   * @brief  Get comparator enable state
749   *         (0: COMP is disabled, 1: COMP is enabled)
750   * @rmtoll CFGR     EN             LL_COMP_IsEnabled
751   * @param  COMPx Comparator instance
752   * @retval State of bit (1 or 0).
753   */
LL_COMP_IsEnabled(COMP_TypeDef * COMPx)754 __STATIC_INLINE uint32_t LL_COMP_IsEnabled(COMP_TypeDef *COMPx)
755 {
756   return ((READ_BIT(COMPx->CFGR, COMP_CFGRx_EN) == (COMP_CFGRx_EN)) ? 1UL : 0UL);
757 }
758 
759 /**
760   * @brief  Lock comparator instance.
761   * @note   Once locked, comparator configuration can be accessed in read-only.
762   * @note   The only way to unlock the comparator is a device hardware reset.
763   * @rmtoll CFGR     LOCK           LL_COMP_Lock
764   * @param  COMPx Comparator instance
765   * @retval None
766   */
LL_COMP_Lock(COMP_TypeDef * COMPx)767 __STATIC_INLINE void LL_COMP_Lock(COMP_TypeDef *COMPx)
768 {
769   SET_BIT(COMPx->CFGR, COMP_CFGRx_LOCK);
770 }
771 
772 /**
773   * @brief  Get comparator lock state
774   *         (0: COMP is unlocked, 1: COMP is locked).
775   * @note   Once locked, comparator configuration can be accessed in read-only.
776   * @note   The only way to unlock the comparator is a device hardware reset.
777   * @rmtoll CFGR     LOCK           LL_COMP_IsLocked
778   * @param  COMPx Comparator instance
779   * @retval State of bit (1 or 0).
780   */
LL_COMP_IsLocked(COMP_TypeDef * COMPx)781 __STATIC_INLINE uint32_t LL_COMP_IsLocked(COMP_TypeDef *COMPx)
782 {
783   return ((READ_BIT(COMPx->CFGR, COMP_CFGRx_LOCK) == (COMP_CFGRx_LOCK)) ? 1UL : 0UL);
784 }
785 
786 /**
787   * @brief  Read comparator instance output level.
788   * @note   The comparator output level depends on the selected polarity
789   *         (Refer to function @ref LL_COMP_SetOutputPolarity()).
790   *         If the comparator polarity is not inverted:
791   *          - Comparator output is low when the input plus
792   *            is at a lower voltage than the input minus
793   *          - Comparator output is high when the input plus
794   *            is at a higher voltage than the input minus
795   *         If the comparator polarity is inverted:
796   *          - Comparator output is high when the input plus
797   *            is at a lower voltage than the input minus
798   *          - Comparator output is low when the input plus
799   *            is at a higher voltage than the input minus
800   * @rmtoll CFGR     VALUE          LL_COMP_ReadOutputLevel
801   * @param  COMPx Comparator instance
802   * @retval Returned value can be one of the following values:
803   *         @arg @ref LL_COMP_OUTPUT_LEVEL_LOW
804   *         @arg @ref LL_COMP_OUTPUT_LEVEL_HIGH
805   */
LL_COMP_ReadOutputLevel(COMP_TypeDef * COMPx)806 __STATIC_INLINE uint32_t LL_COMP_ReadOutputLevel(COMP_TypeDef *COMPx)
807 {
808   if (COMPx == COMP1)
809   {
810     return (uint32_t)(READ_BIT(COMP12->SR, COMP_SR_C1VAL));
811   }
812   else
813   {
814     return (uint32_t)((READ_BIT(COMP12->SR, COMP_SR_C2VAL))>> 1);
815   }
816 }
817 
818 /**
819   * @}
820   */
821 
822 #if defined(USE_FULL_LL_DRIVER)
823 /** @defgroup COMP_LL_EF_Init Initialization and de-initialization functions
824   * @{
825   */
826 
827 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx);
828 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct);
829 void        LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct);
830 
831 /**
832   * @}
833   */
834 #endif /* USE_FULL_LL_DRIVER */
835 
836 /**
837   * @}
838   */
839 
840 /**
841   * @}
842   */
843 
844 #endif /* COMP1 || COMP2 */
845 
846 /**
847   * @}
848   */
849 
850 #ifdef __cplusplus
851 }
852 #endif
853 
854 #endif /* __STM32H7xx_LL_COMP_H */
855 
856