source: trunk/firmware/Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_comp.c

Last change on this file was 6, checked in by f.jahn, 3 months ago
File size: 35.5 KB
Line 
1/**
2  ******************************************************************************
3  * @file    stm32g0xx_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
13  *
14  @verbatim
15================================================================================
16          ##### COMP Peripheral features #####
17================================================================================
18
19  [..]
20      The STM32G0xx device family integrates two analog comparators instances:
21      COMP1, COMP2.
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).
25
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).
30
31      (#) The comparators have interrupt capability through the EXTI controller
32          with wake-up from sleep and stop modes.
33
34      (#) Pairs of comparators instances can be combined in window mode
35          (2 consecutive instances odd and even COMP<x> and COMP<x+1>).
36
37          From the corresponding IRQ handler, the right interrupt source can be retrieved
38          using macro __HAL_COMP_COMPx_EXTI_GET_FLAG().
39
40            ##### How to use this driver #####
41================================================================================
42  [..]
43      This driver provides functions to configure and program the comparator instances
44      of STM32G0xx devices.
45
46      To use the comparator, perform the following steps:
47
48      (#)  Initialize the COMP low level resources by implementing the HAL_COMP_MspInit():
49      (++) Configure the GPIO connected to comparator inputs plus and minus in analog mode
50           using HAL_GPIO_Init().
51      (++) If needed, configure the GPIO connected to comparator output in alternate function mode
52           using HAL_GPIO_Init().
53      (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
54           selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
55           interrupt vector using HAL_NVIC_EnableIRQ() function.
56
57      (#) Configure the comparator using HAL_COMP_Init() function:
58      (++) Select the input minus (inverting input)
59      (++) Select the input plus (non-inverting input)
60      (++) Select the hysteresis
61      (++) Select the blanking source
62      (++) Select the output polarity
63      (++) Select the power mode
64      (++) Select the window mode
65
66      -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE()
67          to enable internal control clock of the comparators.
68          However, this is a legacy strategy. In future STM32 families,
69          COMP clock enable must be implemented by user in "HAL_COMP_MspInit()".
70          Therefore, for compatibility anticipation, it is recommended to
71          implement __HAL_RCC_SYSCFG_CLK_ENABLE() in "HAL_COMP_MspInit()".
72
73      (#) Reconfiguration on-the-fly of comparator can be done by calling again
74          function HAL_COMP_Init() with new input structure parameters values.
75
76      (#) Enable the comparator using HAL_COMP_Start() function.
77
78      (#) Use HAL_COMP_TriggerCallback() or HAL_COMP_GetOutputLevel() functions
79          to manage comparator outputs (events and output level).
80
81      (#) Disable the comparator using HAL_COMP_Stop() function.
82
83      (#) De-initialize the comparator using HAL_COMP_DeInit() function.
84
85      (#) For safety purpose, comparator configuration can be locked using HAL_COMP_Lock() function.
86          The only way to unlock the comparator is a device hardware reset.
87
88    *** Callback registration ***
89    =============================================
90    [..]
91
92     The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
93     allows the user to configure dynamically the driver callbacks.
94     Use Functions @ref HAL_COMP_RegisterCallback()
95     to register an interrupt callback.
96    [..]
97
98     Function @ref HAL_COMP_RegisterCallback() allows to register following callbacks:
99       (+) TriggerCallback       : callback for COMP trigger.
100       (+) MspInitCallback       : callback for Msp Init.
101       (+) MspDeInitCallback     : callback for Msp DeInit.
102     This function takes as parameters the HAL peripheral handle, the Callback ID
103     and a pointer to the user callback function.
104    [..]
105
106     Use function @ref HAL_COMP_UnRegisterCallback to reset a callback to the default
107     weak function.
108    [..]
109
110     @ref HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
111     and the Callback ID.
112     This function allows to reset following callbacks:
113       (+) TriggerCallback       : callback for COMP trigger.
114       (+) MspInitCallback       : callback for Msp Init.
115       (+) MspDeInitCallback     : callback for Msp DeInit.
116     [..]
117
118     By default, after the @ref HAL_COMP_Init() and when the state is @ref HAL_COMP_STATE_RESET
119     all callbacks are set to the corresponding weak functions:
120     example @ref HAL_COMP_TriggerCallback().
121     Exception done for MspInit and MspDeInit functions that are
122     reset to the legacy weak functions in the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit() only when
123     these callbacks are null (not registered beforehand).
124    [..]
125
126     If MspInit or MspDeInit are not null, the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit()
127     keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
128     [..]
129
130     Callbacks can be registered/unregistered in @ref HAL_COMP_STATE_READY state only.
131     Exception done MspInit/MspDeInit functions that can be registered/unregistered
132     in @ref HAL_COMP_STATE_READY or @ref HAL_COMP_STATE_RESET state,
133     thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
134    [..]
135
136     Then, the user first registers the MspInit/MspDeInit user callbacks
137     using @ref HAL_COMP_RegisterCallback() before calling @ref HAL_COMP_DeInit()
138     or @ref HAL_COMP_Init() function.
139     [..]
140
141     When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
142     not defined, the callback registration feature is not available and all callbacks
143     are set to the corresponding weak functions.
144
145  @endverbatim
146  ******************************************************************************
147
148  ******************************************************************************
149  * @attention
150  *
151  * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
152  * All rights reserved.</center></h2>
153  *
154  * This software component is licensed by ST under BSD 3-Clause license,
155  * the "License"; You may not use this file except in compliance with the
156  * License. You may obtain a copy of the License at:
157  *                        opensource.org/licenses/BSD-3-Clause
158  *
159  ******************************************************************************
160  */
161
162/* Includes ------------------------------------------------------------------*/
163#include "stm32g0xx_hal.h"
164
165/** @addtogroup STM32G0xx_HAL_Driver
166  * @{
167  */
168
169#ifdef HAL_COMP_MODULE_ENABLED
170
171#if defined (COMP1) || defined (COMP2)
172
173/** @defgroup COMP COMP
174  * @brief COMP HAL module driver
175  * @{
176  */
177
178/* Private typedef -----------------------------------------------------------*/
179/* Private define ------------------------------------------------------------*/
180/** @addtogroup COMP_Private_Constants
181  * @{
182  */
183
184/* Delay for COMP startup time.                                               */
185/* Note: Delay required to reach propagation delay specification.             */
186/* Literal set to maximum value (refer to device datasheet,                   */
187/* parameter "tSTART").                                                       */
188/* Unit: us                                                                   */
189#define COMP_DELAY_STARTUP_US          (80UL) /*!< Delay for COMP startup time */
190
191/* Delay for COMP voltage scaler stabilization time.                          */
192/* Literal set to maximum value (refer to device datasheet,                   */
193/* parameter "tSTART_SCALER").                                                */
194/* Unit: us                                                                   */
195#define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200UL)  /*!< Delay for COMP voltage scaler stabilization time */
196
197#define COMP_OUTPUT_LEVEL_BITOFFSET_POS    (30UL)
198
199/**
200  * @}
201  */
202
203/* Private macro -------------------------------------------------------------*/
204/* Private variables ---------------------------------------------------------*/
205/* Private function prototypes -----------------------------------------------*/
206/* Exported functions --------------------------------------------------------*/
207
208/** @defgroup COMP_Exported_Functions COMP Exported Functions
209  * @{
210  */
211
212/** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
213  *  @brief    Initialization and de-initialization functions.
214  *
215@verbatim
216 ===============================================================================
217              ##### Initialization and de-initialization functions #####
218 ===============================================================================
219    [..]  This section provides functions to initialize and de-initialize comparators
220
221@endverbatim
222  * @{
223  */
224
225/**
226  * @brief  Initialize the COMP according to the specified
227  *         parameters in the COMP_InitTypeDef and initialize the associated handle.
228  * @note   If the selected comparator is locked, initialization can't be performed.
229  *         To unlock the configuration, perform a system reset.
230  * @param  hcomp  COMP handle
231  * @retval HAL status
232  */
233HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
234{
235  uint32_t tmp_csr;
236  uint32_t exti_line;
237  uint32_t comp_voltage_scaler_initialized; /* Value "0" if comparator voltage scaler is not initialized */
238  __IO uint32_t wait_loop_index = 0UL;
239  HAL_StatusTypeDef status = HAL_OK;
240
241  /* Check the COMP handle allocation and lock status */
242  if(hcomp == NULL)
243  {
244    status = HAL_ERROR;
245  }
246  else if(__HAL_COMP_IS_LOCKED(hcomp))
247  {
248    status = HAL_ERROR;
249  }
250  else
251  {
252    /* Check the parameters */
253    assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
254    assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.InputPlus));
255    assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InputMinus));
256    assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
257    assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode));
258    assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
259    assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
260    assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
261    assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
262    if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
263    {
264      assert_param(IS_COMP_WINDOWOUTPUT(hcomp->Init.WindowOutput));
265    }
266
267    if(hcomp->State == HAL_COMP_STATE_RESET)
268    {
269      /* Allocate lock resource and initialize it */
270      hcomp->Lock = HAL_UNLOCKED;
271
272      /* Set COMP error code to none */
273      COMP_CLEAR_ERRORCODE(hcomp);
274
275
276#if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
277      /* Init the COMP Callback settings */
278      hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
279
280      if (hcomp->MspInitCallback == NULL)
281      {
282        hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit  */
283      }
284
285      /* Init the low level hardware */
286      /* Note: Internal control clock of the comparators must                 */
287      /*       be enabled in "HAL_COMP_MspInit()"                             */
288      /*       using "__HAL_RCC_SYSCFG_CLK_ENABLE()".                         */
289      hcomp->MspInitCallback(hcomp);
290#else
291      /* Init the low level hardware */
292      /* Note: Internal control clock of the comparators must                 */
293      /*       be enabled in "HAL_COMP_MspInit()"                             */
294      /*       using "__HAL_RCC_SYSCFG_CLK_ENABLE()".                         */
295      HAL_COMP_MspInit(hcomp);
296#endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
297    }
298
299    /* Memorize voltage scaler state before initialization */
300    comp_voltage_scaler_initialized = READ_BIT(hcomp->Instance->CSR, (COMP_CSR_INMSEL_1 | COMP_CSR_INMSEL_0));
301
302    /* Set COMP parameters */
303    tmp_csr = (  hcomp->Init.InputMinus
304               | hcomp->Init.InputPlus
305               | hcomp->Init.BlankingSrce
306               | hcomp->Init.Hysteresis
307               | hcomp->Init.OutputPol
308               | hcomp->Init.Mode
309              );
310
311    /* Set parameters in COMP register */
312    /* Note: Update all bits except read-only, lock and enable bits */
313    MODIFY_REG(hcomp->Instance->CSR,
314               COMP_CSR_PWRMODE  | COMP_CSR_INMSEL   | COMP_CSR_INPSEL  |
315               COMP_CSR_WINMODE  | COMP_CSR_POLARITY | COMP_CSR_HYST    |
316               COMP_CSR_BLANKING                                         ,
317               tmp_csr
318              );
319
320    /* Set window mode */
321    /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP     */
322    /*       instances. Therefore, this function can update another COMP      */
323    /*       instance that the one currently selected.                        */
324    if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON)
325    {
326      CLEAR_BIT(COMP12_COMMON->CSR_ODD, COMP_CSR_WINMODE);
327      SET_BIT(COMP12_COMMON->CSR_EVEN, COMP_CSR_WINMODE);
328    }
329    else if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP2_INPUT_PLUS_COMMON)
330    {
331      SET_BIT(COMP12_COMMON->CSR_ODD, COMP_CSR_WINMODE);
332      CLEAR_BIT(COMP12_COMMON->CSR_EVEN, COMP_CSR_WINMODE);
333    }
334    else
335    {
336      CLEAR_BIT(COMP12_COMMON->CSR_ODD, COMP_CSR_WINMODE);
337      CLEAR_BIT(COMP12_COMMON->CSR_EVEN, COMP_CSR_WINMODE);
338    }
339
340    /* Set window mode output */
341    /* Note: Window mode mode output can also be used when window mode        */
342    /*       is disabled, to use comparators in independent mode with their   */
343    /*       output connected through exclusive-or circuitry.                 */
344    switch (hcomp->Init.WindowOutput)
345    {
346      case COMP_WINDOWOUTPUT_COMP1:
347        SET_BIT(COMP12_COMMON->CSR_ODD, COMP_CSR_WINOUT);
348        CLEAR_BIT(COMP12_COMMON->CSR_EVEN, COMP_CSR_WINOUT);
349        break;
350
351      case COMP_WINDOWOUTPUT_COMP2:
352        CLEAR_BIT(COMP12_COMMON->CSR_ODD, COMP_CSR_WINOUT);
353        SET_BIT(COMP12_COMMON->CSR_EVEN, COMP_CSR_WINOUT);
354        break;
355
356      case COMP_WINDOWOUTPUT_BOTH:
357        SET_BIT(COMP12_COMMON->CSR_ODD, COMP_CSR_WINOUT);
358        SET_BIT(COMP12_COMMON->CSR_EVEN, COMP_CSR_WINOUT);
359        break;
360
361      default: /* COMP_WINDOWOUTPUT_EACH_COMP */
362        CLEAR_BIT(COMP12_COMMON->CSR_ODD, COMP_CSR_WINOUT);
363        CLEAR_BIT(COMP12_COMMON->CSR_EVEN, COMP_CSR_WINOUT);
364        break;
365    }
366
367    /* Delay for COMP scaler bridge voltage stabilization */
368    /* Apply the delay if voltage scaler bridge is required and not already enabled */
369    if ((READ_BIT(hcomp->Instance->CSR, (COMP_CSR_INMSEL_1 | COMP_CSR_INMSEL_0)) != 0UL) &&
370        (comp_voltage_scaler_initialized == 0UL)               )
371    {
372      /* Wait loop initialization and execution */
373      /* Note: Variable divided by 2 to compensate partially              */
374      /*       CPU processing cycles, scaling in us split to not          */
375      /*       exceed 32 bits register capacity and handle low frequency. */
376      wait_loop_index = ((COMP_DELAY_VOLTAGE_SCALER_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
377      while(wait_loop_index != 0UL)
378      {
379        wait_loop_index--;
380      }
381    }
382
383    /* Get the EXTI line corresponding to the selected COMP instance */
384    exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
385
386    /* Manage EXTI settings */
387    if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != 0UL)
388    {
389      /* Configure EXTI rising edge */
390      if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != 0UL)
391      {
392        LL_EXTI_EnableRisingTrig_0_31(exti_line);
393      }
394      else
395      {
396        LL_EXTI_DisableRisingTrig_0_31(exti_line);
397      }
398
399      /* Configure EXTI falling edge */
400      if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != 0UL)
401      {
402        LL_EXTI_EnableFallingTrig_0_31(exti_line);
403      }
404      else
405      {
406        LL_EXTI_DisableFallingTrig_0_31(exti_line);
407      }
408
409      /* Clear COMP EXTI pending bit (if any) */
410      LL_EXTI_ClearRisingFlag_0_31(exti_line);
411      LL_EXTI_ClearFallingFlag_0_31(exti_line);
412
413      /* Configure EXTI event mode */
414      if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != 0UL)
415      {
416        LL_EXTI_EnableEvent_0_31(exti_line);
417      }
418      else
419      {
420        LL_EXTI_DisableEvent_0_31(exti_line);
421      }
422
423      /* Configure EXTI interrupt mode */
424      if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != 0UL)
425      {
426        LL_EXTI_EnableIT_0_31(exti_line);
427      }
428      else
429      {
430        LL_EXTI_DisableIT_0_31(exti_line);
431      }
432    }
433    else
434    {
435      /* Disable EXTI event mode */
436      LL_EXTI_DisableEvent_0_31(exti_line);
437
438      /* Disable EXTI interrupt mode */
439      LL_EXTI_DisableIT_0_31(exti_line);
440    }
441
442    /* Set HAL COMP handle state */
443    /* Note: Transition from state reset to state ready,                      */
444    /*       otherwise (coming from state ready or busy) no state update.     */
445    if (hcomp->State == HAL_COMP_STATE_RESET)
446    {
447      hcomp->State = HAL_COMP_STATE_READY;
448    }
449  }
450
451  return status;
452}
453
454/**
455  * @brief  DeInitialize the COMP peripheral.
456  * @note   Deinitialization cannot be performed if the COMP configuration is locked.
457  *         To unlock the configuration, perform a system reset.
458  * @param  hcomp  COMP handle
459  * @retval HAL status
460  */
461HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
462{
463  HAL_StatusTypeDef status = HAL_OK;
464
465  /* Check the COMP handle allocation and lock status */
466  if(hcomp == NULL)
467  {
468    status = HAL_ERROR;
469  }
470  else if(__HAL_COMP_IS_LOCKED(hcomp))
471  {
472    status = HAL_ERROR;
473  }
474  else
475  {
476    /* Check the parameter */
477    assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
478
479    /* Set COMP_CSR register to reset value */
480    WRITE_REG(hcomp->Instance->CSR, 0x00000000UL);
481
482#if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
483    if (hcomp->MspDeInitCallback == NULL)
484    {
485      hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit  */
486    }
487
488    /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
489    hcomp->MspDeInitCallback(hcomp);
490#else
491    /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
492    HAL_COMP_MspDeInit(hcomp);
493#endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
494
495    /* Set HAL COMP handle state */
496    hcomp->State = HAL_COMP_STATE_RESET;
497
498    /* Release Lock */
499    __HAL_UNLOCK(hcomp);
500  }
501
502  return status;
503}
504
505/**
506  * @brief  Initialize the COMP MSP.
507  * @param  hcomp  COMP handle
508  * @retval None
509  */
510__weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
511{
512  /* Prevent unused argument(s) compilation warning */
513  UNUSED(hcomp);
514
515  /* NOTE : This function should not be modified, when the callback is needed,
516            the HAL_COMP_MspInit could be implemented in the user file
517   */
518}
519
520/**
521  * @brief  DeInitialize the COMP MSP.
522  * @param  hcomp  COMP handle
523  * @retval None
524  */
525__weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
526{
527  /* Prevent unused argument(s) compilation warning */
528  UNUSED(hcomp);
529
530  /* NOTE : This function should not be modified, when the callback is needed,
531            the HAL_COMP_MspDeInit could be implemented in the user file
532   */
533}
534
535#if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
536/**
537  * @brief  Register a User COMP Callback
538  *         To be used instead of the weak predefined callback
539  * @param  hcomp Pointer to a COMP_HandleTypeDef structure that contains
540  *                the configuration information for the specified COMP.
541  * @param  CallbackID ID of the callback to be registered
542  *         This parameter can be one of the following values:
543  *          @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
544  *          @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
545  *          @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
546  * @param  pCallback pointer to the Callback function
547  * @retval HAL status
548  */
549HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
550{
551  HAL_StatusTypeDef status = HAL_OK;
552
553  if (pCallback == NULL)
554  {
555    /* Update the error code */
556    hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
557
558    return HAL_ERROR;
559  }
560
561  if (HAL_COMP_STATE_READY == hcomp->State)
562  {
563    switch (CallbackID)
564    {
565      case HAL_COMP_TRIGGER_CB_ID :
566        hcomp->TriggerCallback = pCallback;
567        break;
568
569      case HAL_COMP_MSPINIT_CB_ID :
570        hcomp->MspInitCallback = pCallback;
571        break;
572
573      case HAL_COMP_MSPDEINIT_CB_ID :
574        hcomp->MspDeInitCallback = pCallback;
575        break;
576
577      default :
578        /* Update the error code */
579        hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
580
581        /* Return error status */
582        status = HAL_ERROR;
583        break;
584    }
585  }
586  else if (HAL_COMP_STATE_RESET == hcomp->State)
587  {
588    switch (CallbackID)
589    {
590      case HAL_COMP_MSPINIT_CB_ID :
591        hcomp->MspInitCallback = pCallback;
592        break;
593
594      case HAL_COMP_MSPDEINIT_CB_ID :
595        hcomp->MspDeInitCallback = pCallback;
596        break;
597
598      default :
599        /* Update the error code */
600        hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
601
602        /* Return error status */
603        status = HAL_ERROR;
604        break;
605    }
606  }
607  else
608  {
609    /* Update the error code */
610    hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
611
612    /* Return error status */
613    status =  HAL_ERROR;
614  }
615
616  return status;
617}
618
619/**
620  * @brief  Unregister a COMP Callback
621  *         COMP callback is redirected to the weak predefined callback
622  * @param  hcomp Pointer to a COMP_HandleTypeDef structure that contains
623  *                the configuration information for the specified COMP.
624  * @param  CallbackID ID of the callback to be unregistered
625  *         This parameter can be one of the following values:
626  *          @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
627  *          @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
628  *          @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
629  * @retval HAL status
630  */
631HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
632{
633  HAL_StatusTypeDef status = HAL_OK;
634
635  if (HAL_COMP_STATE_READY == hcomp->State)
636  {
637    switch (CallbackID)
638    {
639      case HAL_COMP_TRIGGER_CB_ID :
640        hcomp->TriggerCallback = HAL_COMP_TriggerCallback;         /* Legacy weak callback */
641        break;
642
643      case HAL_COMP_MSPINIT_CB_ID :
644        hcomp->MspInitCallback = HAL_COMP_MspInit;                 /* Legacy weak MspInit */
645        break;
646
647      case HAL_COMP_MSPDEINIT_CB_ID :
648        hcomp->MspDeInitCallback = HAL_COMP_MspDeInit;             /* Legacy weak MspDeInit */
649        break;
650
651      default :
652        /* Update the error code */
653        hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
654
655        /* Return error status */
656        status =  HAL_ERROR;
657        break;
658    }
659  }
660  else if (HAL_COMP_STATE_RESET == hcomp->State)
661  {
662    switch (CallbackID)
663    {
664      case HAL_COMP_MSPINIT_CB_ID :
665        hcomp->MspInitCallback = HAL_COMP_MspInit;                 /* Legacy weak MspInit */
666        break;
667
668      case HAL_COMP_MSPDEINIT_CB_ID :
669        hcomp->MspDeInitCallback = HAL_COMP_MspDeInit;             /* Legacy weak MspDeInit */
670        break;
671
672      default :
673        /* Update the error code */
674        hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
675
676        /* Return error status */
677        status =  HAL_ERROR;
678        break;
679    }
680  }
681  else
682  {
683    /* Update the error code */
684    hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
685
686    /* Return error status */
687    status =  HAL_ERROR;
688  }
689
690  return status;
691}
692
693#endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
694
695/**
696  * @}
697  */
698
699/** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions
700  *  @brief   Start-Stop operation functions.
701  *
702@verbatim
703 ===============================================================================
704                      ##### IO operation functions #####
705 ===============================================================================
706    [..]  This section provides functions allowing to:
707      (+) Start a comparator instance.
708      (+) Stop a comparator instance.
709
710@endverbatim
711  * @{
712  */
713
714/**
715  * @brief  Start the comparator.
716  * @param  hcomp  COMP handle
717  * @retval HAL status
718  */
719HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
720{
721  __IO uint32_t wait_loop_index = 0UL;
722  HAL_StatusTypeDef status = HAL_OK;
723
724  /* Check the COMP handle allocation and lock status */
725  if(hcomp == NULL)
726  {
727    status = HAL_ERROR;
728  }
729  else if(__HAL_COMP_IS_LOCKED(hcomp))
730  {
731    status = HAL_ERROR;
732  }
733  else
734  {
735    /* Check the parameter */
736    assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
737
738    if(hcomp->State == HAL_COMP_STATE_READY)
739    {
740      /* Enable the selected comparator */
741      SET_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
742
743      /* Set HAL COMP handle state */
744      hcomp->State = HAL_COMP_STATE_BUSY;
745
746      /* Delay for COMP startup time */
747      /* Wait loop initialization and execution */
748      /* Note: Variable divided by 2 to compensate partially              */
749      /*       CPU processing cycles, scaling in us split to not          */
750      /*       exceed 32 bits register capacity and handle low frequency. */
751      wait_loop_index = ((COMP_DELAY_STARTUP_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
752      while(wait_loop_index != 0UL)
753      {
754        wait_loop_index--;
755      }
756    }
757    else
758    {
759      status = HAL_ERROR;
760    }
761  }
762
763  return status;
764}
765
766/**
767  * @brief  Stop the comparator.
768  * @param  hcomp  COMP handle
769  * @retval HAL status
770  */
771HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
772{
773  HAL_StatusTypeDef status = HAL_OK;
774
775  /* Check the COMP handle allocation and lock status */
776  if(hcomp == NULL)
777  {
778    status = HAL_ERROR;
779  }
780  else if(__HAL_COMP_IS_LOCKED(hcomp))
781  {
782    status = HAL_ERROR;
783  }
784  else
785  {
786    /* Check the parameter */
787    assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
788
789    /* Check compliant states: HAL_COMP_STATE_READY or HAL_COMP_STATE_BUSY    */
790    /* (all states except HAL_COMP_STATE_RESET and except locked status.      */
791    if(hcomp->State != HAL_COMP_STATE_RESET)
792    {
793      /* Disable the selected comparator */
794      CLEAR_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
795
796      /* Set HAL COMP handle state */
797      hcomp->State = HAL_COMP_STATE_READY;
798    }
799    else
800    {
801      status = HAL_ERROR;
802    }
803  }
804
805  return status;
806}
807
808/**
809  * @brief  Comparator IRQ handler.
810  * @param  hcomp  COMP handle
811  * @retval None
812  */
813void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
814{
815  /* Get the EXTI line corresponding to the selected COMP instance */
816  uint32_t exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
817  uint32_t comparator_window_mode_odd = READ_BIT(COMP12_COMMON->CSR_ODD, COMP_CSR_WINMODE);
818  uint32_t comparator_window_mode_even = READ_BIT(COMP12_COMMON->CSR_EVEN, COMP_CSR_WINMODE);
819
820  /* Check COMP EXTI flag */
821  if(LL_EXTI_IsActiveRisingFlag_0_31(exti_line) != 0UL)
822  {
823    /* Check whether comparator is in independent or window mode */
824    if(   (comparator_window_mode_odd != 0UL)
825       || (comparator_window_mode_even != 0UL))
826    {
827      /* Clear COMP EXTI line pending bit of the pair of comparators          */
828      /* in window mode.                                                      */
829      /* Note: Pair of comparators in window mode can both trig IRQ when      */
830      /*       input voltage is changing from "out of window" area            */
831      /*       (low or high ) to the other "out of window" area (high or low).*/
832      /*       Both flags must be cleared to call comparator trigger          */
833      /*       callback is called once.                                       */
834      LL_EXTI_ClearRisingFlag_0_31((COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
835    }
836    else
837    {
838      /* Clear COMP EXTI line pending bit */
839      LL_EXTI_ClearRisingFlag_0_31(exti_line);
840    }
841
842    /* COMP trigger user callback */
843#if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
844    hcomp->TriggerCallback(hcomp);
845#else
846    HAL_COMP_TriggerCallback(hcomp);
847#endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
848  }
849  else if(LL_EXTI_IsActiveFallingFlag_0_31(exti_line) != 0UL)
850  {
851    /* Check whether comparator is in independent or window mode */
852    if(   (comparator_window_mode_odd != 0UL)
853       || (comparator_window_mode_even != 0UL))
854    {
855      /* Clear COMP EXTI line pending bit of the pair of comparators          */
856      /* in window mode.                                                      */
857      /* Note: Pair of comparators in window mode can both trig IRQ when      */
858      /*       input voltage is changing from "out of window" area            */
859      /*       (low or high ) to the other "out of window" area (high or low).*/
860      /*       Both flags must be cleared to call comparator trigger          */
861      /*       callback is called once.                                       */
862      LL_EXTI_ClearFallingFlag_0_31((COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
863    }
864    else
865    {
866      /* Clear COMP EXTI line pending bit */
867      LL_EXTI_ClearFallingFlag_0_31(exti_line);
868    }
869
870    /* COMP trigger callback */
871#if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
872    hcomp->TriggerCallback(hcomp);
873#else
874    HAL_COMP_TriggerCallback(hcomp);
875#endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
876  }
877  else
878  {
879    /* nothing to do */
880  }
881}
882
883/**
884  * @}
885  */
886
887/** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
888  *  @brief   Management functions.
889  *
890@verbatim
891 ===============================================================================
892                      ##### Peripheral Control functions #####
893 ===============================================================================
894    [..]
895    This subsection provides a set of functions allowing to control the comparators.
896
897@endverbatim
898  * @{
899  */
900
901/**
902  * @brief  Lock the selected comparator configuration.
903  * @note   A system reset is required to unlock the comparator configuration.
904  * @note   Locking the comparator from reset state is possible
905  *         if __HAL_RCC_SYSCFG_CLK_ENABLE() is being called before.
906  * @param  hcomp  COMP handle
907  * @retval HAL status
908  */
909HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
910{
911  HAL_StatusTypeDef status = HAL_OK;
912
913  /* Check the COMP handle allocation and lock status */
914  if(hcomp == NULL)
915  {
916    status = HAL_ERROR;
917  }
918  else if(__HAL_COMP_IS_LOCKED(hcomp))
919  {
920    status = HAL_ERROR;
921  }
922  else
923  {
924    /* Check the parameter */
925    assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
926
927    /* Set HAL COMP handle state */
928    switch(hcomp->State)
929    {
930      case HAL_COMP_STATE_RESET:
931        hcomp->State = HAL_COMP_STATE_RESET_LOCKED;
932        break;
933      case HAL_COMP_STATE_READY:
934        hcomp->State = HAL_COMP_STATE_READY_LOCKED;
935        break;
936      default: /* HAL_COMP_STATE_BUSY */
937        hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
938        break;
939    }
940  }
941
942  if(status == HAL_OK)
943  {
944    /* Set the lock bit corresponding to selected comparator */
945    __HAL_COMP_LOCK(hcomp);
946  }
947
948  return status;
949}
950
951/**
952  * @brief  Return the output level (high or low) of the selected comparator.
953  *         The output level depends on the selected polarity.
954  *         If the polarity is not inverted:
955  *           - Comparator output is low when the input plus is at a lower
956  *             voltage than the input minus
957  *           - Comparator output is high when the input plus is at a higher
958  *             voltage than the input minus
959  *         If the polarity is inverted:
960  *           - Comparator output is high when the input plus is at a lower
961  *             voltage than the input minus
962  *           - Comparator output is low when the input plus is at a higher
963  *             voltage than the input minus
964  * @param  hcomp  COMP handle
965  * @retval Returns the selected comparator output level:
966  *         @arg COMP_OUTPUT_LEVEL_LOW
967  *         @arg COMP_OUTPUT_LEVEL_HIGH
968  *
969  */
970uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
971{
972  /* Check the parameter */
973  assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
974
975  return (uint32_t)(READ_BIT(hcomp->Instance->CSR, COMP_CSR_VALUE)
976                    >> COMP_OUTPUT_LEVEL_BITOFFSET_POS);
977}
978
979/**
980  * @brief  Comparator trigger callback.
981  * @param  hcomp  COMP handle
982  * @retval None
983  */
984__weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
985{
986  /* Prevent unused argument(s) compilation warning */
987  UNUSED(hcomp);
988
989  /* NOTE : This function should not be modified, when the callback is needed,
990            the HAL_COMP_TriggerCallback should be implemented in the user file
991   */
992}
993
994
995/**
996  * @}
997  */
998
999/** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
1000  *  @brief   Peripheral State functions.
1001  *
1002@verbatim
1003 ===============================================================================
1004                      ##### Peripheral State functions #####
1005 ===============================================================================
1006    [..]
1007    This subsection permit to get in run-time the status of the peripheral.
1008
1009@endverbatim
1010  * @{
1011  */
1012
1013/**
1014  * @brief  Return the COMP handle state.
1015  * @param  hcomp  COMP handle
1016  * @retval HAL state
1017  */
1018HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
1019{
1020  /* Check the COMP handle allocation */
1021  if(hcomp == NULL)
1022  {
1023    return HAL_COMP_STATE_RESET;
1024  }
1025
1026  /* Check the parameter */
1027  assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
1028
1029  /* Return HAL COMP handle state */
1030  return hcomp->State;
1031}
1032
1033/**
1034  * @brief  Return the COMP error code.
1035  * @param hcomp COMP handle
1036  * @retval COMP error code
1037  */
1038uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
1039{
1040  /* Check the parameters */
1041  assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
1042
1043  return hcomp->ErrorCode;
1044}
1045
1046/**
1047  * @}
1048  */
1049
1050/**
1051  * @}
1052  */
1053
1054/**
1055  * @}
1056  */
1057
1058#endif /* COMP1 || COMP2 */
1059
1060#endif /* HAL_COMP_MODULE_ENABLED */
1061
1062/**
1063  * @}
1064  */
1065
1066/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.