Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32G4 / Drivers / STM32G4xx_HAL_Driver / Src / stm32g4xx_ll_comp.c
blob80d1439420e6fd07070b013cbcfe5da8bcf6917b
1 /**
2 ******************************************************************************
3 * @file stm32g4xx_ll_comp.c
4 * @author MCD Application Team
5 * @brief COMP LL module driver
6 ******************************************************************************
7 * @attention
9 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
10 * All rights reserved.</center></h2>
12 * This software component is licensed by ST under BSD 3-Clause license,
13 * the "License"; You may not use this file except in compliance with the
14 * License. You may obtain a copy of the License at:
15 * opensource.org/licenses/BSD-3-Clause
17 ******************************************************************************
19 #if defined(USE_FULL_LL_DRIVER)
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32g4xx_ll_comp.h"
24 #ifdef USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif
30 /** @addtogroup STM32G4xx_LL_Driver
31 * @{
36 /** @addtogroup COMP_LL COMP
37 * @{
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
45 /** @addtogroup COMP_LL_Private_Macros
46 * @{
49 /* Check of parameters for configuration of COMP hierarchical scope: */
50 /* COMP instance. */
52 /* Note: On this STM32 serie, comparator input plus parameters are */
53 /* the same on all COMP instances. */
54 /* However, comparator instance kept as macro parameter for */
55 /* compatibility with other STM32 families. */
56 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
57 ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
58 || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
60 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
61 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
62 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) || \
63 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) || \
64 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) || \
65 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) || \
66 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) || \
67 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) || \
68 (((__COMP_INSTANCE__) == COMP1) && \
69 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) || \
70 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC3_CH1)) \
71 ) || \
72 (((__COMP_INSTANCE__) == COMP2) && \
73 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) || \
74 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC3_CH2)) \
75 ) || \
76 (((__COMP_INSTANCE__) == COMP3) && \
77 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) || \
78 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC3_CH1)) \
79 ) || \
80 (((__COMP_INSTANCE__) == COMP4) && \
81 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) || \
82 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC3_CH2)) \
83 ) || \
84 (((__COMP_INSTANCE__) == COMP5) && \
85 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) || \
86 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC4_CH1)) \
87 ) || \
88 (((__COMP_INSTANCE__) == COMP6) && \
89 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC2_CH1) || \
90 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC4_CH2)) \
91 ) || \
92 (((__COMP_INSTANCE__) == COMP7) && \
93 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC2_CH1) || \
94 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC4_CH1)) \
96 #elif defined(STM32GBK1CB) || defined(STM32G431xx) || defined(STM32G441xx) || defined(STM32G471xx)
97 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
98 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) || \
99 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) || \
100 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) || \
101 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) || \
102 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) || \
103 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) || \
104 (((__COMP_INSTANCE__) == COMP1) && \
105 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) || \
106 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC3_CH1)) \
107 ) || \
108 (((__COMP_INSTANCE__) == COMP2) && \
109 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) || \
110 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC3_CH2)) \
111 ) || \
112 (((__COMP_INSTANCE__) == COMP3) && \
113 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) || \
114 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC3_CH1)) \
115 ) || \
116 (((__COMP_INSTANCE__) == COMP4) && \
117 (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) || \
118 ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC3_CH2)) \
120 #endif
122 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
123 ( ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \
124 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_10MV) \
125 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_20MV) \
126 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_30MV) \
127 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_40MV) \
128 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_50MV) \
129 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_60MV) \
130 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_70MV) \
133 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
134 ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
135 || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
138 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
139 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
140 ((((__INSTANCE__) == COMP1) && \
141 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
142 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) || \
143 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) || \
144 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) || \
145 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP1))) \
146 || \
147 (((__INSTANCE__) == COMP2) && \
148 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
149 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP2) || \
150 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP2) || \
151 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP2) || \
152 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2))) \
153 || \
154 (((__INSTANCE__) == COMP3) && \
155 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
156 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP3) || \
157 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC4_COMP3) || \
158 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP3) || \
159 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP3))) \
160 || \
161 (((__INSTANCE__) == COMP4) && \
162 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
163 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP4) || \
164 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4_COMP4) || \
165 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP4) || \
166 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1_COMP4))) \
167 || \
168 (((__INSTANCE__) == COMP5) && \
169 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
170 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP5) || \
171 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP5) || \
172 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP5) || \
173 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP5))) \
174 || \
175 (((__INSTANCE__) == COMP6) && \
176 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
177 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP6) || \
178 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC4_COMP6) || \
179 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP6) || \
180 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC2_COMP6))) \
181 || \
182 (((__INSTANCE__) == COMP7) && \
183 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
184 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP7) || \
185 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP7) || \
186 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP7) || \
187 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC2_COMP7))) \
188 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM20_OC5) \
189 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1) \
190 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM4_OC3) \
192 #elif defined(STM32GBK1CB) || defined(STM32G431xx) || defined(STM32G441xx) || defined(STM32G471xx)
193 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
194 ((((__INSTANCE__) == COMP1) && \
195 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
196 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) || \
197 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) || \
198 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) || \
199 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP1))) \
200 || \
201 (((__INSTANCE__) == COMP2) && \
202 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
203 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP2) || \
204 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP2) || \
205 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP2) || \
206 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2))) \
207 || \
208 (((__INSTANCE__) == COMP3) && \
209 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
210 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP3) || \
211 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC4_COMP3) || \
212 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP3) || \
213 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP3))) \
214 || \
215 (((__INSTANCE__) == COMP4) && \
216 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) || \
217 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP4) || \
218 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4_COMP4) || \
219 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP4) || \
220 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1_COMP4))) \
221 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1) \
222 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM4_OC3) \
224 #endif
225 #define IS_LL_COMP_DEGLITCHER_MODE(__INSTANCE__, __DEGLITCHER__) \
226 ( ((__DEGLITCHER__) == LL_COMP_DEGLITCHER_DISABLED) \
227 || ((__DEGLITCHER__) == LL_COMP_DEGLITCHER_ENABLED) \
231 * @}
235 /* Private function prototypes -----------------------------------------------*/
237 /* Exported functions --------------------------------------------------------*/
238 /** @addtogroup COMP_LL_Exported_Functions
239 * @{
242 /** @addtogroup COMP_LL_EF_Init
243 * @{
247 * @brief De-initialize registers of the selected COMP instance
248 * to their default reset values.
249 * @note If comparator is locked, de-initialization by software is
250 * not possible.
251 * The only way to unlock the comparator is a device hardware reset.
252 * @param COMPx COMP instance
253 * @retval An ErrorStatus enumeration value:
254 * - SUCCESS: COMP registers are de-initialized
255 * - ERROR: COMP registers are not de-initialized
257 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
259 ErrorStatus status = SUCCESS;
261 /* Check the parameters */
262 assert_param(IS_COMP_ALL_INSTANCE(COMPx));
264 /* Note: Hardware constraint (refer to description of this function): */
265 /* COMP instance must not be locked. */
266 if (LL_COMP_IsLocked(COMPx) == 0UL)
268 LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL);
271 else
273 /* Comparator instance is locked: de-initialization by software is */
274 /* not possible. */
275 /* The only way to unlock the comparator is a device hardware reset. */
276 status = ERROR;
279 return status;
283 * @brief Initialize some features of COMP instance.
284 * @note This function configures features of the selected COMP instance.
285 * Some features are also available at scope COMP common instance
286 * (common to several COMP instances).
287 * Refer to functions having argument "COMPxy_COMMON" as parameter.
288 * @param COMPx COMP instance
289 * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
290 * @retval An ErrorStatus enumeration value:
291 * - SUCCESS: COMP registers are initialized
292 * - ERROR: COMP registers are not initialized
294 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
296 ErrorStatus status = SUCCESS;
298 /* Check the parameters */
299 assert_param(IS_COMP_ALL_INSTANCE(COMPx));
300 assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
301 assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
302 assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
303 assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
304 assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMPx, COMP_InitStruct->OutputBlankingSource));
305 assert_param(IS_LL_COMP_DEGLITCHER_MODE(COMPx, COMP_InitStruct->DeglitcherMode));
307 /* Note: Hardware constraint (refer to description of this function) */
308 /* COMP instance must not be locked. */
309 if (LL_COMP_IsLocked(COMPx) == 0UL)
311 /* Configuration of comparator instance : */
312 /* - InputPlus */
313 /* - InputMinus */
314 /* - InputHysteresis */
315 /* - OutputPolarity */
316 /* - OutputBlankingSource */
317 /* - DeglitcherMode */
318 MODIFY_REG(COMPx->CSR,
319 COMP_CSR_INPSEL
320 | COMP_CSR_SCALEN
321 | COMP_CSR_BRGEN
322 | COMP_CSR_INMSEL
323 | COMP_CSR_HYST
324 | COMP_CSR_POLARITY
325 | COMP_CSR_BLANKING
326 | COMP_CSR_DEGLITCHEN
328 COMP_InitStruct->InputPlus
329 | COMP_InitStruct->InputMinus
330 | COMP_InitStruct->InputHysteresis
331 | COMP_InitStruct->OutputPolarity
332 | COMP_InitStruct->OutputBlankingSource
333 | COMP_InitStruct->DeglitcherMode
337 else
339 /* Initialization error: COMP instance is locked. */
340 status = ERROR;
343 return status;
347 * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
348 * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
349 * whose fields will be set to default values.
350 * @retval None
352 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
354 /* Set COMP_InitStruct fields to default values */
355 COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
356 COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
357 COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE;
358 COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
359 COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
360 COMP_InitStruct->DeglitcherMode = LL_COMP_DEGLITCHER_DISABLED;
364 * @}
368 * @}
372 * @}
378 * @}
381 #endif /* USE_FULL_LL_DRIVER */
383 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/