2 ******************************************************************************
3 * @file stm32f3xx_hal_comp.c
4 * @author MCD Application Team
5 * @brief COMP HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the COMP peripheral:
8 * + Initialization and de-initialization functions
9 * + Start/Stop operation functions in polling mode.
10 * + Start/Stop operation functions in interrupt mode.
11 * + Peripheral Control functions
12 * + Peripheral State functions
15 ================================================================================
16 ##### COMP Peripheral features #####
17 ================================================================================
20 The STM32F3xx device family integrates up to 7 analog comparators COMP1, COMP2...COMP7:
21 (#) The non inverting input and inverting input can be set to GPIO pins.
22 For STM32F3xx devices please refer to the COMP peripheral section in corresponding
25 (#) The COMP output is available using HAL_COMP_GetOutputLevel()
26 and can be set on GPIO pins.
27 For STM32F3xx devices please refer to the COMP peripheral section in corresponding
30 (#) The COMP output can be redirected to embedded timers (TIM1, TIM2, TIM3...).
31 For STM32F3xx devices please refer to the COMP peripheral section in corresponding
34 (#) Each couple of comparators COMP1 and COMP2, COMP3 and COMP4, COMP5 and COMP6 can be combined in window
35 mode and respectively COMP1, COMP3 and COMP5 non inverting input is used as common non-inverting input.
37 (#) The seven comparators have interrupt capability with wake-up
38 from Sleep and Stop modes (through the EXTI controller):
39 (++) COMP1 is internally connected to EXTI Line 21
40 (++) COMP2 is internally connected to EXTI Line 22
41 (++) COMP3 is internally connected to EXTI Line 29
42 (++) COMP4 is internally connected to EXTI Line 30
43 (++) COMP5 is internally connected to EXTI Line 31
44 (++) COMP6 is internally connected to EXTI Line 32
45 (++) COMP7 is internally connected to EXTI Line 33.
47 From the corresponding IRQ handler, the right interrupt source can be retrieved with the
48 adequate macro __HAL_COMP_COMPx_EXTI_GET_FLAG().
51 ##### How to use this driver #####
52 ================================================================================
54 This driver provides functions to configure and program the Comparators of all STM32F3xx devices.
56 To use the comparator, perform the following steps:
58 (#) Fill in the HAL_COMP_MspInit() to
59 (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
60 (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
61 output to the GPIO pin
62 (++) If required enable the COMP interrupt (EXTI line Interrupt): by configuring and enabling EXTI line in Interrupt mode and
63 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
64 interrupt vector using HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() functions.
66 (#) Configure the comparator using HAL_COMP_Init() function:
67 (++) Select the inverting input (input minus)
68 (++) Select the non-inverting input (input plus)
69 (++) Select the output polarity
70 (++) Select the output redirection
71 (++) Select the hysteresis level
72 (++) Select the power mode
73 (++) Select the event/interrupt mode
75 -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order
76 to enable the comparator(s).
78 (#) On-the-fly reconfiguration of comparator(s) may be done by calling again HAL_COMP_Init(
79 function with new input parameter values; HAL_COMP_MspInit() function shall be adapted
80 to support multi configurations.
82 (#) Enable the comparator using HAL_COMP_Start() or HAL_COMP_Start_IT() functions.
84 (#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions
85 to manage comparator outputs (events and output level).
87 (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
90 (#) De-initialize the comparator using HAL_COMP_DeInit() function.
92 (#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function.
93 Only a MCU reset can reset that protection.
96 ******************************************************************************
99 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
101 * Redistribution and use in source and binary forms, with or without modification,
102 * are permitted provided that the following conditions are met:
103 * 1. Redistributions of source code must retain the above copyright notice,
104 * this list of conditions and the following disclaimer.
105 * 2. Redistributions in binary form must reproduce the above copyright notice,
106 * this list of conditions and the following disclaimer in the documentation
107 * and/or other materials provided with the distribution.
108 * 3. Neither the name of STMicroelectronics nor the names of its contributors
109 * may be used to endorse or promote products derived from this software
110 * without specific prior written permission.
112 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
113 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
114 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
115 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
116 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
117 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
118 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
119 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
120 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
121 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
123 ******************************************************************************
129 Table 1. COMP Inputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
130 +------------------------------------------------------------------------------------------+
131 | | | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
132 |-----------------|----------------|---------------|---------------------------------------|
133 | | 1U/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
134 | | 1U/2 VREFINT | OK | OK | OK | OK | OK | OK | OK |
135 | | 3U/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
136 | Inverting Input | VREFINT | OK | OK | OK | OK | OK | OK | OK |
137 | | DAC1 OUT (PA4) | OK | OK | OK | OK | OK | OK | OK |
138 | | DAC2 OUT (PA5) | OK | OK | OK | OK | OK | OK | OK |
139 | | IO1 | PA0 | PA2 | PD15U | PE8 | PD13U | PD10U | PC0 |
140 | | IO2 | --- | --- | PB12U | PB2 | PB10U | PB15U | --- |
141 |-----------------|----------------|-------|-------|-------|-------|-------|-------|-------|
142 | Non Inverting | IO1 | PA1 | PA7 | PB14U | PB0 | PD12U | PD11U | PA0 |
143 | Input | IO2 | --- | PA3 | PD14U | PE7 | PB13U | PB11U | PC1 |
144 +------------------------------------------------------------------------------------------+
146 Table 2. COMP Outputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
147 +-------------------------------------------------------+
148 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
149 |-------|-------|-------|-------|-------|-------|-------|
150 | PA0 | PA2 | PB1 | PC8 | PC7 | PA10U | PC2 |
151 | PF4 | PA7 | --- | PA8 | PA9 | PC6 | --- |
152 | PA6 | PA12U | --- | --- | --- | --- | --- |
153 | PA11U | PB9 | --- | --- | --- | --- | --- |
154 | PB8 | --- | --- | --- | --- | --- | --- |
155 +-------------------------------------------------------+
157 Table 3. COMP Outputs redirection to embedded timers for the STM32F303xB/STM32F303xC devices
158 +----------------------------------------------------------------------------------------------------------------------+
159 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
160 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
161 | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN |
163 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
165 | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN |
167 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
169 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
170 | + | + | + | + | + | + | + |
171 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
173 | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
175 | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
177 | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
179 | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
181 | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
183 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
184 +----------------------------------------------------------------------------------------------------------------------+
186 Table 4. COMP Outputs redirection to embedded timers for the STM32F303xE devices
187 +----------------------------------------------------------------------------------------------------------------------+
188 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
189 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
190 | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1U) | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1U) |
192 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
194 | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1U) | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1U) |
196 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
198 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
199 | + | + | + | + | + | + | + |
200 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
202 | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
204 | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
206 | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
208 | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
210 | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
212 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
214 | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1U)| TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1U)|
216 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
218 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
219 | + | + | + | + | + | + | + |
220 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
221 | + | + | + | + | + | + | + |
222 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
224 +----------------------------------------------------------------------------------------------------------------------+
225 (1U): This connection consists of connecting both GPIO and COMP output to TIM1/8U/20 BRK input through an OR gate, instead
226 of connecting the GPIO to the TIM1/8U/20 BRK input and the COMP output to the TIM1/8U/20 BRK_ACTH input. The aim is to
227 add a digital filter (3 bits) on the COMP output.
229 Table 5. COMP Outputs blanking sources for the STM32F303xB/STM32F303xC/STM32F303xE devices
230 +----------------------------------------------------------------------------------------------------------------------+
231 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
232 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
233 | TIM1 OC5 | TIM1 OC5 | TIM1 OC5 | TIM3 OC4 | -------- | TIM8 OC5 | TIM1 OC5 |
235 | TIM2 OC3 | TIM2 OC3 | -------- | TIM8 OC5 | TIM3 OC3 | TIM2 OC4 | TIM8 OC5 |
237 | TIM3 OC3 | TIM3 OC3 | TIM2 OC4 | TIM15 OC1 | TIM8 OC5 | TIM15 OC2 | TIM15 OC2 |
239 +----------------------------------------------------------------------------------------------------------------------+
243 /* Includes ------------------------------------------------------------------*/
244 #include "stm32f3xx_hal.h"
246 /** @addtogroup STM32F3xx_HAL_Driver
250 /** @defgroup COMP COMP
251 * @brief COMP HAL module driver
255 #ifdef HAL_COMP_MODULE_ENABLED
257 /* Private typedef -----------------------------------------------------------*/
258 /* Private define ------------------------------------------------------------*/
259 /** @defgroup COMP_Private_Constants COMP Private Constants
262 #define COMP_LOCK_DISABLE (0x00000000U)
263 #define COMP_LOCK_ENABLE COMP_CSR_COMPxLOCK
268 /* Private macro -------------------------------------------------------------*/
269 /* Private variables ---------------------------------------------------------*/
270 /* Private function prototypes -----------------------------------------------*/
271 /* Exported functions --------------------------------------------------------*/
273 /** @defgroup COMP_Exported_Functions COMP Exported Functions
277 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
278 * @brief Initialization and de-initialization functions.
281 ===============================================================================
282 ##### Initialization and de-initialization functions #####
283 ===============================================================================
284 [..] This section provides functions to initialize and de-initialize comparators.
291 * @brief Initialize the COMP peripheral according to the specified
292 * parameters in the COMP_InitTypeDef and initialize the associated handle.
293 * @note If the selected comparator is locked, initialization cannot be performed.
294 * To unlock the configuration, perform a system reset.
295 * @param hcomp COMP handle
298 HAL_StatusTypeDef
HAL_COMP_Init(COMP_HandleTypeDef
*hcomp
)
300 HAL_StatusTypeDef status
= HAL_OK
;
302 /* Check the COMP handle allocation and lock status */
303 if((hcomp
== NULL
) || ((hcomp
->State
& COMP_STATE_BIT_LOCK
) != RESET
))
309 /* Check the parameters */
310 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
311 assert_param(IS_COMP_INVERTINGINPUT(hcomp
->Init
.InvertingInput
));
312 assert_param(IS_COMP_NONINVERTINGINPUT(hcomp
->Init
.NonInvertingInput
));
313 assert_param(IS_COMP_NONINVERTINGINPUT_INSTANCE(hcomp
->Instance
, hcomp
->Init
.NonInvertingInput
));
314 assert_param(IS_COMP_OUTPUT(hcomp
->Init
.Output
));
315 assert_param(IS_COMP_OUTPUT_INSTANCE(hcomp
->Instance
, hcomp
->Init
.Output
));
316 assert_param(IS_COMP_OUTPUTPOL(hcomp
->Init
.OutputPol
));
317 assert_param(IS_COMP_HYSTERESIS(hcomp
->Init
.Hysteresis
));
318 assert_param(IS_COMP_MODE(hcomp
->Init
.Mode
));
319 assert_param(IS_COMP_BLANKINGSRCE(hcomp
->Init
.BlankingSrce
));
320 assert_param(IS_COMP_BLANKINGSRCE_INSTANCE(hcomp
->Instance
, hcomp
->Init
.BlankingSrce
));
321 assert_param(IS_COMP_TRIGGERMODE(hcomp
->Init
.TriggerMode
));
323 if(hcomp
->Init
.WindowMode
!= COMP_WINDOWMODE_DISABLE
)
325 assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp
->Instance
));
326 assert_param(IS_COMP_WINDOWMODE(hcomp
->Init
.WindowMode
));
329 /* Init SYSCFG and the low level hardware to access comparators */
330 __HAL_RCC_SYSCFG_CLK_ENABLE();
331 /* Init the low level hardware : SYSCFG to access comparators */
332 HAL_COMP_MspInit(hcomp
);
334 if(hcomp
->State
== HAL_COMP_STATE_RESET
)
336 /* Allocate lock resource and initialize it */
337 hcomp
->Lock
= HAL_UNLOCKED
;
340 /* Manage inverting input comparator inverting input connected to a GPIO */
341 /* for STM32F302x, STM32F32xx, STM32F33x. */
342 hcomp
->Init
.InvertingInput
= COMP_INVERTINGINPUT_SELECTION(hcomp
->Instance
, hcomp
->Init
.InvertingInput
);
344 /* Set COMP parameters */
345 /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
346 /* Set COMPxNONINSEL bits according to hcomp->Init.NonInvertingInput value */
347 /* Set COMPxBLANKING bits according to hcomp->Init.BlankingSrce value */
348 /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */
349 /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
350 /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */
351 /* Set COMPxMODE bits according to hcomp->Init.Mode value */
354 /* Initialize the COMP state*/
355 hcomp
->State
= HAL_COMP_STATE_READY
;
362 * @brief DeInitialize the COMP peripheral.
363 * @note If the selected comparator is locked, deinitialization cannot be performed.
364 * To unlock the configuration, perform a system reset.
365 * @param hcomp COMP handle
368 HAL_StatusTypeDef
HAL_COMP_DeInit(COMP_HandleTypeDef
*hcomp
)
370 HAL_StatusTypeDef status
= HAL_OK
;
372 /* Check the COMP handle allocation and lock status */
373 if((hcomp
== NULL
) || ((hcomp
->State
& COMP_STATE_BIT_LOCK
) != RESET
))
379 /* Check the parameter */
380 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
382 /* Set COMP_CSR register to reset value */
385 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
386 HAL_COMP_MspDeInit(hcomp
);
388 hcomp
->State
= HAL_COMP_STATE_RESET
;
398 * @brief Initialize the COMP MSP.
399 * @param hcomp COMP handle
402 __weak
void HAL_COMP_MspInit(COMP_HandleTypeDef
*hcomp
)
404 /* Prevent unused argument(s) compilation warning */
407 /* NOTE : This function should not be modified, when the callback is needed,
408 the HAL_COMP_MspInit could be implemented in the user file
413 * @brief DeInitialize the COMP MSP.
414 * @param hcomp COMP handle
417 __weak
void HAL_COMP_MspDeInit(COMP_HandleTypeDef
*hcomp
)
419 /* Prevent unused argument(s) compilation warning */
422 /* NOTE : This function should not be modified, when the callback is needed,
423 the HAL_COMP_MspDeInit could be implemented in the user file
431 /** @defgroup COMP_Exported_Functions_Group2 Start Stop operation functions
432 * @brief Start-Stop operation functions.
435 ===============================================================================
436 ##### Start Stop operation functions #####
437 ===============================================================================
438 [..] This section provides functions allowing to:
439 (+) Start a comparator without interrupt generation.
440 (+) Stop a comparator without interrupt generation.
441 (+) Start a comparator with interrupt generation.
442 (+) Stop a comparator with interrupt generation.
443 (+) Handle interrupts from a comparator with associated callback function.
450 * @brief Start the comparator.
451 * @param hcomp COMP handle
454 HAL_StatusTypeDef
HAL_COMP_Start(COMP_HandleTypeDef
*hcomp
)
456 HAL_StatusTypeDef status
= HAL_OK
;
457 uint32_t extiline
= 0U;
459 /* Check the COMP handle allocation and lock status */
460 if((hcomp
== NULL
) || ((hcomp
->State
& COMP_STATE_BIT_LOCK
) != RESET
))
466 /* Check the parameter */
467 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
469 if(hcomp
->State
== HAL_COMP_STATE_READY
)
471 /* Get the EXTI Line output configuration */
472 extiline
= COMP_GET_EXTI_LINE(hcomp
->Instance
);
474 /* Configure the event generation */
475 if((hcomp
->Init
.TriggerMode
& (COMP_TRIGGERMODE_EVENT_RISING
|COMP_TRIGGERMODE_EVENT_FALLING
)) != RESET
)
477 /* Configure the event trigger rising edge */
478 if((hcomp
->Init
.TriggerMode
& COMP_TRIGGERMODE_EVENT_RISING
) != RESET
)
480 COMP_EXTI_RISING_ENABLE(extiline
);
484 COMP_EXTI_RISING_DISABLE(extiline
);
487 /* Configure the trigger falling edge */
488 if((hcomp
->Init
.TriggerMode
& COMP_TRIGGERMODE_EVENT_FALLING
) != RESET
)
490 COMP_EXTI_FALLING_ENABLE(extiline
);
494 COMP_EXTI_FALLING_DISABLE(extiline
);
497 /* Enable EXTI event generation */
498 COMP_EXTI_ENABLE_EVENT(extiline
);
500 /* Clear COMP EXTI pending bit */
501 COMP_EXTI_CLEAR_FLAG(extiline
);
504 /* Enable the selected comparator */
505 __HAL_COMP_ENABLE(hcomp
);
507 hcomp
->State
= HAL_COMP_STATE_BUSY
;
519 * @brief Stop the comparator.
520 * @param hcomp COMP handle
523 HAL_StatusTypeDef
HAL_COMP_Stop(COMP_HandleTypeDef
*hcomp
)
525 HAL_StatusTypeDef status
= HAL_OK
;
527 /* Check the COMP handle allocation and lock status */
528 if((hcomp
== NULL
) || ((hcomp
->State
& COMP_STATE_BIT_LOCK
) != RESET
))
534 /* Check the parameter */
535 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
537 if(hcomp
->State
== HAL_COMP_STATE_BUSY
)
539 /* Disable the EXTI Line event mode if any */
540 COMP_EXTI_DISABLE_EVENT(COMP_GET_EXTI_LINE(hcomp
->Instance
));
542 /* Disable the selected comparator */
543 __HAL_COMP_DISABLE(hcomp
);
545 hcomp
->State
= HAL_COMP_STATE_READY
;
557 * @brief Start the comparator in Interrupt mode.
558 * @param hcomp COMP handle
559 * @retval HAL status.
561 HAL_StatusTypeDef
HAL_COMP_Start_IT(COMP_HandleTypeDef
*hcomp
)
563 HAL_StatusTypeDef status
= HAL_OK
;
564 uint32_t extiline
= 0U;
566 /* Check the COMP handle allocation and lock status */
567 if((hcomp
== NULL
) || ((hcomp
->State
& COMP_STATE_BIT_LOCK
) != RESET
))
573 /* Check the parameter */
574 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
576 if(hcomp
->State
== HAL_COMP_STATE_READY
)
578 /* Configure the EXTI event generation */
579 if((hcomp
->Init
.TriggerMode
& (COMP_TRIGGERMODE_IT_RISING
|COMP_TRIGGERMODE_IT_FALLING
)) != RESET
)
581 /* Get the EXTI Line output configuration */
582 extiline
= COMP_GET_EXTI_LINE(hcomp
->Instance
);
584 /* Configure the trigger rising edge */
585 if((hcomp
->Init
.TriggerMode
& COMP_TRIGGERMODE_IT_RISING
) != RESET
)
587 COMP_EXTI_RISING_ENABLE(extiline
);
591 COMP_EXTI_RISING_DISABLE(extiline
);
593 /* Configure the trigger falling edge */
594 if((hcomp
->Init
.TriggerMode
& COMP_TRIGGERMODE_IT_FALLING
) != RESET
)
596 COMP_EXTI_FALLING_ENABLE(extiline
);
600 COMP_EXTI_FALLING_DISABLE(extiline
);
603 /* Clear COMP EXTI pending bit if any */
604 COMP_EXTI_CLEAR_FLAG(extiline
);
606 /* Enable EXTI interrupt mode */
607 COMP_EXTI_ENABLE_IT(extiline
);
609 /* Enable the selected comparator */
610 __HAL_COMP_ENABLE(hcomp
);
612 hcomp
->State
= HAL_COMP_STATE_BUSY
;
629 * @brief Stop the comparator in Interrupt mode.
630 * @param hcomp COMP handle
633 HAL_StatusTypeDef
HAL_COMP_Stop_IT(COMP_HandleTypeDef
*hcomp
)
635 HAL_StatusTypeDef status
= HAL_OK
;
637 /* Disable the EXTI Line interrupt mode */
638 COMP_EXTI_DISABLE_IT(COMP_GET_EXTI_LINE(hcomp
->Instance
));
640 status
= HAL_COMP_Stop(hcomp
);
646 * @brief Comparator IRQ Handler.
647 * @param hcomp COMP handle
650 void HAL_COMP_IRQHandler(COMP_HandleTypeDef
*hcomp
)
652 uint32_t extiline
= COMP_GET_EXTI_LINE(hcomp
->Instance
);
654 /* Check COMP EXTI flag */
655 if(COMP_EXTI_GET_FLAG(extiline
) != RESET
)
657 /* Clear COMP EXTI pending bit */
658 COMP_EXTI_CLEAR_FLAG(extiline
);
660 /* COMP trigger user callback */
661 HAL_COMP_TriggerCallback(hcomp
);
666 * @brief Comparator callback.
667 * @param hcomp COMP handle
670 __weak
void HAL_COMP_TriggerCallback(COMP_HandleTypeDef
*hcomp
)
672 /* Prevent unused argument(s) compilation warning */
675 /* NOTE : This function should not be modified, when the callback is needed,
676 the HAL_COMP_TriggerCallback should be implemented in the user file
684 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
685 * @brief Management functions.
688 ===============================================================================
689 ##### Peripheral Control functions #####
690 ===============================================================================
692 This subsection provides a set of functions allowing to control the comparators.
699 * @brief Lock the selected comparator configuration.
700 * @note A system reset is required to unlock the comparator configuration.
701 * @param hcomp COMP handle
704 HAL_StatusTypeDef
HAL_COMP_Lock(COMP_HandleTypeDef
*hcomp
)
706 HAL_StatusTypeDef status
= HAL_OK
;
708 /* Check the COMP handle allocation and lock status */
709 if((hcomp
== NULL
) || ((hcomp
->State
& COMP_STATE_BIT_LOCK
) != RESET
))
715 /* Check the parameter */
716 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
718 /* Set lock flag on state */
721 case HAL_COMP_STATE_BUSY
:
722 hcomp
->State
= HAL_COMP_STATE_BUSY_LOCKED
;
724 case HAL_COMP_STATE_READY
:
725 hcomp
->State
= HAL_COMP_STATE_READY_LOCKED
;
728 /* unexpected state */
736 /* Set the lock bit corresponding to selected comparator */
737 __HAL_COMP_LOCK(hcomp
);
744 * @brief Return the output level (high or low) of the selected comparator.
745 * The output level depends on the selected polarity.
746 * If the polarity is not inverted:
747 * - Comparator output is low when the non-inverting input is at a lower
748 * voltage than the inverting input
749 * - Comparator output is high when the non-inverting input is at a higher
750 * voltage than the inverting input
751 * If the polarity is inverted:
752 * - Comparator output is high when the non-inverting input is at a lower
753 * voltage than the inverting input
754 * - Comparator output is low when the non-inverting input is at a higher
755 * voltage than the inverting input
756 * @param hcomp COMP handle
757 * @retval Returns the selected comparator output level:
758 * @arg @ref COMP_OUTPUTLEVEL_LOW
759 * @arg @ref COMP_OUTPUTLEVEL_HIGH
762 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef
*hcomp
)
766 /* Check the parameter */
767 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
769 level
= READ_BIT(hcomp
->Instance
->CSR
, COMP_CSR_COMPxOUT
);
773 return(COMP_OUTPUTLEVEL_HIGH
);
775 return(COMP_OUTPUTLEVEL_LOW
);
782 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
783 * @brief Peripheral State functions.
786 ===============================================================================
787 ##### Peripheral State functions #####
788 ===============================================================================
790 This subsection permits to get in run-time the status of the peripheral.
797 * @brief Return the COMP handle state.
798 * @param hcomp COMP handle
801 HAL_COMP_StateTypeDef
HAL_COMP_GetState(COMP_HandleTypeDef
*hcomp
)
803 /* Check the COMP handle allocation */
806 return HAL_COMP_STATE_RESET
;
809 /* Check the parameter */
810 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
812 /* Return COMP handle state */
823 #endif /* HAL_COMP_MODULE_ENABLED */
832 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/