Move telemetry displayport init and cms device registering
[betaflight.git] / lib / main / STM32F3 / Drivers / STM32F30x_StdPeriph_Driver / src / stm32f30x_comp.c
blob588b2c403dfef621b16009560ad4c2dfe762cd97
1 /**
2 ******************************************************************************
3 * @file stm32f30x_comp.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 04-April-2014
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the 7 analog comparators (COMP1, COMP2...COMP7) peripheral:
9 * + Comparators configuration
10 * + Window mode control
12 @verbatim
14 ==============================================================================
15 ##### COMP Peripheral features #####
16 ==============================================================================
17 [..]
18 The device integrates 7 analog comparators COMP1, COMP2...COMP7:
19 (#) The non inverting input and inverting input can be set to GPIO pins
20 as shown in table1. COMP Inputs below.
22 (#) The COMP output is internally is available using COMP_GetOutputLevel()
23 and can be set on GPIO pins. Refer to table 2. COMP Outputs below.
25 (#) The COMP output can be redirected to embedded timers (TIM1, TIM2, TIM3...)
26 Refer to table 3. COMP Outputs redirection to embedded timers below.
28 (#) The comparators COMP1 and COMP2, COMP3 and COMP4, COMP5 and COMP6 can be combined in window
29 mode and only COMP1, COMP3 and COMP5 non inverting input can be used as non-inverting input.
31 (#) The seven comparators have interrupt capability with wake-up
32 from Sleep and Stop modes (through the EXTI controller):
33 (++) COMP1 is internally connected to EXTI Line 21
34 (++) COMP2 is internally connected to EXTI Line 22
35 (++) COMP3 is internally connected to EXTI Line 29
36 (++) COMP4 is internally connected to EXTI Line 30
37 (++) COMP5 is internally connected to EXTI Line 31
38 (++) COMP6 is internally connected to EXTI Line 32
39 (++) COMP7 is internally connected to EXTI Line 33
41 [..] Table 1. COMP Inputs
42 +------------------------------------------------------------------------------------------+
43 | | | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
44 |-----------------|----------------|---------------|---------------------------------------|
45 | | 1/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
46 | | 1/2 VREFINT | OK | OK | OK | OK | OK | OK | OK |
47 | | 3/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
48 | Inverting Input | VREFINT | OK | OK | OK | OK | OK | OK | OK |
49 | | DAC1 OUT1(PA4) | OK | OK | OK | OK | OK | OK | OK |
50 | | DAC1 OUT2(PA5) | OK | OK | OK | OK | OK | OK | OK |
51 | | IO1 | PA0 | PA2 | PD15 | PE8 | PD13 | PD10 | PC0 |
52 | | IO2 | --- | --- | PB12 | PB2 | PB10 | PB15 | --- |
53 | | DAC2 OUT1(PA6) | --- | OK | --- | OK | --- | OK | --- |
54 |-----------------|----------------|-------|-------|-------|-------|-------|-------|-------|
55 | Non Inverting | IO1 | PA1 | PA7 | PB14 | PB0 | PD12 | PD11 | PA0 |
56 | Input | IO2 | --- | PA3 | PD14 | PE7 | PB13 | PB11 | PC1 |
57 +------------------------------------------------------------------------------------------+
59 [..] Table 2. COMP Outputs
60 +-------------------------------------------------------+
61 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
62 |-------|-------|-------|-------|-------|-------|-------|
63 | PA0 | PA2 | PB1 | PC8 | PC7 | PA10 | PC2 |
64 | PF4 | PA7 | --- | PA8 | PA9 | PC6 | --- |
65 | PA6 | PA12 | --- | --- | --- | --- | --- |
66 | PA11 | PB9 | --- | --- | --- | --- | --- |
67 | PB8 | --- | --- | --- | --- | --- | --- |
68 +-------------------------------------------------------+
70 [..] Table 3. COMP Outputs redirection to embedded timers
71 +----------------------------------------------------------------------------------------------------------------------+
72 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
73 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
74 | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN |
75 | | | | | | | |
76 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
77 | | | | | | | |
78 | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN |
79 | | | | | | | |
80 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
81 | | | | | | | |
82 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
83 | + | + | + | + | + | + | + |
84 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
85 | | | | | | | |
86 | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
87 | | | | | | | |
88 | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
89 | | | | | | | |
90 | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
91 | | | | | | | |
92 | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
93 | | | | | | | |
94 | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
95 | | | | | | | |
96 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
97 +----------------------------------------------------------------------------------------------------------------------+
99 [..] Table 4. COMP Outputs blanking sources
100 +----------------------------------------------------------------------------------------------------------------------+
101 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
102 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
103 | TIM1 OC5 | TIM1 OC5 | TIM1 OC5 | TIM3 OC4 | TIM3 OC3 | TIM2 OC4 | TIM1 OC5 |
104 | | | | | | | |
105 | TIM2 OC3 | TIM2 OC3 | -------- | TIM8 OC5 | TIM8 OC5 | TIM8 OC5 | TIM8 OC5 |
106 | | | | | | | |
107 | TIM3 OC3 | TIM3 OC3 | TIM2 OC4 | TIM15 OC1 | TIM8 BKIN | TIM15 OC2 | TIM15 OC2 |
108 | | | | | | | |
109 +----------------------------------------------------------------------------------------------------------------------+
112 ##### How to use this driver #####
113 ==============================================================================
114 [..]
115 This driver provides functions to configure and program the Comparators
116 of all STM32F30x devices.
118 To use the comparator, perform the following steps:
120 (#) Enable the SYSCFG APB clock to get write access to comparator
121 register using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
123 (#) Configure the comparator input in analog mode using GPIO_Init()
125 (#) Configure the comparator output in alternate function mode
126 using GPIO_Init() and use GPIO_PinAFConfig() function to map the
127 comparator output to the GPIO pin
129 (#) Configure the comparator using COMP_Init() function:
130 (++) Select the inverting input
131 (++) Select the non-inverting input
132 (++) Select the output polarity
133 (++) Select the output redirection
134 (++) Select the hysteresis level
135 (++) Select the power mode
137 (#) Enable the comparator using COMP_Cmd() function
139 (#) If required enable the COMP interrupt by configuring and enabling
140 EXTI line in Interrupt mode and selecting the desired sensitivity
141 level using EXTI_Init() function. After that enable the comparator
142 interrupt vector using NVIC_Init() function.
144 @endverbatim
146 ******************************************************************************
147 * @attention
149 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
151 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
152 * You may not use this file except in compliance with the License.
153 * You may obtain a copy of the License at:
155 * http://www.st.com/software_license_agreement_liberty_v2
157 * Unless required by applicable law or agreed to in writing, software
158 * distributed under the License is distributed on an "AS IS" BASIS,
159 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
160 * See the License for the specific language governing permissions and
161 * limitations under the License.
163 ******************************************************************************
166 /* Includes ------------------------------------------------------------------*/
167 #include "stm32f30x_comp.h"
169 /** @addtogroup STM32F30x_StdPeriph_Driver
170 * @{
173 /** @defgroup COMP
174 * @brief COMP driver modules
175 * @{
178 /* Private typedef -----------------------------------------------------------*/
179 /* Private define ------------------------------------------------------------*/
180 /* CSR register Mask */
181 #define COMP_CSR_CLEAR_MASK ((uint32_t)0x00000003)
183 /* Private macro -------------------------------------------------------------*/
184 /* Private variables ---------------------------------------------------------*/
185 /* Private function prototypes -----------------------------------------------*/
186 /* Private functions ---------------------------------------------------------*/
188 /** @defgroup COMP_Private_Functions
189 * @{
192 /** @defgroup COMP_Group1 Initialization and Configuration functions
193 * @brief Initialization and Configuration functions
195 @verbatim
196 ===============================================================================
197 ##### Initialization and Configuration functions #####
198 ===============================================================================
200 @endverbatim
201 * @{
205 * @brief Deinitializes COMP peripheral registers to their default reset values.
206 * @note Deinitialization can't be performed if the COMP configuration is locked.
207 * To unlock the configuration, perform a system reset.
208 * @param COMP_Selection: the selected comparator.
209 * This parameter can be COMP_Selection_COMPx where x can be 1 to 7
210 * to select the COMP peripheral.
211 * @param None
212 * @retval None
214 void COMP_DeInit(uint32_t COMP_Selection)
216 /*!< Set COMP_CSR register to reset value */
217 *(__IO uint32_t *) (COMP_BASE + COMP_Selection) = ((uint32_t)0x00000000);
221 * @brief Initializes the COMP peripheral according to the specified parameters
222 * in COMP_InitStruct
223 * @note If the selected comparator is locked, initialization can't be performed.
224 * To unlock the configuration, perform a system reset.
225 * @note By default, PA1 is selected as COMP1 non inverting input.
226 * To use PA4 as COMP1 non inverting input call COMP_SwitchCmd() after COMP_Init()
227 * @param COMP_Selection: the selected comparator.
228 * This parameter can be COMP_Selection_COMPx where x can be 1 to 7
229 * to select the COMP peripheral.
230 * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure that contains
231 * the configuration information for the specified COMP peripheral.
232 * - COMP_InvertingInput specifies the inverting input of COMP
233 * - COMP_NonInvertingInput specifies the non inverting input of COMP
234 * - COMP_Output connect COMP output to selected timer
235 * input (Input capture / Output Compare Reference Clear / Break Input)
236 * - COMP_BlankingSrce specifies the blanking source of COMP
237 * - COMP_OutputPol select output polarity
238 * - COMP_Hysteresis configures COMP hysteresis value
239 * - COMP_Mode configures COMP power mode
240 * @retval None
242 void COMP_Init(uint32_t COMP_Selection, COMP_InitTypeDef* COMP_InitStruct)
244 uint32_t tmpreg = 0;
246 /* Check the parameters */
247 assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
248 assert_param(IS_COMP_INVERTING_INPUT(COMP_InitStruct->COMP_InvertingInput));
249 assert_param(IS_COMP_NONINVERTING_INPUT(COMP_InitStruct->COMP_NonInvertingInput));
250 assert_param(IS_COMP_OUTPUT(COMP_InitStruct->COMP_Output));
251 assert_param(IS_COMP_BLANKING_SOURCE(COMP_InitStruct->COMP_BlankingSrce));
252 assert_param(IS_COMP_OUTPUT_POL(COMP_InitStruct->COMP_OutputPol));
253 assert_param(IS_COMP_HYSTERESIS(COMP_InitStruct->COMP_Hysteresis));
254 assert_param(IS_COMP_MODE(COMP_InitStruct->COMP_Mode));
256 /*!< Get the COMPx_CSR register value */
257 tmpreg = *(__IO uint32_t *) (COMP_BASE + COMP_Selection);
259 /*!< Clear the COMP1SW1, COMPxINSEL, COMPxOUTSEL, COMPxPOL, COMPxHYST and COMPxMODE bits */
260 tmpreg &= (uint32_t) (COMP_CSR_CLEAR_MASK);
262 /*!< Configure COMP: inverting input, output redirection, hysteresis value and power mode */
263 /*!< Set COMPxINSEL bits according to COMP_InitStruct->COMP_InvertingInput value */
264 /*!< Set COMPxNONINSEL bits according to COMP_InitStruct->COMP_NonInvertingInput value */
265 /*!< Set COMPxBLANKING bits according to COMP_InitStruct->COMP_BlankingSrce value */
266 /*!< Set COMPxOUTSEL bits according to COMP_InitStruct->COMP_Output value */
267 /*!< Set COMPxPOL bit according to COMP_InitStruct->COMP_OutputPol value */
268 /*!< Set COMPxHYST bits according to COMP_InitStruct->COMP_Hysteresis value */
269 /*!< Set COMPxMODE bits according to COMP_InitStruct->COMP_Mode value */
270 tmpreg |= (uint32_t)(COMP_InitStruct->COMP_InvertingInput | COMP_InitStruct->COMP_NonInvertingInput |
271 COMP_InitStruct->COMP_Output | COMP_InitStruct->COMP_OutputPol | COMP_InitStruct->COMP_BlankingSrce |
272 COMP_InitStruct->COMP_Hysteresis | COMP_InitStruct->COMP_Mode);
274 /*!< Write to COMPx_CSR register */
275 *(__IO uint32_t *) (COMP_BASE + COMP_Selection) = tmpreg;
279 * @brief Fills each COMP_InitStruct member with its default value.
280 * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure which will
281 * be initialized.
282 * @retval None
284 void COMP_StructInit(COMP_InitTypeDef* COMP_InitStruct)
286 COMP_InitStruct->COMP_InvertingInput = COMP_InvertingInput_1_4VREFINT;
287 COMP_InitStruct->COMP_NonInvertingInput = COMP_NonInvertingInput_IO1;
288 COMP_InitStruct->COMP_Output = COMP_Output_None;
289 COMP_InitStruct->COMP_BlankingSrce = COMP_BlankingSrce_None;
290 COMP_InitStruct->COMP_OutputPol = COMP_OutputPol_NonInverted;
291 COMP_InitStruct->COMP_Hysteresis = COMP_Hysteresis_No;
292 COMP_InitStruct->COMP_Mode = COMP_Mode_UltraLowPower;
296 * @brief Enable or disable the COMP peripheral.
297 * @note If the selected comparator is locked, enable/disable can't be performed.
298 * To unlock the configuration, perform a system reset.
299 * @param COMP_Selection: the selected comparator.
300 * This parameter can be COMP_Selection_COMPx where x can be 1 to 7
301 * to select the COMP peripheral.
302 * @param NewState: new state of the COMP peripheral.
303 * This parameter can be: ENABLE or DISABLE.
304 * When enabled, the comparator compares the non inverting input with
305 * the inverting input and the comparison result is available
306 * on comparator output.
307 * When disabled, the comparator doesn't perform comparison and the
308 * output level is low.
309 * @retval None
311 void COMP_Cmd(uint32_t COMP_Selection, FunctionalState NewState)
313 /* Check the parameters */
314 assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
315 assert_param(IS_FUNCTIONAL_STATE(NewState));
317 if (NewState != DISABLE)
319 /* Enable the selected COMPx peripheral */
320 *(__IO uint32_t *) (COMP_BASE + COMP_Selection) |= (uint32_t) (COMP_CSR_COMPxEN);
322 else
324 /* Disable the selected COMP peripheral */
325 *(__IO uint32_t *) (COMP_BASE + COMP_Selection) &= (uint32_t)(~COMP_CSR_COMPxEN);
330 * @brief Close or Open the SW1 switch.
331 * @note If the COMP1 is locked, Close/Open the SW1 switch can't be performed.
332 * To unlock the configuration, perform a system reset.
333 * @note This switch is solely intended to redirect signals onto high
334 * impedance input, such as COMP1 non-inverting input (highly resistive switch)
335 * @param NewState: New state of the analog switch.
336 * This parameter can be
337 * ENABLE so the SW1 is closed; PA1 is connected to PA4
338 * or DISABLE so the SW1 switch is open; PA1 is disconnected from PA4
339 * @retval None
341 void COMP_SwitchCmd(uint32_t COMP_Selection, FunctionalState NewState)
343 /* Check the parameter */
344 assert_param(IS_FUNCTIONAL_STATE(NewState));
346 if (NewState != DISABLE)
348 /* Close SW1 switch */
349 *(__IO uint32_t *) (COMP_BASE + COMP_Selection) |= (uint32_t) (COMP_CSR_COMP1SW1);
351 else
353 /* Open SW1 switch */
354 *(__IO uint32_t *) (COMP_BASE + COMP_Selection) &= (uint32_t)(~COMP_CSR_COMP1SW1);
359 * @brief Return the output level (high or low) of the selected comparator.
360 * The output level depends on the selected polarity.
361 * If the polarity is not inverted:
362 * - Comparator output is low when the non-inverting input is at a lower
363 * voltage than the inverting input
364 * - Comparator output is high when the non-inverting input is at a higher
365 * voltage than the inverting input
366 * If the polarity is inverted:
367 * - Comparator output is high when the non-inverting input is at a lower
368 * voltage than the inverting input
369 * - Comparator output is low when the non-inverting input is at a higher
370 * voltage than the inverting input
371 * @param COMP_Selection: the selected comparator.
372 * This parameter can be COMP_Selection_COMPx where x can be 1 to 7
373 * to select the COMP peripheral.
374 * @retval Returns the selected comparator output level: low or high.
377 uint32_t COMP_GetOutputLevel(uint32_t COMP_Selection)
379 uint32_t compout = 0x0;
381 /* Check the parameters */
382 assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
384 /* Check if selected comparator output is high */
385 if ((*(__IO uint32_t *) (COMP_BASE + COMP_Selection) & (COMP_CSR_COMPxOUT)) != 0)
387 compout = COMP_OutputLevel_High;
389 else
391 compout = COMP_OutputLevel_Low;
394 /* Return the comparator output level */
395 return (uint32_t)(compout);
399 * @}
402 /** @defgroup COMP_Group2 Window mode control function
403 * @brief Window mode control function
405 @verbatim
406 ===============================================================================
407 ##### Window mode control function #####
408 ===============================================================================
410 @endverbatim
411 * @{
415 * @brief Enables or disables the window mode.
416 * Window mode for comparators makes use of two comparators:
417 * COMP1 and COM2, COMP3 and COMP4, COMP5 and COMP6.
418 * In window mode, COMPx and COMPx-1 (where x can be 2, 4 or 6)
419 * non inverting inputs are connected together and only COMPx-1 non
420 * inverting input can be used.
421 * e.g When window mode enabled for COMP4, COMP3 non inverting input (PB14 or PD14)
422 * is to be used.
423 * @note If the COMPx is locked, ENABLE/DISABLE the window mode can't be performed.
424 * To unlock the configuration, perform a system reset.
425 * @param COMP_Selection: the selected comparator.
426 * This parameter can be COMP_Selection_COMPx where x can be 2, 4 or 6
427 * to select the COMP peripheral.
428 * param NewState: new state of the window mode.
429 * This parameter can be ENABLE or DISABLE.
430 * When enbaled, COMPx and COMPx-1 non inverting inputs are connected together.
431 * When disabled, COMPx and COMPx-1 non inverting inputs are disconnected.
432 * @retval None
434 void COMP_WindowCmd(uint32_t COMP_Selection, FunctionalState NewState)
436 /* Check the parameters */
437 assert_param(IS_FUNCTIONAL_STATE(NewState));
438 assert_param(IS_COMP_WINDOW(COMP_Selection));
440 if (NewState != DISABLE)
442 /* Enable the window mode */
443 *(__IO uint32_t *) (COMP_BASE + COMP_Selection) |= (uint32_t) COMP_CSR_COMPxWNDWEN;
445 else
447 /* Disable the window mode */
448 *(__IO uint32_t *) (COMP_BASE + COMP_Selection) &= (uint32_t)(~COMP_CSR_COMPxWNDWEN);
453 * @}
456 /** @defgroup COMP_Group3 COMP configuration locking function
457 * @brief COMP1, COMP2,...COMP7 configuration locking function
458 * COMP1, COMP2,...COMP7 configuration can be locked each separately.
459 * Unlocking is performed by system reset.
461 @verbatim
462 ===============================================================================
463 ##### Configuration Lock function #####
464 ===============================================================================
466 @endverbatim
467 * @{
471 * @brief Lock the selected comparator (COMP1/COMP2) configuration.
472 * @note Locking the configuration means that all control bits are read-only.
473 * To unlock the comparator configuration, perform a system reset.
474 * @param COMP_Selection: the selected comparator.
475 * This parameter can be COMP_Selection_COMPx where x can be 1 to 7
476 * to select the COMP peripheral.
477 * @retval None
479 void COMP_LockConfig(uint32_t COMP_Selection)
481 /* Check the parameter */
482 assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
484 /* Set the lock bit corresponding to selected comparator */
485 *(__IO uint32_t *) (COMP_BASE + COMP_Selection) |= (uint32_t) (COMP_CSR_COMPxLOCK);
489 * @}
493 * @}
497 * @}
501 * @}
504 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/