STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_ll_comp.c
Go to the documentation of this file.
1 
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32l4xx_ll_comp.h"
23 
24 #ifdef USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif
29 
34 #if defined (COMP1) || defined (COMP2)
35 
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
44 
49 /* Check of parameters for configuration of COMP hierarchical scope: */
50 /* COMP instance. */
51 
52 #define IS_LL_COMP_POWER_MODE(__POWER_MODE__) \
53  ( ((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED) \
54  || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \
55  || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER) \
56  )
57 
58 /* Note: On this STM32 serie, comparator input plus parameters are */
59 /* the same on all COMP instances. */
60 /* However, comparator instance kept as macro parameter for */
61 /* compatibility with other STM32 families. */
62 #if defined(COMP_CSR_INPSEL_1)
63 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
64  ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
65  || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
66  || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \
67  )
68 #else
69 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
70  ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
71  || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
72  )
73 #endif
74 
75 /* Note: On this STM32 serie, comparator input minus parameters are */
76 /* the same on all COMP instances. */
77 /* However, comparator instance kept as macro parameter for */
78 /* compatibility with other STM32 families. */
79 #if defined(COMP_CSR_INMESEL_1)
80 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
81  ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
82  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
83  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
84  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
85  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
86  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
87  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
88  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
89  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3) \
90  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO4) \
91  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO5) \
92  )
93 #else
94 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
95  ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
96  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
97  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
98  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
99  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
100  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
101  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
102  || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
103  )
104 #endif
105 
106 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
107  ( ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \
108  || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW) \
109  || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM) \
110  || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH) \
111  )
112 
113 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
114  ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
115  || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
116  )
117 
118 #if defined(COMP2)
119 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
120  (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
121  ? ( \
122  (1UL) \
123  ) \
124  : \
125  (((__COMP_INSTANCE__) == COMP1) \
126  ? ( \
127  ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
128  || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
129  || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) \
130  ) \
131  : \
132  ( \
133  ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2) \
134  || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2) \
135  || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1_COMP2) \
136  ) \
137  ) \
138  )
139 #else
140 #if defined(TIM3)
141 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
142  (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
143  ? ( \
144  (1UL) \
145  ) \
146  : \
147  ( \
148  ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
149  || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
150  || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) \
151  ) \
152  )
153 #else
154 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
155  (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
156  ? ( \
157  (1UL) \
158  ) \
159  : \
160  ( \
161  ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
162  || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
163  ) \
164  )
165 #endif /* TIM3 */
166 #endif /* COMP2 */
167 
172 /* Private function prototypes -----------------------------------------------*/
173 
174 /* Exported functions --------------------------------------------------------*/
194 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
195 {
196  ErrorStatus status = SUCCESS;
197 
198  /* Check the parameters */
199  assert_param(IS_COMP_ALL_INSTANCE(COMPx));
200 
201  /* Note: Hardware constraint (refer to description of this function): */
202  /* COMP instance must not be locked. */
203  if (LL_COMP_IsLocked(COMPx) == 0UL)
204  {
205  LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL);
206 
207  }
208  else
209  {
210  /* Comparator instance is locked: de-initialization by software is */
211  /* not possible. */
212  /* The only way to unlock the comparator is a device hardware reset. */
213  status = ERROR;
214  }
215 
216  return status;
217 }
218 
231 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
232 {
233  ErrorStatus status = SUCCESS;
234 
235  /* Check the parameters */
236  assert_param(IS_COMP_ALL_INSTANCE(COMPx));
237  assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
238  assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
239  assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
240  assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
241  assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
242  assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMPx, COMP_InitStruct->OutputBlankingSource));
243 
244  /* Note: Hardware constraint (refer to description of this function) */
245  /* COMP instance must not be locked. */
246  if (LL_COMP_IsLocked(COMPx) == 0UL)
247  {
248  /* Configuration of comparator instance : */
249  /* - PowerMode */
250  /* - InputPlus */
251  /* - InputMinus */
252  /* - InputHysteresis */
253  /* - OutputPolarity */
254  /* - OutputBlankingSource */
255 #if defined(COMP_CSR_INMESEL_1)
256  MODIFY_REG(COMPx->CSR,
257  COMP_CSR_PWRMODE
258  | COMP_CSR_INPSEL
259  | COMP_CSR_SCALEN
260  | COMP_CSR_BRGEN
261  | COMP_CSR_INMESEL
262  | COMP_CSR_INMSEL
263  | COMP_CSR_HYST
264  | COMP_CSR_POLARITY
265  | COMP_CSR_BLANKING
266  ,
267  COMP_InitStruct->PowerMode
268  | COMP_InitStruct->InputPlus
269  | COMP_InitStruct->InputMinus
270  | COMP_InitStruct->InputHysteresis
271  | COMP_InitStruct->OutputPolarity
272  | COMP_InitStruct->OutputBlankingSource
273  );
274 #else
275  MODIFY_REG(COMPx->CSR,
276  COMP_CSR_PWRMODE
277  | COMP_CSR_INPSEL
278  | COMP_CSR_SCALEN
279  | COMP_CSR_BRGEN
280  | COMP_CSR_INMSEL
281  | COMP_CSR_HYST
282  | COMP_CSR_POLARITY
283  | COMP_CSR_BLANKING
284  ,
285  COMP_InitStruct->PowerMode
286  | COMP_InitStruct->InputPlus
287  | COMP_InitStruct->InputMinus
288  | COMP_InitStruct->InputHysteresis
289  | COMP_InitStruct->OutputPolarity
290  | COMP_InitStruct->OutputBlankingSource
291  );
292 #endif
293 
294  }
295  else
296  {
297  /* Initialization error: COMP instance is locked. */
298  status = ERROR;
299  }
300 
301  return status;
302 }
303 
311 {
312  /* Set COMP_InitStruct fields to default values */
313  COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_ULTRALOWPOWER;
314  COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
315  COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
316  COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE;
317  COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
318  COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
319 }
320 
333 #endif /* COMP1 || COMP2 */
334 
339 #endif /* USE_FULL_LL_DRIVER */
340 
341 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
Initialize some features of COMP instance.
Structure definition of some features of COMP instance.
__STATIC_INLINE uint32_t LL_COMP_IsLocked(COMP_TypeDef *COMPx)
Get comparator lock state (0: COMP is unlocked, 1: COMP is locked).
Header file of COMP LL module.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
De-initialize registers of the selected COMP instance to their default reset values.
void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
Set each LL_COMP_InitTypeDef field to default value.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))