2 ******************************************************************************
3 * @file stm32h7xx_ll_comp.c
4 * @author MCD Application Team
5 * @brief COMP LL module driver
6 ******************************************************************************
9 * Copyright (c) 2017 STMicroelectronics.
10 * All rights reserved.
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.
16 ******************************************************************************
18 #if defined(USE_FULL_LL_DRIVER)
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32h7xx_ll_comp.h"
23 #ifdef USE_FULL_ASSERT
24 #include "stm32_assert.h"
26 #define assert_param(expr) ((void)0U)
29 /** @addtogroup STM32H7xx_LL_Driver
33 #if defined (COMP1) || defined (COMP2)
35 /** @addtogroup COMP_LL COMP
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /* Private macros ------------------------------------------------------------*/
44 /** @addtogroup COMP_LL_Private_Macros
48 /* Check of parameters for configuration of COMP hierarchical scope: */
51 #define IS_LL_COMP_POWER_MODE(__POWER_MODE__) \
52 ( ((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED) \
53 || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \
54 || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER) \
57 /* Note: On this STM32 series, comparator input plus parameters are */
58 /* the same on all COMP instances. */
59 /* However, comparator instance kept as macro parameter for */
60 /* compatibility with other STM32 families. */
61 #if defined (COMP_CFGRx_INP2SEL)
62 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
63 ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
64 || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
65 || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_DAC2_CH1))
67 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
68 ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
69 || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2))
72 /* Note: On this STM32 series, comparator input minus parameters are */
73 /* the same on all COMP instances. */
74 /* However, comparator instance kept as macro parameter for */
75 /* compatibility with other STM32 families. */
76 #if defined (COMP_CFGRx_INMSEL_3)
77 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
78 ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
79 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
80 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
81 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
82 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
83 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
84 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
85 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
86 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_TPSENS_DAC2CH1) \
87 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VBAT_VDDAP))
89 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
90 ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
91 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
92 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
93 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
94 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
95 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
96 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
97 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2))
100 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
101 ( ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \
102 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW) \
103 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM) \
104 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH) \
107 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
108 ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
109 || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
112 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__OUTPUT_BLANKING_SOURCE__) \
113 ( ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
114 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5) \
115 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3) \
116 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3) \
117 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4) \
118 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5) \
119 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1) \
127 /* Private function prototypes -----------------------------------------------*/
129 /* Exported functions --------------------------------------------------------*/
130 /** @addtogroup COMP_LL_Exported_Functions
134 /** @addtogroup COMP_LL_EF_Init
139 * @brief De-initialize registers of the selected COMP instance
140 * to their default reset values.
141 * @note If comparator is locked, de-initialization by software is
143 * The only way to unlock the comparator is a device hardware reset.
144 * @param COMPx COMP instance
145 * @retval An ErrorStatus enumeration value:
146 * - SUCCESS: COMP registers are de-initialized
147 * - ERROR: COMP registers are not de-initialized
149 ErrorStatus
LL_COMP_DeInit(COMP_TypeDef
*COMPx
)
151 ErrorStatus status
= SUCCESS
;
153 /* Check the parameters */
154 assert_param(IS_COMP_ALL_INSTANCE(COMPx
));
156 /* Note: Hardware constraint (refer to description of this function): */
157 /* COMP instance must not be locked. */
158 if(LL_COMP_IsLocked(COMPx
) == 0UL)
160 LL_COMP_WriteReg((COMPx
), CFGR
, 0x00000000UL
);
165 /* Comparator instance is locked: de-initialization by software is */
167 /* The only way to unlock the comparator is a device hardware reset. */
175 * @brief Initialize some features of COMP instance.
176 * @note This function configures features of the selected COMP instance.
177 * Some features are also available at scope COMP common instance
178 * (common to several COMP instances).
179 * Refer to functions having argument "COMPxy_COMMON" as parameter.
180 * @param COMPx COMP instance
181 * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
182 * @retval An ErrorStatus enumeration value:
183 * - SUCCESS: COMP registers are initialized
184 * - ERROR: COMP registers are not initialized
186 ErrorStatus
LL_COMP_Init(COMP_TypeDef
*COMPx
, LL_COMP_InitTypeDef
*COMP_InitStruct
)
188 ErrorStatus status
= SUCCESS
;
190 /* Check the parameters */
191 assert_param(IS_COMP_ALL_INSTANCE(COMPx
));
192 assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct
->PowerMode
));
193 assert_param(IS_LL_COMP_INPUT_PLUS(COMPx
, COMP_InitStruct
->InputPlus
));
194 assert_param(IS_LL_COMP_INPUT_MINUS(COMPx
, COMP_InitStruct
->InputMinus
));
195 assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct
->InputHysteresis
));
196 assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct
->OutputPolarity
));
197 assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMP_InitStruct
->OutputBlankingSource
));
199 /* Note: Hardware constraint (refer to description of this function) */
200 /* COMP instance must not be locked. */
201 if(LL_COMP_IsLocked(COMPx
) == 0UL)
203 /* Configuration of comparator instance : */
207 /* - InputHysteresis */
208 /* - OutputPolarity */
209 /* - OutputBlankingSource */
210 #if defined (COMP_CFGRx_INP2SEL)
211 MODIFY_REG(COMPx
->CFGR
,
219 | COMP_CFGRx_POLARITY
220 | COMP_CFGRx_BLANKING
222 COMP_InitStruct
->PowerMode
223 | COMP_InitStruct
->InputPlus
224 | COMP_InitStruct
->InputMinus
225 | COMP_InitStruct
->InputHysteresis
226 | COMP_InitStruct
->OutputPolarity
227 | COMP_InitStruct
->OutputBlankingSource
230 MODIFY_REG(COMPx
->CFGR
,
237 | COMP_CFGRx_POLARITY
238 | COMP_CFGRx_BLANKING
240 COMP_InitStruct
->PowerMode
241 | COMP_InitStruct
->InputPlus
242 | COMP_InitStruct
->InputMinus
243 | COMP_InitStruct
->InputHysteresis
244 | COMP_InitStruct
->OutputPolarity
245 | COMP_InitStruct
->OutputBlankingSource
251 /* Initialization error: COMP instance is locked. */
259 * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
260 * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
261 * whose fields will be set to default values.
264 void LL_COMP_StructInit(LL_COMP_InitTypeDef
*COMP_InitStruct
)
266 /* Set COMP_InitStruct fields to default values */
267 COMP_InitStruct
->PowerMode
= LL_COMP_POWERMODE_ULTRALOWPOWER
;
268 COMP_InitStruct
->InputPlus
= LL_COMP_INPUT_PLUS_IO1
;
269 COMP_InitStruct
->InputMinus
= LL_COMP_INPUT_MINUS_VREFINT
;
270 COMP_InitStruct
->InputHysteresis
= LL_COMP_HYSTERESIS_NONE
;
271 COMP_InitStruct
->OutputPolarity
= LL_COMP_OUTPUTPOL_NONINVERTED
;
272 COMP_InitStruct
->OutputBlankingSource
= LL_COMP_BLANKINGSRC_NONE
;
287 #endif /* COMP1 || COMP2 */
293 #endif /* USE_FULL_LL_DRIVER */