1 /**
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   *           + Peripheral Control functions
10   *           + Peripheral State functions
11   *
12   ******************************************************************************
13   * @attention
14   *
15   * Copyright (c) 2016 STMicroelectronics.
16   * All rights reserved.
17   *
18   * This software is licensed under terms that can be found in the LICENSE file
19   * in the root directory of this software component.
20   * If no LICENSE file comes with this software, it is provided AS-IS.
21   *
22   ******************************************************************************
23   @verbatim
24 ================================================================================
25           ##### COMP Peripheral features #####
26 ================================================================================
27 
28   [..]
29       The STM32F3xx device family integrates up to 7 analog comparators COMP1, COMP2...COMP7:
30       (#) The non inverting input and inverting input can be set to GPIO pins.
31           For STM32F3xx devices please refer to the COMP peripheral section in corresponding
32           Reference Manual.
33 
34       (#) The COMP output is available using HAL_COMP_GetOutputLevel()
35           and can be set on GPIO pins.
36           For STM32F3xx devices please refer to the COMP peripheral section in corresponding
37           Reference Manual.
38 
39       (#) The COMP output can be redirected to embedded timers (TIM1, TIM2, TIM3...).
40           For STM32F3xx devices please refer to the COMP peripheral section in corresponding
41           Reference Manual.
42 
43       (#) Each couple of comparators COMP1 and COMP2, COMP3 and COMP4, COMP5 and COMP6 can be combined in window
44           mode and respectively COMP1, COMP3 and COMP5 non inverting input is used as common non-inverting input.
45 
46       (#) The seven comparators have interrupt capability with wake-up
47           from Sleep and Stop modes (through the EXTI controller):
48           (++) COMP1 is internally connected to EXTI Line 21
49           (++) COMP2 is internally connected to EXTI Line 22
50           (++) COMP3 is internally connected to EXTI Line 29
51           (++) COMP4 is internally connected to EXTI Line 30
52           (++) COMP5 is internally connected to EXTI Line 31
53           (++) COMP6 is internally connected to EXTI Line 32
54           (++) COMP7 is internally connected to EXTI Line 33.
55 
56           From the corresponding IRQ handler, the right interrupt source can be retrieved with the
57           adequate macro __HAL_COMP_COMPx_EXTI_GET_FLAG().
58 
59 
60             ##### How to use this driver #####
61 ================================================================================
62   [..]
63       This driver provides functions to configure and program the Comparators of all STM32F3xx devices.
64 
65       To use the comparator, perform the following steps:
66 
67       (#) Fill in the HAL_COMP_MspInit() to
68       (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
69       (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
70            output to the GPIO pin
71       (++) If required enable the COMP interrupt (EXTI line Interrupt): by configuring and enabling EXTI line in Interrupt mode and
72            selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
73            interrupt vector using HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() functions.
74 
75       (#) Configure the comparator using HAL_COMP_Init() function:
76       (++) Select the inverting input (input minus)
77       (++) Select the non-inverting input (input plus)
78       (++) Select the output polarity
79       (++) Select the output redirection
80       (++) Select the hysteresis level
81       (++) Select the power mode
82       (++) Select the event/interrupt mode
83 
84       -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order
85           to enable the comparator(s).
86 
87       (#) On-the-fly reconfiguration of comparator(s) may be done by calling again HAL_COMP_Init(
88           function with new input parameter values; HAL_COMP_MspInit() function shall be adapted
89           to support multi configurations.
90 
91       (#) Enable the comparator using HAL_COMP_Start() or HAL_COMP_Start_IT() functions.
92 
93       (#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions
94           to manage comparator outputs (events and output level).
95 
96       (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
97           function.
98 
99       (#) De-initialize the comparator using HAL_COMP_DeInit() function.
100 
101       (#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function.
102           Only a MCU reset can reset that protection.
103 
104     *** Callback registration ***
105     =============================================
106     [..]
107 
108      The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
109      allows the user to configure dynamically the driver callbacks.
110      Use Functions HAL_COMP_RegisterCallback()
111      to register an interrupt callback.
112     [..]
113 
114      Function HAL_COMP_RegisterCallback() allows to register following callbacks:
115        (+) OperationCpltCallback : callback for End of operation.
116        (+) ErrorCallback         : callback for error detection.
117        (+) MspInitCallback       : callback for Msp Init.
118        (+) MspDeInitCallback     : callback for Msp DeInit.
119      This function takes as parameters the HAL peripheral handle, the Callback ID
120      and a pointer to the user callback function.
121     [..]
122 
123      Use function HAL_COMP_UnRegisterCallback to reset a callback to the default
124      weak function.
125     [..]
126 
127      HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
128      and the Callback ID.
129      This function allows to reset following callbacks:
130        (+) OperationCpltCallback : callback for End of operation.
131        (+) ErrorCallback         : callback for error detection.
132        (+) MspInitCallback       : callback for Msp Init.
133        (+) MspDeInitCallback     : callback for Msp DeInit.
134      [..]
135 
136      By default, after the HAL_COMP_Init() and when the state is HAL_COMP_STATE_RESET
137      all callbacks are set to the corresponding weak functions:
138      examples HAL_COMP_OperationCpltCallback(), HAL_COMP_ErrorCallback().
139      Exception done for MspInit and MspDeInit functions that are
140      reset to the legacy weak functions in the HAL_COMP_Init()/ HAL_COMP_DeInit() only when
141      these callbacks are null (not registered beforehand).
142     [..]
143 
144      If MspInit or MspDeInit are not null, the HAL_COMP_Init()/ HAL_COMP_DeInit()
145      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
146      [..]
147 
148      Callbacks can be registered/unregistered in HAL_COMP_STATE_READY state only.
149      Exception done MspInit/MspDeInit functions that can be registered/unregistered
150      in HAL_COMP_STATE_READY or HAL_COMP_STATE_RESET state,
151      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
152     [..]
153 
154      Then, the user first registers the MspInit/MspDeInit user callbacks
155      using HAL_COMP_RegisterCallback() before calling HAL_COMP_DeInit()
156      or HAL_COMP_Init() function.
157      [..]
158 
159      When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
160      not defined, the callback registration feature is not available and all callbacks
161      are set to the corresponding weak functions.
162 
163   ******************************************************************************
164   */
165 
166 /*
167   Additional Tables:
168 
169     Table 1. COMP Inputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
170     +------------------------------------------------------------------------------------------+
171     |                 |                | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
172     |-----------------|----------------|---------------|---------------------------------------|
173     |                 | 1U/4 VREFINT    |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |
174     |                 | 1U/2 VREFINT    |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |
175     |                 | 3U/4 VREFINT    |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |
176     | Inverting Input | VREFINT        |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |
177     |                 | DAC1 OUT (PA4) |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |
178     |                 | DAC2 OUT (PA5) |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |  OK   |
179     |                 | IO1            |  PA0  |  PA2  |  PD15U |  PE8  |  PD13U |  PD10U |  PC0  |
180     |                 | IO2            |  ---  |  ---  |  PB12U |  PB2  |  PB10U |  PB15U |  ---  |
181     |-----------------|----------------|-------|-------|-------|-------|-------|-------|-------|
182     |  Non Inverting  | IO1            |  PA1  |  PA7  |  PB14U |  PB0  |  PD12U |  PD11U |  PA0  |
183     |    Input        | IO2            |  ---  |  PA3  |  PD14U |  PE7  |  PB13U |  PB11U |  PC1  |
184     +------------------------------------------------------------------------------------------+
185 
186     Table 2. COMP Outputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
187     +-------------------------------------------------------+
188     | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
189     |-------|-------|-------|-------|-------|-------|-------|
190     |  PA0  |  PA2  |  PB1  |  PC8  |  PC7  |  PA10U |  PC2  |
191     |  PF4  |  PA7  |  ---  |  PA8  |  PA9  |  PC6  |  ---  |
192     |  PA6  |  PA12U |  ---  |  ---  |  ---  |  ---  |  ---  |
193     |  PA11U |  PB9  |  ---  |  ---  |  ---  |  ---  |  ---  |
194     |  PB8  |  ---  |  ---  |  ---  |  ---  |  ---  |  ---  |
195     +-------------------------------------------------------+
196 
197     Table 3. COMP Outputs redirection to embedded timers for the STM32F303xB/STM32F303xC devices
198     +----------------------------------------------------------------------------------------------------------------------+
199     |     COMP1      |     COMP2      |     COMP3      |     COMP4      |     COMP5      |     COMP6      |     COMP7      |
200     |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
201     |  TIM1 BKIN     |  TIM1 BKIN     |  TIM1 BKIN     |  TIM1 BKIN     |  TIM1 BKIN     |  TIM1 BKIN     |  TIM1 BKIN     |
202     |                |                |                |                |                |                |                |
203     |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |
204     |                |                |                |                |                |                |                |
205     |  TIM8 BKIN     |  TIM8 BKIN     |  TIM8 BKIN     |  TIM8 BKIN     |  TIM8 BKIN     |  TIM8 BKIN     |  TIM8 BKIN     |
206     |                |                |                |                |                |                |                |
207     |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |
208     |                |                |                |                |                |                |                |
209     |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |
210     |     +          |     +          |     +          |     +          |     +          |     +          |     +          |
211     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |
212     |                |                |                |                |                |                |                |
213     |  TIM1 OCREFCLR |  TIM1 OCREFCLR |  TIM1 OCREFCLR |  TIM8 OCREFCLR |  TIM8 OCREFCLR |  TIM8 OCREFCLR |  TIM1 OCREFCLR |
214     |                |                |                |                |                |                |                |
215     |  TIM1 IC1      |  TIM1 IC1      |  TIM2 OCREFCLR |  TIM3 IC3      |  TIM2 IC1      |  TIM2 IC2      |  TIM8 OCREFCLR |
216     |                |                |                |                |                |                |                |
217     |  TIM2 IC4      |  TIM2 IC4      |  TIM3 IC2      |  TIM3 OCREFCLR |  TIM3 OCREFCLR |  TIM2 OCREFCLR |  TIM2 IC3      |
218     |                |                |                |                |                |                |                |
219     |  TIM2 OCREFCLR |  TIM2 OCREFCLR |  TIM4 IC1      |  TIM4 IC2      |  TIM4 IC3      |  TIM16 OCREFCLR|  TIM1 IC2      |
220     |                |                |                |                |                |                |                |
221     |  TIM3 IC1      |  TIM3 IC1      |  TIM15 IC1     |  TIM15 OCREFCLR|  TIM16 BKIN    |  TIM16 IC1     |  TIM17 OCREFCLR|
222     |                |                |                |                |                |                |                |
223     |  TIM3 OCREFCLR |  TIM3 OCREFCLR |  TIM15 BKIN    |  TIM15 IC2     |  TIM17 IC1     |  TIM4 IC4      |  TIM17 BKIN    |
224     +----------------------------------------------------------------------------------------------------------------------+
225 
226     Table 4. COMP Outputs redirection to embedded timers for the STM32F303xE devices
227     +----------------------------------------------------------------------------------------------------------------------+
228     |     COMP1      |     COMP2      |     COMP3      |     COMP4      |     COMP5      |     COMP6      |     COMP7      |
229     |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
230     |  TIM1 BKIN     |  TIM1 BKIN     |  TIM1 BKIN     |  TIM1 BKIN (1U) |  TIM1 BKIN     |  TIM1 BKIN     |  TIM1 BKIN (1U) |
231     |                |                |                |                |                |                |                |
232     |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |
233     |                |                |                |                |                |                |                |
234     |  TIM8 BKIN     |  TIM8 BKIN     |  TIM8 BKIN     |  TIM8 BKIN (1U) |  TIM8 BKIN     |  TIM8 BKIN     |  TIM8 BKIN (1U) |
235     |                |                |                |                |                |                |                |
236     |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |
237     |                |                |                |                |                |                |                |
238     |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |
239     |     +          |     +          |     +          |     +          |     +          |     +          |     +          |
240     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |  TIM8BKIN2     |
241     |                |                |                |                |                |                |                |
242     |  TIM1 OCREFCLR |  TIM1 OCREFCLR |  TIM1 OCREFCLR |  TIM8 OCREFCLR |  TIM8 OCREFCLR |  TIM8 OCREFCLR |  TIM1 OCREFCLR |
243     |                |                |                |                |                |                |                |
244     |  TIM1 IC1      |  TIM1 IC1      |  TIM2 OCREFCLR |  TIM3 IC3      |  TIM2 IC1      |  TIM2 IC2      |  TIM8 OCREFCLR |
245     |                |                |                |                |                |                |                |
246     |  TIM2 IC4      |  TIM2 IC4      |  TIM3 IC2      |  TIM3 OCREFCLR |  TIM3 OCREFCLR |  TIM2 OCREFCLR |  TIM2 IC3      |
247     |                |                |                |                |                |                |                |
248     |  TIM2 OCREFCLR |  TIM2 OCREFCLR |  TIM4 IC1      |  TIM4 IC2      |  TIM4 IC3      |  TIM16 OCREFCLR|  TIM1 IC2      |
249     |                |                |                |                |                |                |                |
250     |  TIM3 IC1      |  TIM3 IC1      |  TIM15 IC1     |  TIM15 OCREFCLR|  TIM16 BKIN    |  TIM16 IC1     |  TIM17 OCREFCLR|
251     |                |                |                |                |                |                |                |
252     |  TIM3 OCREFCLR |  TIM3 OCREFCLR |  TIM15 BKIN    |  TIM15 IC2     |  TIM17 IC1     |  TIM4 IC4      |  TIM17 BKIN    |
253     |                |                |                |                |                |                |                |
254     |  TIM20 BKIN    |  TIM20 BKIN    |  TIM20 BKIN    |  TIM20 BKIN (1U)|  TIM20 BKIN    |  TIM20 BKIN    |  TIM20 BKIN (1U)|
255     |                |                |                |                |                |                |                |
256     |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |
257     |                |                |                |                |                |                |                |
258     |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |  TIM1 BKIN2    |
259     |     +          |     +          |     +          |     +          |     +          |     +          |     +          |
260     |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |  TIM8 BKIN2    |
261     |     +          |     +          |     +          |     +          |     +          |     +          |     +          |
262     |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |  TIM20 BKIN2   |
263     |                |                |                |                |                |                |                |
264     +----------------------------------------------------------------------------------------------------------------------+
265     (1U):  This connection consists of connecting both GPIO and COMP output to TIM1/8U/20 BRK input through an OR gate, instead
266           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
267           add a digital filter (3  bits) on the COMP output.
268 
269     Table 5. COMP Outputs blanking sources for the STM32F303xB/STM32F303xC/STM32F303xE devices
270     +----------------------------------------------------------------------------------------------------------------------+
271     |     COMP1      |     COMP2      |     COMP3      |     COMP4      |     COMP5      |     COMP6      |     COMP7      |
272     |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
273     |  TIM1 OC5      |  TIM1 OC5      |  TIM1 OC5      |  TIM3 OC4      |  --------      |  TIM8 OC5      |  TIM1 OC5      |
274     |                |                |                |                |                |                |                |
275     |  TIM2 OC3      |  TIM2 OC3      |  --------      |  TIM8 OC5      |  TIM3 OC3      |  TIM2 OC4      |  TIM8 OC5      |
276     |                |                |                |                |                |                |                |
277     |  TIM3 OC3      |  TIM3 OC3      |  TIM2 OC4      |  TIM15 OC1     |  TIM8 OC5      |  TIM15 OC2     |  TIM15 OC2     |
278     |                |                |                |                |                |                |                |
279     +----------------------------------------------------------------------------------------------------------------------+
280 
281 */
282 
283 /* Includes ------------------------------------------------------------------*/
284 #include "stm32f3xx_hal.h"
285 
286 /** @addtogroup STM32F3xx_HAL_Driver
287   * @{
288   */
289 
290 /** @defgroup COMP COMP
291   * @brief COMP HAL module driver
292   * @{
293   */
294 
295 #ifdef HAL_COMP_MODULE_ENABLED
296 
297 /* Private typedef -----------------------------------------------------------*/
298 /* Private define ------------------------------------------------------------*/
299 /** @defgroup COMP_Private_Constants COMP Private Constants
300   * @{
301   */
302 #define COMP_LOCK_DISABLE                      (0x00000000U)
303 #define COMP_LOCK_ENABLE                       COMP_CSR_COMPxLOCK
304 
305 /* Delay for COMP startup time.                                               */
306 /* Note: Delay required to reach propagation delay specification.             */
307 /* Literal set to maximum value (refer to device datasheet,                   */
308 /* parameter "tSTART").                                                       */
309 /* Unit: us                                                                   */
310 #define COMP_DELAY_STARTUP_US          (80UL)      /*!< Delay for COMP startup time */
311 
312 /* Delay for COMP voltage scaler stabilization time.                          */
313 /* Literal set to maximum value (refer to device datasheet,                   */
314 /* parameter "tSTART_SCALER").                                                */
315 /* Unit: us                                                                   */
316 #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200UL)  /*!< Delay for COMP voltage scaler stabilization time */
317 
318 /**
319   * @}
320   */
321 
322 /* Private macro -------------------------------------------------------------*/
323 /* Private variables ---------------------------------------------------------*/
324 /* Private function prototypes -----------------------------------------------*/
325 /* Exported functions --------------------------------------------------------*/
326 
327 /** @defgroup COMP_Exported_Functions COMP Exported Functions
328   * @{
329   */
330 
331 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
332  *  @brief    Initialization and de-initialization functions.
333  *
334 @verbatim
335  ===============================================================================
336               ##### Initialization and de-initialization functions #####
337  ===============================================================================
338     [..]  This section provides functions to initialize and de-initialize comparators.
339 
340 @endverbatim
341   * @{
342   */
343 
344 /**
345   * @brief  Initialize the COMP peripheral according to the specified
346   *         parameters in the COMP_InitTypeDef and initialize the associated handle.
347   * @note   If the selected comparator is locked, initialization cannot be performed.
348   *         To unlock the configuration, perform a system reset.
349   * @param  hcomp  COMP handle
350   * @retval HAL status
351   */
HAL_COMP_Init(COMP_HandleTypeDef * hcomp)352 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
353 {
354   uint32_t comp_voltage_scaler_initialized; /* Value "0" if comparator voltage scaler is not initialized */
355   __IO uint32_t wait_loop_index = 0UL;
356   HAL_StatusTypeDef status = HAL_OK;
357 
358   /* Check the COMP handle allocation and lock status */
359   if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
360   {
361     status = HAL_ERROR;
362   }
363   else
364   {
365     /* Check the parameters */
366     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
367     assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
368     assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
369     assert_param(IS_COMP_NONINVERTINGINPUT_INSTANCE(hcomp->Instance, hcomp->Init.NonInvertingInput));
370     assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
371     assert_param(IS_COMP_OUTPUT_INSTANCE(hcomp->Instance, hcomp->Init.Output));
372     assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
373     assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
374     assert_param(IS_COMP_MODE(hcomp->Init.Mode));
375     assert_param(IS_COMP_BLANKINGSRCE(hcomp->Init.BlankingSrce));
376     assert_param(IS_COMP_BLANKINGSRCE_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
377     assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
378 
379     if (hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
380     {
381       assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
382       assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
383     }
384 
385     /* Init SYSCFG and the low level hardware to access comparators */
386     __HAL_RCC_SYSCFG_CLK_ENABLE();
387 
388 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
389       /* Init the COMP Callback settings */
390       hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
391 
392       if (hcomp->MspInitCallback == NULL)
393       {
394         hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit  */
395       }
396 
397       /* Init the low level hardware */
398       hcomp->MspInitCallback(hcomp);
399 #else
400     /* Init the low level hardware : SYSCFG to access comparators */
401       HAL_COMP_MspInit(hcomp);
402 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
403 
404     /* Memorize voltage scaler state before initialization */
405     comp_voltage_scaler_initialized = READ_BIT(hcomp->Instance->CSR, (COMP_CSR_COMPxINSEL_1 | COMP_CSR_COMPxINSEL_0));
406 
407     if (hcomp->State == HAL_COMP_STATE_RESET)
408     {
409       /* Allocate lock resource and initialize it */
410       hcomp->Lock = HAL_UNLOCKED;
411     }
412 
413     /* Manage inverting input comparator inverting input connected to a GPIO  */
414     /* for STM32F302x, STM32F32xx, STM32F33x.                                 */
415     hcomp->Init.InvertingInput = COMP_INVERTINGINPUT_SELECTION(hcomp->Instance, hcomp->Init.InvertingInput);
416 
417     /* Set COMP parameters */
418     /*     Set COMPxINSEL bits according to hcomp->Init.InvertingInput value        */
419     /*     Set COMPxNONINSEL bits according to hcomp->Init.NonInvertingInput value  */
420     /*     Set COMPxBLANKING bits according to hcomp->Init.BlankingSrce value       */
421     /*     Set COMPxOUTSEL bits according to hcomp->Init.Output value               */
422     /*     Set COMPxPOL bit according to hcomp->Init.OutputPol value                */
423     /*     Set COMPxHYST bits according to hcomp->Init.Hysteresis value             */
424     /*     Set COMPxMODE bits according to hcomp->Init.Mode value                   */
425     COMP_INIT(hcomp);
426 
427     /* Delay for COMP scaler bridge voltage stabilization */
428     /* Apply the delay if voltage scaler bridge is required and not already enabled */
429     if ((READ_BIT(hcomp->Instance->CSR, (COMP_CSR_COMPxINSEL_1 | COMP_CSR_COMPxINSEL_0)) != 0UL) &&
430         (comp_voltage_scaler_initialized == 0UL))
431     {
432       /* Wait loop initialization and execution */
433       /* Note: Variable divided by 2 to compensate partially              */
434       /*       CPU processing cycles, scaling in us split to not          */
435       /*       exceed 32 bits register capacity and handle low frequency. */
436       wait_loop_index = ((COMP_DELAY_VOLTAGE_SCALER_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
437       while (wait_loop_index != 0UL)
438       {
439         wait_loop_index--;
440       }
441     }
442 
443     /* Initialize the COMP state*/
444     hcomp->State = HAL_COMP_STATE_READY;
445   }
446 
447   return status;
448 }
449 
450 /**
451   * @brief  DeInitialize the COMP peripheral.
452   * @note   If the selected comparator is locked, deinitialization cannot be performed.
453   *         To unlock the configuration, perform a system reset.
454   * @param  hcomp  COMP handle
455   * @retval HAL status
456   */
HAL_COMP_DeInit(COMP_HandleTypeDef * hcomp)457 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
458 {
459   HAL_StatusTypeDef status = HAL_OK;
460 
461   /* Check the COMP handle allocation and lock status */
462   if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
463   {
464     status = HAL_ERROR;
465   }
466   else
467   {
468     /* Check the parameter */
469     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
470 
471     /* Set COMP_CSR register to reset value */
472     COMP_DEINIT(hcomp);
473 
474 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
475     if (hcomp->MspDeInitCallback == NULL)
476     {
477       hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit  */
478     }
479 
480     /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
481     hcomp->MspDeInitCallback(hcomp);
482 #else
483     /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
484     HAL_COMP_MspDeInit(hcomp);
485 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
486 
487     hcomp->State = HAL_COMP_STATE_RESET;
488 
489     /* Release Lock */
490     __HAL_UNLOCK(hcomp);
491   }
492 
493   return status;
494 }
495 
496 /**
497   * @brief  Initialize the COMP MSP.
498   * @param  hcomp  COMP handle
499   * @retval None
500   */
HAL_COMP_MspInit(COMP_HandleTypeDef * hcomp)501 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
502 {
503   /* Prevent unused argument(s) compilation warning */
504   UNUSED(hcomp);
505 
506   /* NOTE : This function should not be modified, when the callback is needed,
507             the HAL_COMP_MspInit could be implemented in the user file
508    */
509 }
510 
511 /**
512   * @brief  DeInitialize the COMP MSP.
513   * @param  hcomp  COMP handle
514   * @retval None
515   */
HAL_COMP_MspDeInit(COMP_HandleTypeDef * hcomp)516 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
517 {
518   /* Prevent unused argument(s) compilation warning */
519   UNUSED(hcomp);
520 
521   /* NOTE : This function should not be modified, when the callback is needed,
522             the HAL_COMP_MspDeInit could be implemented in the user file
523    */
524 }
525 
526 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
527 /**
528   * @brief  Register a User COMP Callback
529   *         To be used instead of the weak predefined callback
530   * @param  hcomp Pointer to a COMP_HandleTypeDef structure that contains
531   *                the configuration information for the specified COMP.
532   * @param  CallbackID ID of the callback to be registered
533   *         This parameter can be one of the following values:
534   *          @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
535   *          @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
536   *          @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
537   * @param  pCallback pointer to the Callback function
538   * @retval HAL status
539   */
HAL_COMP_RegisterCallback(COMP_HandleTypeDef * hcomp,HAL_COMP_CallbackIDTypeDef CallbackID,pCOMP_CallbackTypeDef pCallback)540 HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
541 {
542   HAL_StatusTypeDef status = HAL_OK;
543 
544   if (pCallback == NULL)
545   {
546     /* Update the error code */
547     hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
548 
549     return HAL_ERROR;
550   }
551 
552   if (HAL_COMP_STATE_READY == hcomp->State)
553   {
554     switch (CallbackID)
555     {
556       case HAL_COMP_TRIGGER_CB_ID :
557         hcomp->TriggerCallback = pCallback;
558         break;
559 
560       case HAL_COMP_MSPINIT_CB_ID :
561         hcomp->MspInitCallback = pCallback;
562         break;
563 
564       case HAL_COMP_MSPDEINIT_CB_ID :
565         hcomp->MspDeInitCallback = pCallback;
566         break;
567 
568       default :
569         /* Update the error code */
570         hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
571 
572         /* Return error status */
573         status = HAL_ERROR;
574         break;
575     }
576   }
577   else if (HAL_COMP_STATE_RESET == hcomp->State)
578   {
579     switch (CallbackID)
580     {
581       case HAL_COMP_MSPINIT_CB_ID :
582         hcomp->MspInitCallback = pCallback;
583         break;
584 
585       case HAL_COMP_MSPDEINIT_CB_ID :
586         hcomp->MspDeInitCallback = pCallback;
587         break;
588 
589       default :
590         /* Update the error code */
591         hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
592 
593         /* Return error status */
594         status = HAL_ERROR;
595         break;
596     }
597   }
598   else
599   {
600     /* Update the error code */
601     hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
602 
603     /* Return error status */
604     status =  HAL_ERROR;
605   }
606 
607   return status;
608 }
609 
610 /**
611   * @brief  Unregister a COMP Callback
612   *         COMP callback is redirected to the weak predefined callback
613   * @param  hcomp Pointer to a COMP_HandleTypeDef structure that contains
614   *                the configuration information for the specified COMP.
615   * @param  CallbackID ID of the callback to be unregistered
616   *         This parameter can be one of the following values:
617   *          @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
618   *          @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
619   *          @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
620   * @retval HAL status
621   */
HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef * hcomp,HAL_COMP_CallbackIDTypeDef CallbackID)622 HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
623 {
624   HAL_StatusTypeDef status = HAL_OK;
625 
626   if (HAL_COMP_STATE_READY == hcomp->State)
627   {
628     switch (CallbackID)
629     {
630       case HAL_COMP_TRIGGER_CB_ID :
631         hcomp->TriggerCallback = HAL_COMP_TriggerCallback;         /* Legacy weak callback */
632         break;
633 
634       case HAL_COMP_MSPINIT_CB_ID :
635         hcomp->MspInitCallback = HAL_COMP_MspInit;                 /* Legacy weak MspInit */
636         break;
637 
638       case HAL_COMP_MSPDEINIT_CB_ID :
639         hcomp->MspDeInitCallback = HAL_COMP_MspDeInit;             /* Legacy weak MspDeInit */
640         break;
641 
642       default :
643         /* Update the error code */
644         hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
645 
646         /* Return error status */
647         status =  HAL_ERROR;
648         break;
649     }
650   }
651   else if (HAL_COMP_STATE_RESET == hcomp->State)
652   {
653     switch (CallbackID)
654     {
655       case HAL_COMP_MSPINIT_CB_ID :
656         hcomp->MspInitCallback = HAL_COMP_MspInit;                 /* Legacy weak MspInit */
657         break;
658 
659       case HAL_COMP_MSPDEINIT_CB_ID :
660         hcomp->MspDeInitCallback = HAL_COMP_MspDeInit;             /* Legacy weak MspDeInit */
661         break;
662 
663       default :
664         /* Update the error code */
665         hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
666 
667         /* Return error status */
668         status =  HAL_ERROR;
669         break;
670     }
671   }
672   else
673   {
674     /* Update the error code */
675     hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
676 
677     /* Return error status */
678     status =  HAL_ERROR;
679   }
680 
681   return status;
682 }
683 
684 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
685 
686 /**
687   * @}
688   */
689 
690 /** @defgroup COMP_Exported_Functions_Group2 Start Stop operation functions
691  *  @brief   Start-Stop operation functions.
692  *
693 @verbatim
694  ===============================================================================
695                       ##### Start Stop operation functions #####
696  ===============================================================================
697     [..]  This section provides functions allowing to:
698       (+) Start a comparator without interrupt generation.
699       (+) Stop a comparator without interrupt generation.
700       (+) Start a comparator with interrupt generation.
701       (+) Stop a comparator with interrupt generation.
702       (+) Handle interrupts from a comparator with associated callback function.
703 
704 @endverbatim
705   * @{
706   */
707 
708 /**
709   * @brief  Start the comparator.
710   * @param  hcomp  COMP handle
711   * @retval HAL status
712   */
HAL_COMP_Start(COMP_HandleTypeDef * hcomp)713 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
714 {
715   __IO uint32_t wait_loop_index = 0UL;
716   HAL_StatusTypeDef status = HAL_OK;
717   uint32_t extiline = 0U;
718 
719   /* Check the COMP handle allocation and lock status */
720   if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
721   {
722     status = HAL_ERROR;
723   }
724   else
725   {
726     /* Check the parameter */
727     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
728 
729     if (hcomp->State == HAL_COMP_STATE_READY)
730     {
731       /* Get the EXTI Line output configuration */
732       extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
733 
734       /* Configure the event generation */
735       if ((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_EVENT_RISING | COMP_TRIGGERMODE_EVENT_FALLING)) != RESET)
736       {
737         /* Configure the event trigger rising edge */
738         if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_RISING) != RESET)
739         {
740           COMP_EXTI_RISING_ENABLE(extiline);
741         }
742         else
743         {
744           COMP_EXTI_RISING_DISABLE(extiline);
745         }
746 
747         /* Configure the trigger falling edge */
748         if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_FALLING) != RESET)
749         {
750           COMP_EXTI_FALLING_ENABLE(extiline);
751         }
752         else
753         {
754           COMP_EXTI_FALLING_DISABLE(extiline);
755         }
756 
757         /* Enable EXTI event generation */
758         COMP_EXTI_ENABLE_EVENT(extiline);
759 
760         /* Clear COMP EXTI pending bit */
761         COMP_EXTI_CLEAR_FLAG(extiline);
762       }
763 
764       /* Enable the selected comparator */
765       __HAL_COMP_ENABLE(hcomp);
766 
767       hcomp->State = HAL_COMP_STATE_BUSY;
768 
769       /* Delay for COMP startup time */
770       /* Wait loop initialization and execution */
771       /* Note: Variable divided by 2 to compensate partially              */
772       /*       CPU processing cycles, scaling in us split to not          */
773       /*       exceed 32 bits register capacity and handle low frequency. */
774       wait_loop_index = ((COMP_DELAY_STARTUP_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
775       while (wait_loop_index != 0UL)
776       {
777         wait_loop_index--;
778       }
779     }
780     else
781     {
782       status = HAL_ERROR;
783     }
784   }
785 
786   return status;
787 }
788 
789 /**
790   * @brief  Stop the comparator.
791   * @param  hcomp  COMP handle
792   * @retval HAL status
793   */
HAL_COMP_Stop(COMP_HandleTypeDef * hcomp)794 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
795 {
796   HAL_StatusTypeDef status = HAL_OK;
797 
798   /* Check the COMP handle allocation and lock status */
799   if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
800   {
801     status = HAL_ERROR;
802   }
803   else
804   {
805     /* Check the parameter */
806     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
807 
808     if (hcomp->State == HAL_COMP_STATE_BUSY)
809     {
810       /* Disable the EXTI Line event mode if any */
811       COMP_EXTI_DISABLE_EVENT(COMP_GET_EXTI_LINE(hcomp->Instance));
812 
813       /* Disable the selected comparator */
814       __HAL_COMP_DISABLE(hcomp);
815 
816       hcomp->State = HAL_COMP_STATE_READY;
817     }
818     else
819     {
820       status = HAL_ERROR;
821     }
822   }
823 
824   return status;
825 }
826 
827 /**
828   * @brief  Start the comparator in Interrupt mode.
829   * @param  hcomp  COMP handle
830   * @retval HAL status.
831   */
HAL_COMP_Start_IT(COMP_HandleTypeDef * hcomp)832 HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
833 {
834   HAL_StatusTypeDef status = HAL_OK;
835   uint32_t extiline = 0U;
836 
837   /* Check the COMP handle allocation and lock status */
838   if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
839   {
840     status = HAL_ERROR;
841   }
842   else
843   {
844     /* Check the parameter */
845     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
846 
847     if (hcomp->State == HAL_COMP_STATE_READY)
848     {
849       /* Configure the EXTI event generation */
850       if ((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_IT_RISING | COMP_TRIGGERMODE_IT_FALLING)) != RESET)
851       {
852         /* Get the EXTI Line output configuration */
853         extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
854 
855         /* Configure the trigger rising edge */
856         if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
857         {
858           COMP_EXTI_RISING_ENABLE(extiline);
859         }
860         else
861         {
862           COMP_EXTI_RISING_DISABLE(extiline);
863         }
864         /* Configure the trigger falling edge */
865         if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
866         {
867           COMP_EXTI_FALLING_ENABLE(extiline);
868         }
869         else
870         {
871           COMP_EXTI_FALLING_DISABLE(extiline);
872         }
873 
874         /* Clear COMP EXTI pending bit if any */
875         COMP_EXTI_CLEAR_FLAG(extiline);
876 
877         /* Enable EXTI interrupt mode */
878         COMP_EXTI_ENABLE_IT(extiline);
879 
880         /* Enable the selected comparator */
881         __HAL_COMP_ENABLE(hcomp);
882 
883         hcomp->State = HAL_COMP_STATE_BUSY;
884       }
885       else
886       {
887         status = HAL_ERROR;
888       }
889     }
890     else
891     {
892       status = HAL_ERROR;
893     }
894   }
895 
896   return status;
897 }
898 
899 /**
900   * @brief  Stop the comparator in Interrupt mode.
901   * @param  hcomp  COMP handle
902   * @retval HAL status
903   */
HAL_COMP_Stop_IT(COMP_HandleTypeDef * hcomp)904 HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
905 {
906   HAL_StatusTypeDef status = HAL_OK;
907 
908   /* Disable the EXTI Line interrupt mode */
909   COMP_EXTI_DISABLE_IT(COMP_GET_EXTI_LINE(hcomp->Instance));
910 
911   status = HAL_COMP_Stop(hcomp);
912 
913   return status;
914 }
915 
916 /**
917   * @brief  Comparator IRQ Handler.
918   * @param  hcomp  COMP handle
919   * @retval HAL status
920   */
HAL_COMP_IRQHandler(COMP_HandleTypeDef * hcomp)921 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
922 {
923   uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
924 
925   /* Check COMP EXTI flag */
926   if (COMP_EXTI_GET_FLAG(extiline) != RESET)
927   {
928     /* Clear COMP EXTI pending bit */
929     COMP_EXTI_CLEAR_FLAG(extiline);
930 
931     /* COMP trigger callback */
932 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
933     hcomp->TriggerCallback(hcomp);
934 #else
935     HAL_COMP_TriggerCallback(hcomp);
936 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
937   }
938 }
939 
940 /**
941   * @brief  Comparator trigger callback.
942   * @param  hcomp  COMP handle
943   * @retval None
944   */
HAL_COMP_TriggerCallback(COMP_HandleTypeDef * hcomp)945 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
946 {
947   /* Prevent unused argument(s) compilation warning */
948   UNUSED(hcomp);
949 
950   /* NOTE : This function should not be modified, when the callback is needed,
951             the HAL_COMP_TriggerCallback should be implemented in the user file
952    */
953 }
954 
955 /**
956   * @}
957   */
958 
959 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
960  *  @brief   Management functions.
961  *
962 @verbatim
963  ===============================================================================
964                       ##### Peripheral Control functions #####
965  ===============================================================================
966     [..]
967     This subsection provides a set of functions allowing to control the comparators.
968 
969 @endverbatim
970   * @{
971   */
972 
973 /**
974   * @brief  Lock the selected comparator configuration.
975   * @note   A system reset is required to unlock the comparator configuration.
976   * @param  hcomp  COMP handle
977   * @retval HAL status
978   */
HAL_COMP_Lock(COMP_HandleTypeDef * hcomp)979 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
980 {
981   HAL_StatusTypeDef status = HAL_OK;
982 
983   /* Check the COMP handle allocation and lock status */
984   if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
985   {
986     status = HAL_ERROR;
987   }
988   else
989   {
990     /* Check the parameter */
991     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
992 
993     /* Set lock flag on state */
994     switch (hcomp->State)
995     {
996       case HAL_COMP_STATE_BUSY:
997         hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
998         break;
999       case HAL_COMP_STATE_READY:
1000         hcomp->State = HAL_COMP_STATE_READY_LOCKED;
1001         break;
1002       default:
1003         /* unexpected state */
1004         status = HAL_ERROR;
1005         break;
1006     }
1007   }
1008 
1009   if (status == HAL_OK)
1010   {
1011     /* Set the lock bit corresponding to selected comparator */
1012     __HAL_COMP_LOCK(hcomp);
1013   }
1014 
1015   return status;
1016 }
1017 
1018 /**
1019   * @brief  Return the output level (high or low) of the selected comparator.
1020   *         The output level depends on the selected polarity.
1021   *         If the polarity is not inverted:
1022   *           - Comparator output is low when the non-inverting input is at a lower
1023   *             voltage than the inverting input
1024   *           - Comparator output is high when the non-inverting input is at a higher
1025   *             voltage than the inverting input
1026   *         If the polarity is inverted:
1027   *           - Comparator output is high when the non-inverting input is at a lower
1028   *             voltage than the inverting input
1029   *           - Comparator output is low when the non-inverting input is at a higher
1030   *             voltage than the inverting input
1031   * @param  hcomp  COMP handle
1032   * @retval Returns the selected comparator output level:
1033   *         @arg @ref COMP_OUTPUTLEVEL_LOW
1034   *         @arg @ref COMP_OUTPUTLEVEL_HIGH
1035   *
1036   */
HAL_COMP_GetOutputLevel(COMP_HandleTypeDef * hcomp)1037 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
1038 {
1039   uint32_t level = 0U;
1040 
1041   /* Check the parameter */
1042   assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
1043 
1044   level = READ_BIT(hcomp->Instance->CSR, COMP_CSR_COMPxOUT);
1045 
1046   if (level != 0U)
1047   {
1048     return (COMP_OUTPUTLEVEL_HIGH);
1049   }
1050   return (COMP_OUTPUTLEVEL_LOW);
1051 }
1052 
1053 /**
1054   * @}
1055   */
1056 
1057 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
1058  *  @brief   Peripheral State functions.
1059  *
1060 @verbatim
1061  ===============================================================================
1062                       ##### Peripheral State functions #####
1063  ===============================================================================
1064     [..]
1065     This subsection permits to get in run-time the status of the peripheral.
1066 
1067 @endverbatim
1068   * @{
1069   */
1070 
1071 /**
1072   * @brief  Return the COMP handle state.
1073   * @param  hcomp  COMP handle
1074   * @retval HAL state
1075   */
HAL_COMP_GetState(COMP_HandleTypeDef * hcomp)1076 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
1077 {
1078   /* Check the COMP handle allocation */
1079   if (hcomp == NULL)
1080   {
1081     return HAL_COMP_STATE_RESET;
1082   }
1083 
1084   /* Check the parameter */
1085   assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
1086 
1087   /* Return COMP handle state */
1088   return hcomp->State;
1089 }
1090 
1091 /**
1092   * @brief  Return the COMP error code.
1093   * @param hcomp COMP handle
1094   * @retval COMP error code
1095   */
HAL_COMP_GetError(COMP_HandleTypeDef * hcomp)1096 uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
1097 {
1098   /* Check the parameters */
1099   assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
1100 
1101   return hcomp->ErrorCode;
1102 }
1103 
1104 /**
1105   * @}
1106   */
1107 
1108 /**
1109   * @}
1110   */
1111 
1112 #endif /* HAL_COMP_MODULE_ENABLED */
1113 /**
1114   * @}
1115   */
1116 
1117 /**
1118   * @}
1119   */
1120 
1121