2 ******************************************************************************
3 * @file stm32g4xx_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 (through EXTI interrupt)
11 * + Peripheral control functions
12 * + Peripheral state functions
15 ================================================================================
16 ##### COMP Peripheral features #####
17 ================================================================================
20 The STM32G4xx device family integrates seven analog comparators instances:
21 COMP1, COMP2, COMP3, COMP4, COMP5, COMP6 and COMP7.
22 (#) Comparators input minus (inverting input) and input plus (non inverting input)
23 can be set to internal references or to GPIO pins
24 (refer to GPIO list in reference manual).
26 (#) Comparators output level is available using HAL_COMP_GetOutputLevel()
27 and can be redirected to other peripherals: GPIO pins (in mode
28 alternate functions for comparator), timers.
29 (refer to GPIO list in reference manual).
31 (#) The comparators have interrupt capability through the EXTI controller
32 with wake-up from sleep and stop modes.
34 From the corresponding IRQ handler, the right interrupt source can be retrieved
35 using macro __HAL_COMP_COMPx_EXTI_GET_FLAG().
37 ##### How to use this driver #####
38 ================================================================================
40 This driver provides functions to configure and program the comparator instances
43 To use the comparator, perform the following steps:
45 (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit():
46 (++) Configure the GPIO connected to comparator inputs plus and minus in analog mode
47 using HAL_GPIO_Init().
48 (++) If needed, configure the GPIO connected to comparator output in alternate function mode
49 using HAL_GPIO_Init().
50 (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
51 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
52 interrupt vector using HAL_NVIC_EnableIRQ() function.
54 (#) Configure the comparator using HAL_COMP_Init() function:
55 (++) Select the input minus (inverting input)
56 (++) Select the input plus (non-inverting input)
57 (++) Select the hysteresis
58 (++) Select the blanking source
59 (++) Select the output polarity
60 (++) Select the deglitcher mode
62 -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE()
63 to enable internal control clock of the comparators.
64 However, this is a legacy strategy. In future STM32 families,
65 COMP clock enable must be implemented by user in "HAL_COMP_MspInit()".
66 Therefore, for compatibility anticipation, it is recommended to
67 implement __HAL_RCC_SYSCFG_CLK_ENABLE() in "HAL_COMP_MspInit()".
69 (#) Reconfiguration on-the-fly of comparator can be done by calling again
70 function HAL_COMP_Init() with new input structure parameters values.
72 (#) Enable the comparator using HAL_COMP_Start() function.
74 (#) Use HAL_COMP_TriggerCallback() or HAL_COMP_GetOutputLevel() functions
75 to manage comparator outputs (events and output level).
77 (#) Disable the comparator using HAL_COMP_Stop() function.
79 (#) De-initialize the comparator using HAL_COMP_DeInit() function.
81 (#) For safety purpose, comparator configuration can be locked using HAL_COMP_Lock() function.
82 The only way to unlock the comparator is a device hardware reset.
84 *** Callback registration ***
85 =============================================
88 The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
89 allows the user to configure dynamically the driver callbacks.
90 Use Functions @ref HAL_COMP_RegisterCallback()
91 to register an interrupt callback.
94 Function @ref HAL_COMP_RegisterCallback() allows to register following callbacks:
95 (+) TriggerCallback : callback for COMP trigger.
96 (+) MspInitCallback : callback for Msp Init.
97 (+) MspDeInitCallback : callback for Msp DeInit.
98 This function takes as parameters the HAL peripheral handle, the Callback ID
99 and a pointer to the user callback function.
102 Use function @ref HAL_COMP_UnRegisterCallback to reset a callback to the default
106 @ref HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
108 This function allows to reset following callbacks:
109 (+) TriggerCallback : callback for COMP trigger.
110 (+) MspInitCallback : callback for Msp Init.
111 (+) MspDeInitCallback : callback for Msp DeInit.
114 By default, after the @ref HAL_COMP_Init() and when the state is @ref HAL_COMP_STATE_RESET
115 all callbacks are set to the corresponding weak functions:
116 example @ref HAL_COMP_TriggerCallback().
117 Exception done for MspInit and MspDeInit functions that are
118 reset to the legacy weak functions in the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit() only when
119 these callbacks are null (not registered beforehand).
122 If MspInit or MspDeInit are not null, the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit()
123 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
126 Callbacks can be registered/unregistered in @ref HAL_COMP_STATE_READY state only.
127 Exception done MspInit/MspDeInit functions that can be registered/unregistered
128 in @ref HAL_COMP_STATE_READY or @ref HAL_COMP_STATE_RESET state,
129 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
132 Then, the user first registers the MspInit/MspDeInit user callbacks
133 using @ref HAL_COMP_RegisterCallback() before calling @ref HAL_COMP_DeInit()
134 or @ref HAL_COMP_Init() function.
137 When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
138 not defined, the callback registration feature is not available and all callbacks
139 are set to the corresponding weak functions.
142 ******************************************************************************
144 ******************************************************************************
147 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
148 * All rights reserved.</center></h2>
150 * This software component is licensed by ST under BSD 3-Clause license,
151 * the "License"; You may not use this file except in compliance with the
152 * License. You may obtain a copy of the License at:
153 * opensource.org/licenses/BSD-3-Clause
155 ******************************************************************************
158 /* Includes ------------------------------------------------------------------*/
159 #include "stm32g4xx_hal.h"
161 /** @addtogroup STM32G4xx_HAL_Driver
165 #ifdef HAL_COMP_MODULE_ENABLED
169 /** @defgroup COMP COMP
170 * @brief COMP HAL module driver
174 /* Private typedef -----------------------------------------------------------*/
175 /* Private define ------------------------------------------------------------*/
176 /** @addtogroup COMP_Private_Constants
180 /* Delay for COMP startup time. */
181 /* Note: Delay required to reach propagation delay specification. */
182 /* Literal set to maximum value (refer to device datasheet, */
183 /* parameter "tSTART"). */
185 #define COMP_DELAY_STARTUP_US (5UL) /*!< Delay for COMP startup time */
187 /* Delay for COMP voltage scaler stabilization time. */
188 /* Literal set to maximum value (refer to device datasheet, */
189 /* parameter "tSTART_SCALER"). */
191 #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200UL) /*!< Delay for COMP voltage scaler stabilization time */
193 #define COMP_OUTPUT_LEVEL_BITOFFSET_POS (30UL)
199 /* Private macro -------------------------------------------------------------*/
200 /* Private variables ---------------------------------------------------------*/
201 /* Private function prototypes -----------------------------------------------*/
202 /* Exported functions --------------------------------------------------------*/
204 /** @defgroup COMP_Exported_Functions COMP Exported Functions
208 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
209 * @brief Initialization and de-initialization functions.
212 ===============================================================================
213 ##### Initialization and de-initialization functions #####
214 ===============================================================================
215 [..] This section provides functions to initialize and de-initialize comparators
222 * @brief Initialize the COMP according to the specified
223 * parameters in the COMP_InitTypeDef and initialize the associated handle.
224 * @note If the selected comparator is locked, initialization can't be performed.
225 * To unlock the configuration, perform a system reset.
226 * @param hcomp COMP handle
229 HAL_StatusTypeDef
HAL_COMP_Init(COMP_HandleTypeDef
*hcomp
)
233 uint32_t comp_voltage_scaler_initialized
; /* Value "0" if comparator voltage scaler is not initialized */
234 __IO
uint32_t wait_loop_index
= 0UL;
235 HAL_StatusTypeDef status
= HAL_OK
;
237 /* Check the COMP handle allocation and lock status */
242 else if(__HAL_COMP_IS_LOCKED(hcomp
))
248 /* Check the parameters */
249 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
250 assert_param(IS_COMP_INPUT_PLUS(hcomp
->Instance
, hcomp
->Init
.InputPlus
));
251 assert_param(IS_COMP_INPUT_MINUS(hcomp
->Instance
, hcomp
->Init
.InputMinus
));
252 assert_param(IS_COMP_OUTPUTPOL(hcomp
->Init
.OutputPol
));
253 assert_param(IS_COMP_HYSTERESIS(hcomp
->Init
.Hysteresis
));
254 assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp
->Instance
, hcomp
->Init
.BlankingSrce
));
255 assert_param(IS_COMP_TRIGGERMODE(hcomp
->Init
.TriggerMode
));
256 assert_param(IS_COMP_DEGLITCHER_MODE(hcomp
->Init
.DeglitcherMode
));
258 if(hcomp
->State
== HAL_COMP_STATE_RESET
)
260 /* Allocate lock resource and initialize it */
261 hcomp
->Lock
= HAL_UNLOCKED
;
263 /* Set COMP error code to none */
264 COMP_CLEAR_ERRORCODE(hcomp
);
267 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
268 /* Init the COMP Callback settings */
269 hcomp
->TriggerCallback
= HAL_COMP_TriggerCallback
; /* Legacy weak callback */
271 if (hcomp
->MspInitCallback
== NULL
)
273 hcomp
->MspInitCallback
= HAL_COMP_MspInit
; /* Legacy weak MspInit */
276 /* Init the low level hardware */
277 /* Note: Internal control clock of the comparators must */
278 /* be enabled in "HAL_COMP_MspInit()" */
279 /* using "__HAL_RCC_SYSCFG_CLK_ENABLE()". */
280 hcomp
->MspInitCallback(hcomp
);
282 /* Init the low level hardware */
283 /* Note: Internal control clock of the comparators must */
284 /* be enabled in "HAL_COMP_MspInit()" */
285 /* using "__HAL_RCC_SYSCFG_CLK_ENABLE()". */
286 HAL_COMP_MspInit(hcomp
);
287 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
290 /* Memorize voltage scaler state before initialization */
291 comp_voltage_scaler_initialized
= READ_BIT(hcomp
->Instance
->CSR
, COMP_CSR_SCALEN
);
293 /* Set COMP parameters */
294 tmp_csr
= ( hcomp
->Init
.InputMinus
295 | hcomp
->Init
.InputPlus
296 | hcomp
->Init
.BlankingSrce
297 | hcomp
->Init
.Hysteresis
298 | hcomp
->Init
.OutputPol
299 | hcomp
->Init
.DeglitcherMode
302 /* Set parameters in COMP register */
303 /* Note: Update all bits except read-only, lock and enable bits */
304 MODIFY_REG(hcomp
->Instance
->CSR
,
305 COMP_CSR_INMSEL
| COMP_CSR_INPSEL
| COMP_CSR_DEGLITCHEN
|
306 COMP_CSR_POLARITY
| COMP_CSR_HYST
|
307 COMP_CSR_BLANKING
| COMP_CSR_BRGEN
| COMP_CSR_SCALEN
,
311 /* Delay for COMP scaler bridge voltage stabilization */
312 /* Apply the delay if voltage scaler bridge is required and not already enabled */
313 if ((READ_BIT(hcomp
->Instance
->CSR
, COMP_CSR_SCALEN
) != 0UL) &&
314 (comp_voltage_scaler_initialized
== 0UL) )
316 /* Wait loop initialization and execution */
317 /* Note: Variable divided by 2 to compensate partially */
318 /* CPU processing cycles, scaling in us split to not */
319 /* exceed 32 bits register capacity and handle low frequency. */
320 wait_loop_index
= ((COMP_DELAY_VOLTAGE_SCALER_STAB_US
/ 10UL) * (SystemCoreClock
/ (100000UL * 2UL)));
321 while(wait_loop_index
!= 0UL)
327 /* Get the EXTI line corresponding to the selected COMP instance */
328 exti_line
= COMP_GET_EXTI_LINE(hcomp
->Instance
);
330 /* Manage EXTI settings */
331 if((hcomp
->Init
.TriggerMode
& (COMP_EXTI_IT
| COMP_EXTI_EVENT
)) != 0UL)
333 /* Configure EXTI rising edge */
334 if((hcomp
->Init
.TriggerMode
& COMP_EXTI_RISING
) != 0UL)
336 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
337 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
339 LL_EXTI_EnableRisingTrig_32_63(exti_line
);
343 LL_EXTI_EnableRisingTrig_0_31(exti_line
);
346 LL_EXTI_EnableRisingTrig_0_31(exti_line
);
351 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
352 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
354 LL_EXTI_DisableRisingTrig_32_63(exti_line
);
358 LL_EXTI_DisableRisingTrig_0_31(exti_line
);
361 LL_EXTI_DisableRisingTrig_0_31(exti_line
);
365 /* Configure EXTI falling edge */
366 if((hcomp
->Init
.TriggerMode
& COMP_EXTI_FALLING
) != 0UL)
368 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
369 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
371 LL_EXTI_EnableFallingTrig_32_63(exti_line
);
375 LL_EXTI_EnableFallingTrig_0_31(exti_line
);
378 LL_EXTI_EnableFallingTrig_0_31(exti_line
);
383 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
384 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
386 LL_EXTI_DisableFallingTrig_32_63(exti_line
);
390 LL_EXTI_DisableFallingTrig_0_31(exti_line
);
393 LL_EXTI_DisableFallingTrig_0_31(exti_line
);
397 /* Clear COMP EXTI pending bit (if any) */
398 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
399 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
401 LL_EXTI_ClearFlag_32_63(exti_line
);
405 LL_EXTI_ClearFlag_0_31(exti_line
);
408 LL_EXTI_ClearFlag_0_31(exti_line
);
411 /* Configure EXTI event mode */
412 if((hcomp
->Init
.TriggerMode
& COMP_EXTI_EVENT
) != 0UL)
414 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
415 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
417 LL_EXTI_EnableEvent_32_63(exti_line
);
421 LL_EXTI_EnableEvent_0_31(exti_line
);
424 LL_EXTI_EnableEvent_0_31(exti_line
);
429 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
430 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
432 LL_EXTI_DisableEvent_32_63(exti_line
);
436 LL_EXTI_DisableEvent_0_31(exti_line
);
439 LL_EXTI_DisableEvent_0_31(exti_line
);
443 /* Configure EXTI interrupt mode */
444 if((hcomp
->Init
.TriggerMode
& COMP_EXTI_IT
) != 0UL)
446 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
447 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
449 LL_EXTI_EnableIT_32_63(exti_line
);
453 LL_EXTI_EnableIT_0_31(exti_line
);
456 LL_EXTI_EnableIT_0_31(exti_line
);
461 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
462 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
464 LL_EXTI_DisableIT_32_63(exti_line
);
468 LL_EXTI_DisableIT_0_31(exti_line
);
471 LL_EXTI_DisableIT_0_31(exti_line
);
477 /* Disable EXTI event mode */
478 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
479 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
481 LL_EXTI_DisableEvent_32_63(exti_line
);
485 LL_EXTI_DisableEvent_0_31(exti_line
);
488 LL_EXTI_DisableEvent_0_31(exti_line
);
491 /* Disable EXTI interrupt mode */
492 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
493 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
495 LL_EXTI_DisableIT_32_63(exti_line
);
499 LL_EXTI_DisableIT_0_31(exti_line
);
502 LL_EXTI_DisableIT_0_31(exti_line
);
506 /* Set HAL COMP handle state */
507 /* Note: Transition from state reset to state ready, */
508 /* otherwise (coming from state ready or busy) no state update. */
509 if (hcomp
->State
== HAL_COMP_STATE_RESET
)
511 hcomp
->State
= HAL_COMP_STATE_READY
;
519 * @brief DeInitialize the COMP peripheral.
520 * @note Deinitialization cannot be performed if the COMP configuration is locked.
521 * To unlock the configuration, perform a system reset.
522 * @param hcomp COMP handle
525 HAL_StatusTypeDef
HAL_COMP_DeInit(COMP_HandleTypeDef
*hcomp
)
527 HAL_StatusTypeDef status
= HAL_OK
;
529 /* Check the COMP handle allocation and lock status */
534 else if(__HAL_COMP_IS_LOCKED(hcomp
))
540 /* Check the parameter */
541 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
543 /* Set COMP_CSR register to reset value */
544 WRITE_REG(hcomp
->Instance
->CSR
, 0x00000000UL
);
546 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
547 if (hcomp
->MspDeInitCallback
== NULL
)
549 hcomp
->MspDeInitCallback
= HAL_COMP_MspDeInit
; /* Legacy weak MspDeInit */
552 /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
553 hcomp
->MspDeInitCallback(hcomp
);
555 /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
556 HAL_COMP_MspDeInit(hcomp
);
557 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
559 /* Set HAL COMP handle state */
560 hcomp
->State
= HAL_COMP_STATE_RESET
;
570 * @brief Initialize the COMP MSP.
571 * @param hcomp COMP handle
574 __weak
void HAL_COMP_MspInit(COMP_HandleTypeDef
*hcomp
)
576 /* Prevent unused argument(s) compilation warning */
579 /* NOTE : This function should not be modified, when the callback is needed,
580 the HAL_COMP_MspInit could be implemented in the user file
585 * @brief DeInitialize the COMP MSP.
586 * @param hcomp COMP handle
589 __weak
void HAL_COMP_MspDeInit(COMP_HandleTypeDef
*hcomp
)
591 /* Prevent unused argument(s) compilation warning */
594 /* NOTE : This function should not be modified, when the callback is needed,
595 the HAL_COMP_MspDeInit could be implemented in the user file
599 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
601 * @brief Register a User COMP Callback
602 * To be used instead of the weak predefined callback
603 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
604 * the configuration information for the specified COMP.
605 * @param CallbackID ID of the callback to be registered
606 * This parameter can be one of the following values:
607 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
608 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
609 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
610 * @param pCallback pointer to the Callback function
613 HAL_StatusTypeDef
HAL_COMP_RegisterCallback(COMP_HandleTypeDef
*hcomp
, HAL_COMP_CallbackIDTypeDef CallbackID
, pCOMP_CallbackTypeDef pCallback
)
615 HAL_StatusTypeDef status
= HAL_OK
;
617 if (pCallback
== NULL
)
619 /* Update the error code */
620 hcomp
->ErrorCode
|= HAL_COMP_ERROR_INVALID_CALLBACK
;
625 if (HAL_COMP_STATE_READY
== hcomp
->State
)
629 case HAL_COMP_TRIGGER_CB_ID
:
630 hcomp
->TriggerCallback
= pCallback
;
633 case HAL_COMP_MSPINIT_CB_ID
:
634 hcomp
->MspInitCallback
= pCallback
;
637 case HAL_COMP_MSPDEINIT_CB_ID
:
638 hcomp
->MspDeInitCallback
= pCallback
;
642 /* Update the error code */
643 hcomp
->ErrorCode
|= HAL_COMP_ERROR_INVALID_CALLBACK
;
645 /* Return error status */
650 else if (HAL_COMP_STATE_RESET
== hcomp
->State
)
654 case HAL_COMP_MSPINIT_CB_ID
:
655 hcomp
->MspInitCallback
= pCallback
;
658 case HAL_COMP_MSPDEINIT_CB_ID
:
659 hcomp
->MspDeInitCallback
= pCallback
;
663 /* Update the error code */
664 hcomp
->ErrorCode
|= HAL_COMP_ERROR_INVALID_CALLBACK
;
666 /* Return error status */
673 /* Update the error code */
674 hcomp
->ErrorCode
|= HAL_COMP_ERROR_INVALID_CALLBACK
;
676 /* Return error status */
684 * @brief Unregister a COMP Callback
685 * COMP callback is redirected to the weak predefined callback
686 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
687 * the configuration information for the specified COMP.
688 * @param CallbackID ID of the callback to be unregistered
689 * This parameter can be one of the following values:
690 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
691 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
692 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
695 HAL_StatusTypeDef
HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef
*hcomp
, HAL_COMP_CallbackIDTypeDef CallbackID
)
697 HAL_StatusTypeDef status
= HAL_OK
;
699 if (HAL_COMP_STATE_READY
== hcomp
->State
)
703 case HAL_COMP_TRIGGER_CB_ID
:
704 hcomp
->TriggerCallback
= HAL_COMP_TriggerCallback
; /* Legacy weak callback */
707 case HAL_COMP_MSPINIT_CB_ID
:
708 hcomp
->MspInitCallback
= HAL_COMP_MspInit
; /* Legacy weak MspInit */
711 case HAL_COMP_MSPDEINIT_CB_ID
:
712 hcomp
->MspDeInitCallback
= HAL_COMP_MspDeInit
; /* Legacy weak MspDeInit */
716 /* Update the error code */
717 hcomp
->ErrorCode
|= HAL_COMP_ERROR_INVALID_CALLBACK
;
719 /* Return error status */
724 else if (HAL_COMP_STATE_RESET
== hcomp
->State
)
728 case HAL_COMP_MSPINIT_CB_ID
:
729 hcomp
->MspInitCallback
= HAL_COMP_MspInit
; /* Legacy weak MspInit */
732 case HAL_COMP_MSPDEINIT_CB_ID
:
733 hcomp
->MspDeInitCallback
= HAL_COMP_MspDeInit
; /* Legacy weak MspDeInit */
737 /* Update the error code */
738 hcomp
->ErrorCode
|= HAL_COMP_ERROR_INVALID_CALLBACK
;
740 /* Return error status */
747 /* Update the error code */
748 hcomp
->ErrorCode
|= HAL_COMP_ERROR_INVALID_CALLBACK
;
750 /* Return error status */
757 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
763 /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions
764 * @brief Start-Stop operation functions.
767 ===============================================================================
768 ##### IO operation functions #####
769 ===============================================================================
770 [..] This section provides functions allowing to:
771 (+) Start a comparator instance.
772 (+) Stop a comparator instance.
779 * @brief Start the comparator.
780 * @param hcomp COMP handle
783 HAL_StatusTypeDef
HAL_COMP_Start(COMP_HandleTypeDef
*hcomp
)
785 __IO
uint32_t wait_loop_index
= 0UL;
786 HAL_StatusTypeDef status
= HAL_OK
;
788 /* Check the COMP handle allocation and lock status */
793 else if(__HAL_COMP_IS_LOCKED(hcomp
))
799 /* Check the parameter */
800 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
802 if(hcomp
->State
== HAL_COMP_STATE_READY
)
804 /* Enable the selected comparator */
805 SET_BIT(hcomp
->Instance
->CSR
, COMP_CSR_EN
);
807 /* Set HAL COMP handle state */
808 hcomp
->State
= HAL_COMP_STATE_BUSY
;
810 /* Delay for COMP startup time */
811 /* Wait loop initialization and execution */
812 /* Note: Variable divided by 2 to compensate partially */
813 /* CPU processing cycles. */
814 /* Note: In case of system low frequency (below 1Mhz), short delay */
815 /* of startup time (few us) is within CPU processing cycles */
816 /* of following instructions. */
817 wait_loop_index
= (COMP_DELAY_STARTUP_US
* (SystemCoreClock
/ (1000000UL * 2UL)));
818 while(wait_loop_index
!= 0UL)
833 * @brief Stop the comparator.
834 * @param hcomp COMP handle
837 HAL_StatusTypeDef
HAL_COMP_Stop(COMP_HandleTypeDef
*hcomp
)
839 HAL_StatusTypeDef status
= HAL_OK
;
841 /* Check the COMP handle allocation and lock status */
846 else if(__HAL_COMP_IS_LOCKED(hcomp
))
852 /* Check the parameter */
853 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
855 /* Check compliant states: HAL_COMP_STATE_READY or HAL_COMP_STATE_BUSY */
856 /* (all states except HAL_COMP_STATE_RESET and except locked status. */
857 if(hcomp
->State
!= HAL_COMP_STATE_RESET
)
859 /* Disable the selected comparator */
860 CLEAR_BIT(hcomp
->Instance
->CSR
, COMP_CSR_EN
);
862 /* Set HAL COMP handle state */
863 hcomp
->State
= HAL_COMP_STATE_READY
;
875 * @brief Comparator IRQ handler.
876 * @param hcomp COMP handle
879 void HAL_COMP_IRQHandler(COMP_HandleTypeDef
*hcomp
)
881 /* Get the EXTI line corresponding to the selected COMP instance */
882 uint32_t exti_line
= COMP_GET_EXTI_LINE(hcomp
->Instance
);
883 uint32_t tmp_comp_exti_flag_set
= 0UL;
885 /* Check COMP EXTI flag */
886 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
887 if((hcomp
->Instance
== COMP6
) || (hcomp
->Instance
== COMP7
))
889 if(LL_EXTI_IsActiveFlag_32_63(exti_line
) != 0UL)
891 tmp_comp_exti_flag_set
= 2UL;
896 if(LL_EXTI_IsActiveFlag_0_31(exti_line
) != 0UL)
898 tmp_comp_exti_flag_set
= 1UL;
902 if(LL_EXTI_IsActiveFlag_0_31(exti_line
) != 0UL)
904 tmp_comp_exti_flag_set
= 1UL;
908 if(tmp_comp_exti_flag_set
!= 0UL)
910 /* Clear COMP EXTI line pending bit */
911 #if defined(STM32G474xx) || defined(STM32G484xx) || defined(STM32G473xx)
912 if(tmp_comp_exti_flag_set
== 2UL)
914 LL_EXTI_ClearFlag_32_63(exti_line
);
918 LL_EXTI_ClearFlag_0_31(exti_line
);
921 LL_EXTI_ClearFlag_0_31(exti_line
);
924 /* COMP trigger user callback */
925 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
926 hcomp
->TriggerCallback(hcomp
);
928 HAL_COMP_TriggerCallback(hcomp
);
929 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
937 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
938 * @brief Management functions.
941 ===============================================================================
942 ##### Peripheral Control functions #####
943 ===============================================================================
945 This subsection provides a set of functions allowing to control the comparators.
952 * @brief Lock the selected comparator configuration.
953 * @note A system reset is required to unlock the comparator configuration.
954 * @note Locking the comparator from reset state is possible
955 * if __HAL_RCC_SYSCFG_CLK_ENABLE() is being called before.
956 * @param hcomp COMP handle
959 HAL_StatusTypeDef
HAL_COMP_Lock(COMP_HandleTypeDef
*hcomp
)
961 HAL_StatusTypeDef status
= HAL_OK
;
963 /* Check the COMP handle allocation and lock status */
968 else if(__HAL_COMP_IS_LOCKED(hcomp
))
974 /* Check the parameter */
975 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
977 /* Set HAL COMP handle state */
980 case HAL_COMP_STATE_RESET
:
981 hcomp
->State
= HAL_COMP_STATE_RESET_LOCKED
;
983 case HAL_COMP_STATE_READY
:
984 hcomp
->State
= HAL_COMP_STATE_READY_LOCKED
;
986 default: /* HAL_COMP_STATE_BUSY */
987 hcomp
->State
= HAL_COMP_STATE_BUSY_LOCKED
;
994 /* Set the lock bit corresponding to selected comparator */
995 __HAL_COMP_LOCK(hcomp
);
1002 * @brief Return the output level (high or low) of the selected comparator.
1003 * On this STM32 serie, comparator 'value' is taken before
1004 * polarity and blanking are applied, thus:
1005 * - Comparator output is low when the input plus is at a lower
1006 * voltage than the input minus
1007 * - Comparator output is high when the input plus is at a higher
1008 * voltage than the input minus
1009 * @param hcomp COMP handle
1010 * @retval Returns the selected comparator output level:
1011 * @arg COMP_OUTPUT_LEVEL_LOW
1012 * @arg COMP_OUTPUT_LEVEL_HIGH
1015 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef
*hcomp
)
1017 /* Check the parameter */
1018 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
1020 return (uint32_t)(READ_BIT(hcomp
->Instance
->CSR
, COMP_CSR_VALUE
)
1021 >> COMP_OUTPUT_LEVEL_BITOFFSET_POS
);
1025 * @brief Comparator trigger callback.
1026 * @param hcomp COMP handle
1029 __weak
void HAL_COMP_TriggerCallback(COMP_HandleTypeDef
*hcomp
)
1031 /* Prevent unused argument(s) compilation warning */
1034 /* NOTE : This function should not be modified, when the callback is needed,
1035 the HAL_COMP_TriggerCallback should be implemented in the user file
1044 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
1045 * @brief Peripheral State functions.
1048 ===============================================================================
1049 ##### Peripheral State functions #####
1050 ===============================================================================
1052 This subsection permit to get in run-time the status of the peripheral.
1059 * @brief Return the COMP handle state.
1060 * @param hcomp COMP handle
1063 HAL_COMP_StateTypeDef
HAL_COMP_GetState(COMP_HandleTypeDef
*hcomp
)
1065 /* Check the COMP handle allocation */
1068 return HAL_COMP_STATE_RESET
;
1071 /* Check the parameter */
1072 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
1074 /* Return HAL COMP handle state */
1075 return hcomp
->State
;
1079 * @brief Return the COMP error code.
1080 * @param hcomp COMP handle
1081 * @retval COMP error code
1083 uint32_t HAL_COMP_GetError(COMP_HandleTypeDef
*hcomp
)
1085 /* Check the parameters */
1086 assert_param(IS_COMP_ALL_INSTANCE(hcomp
->Instance
));
1088 return hcomp
->ErrorCode
;
1105 #endif /* HAL_COMP_MODULE_ENABLED */
1111 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/