Subversion Repositories FuelGauge

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f0xx_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/de-initialization functions
  9.   *           + I/O operation functions
  10.   *           + Peripheral Control functions
  11.   *           + Peripheral State functions
  12.   *        
  13.   @verbatim
  14. ================================================================================
  15.           ##### COMP Peripheral features #####
  16. ================================================================================
  17.            
  18.   [..]      
  19.       The STM32F0xx device family integrates up to 2 analog comparators COMP1 and COMP2:
  20.       (+) The non inverting input and inverting input can be set to GPIO pins.
  21.  
  22.       (+) The COMP output is available using HAL_COMP_GetOutputLevel()
  23.           and can be set on GPIO pins.
  24.  
  25.       (+) The COMP output can be redirected to embedded timers (TIM1, TIM2 and TIM3).
  26.  
  27.       (+) The comparators COMP1 and COMP2 can be combined in window mode.
  28.  
  29.       (+) The comparators have interrupt capability with wake-up
  30.           from Sleep and Stop modes (through the EXTI controller):
  31.           (++) COMP1 is internally connected to EXTI Line 21
  32.           (++) COMP2 is internally connected to EXTI Line 22
  33.  
  34.       (+) From the corresponding IRQ handler, the right interrupt source can be retrieved with the
  35.           macros __HAL_COMP_COMP1_EXTI_GET_FLAG() and __HAL_COMP_COMP2_EXTI_GET_FLAG().
  36.  
  37.  
  38.             ##### How to use this driver #####
  39. ================================================================================
  40.   [..]
  41.       This driver provides functions to configure and program the Comparators of STM32F05x, STM32F07x and STM32F09x devices.
  42.  
  43.       To use the comparator, perform the following steps:
  44.  
  45.       (#) Fill in the HAL_COMP_MspInit() to
  46.       (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
  47.       (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
  48.            output to the GPIO pin
  49.       (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
  50.            selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
  51.            interrupt vector using HAL_NVIC_EnableIRQ() function.
  52.  
  53.       (#) Configure the comparator using HAL_COMP_Init() function:
  54.       (++) Select the inverting input (input minus)
  55.       (++) Select the non inverting input (input plus)
  56.       (++) Select the output polarity  
  57.       (++) Select the output redirection
  58.       (++) Select the hysteresis level
  59.       (++) Select the power mode
  60.       (++) Select the event/interrupt mode
  61.       (++) Select the window mode
  62.  
  63.       -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order
  64.           to access the comparator(s) registers.
  65.  
  66.       (#) Enable the comparator using HAL_COMP_Start() function or HAL_COMP_Start_IT() function for interrupt mode.
  67.    
  68.       (#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions
  69.           to manage comparator outputs (event/interrupt triggered and output level).
  70.  
  71.       (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
  72.           function.
  73.  
  74.       (#) De-initialize the comparator using HAL_COMP_DeInit() function.
  75.  
  76.       (#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function.
  77.           Only a MCU reset can reset that protection.
  78.  
  79.     *** Callback registration ***
  80.     =============================================
  81.     [..]
  82.  
  83.      The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
  84.      allows the user to configure dynamically the driver callbacks.
  85.      Use Functions HAL_COMP_RegisterCallback()
  86.      to register an interrupt callback.
  87.     [..]
  88.  
  89.      Function HAL_COMP_RegisterCallback() allows to register following callbacks:
  90.        (+) OperationCpltCallback : callback for End of operation.
  91.        (+) ErrorCallback         : callback for error detection.
  92.        (+) MspInitCallback       : callback for Msp Init.
  93.        (+) MspDeInitCallback     : callback for Msp DeInit.
  94.      This function takes as parameters the HAL peripheral handle, the Callback ID
  95.      and a pointer to the user callback function.
  96.     [..]
  97.  
  98.      Use function HAL_COMP_UnRegisterCallback to reset a callback to the default
  99.      weak function.
  100.     [..]
  101.  
  102.      HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
  103.      and the Callback ID.
  104.      This function allows to reset following callbacks:
  105.        (+) OperationCpltCallback : callback for End of operation.
  106.        (+) ErrorCallback         : callback for error detection.
  107.        (+) MspInitCallback       : callback for Msp Init.
  108.        (+) MspDeInitCallback     : callback for Msp DeInit.
  109.      [..]
  110.  
  111.      By default, after the HAL_COMP_Init() and when the state is HAL_COMP_STATE_RESET
  112.      all callbacks are set to the corresponding weak functions:
  113.      examples HAL_COMP_OperationCpltCallback(), HAL_COMP_ErrorCallback().
  114.      Exception done for MspInit and MspDeInit functions that are
  115.      reset to the legacy weak functions in the HAL_COMP_Init()/ HAL_COMP_DeInit() only when
  116.      these callbacks are null (not registered beforehand).
  117.     [..]
  118.  
  119.      If MspInit or MspDeInit are not null, the HAL_COMP_Init()/ HAL_COMP_DeInit()
  120.      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  121.      [..]
  122.  
  123.      Callbacks can be registered/unregistered in HAL_COMP_STATE_READY state only.
  124.      Exception done MspInit/MspDeInit functions that can be registered/unregistered
  125.      in HAL_COMP_STATE_READY or HAL_COMP_STATE_RESET state,
  126.      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  127.     [..]
  128.  
  129.      Then, the user first registers the MspInit/MspDeInit user callbacks
  130.      using HAL_COMP_RegisterCallback() before calling HAL_COMP_DeInit()
  131.      or HAL_COMP_Init() function.
  132.      [..]
  133.  
  134.      When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
  135.      not defined, the callback registration feature is not available and all callbacks
  136.      are set to the corresponding weak functions.
  137.  
  138.   @endverbatim
  139.   ******************************************************************************
  140.   * @attention
  141.   *
  142.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  143.   * All rights reserved.</center></h2>
  144.   *
  145.   * This software component is licensed by ST under BSD 3-Clause license,
  146.   * the "License"; You may not use this file except in compliance with the
  147.   * License. You may obtain a copy of the License at:
  148.   *                        opensource.org/licenses/BSD-3-Clause
  149.   *
  150.   ******************************************************************************
  151.   */
  152.  
  153. /*
  154.     Additional Tables:
  155.  
  156.     Table 1. COMP Inputs for the STM32F05x, STM32F07x and STM32F09x devices
  157.     +--------------------------------------------------+    
  158.     |                 |                | COMP1 | COMP2 |
  159.     |-----------------|----------------|---------------|
  160.     |                 | 1/4 VREFINT    |  OK   |  OK   |
  161.     |                 | 1/2 VREFINT    |  OK   |  OK   |
  162.     |                 | 3/4 VREFINT    |  OK   |  OK   |
  163.     | Inverting Input | VREFINT        |  OK   |  OK   |
  164.     |                 | DAC1 OUT (PA4) |  OK   |  OK   |
  165.     |                 | DAC2 OUT (PA5) |  OK   |  OK   |
  166.     |                 | IO1            |  PA0  |  PA2  |
  167.     |-----------------|----------------|-------|-------|
  168.     |  Non Inverting  |                |  PA1  |  PA3  |
  169.     |    Input        |                |       |       |
  170.     +--------------------------------------------------+  
  171.  
  172.     Table 2. COMP Outputs for the STM32F05x, STM32F07x and STM32F09x devices
  173.     +---------------+    
  174.     | COMP1 | COMP2 |
  175.     |-------|-------|
  176.     |  PA0  |  PA2  |
  177.     |  PA6  |  PA7  |
  178.     |  PA11 |  PA12 |
  179.     +---------------+
  180.  
  181.     Table 3. COMP Outputs redirection to embedded timers for the STM32F05x, STM32F07x and STM32F09x devices
  182.     +---------------------------------+    
  183.     |     COMP1      |     COMP2      |
  184.     |----------------|----------------|
  185.     |  TIM1 BKIN     |  TIM1 BKIN     |
  186.     |                |                |
  187.     |  TIM1 OCREFCLR |  TIM1 OCREFCLR |
  188.     |                |                |
  189.     |  TIM1 IC1      |  TIM1 IC1      |
  190.     |                |                |
  191.     |  TIM2 IC4      |  TIM2 IC4      |
  192.     |                |                |
  193.     |  TIM2 OCREFCLR |  TIM2 OCREFCLR |
  194.     |                |                |
  195.     |  TIM3 IC1      |  TIM3 IC1      |
  196.     |                |                |
  197.     |  TIM3 OCREFCLR |  TIM3 OCREFCLR |
  198.     +---------------------------------+
  199.  
  200. */
  201.  
  202. /* Includes ------------------------------------------------------------------*/
  203. #include "stm32f0xx_hal.h"
  204.  
  205. #ifdef HAL_COMP_MODULE_ENABLED
  206.  
  207. #if defined (COMP1) || defined (COMP2)
  208.  
  209. /** @addtogroup STM32F0xx_HAL_Driver
  210.   * @{
  211.   */
  212.  
  213. /** @defgroup COMP COMP
  214.   * @brief COMP HAL module driver
  215.   * @{
  216.   */
  217.  
  218. /* Private typedef -----------------------------------------------------------*/
  219. /* Private define ------------------------------------------------------------*/
  220.  
  221. /** @defgroup COMP_Private_Constants COMP Private Constants
  222.   * @{
  223.   */
  224.  
  225. /* Delay for COMP startup time.                                               */
  226. /* Note: Delay required to reach propagation delay specification.             */
  227. /* Literal set to maximum value (refer to device datasheet,                   */
  228. /* parameter "tSTART").                                                       */
  229. /* Unit: us                                                                   */
  230. #define COMP_DELAY_STARTUP_US           (60U)  /*!< Delay for COMP startup time */
  231.  
  232. /* CSR register reset value */
  233. #define COMP_CSR_RESET_VALUE            (0x00000000U)
  234. /* CSR register masks */
  235. #define COMP_CSR_RESET_PARAMETERS_MASK   (0x00003FFFU)
  236. #define COMP_CSR_UPDATE_PARAMETERS_MASK  (0x00003FFEU)
  237. /* CSR COMPx non inverting input mask */
  238. #define COMP_CSR_COMPxNONINSEL_MASK      ((uint16_t)COMP_CSR_COMP1SW1)
  239. /* CSR COMP2 shift */
  240. #define COMP_CSR_COMP1_SHIFT             0U
  241. #define COMP_CSR_COMP2_SHIFT             16U
  242. /**
  243.   * @}
  244.   */
  245. /* Private macro -------------------------------------------------------------*/
  246. /* Private variables ---------------------------------------------------------*/
  247. /* Private function prototypes -----------------------------------------------*/
  248. /* Private functions ---------------------------------------------------------*/
  249.  
  250. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  251.   * @{
  252.   */
  253.  
  254. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  255.  *  @brief    Initialization and Configuration functions
  256.  *
  257. @verbatim    
  258.  ===============================================================================
  259.               ##### Initialization and Configuration functions #####
  260.  ===============================================================================
  261.     [..]  This section provides functions to initialize and de-initialize comparators
  262.  
  263. @endverbatim
  264.   * @{
  265.   */
  266.  
  267. /**
  268.   * @brief  Initializes the COMP according to the specified
  269.   *         parameters in the COMP_InitTypeDef and create the associated handle.
  270.   * @note   If the selected comparator is locked, initialization can't be performed.
  271.   *         To unlock the configuration, perform a system reset.
  272.   * @param  hcomp COMP handle
  273.   * @retval HAL status
  274.   */
  275. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  276. {
  277.   HAL_StatusTypeDef status = HAL_OK;
  278.   uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  279.  
  280.   /* Check the COMP handle allocation and lock status */
  281.   if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  282.   {
  283.     status = HAL_ERROR;
  284.   }
  285.   else
  286.   {
  287.     /* Check the parameter */
  288.     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  289.     assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
  290.     assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
  291.     assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
  292.     assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  293.     assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  294.     assert_param(IS_COMP_MODE(hcomp->Init.Mode));
  295.    
  296.     if(hcomp->Init.NonInvertingInput == COMP_NONINVERTINGINPUT_DAC1SWITCHCLOSED)
  297.     {
  298.       assert_param(IS_COMP_DAC1SWITCH_INSTANCE(hcomp->Instance));
  299.     }
  300.  
  301.     if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
  302.     {
  303.       assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
  304.     }
  305.  
  306.     /* Init SYSCFG and the low level hardware to access comparators */
  307.     __HAL_RCC_SYSCFG_CLK_ENABLE();  
  308.    
  309. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  310.     /* Init the COMP Callback settings */
  311.     hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  312.  
  313.     if (hcomp->MspInitCallback == NULL)
  314.     {
  315.       hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit  */
  316.     }
  317.  
  318.     /* Init the low level hardware */
  319.     hcomp->MspInitCallback(hcomp);
  320. #else
  321.     /* Init the low level hardware : SYSCFG to access comparators */
  322.     HAL_COMP_MspInit(hcomp);
  323. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  324.  
  325.     if(hcomp->State == HAL_COMP_STATE_RESET)
  326.     {
  327.       /* Allocate lock resource and initialize it */
  328.       hcomp->Lock = HAL_UNLOCKED;
  329.     }
  330.  
  331.     /* Change COMP peripheral state */
  332.     hcomp->State = HAL_COMP_STATE_BUSY;
  333.  
  334.     /* Set COMP parameters */
  335.     /*     Set COMPxINSEL bits according to hcomp->Init.InvertingInput value        */
  336.     /*     Set COMPxOUTSEL bits according to hcomp->Init.Output value               */
  337.     /*     Set COMPxPOL bit according to hcomp->Init.OutputPol value                */
  338.     /*     Set COMPxHYST bits according to hcomp->Init.Hysteresis value             */
  339.     /*     Set COMPxMODE bits according to hcomp->Init.Mode value                   */
  340.     if(hcomp->Instance == COMP2)
  341.     {
  342.       regshift = COMP_CSR_COMP2_SHIFT;
  343.     }
  344.     MODIFY_REG(COMP->CSR,
  345.                (COMP_CSR_COMPxINSEL  | COMP_CSR_COMPxNONINSEL_MASK | \
  346.                 COMP_CSR_COMPxOUTSEL | COMP_CSR_COMPxPOL           | \
  347.                 COMP_CSR_COMPxHYST   | COMP_CSR_COMPxMODE) << regshift,
  348.                (hcomp->Init.InvertingInput    | \
  349.                 hcomp->Init.NonInvertingInput | \
  350.                 hcomp->Init.Output            | \
  351.                 hcomp->Init.OutputPol         | \
  352.                 hcomp->Init.Hysteresis        | \
  353.                 hcomp->Init.Mode) << regshift);  
  354.    
  355.     if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
  356.     {
  357.       COMP->CSR |= COMP_CSR_WNDWEN;
  358.     }
  359.  
  360.     /* Initialize the COMP state*/
  361.     hcomp->State = HAL_COMP_STATE_READY;
  362.   }
  363.  
  364.   return status;
  365. }
  366.  
  367. /**
  368.   * @brief  DeInitializes the COMP peripheral
  369.   * @note   Deinitialization can't be performed if the COMP configuration is locked.
  370.   *         To unlock the configuration, perform a system reset.
  371.   * @param  hcomp COMP handle
  372.   * @retval HAL status
  373.   */
  374. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  375. {
  376.   HAL_StatusTypeDef status = HAL_OK;
  377.   uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  378.  
  379.   /* Check the COMP handle allocation and lock status */
  380.   if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  381.   {
  382.     status = HAL_ERROR;
  383.   }
  384.   else
  385.   {
  386.     /* Check the parameter */
  387.     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  388.  
  389.     /* Set COMP_CSR register to reset value for the corresponding COMP instance */
  390.     if(hcomp->Instance == COMP2)
  391.     {
  392.       regshift = COMP_CSR_COMP2_SHIFT;
  393.     }
  394.     MODIFY_REG(COMP->CSR,
  395.                COMP_CSR_RESET_PARAMETERS_MASK << regshift,
  396.                COMP_CSR_RESET_VALUE << regshift);
  397.  
  398. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  399.     if (hcomp->MspDeInitCallback == NULL)
  400.     {
  401.       hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit  */
  402.     }
  403.  
  404.     /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  405.     hcomp->MspDeInitCallback(hcomp);
  406. #else
  407.     /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  408.     HAL_COMP_MspDeInit(hcomp);
  409. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  410.  
  411.     hcomp->State = HAL_COMP_STATE_RESET;
  412.    
  413.     /* Release Lock */
  414.     __HAL_UNLOCK(hcomp);
  415.   }
  416.  
  417.   return status;
  418. }
  419.  
  420. /**
  421.   * @brief  Initializes the COMP MSP.
  422.   * @param  hcomp COMP handle
  423.   * @retval None
  424.   */
  425. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  426. {
  427.   /* Prevent unused argument(s) compilation warning */
  428.   UNUSED(hcomp);
  429.  
  430.   /* NOTE : This function Should not be modified, when the callback is needed,
  431.             the HAL_COMP_MspInit could be implenetd in the user file
  432.    */
  433. }
  434.  
  435. /**
  436.   * @brief  DeInitializes COMP MSP.
  437.   * @param  hcomp COMP handle
  438.   * @retval None
  439.   */
  440. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  441. {
  442.   /* Prevent unused argument(s) compilation warning */
  443.   UNUSED(hcomp);
  444.  
  445.   /* NOTE : This function Should not be modified, when the callback is needed,
  446.             the HAL_COMP_MspDeInit could be implenetd in the user file
  447.    */
  448. }
  449.  
  450. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  451. /**
  452.   * @brief  Register a User COMP Callback
  453.   *         To be used instead of the weak predefined callback
  454.   * @param  hcomp Pointer to a COMP_HandleTypeDef structure that contains
  455.   *                the configuration information for the specified COMP.
  456.   * @param  CallbackID ID of the callback to be registered
  457.   *         This parameter can be one of the following values:
  458.   *          @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  459.   *          @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  460.   *          @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  461.   * @param  pCallback pointer to the Callback function
  462.   * @retval HAL status
  463.   */
  464. HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
  465. {
  466.   HAL_StatusTypeDef status = HAL_OK;
  467.  
  468.   if (pCallback == NULL)
  469.   {
  470.     /* Update the error code */
  471.     hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  472.  
  473.     return HAL_ERROR;
  474.   }
  475.  
  476.   if (HAL_COMP_STATE_READY == hcomp->State)
  477.   {
  478.     switch (CallbackID)
  479.     {
  480.     case HAL_COMP_TRIGGER_CB_ID :
  481.       hcomp->TriggerCallback = pCallback;
  482.       break;
  483.  
  484.     case HAL_COMP_MSPINIT_CB_ID :
  485.       hcomp->MspInitCallback = pCallback;
  486.       break;
  487.  
  488.     case HAL_COMP_MSPDEINIT_CB_ID :
  489.       hcomp->MspDeInitCallback = pCallback;
  490.       break;
  491.  
  492.     default :
  493.       /* Update the error code */
  494.       hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  495.      
  496.       /* Return error status */
  497.       status = HAL_ERROR;
  498.       break;
  499.     }
  500.   }
  501.   else if (HAL_COMP_STATE_RESET == hcomp->State)
  502.   {
  503.     switch (CallbackID)
  504.     {
  505.     case HAL_COMP_MSPINIT_CB_ID :
  506.       hcomp->MspInitCallback = pCallback;
  507.       break;
  508.  
  509.     case HAL_COMP_MSPDEINIT_CB_ID :
  510.       hcomp->MspDeInitCallback = pCallback;
  511.       break;
  512.  
  513.     default :
  514.       /* Update the error code */
  515.       hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  516.  
  517.       /* Return error status */
  518.       status = HAL_ERROR;
  519.       break;
  520.     }
  521.   }
  522.   else
  523.   {
  524.     /* Update the error code */
  525.     hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  526.  
  527.     /* Return error status */
  528.     status =  HAL_ERROR;
  529.   }
  530.  
  531.   return status;
  532. }
  533.  
  534. /**
  535.   * @brief  Unregister a COMP Callback
  536.   *         COMP callback is redirected to the weak predefined callback
  537.   * @param  hcomp Pointer to a COMP_HandleTypeDef structure that contains
  538.   *                the configuration information for the specified COMP.
  539.   * @param  CallbackID ID of the callback to be unregistered
  540.   *         This parameter can be one of the following values:
  541.   *          @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  542.   *          @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  543.   *          @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  544.   * @retval HAL status
  545.   */
  546. HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
  547. {
  548.   HAL_StatusTypeDef status = HAL_OK;
  549.  
  550.   if (HAL_COMP_STATE_READY == hcomp->State)
  551.   {
  552.     switch (CallbackID)
  553.     {
  554.     case HAL_COMP_TRIGGER_CB_ID :
  555.       hcomp->TriggerCallback = HAL_COMP_TriggerCallback;         /* Legacy weak callback */
  556.       break;
  557.  
  558.     case HAL_COMP_MSPINIT_CB_ID :
  559.       hcomp->MspInitCallback = HAL_COMP_MspInit;                 /* Legacy weak MspInit */
  560.       break;
  561.  
  562.     case HAL_COMP_MSPDEINIT_CB_ID :
  563.       hcomp->MspDeInitCallback = HAL_COMP_MspDeInit;             /* Legacy weak MspDeInit */
  564.       break;
  565.  
  566.     default :
  567.       /* Update the error code */
  568.       hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  569.  
  570.       /* Return error status */
  571.       status =  HAL_ERROR;
  572.       break;
  573.     }
  574.   }
  575.   else if (HAL_COMP_STATE_RESET == hcomp->State)
  576.   {
  577.     switch (CallbackID)
  578.     {
  579.     case HAL_COMP_MSPINIT_CB_ID :
  580.       hcomp->MspInitCallback = HAL_COMP_MspInit;                 /* Legacy weak MspInit */
  581.       break;
  582.  
  583.     case HAL_COMP_MSPDEINIT_CB_ID :
  584.       hcomp->MspDeInitCallback = HAL_COMP_MspDeInit;             /* Legacy weak MspDeInit */
  585.       break;
  586.  
  587.     default :
  588.       /* Update the error code */
  589.       hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  590.  
  591.       /* Return error status */
  592.       status =  HAL_ERROR;
  593.       break;
  594.     }
  595.   }
  596.   else
  597.   {
  598.     /* Update the error code */
  599.     hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  600.    
  601.     /* Return error status */
  602.     status =  HAL_ERROR;
  603.   }
  604.  
  605.   return status;
  606. }
  607.  
  608. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  609.  
  610. /**
  611.   * @}
  612.   */
  613.  
  614. /** @defgroup COMP_Exported_Functions_Group2 I/O operation functions
  615.  *  @brief   Data transfers functions
  616.  *
  617. @verbatim  
  618.  ===============================================================================
  619.                       ##### IO operation functions #####
  620.  ===============================================================================  
  621.     [..]
  622.     This subsection provides a set of functions allowing to manage the COMP data
  623.     transfers.
  624.  
  625. @endverbatim
  626.   * @{
  627.   */
  628.  
  629. /**
  630.   * @brief  Start the comparator
  631.   * @param  hcomp COMP handle
  632.   * @retval HAL status
  633.   */
  634. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  635. {
  636.   uint32_t wait_loop_index = 0U;
  637.   HAL_StatusTypeDef status = HAL_OK;
  638.   uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  639.  
  640.   /* Check the COMP handle allocation and lock status */
  641.   if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  642.   {
  643.     status = HAL_ERROR;
  644.   }
  645.   else
  646.   {
  647.     /* Check the parameter */
  648.     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  649.  
  650.     if(hcomp->State == HAL_COMP_STATE_READY)
  651.     {
  652.       /* Enable the selected comparator */
  653.       if(hcomp->Instance == COMP2)
  654.       {
  655.         regshift = COMP_CSR_COMP2_SHIFT;
  656.       }
  657.       SET_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift);
  658.      
  659.       /* Set HAL COMP handle state */
  660.       hcomp->State = HAL_COMP_STATE_BUSY;
  661.      
  662.       /* Delay for COMP startup time */
  663.       wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / 1000000U));
  664.       while(wait_loop_index != 0U)
  665.       {
  666.         wait_loop_index--;
  667.       }    
  668.     }
  669.     else
  670.     {
  671.       status = HAL_ERROR;
  672.     }
  673.   }
  674.  
  675.   return status;
  676. }
  677.  
  678. /**
  679.   * @brief  Stop the comparator
  680.   * @param  hcomp COMP handle
  681.   * @retval HAL status
  682.   */
  683. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  684. {
  685.   HAL_StatusTypeDef status = HAL_OK;
  686.   uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  687.  
  688.   /* Check the COMP handle allocation and lock status */
  689.   if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  690.   {
  691.     status = HAL_ERROR;
  692.   }
  693.   else
  694.   {
  695.     /* Check the parameter */
  696.     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  697.  
  698.     if(hcomp->State == HAL_COMP_STATE_BUSY)
  699.     {
  700.       /* Disable the selected comparator */
  701.       if(hcomp->Instance == COMP2)
  702.       {
  703.         regshift = COMP_CSR_COMP2_SHIFT;
  704.       }
  705.       CLEAR_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift);
  706.  
  707.       hcomp->State = HAL_COMP_STATE_READY;
  708.     }
  709.     else
  710.     {
  711.       status = HAL_ERROR;
  712.     }
  713.   }
  714.  
  715.   return status;
  716. }
  717.  
  718. /**
  719.   * @brief  Enables the interrupt and starts the comparator
  720.   * @param  hcomp COMP handle
  721.   * @retval HAL status.
  722.   */
  723. HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
  724. {
  725.   HAL_StatusTypeDef status = HAL_OK;
  726.   uint32_t extiline = 0U;
  727.  
  728.   /* Check the parameter */
  729.   assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  730.  
  731.   status = HAL_COMP_Start(hcomp);
  732.   if(status == HAL_OK)
  733.   {
  734.     /* Check the Exti Line output configuration */
  735.     extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  736.     /* Configure the rising edge */
  737.     if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
  738.     {
  739.       SET_BIT(EXTI->RTSR, extiline);
  740.     }
  741.     else
  742.     {
  743.       CLEAR_BIT(EXTI->RTSR, extiline);
  744.     }
  745.     /* Configure the falling edge */
  746.     if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
  747.     {
  748.       SET_BIT(EXTI->FTSR, extiline);
  749.     }
  750.     else
  751.     {
  752.       CLEAR_BIT(EXTI->FTSR, extiline);
  753.     }
  754.  
  755.     /* Clear COMP EXTI pending bit */
  756.     WRITE_REG(EXTI->PR, extiline);
  757.    
  758.     /* Enable Exti interrupt mode */
  759.     SET_BIT(EXTI->IMR, extiline);
  760.   }
  761.  
  762.   return status;
  763. }
  764.  
  765. /**
  766.   * @brief  Disable the interrupt and Stop the comparator
  767.   * @param  hcomp COMP handle
  768.   * @retval HAL status
  769.   */
  770. HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
  771. {
  772.   HAL_StatusTypeDef status = HAL_OK;
  773.  
  774.   /* Disable the Exti Line interrupt mode */
  775.   CLEAR_BIT(EXTI->IMR, COMP_GET_EXTI_LINE(hcomp->Instance));
  776.  
  777.   status = HAL_COMP_Stop(hcomp);
  778.  
  779.   return status;
  780. }
  781.  
  782. /**
  783.   * @brief  Comparator IRQ Handler
  784.   * @param  hcomp COMP handle
  785.   * @retval HAL status
  786.   */
  787. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  788. {
  789.   uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  790.  
  791.   /* Check COMP Exti flag */
  792.   if(READ_BIT(EXTI->PR, extiline) != RESET)
  793.   {
  794.     /* Clear COMP Exti pending bit */
  795.     WRITE_REG(EXTI->PR, extiline);
  796.  
  797.     /* COMP trigger callback */
  798. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  799.     hcomp->TriggerCallback(hcomp);
  800. #else
  801.     HAL_COMP_TriggerCallback(hcomp);
  802. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */  
  803.   }
  804. }
  805.  
  806. /**
  807.   * @}
  808.   */
  809.  
  810. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  811.  *  @brief   management functions
  812.  *
  813. @verbatim  
  814.  ===============================================================================
  815.                       ##### Peripheral Control functions #####
  816.  ===============================================================================  
  817.     [..]
  818.     This subsection provides a set of functions allowing to control the COMP data
  819.     transfers.
  820.  
  821. @endverbatim
  822.   * @{
  823.   */
  824.  
  825. /**
  826.   * @brief  Lock the selected comparator configuration.
  827.   * @param  hcomp COMP handle
  828.   * @retval HAL status
  829.   */
  830. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  831. {
  832.   HAL_StatusTypeDef status = HAL_OK;
  833.   uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  834.  
  835.   /* Check the COMP handle allocation and lock status */
  836.   if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  837.   {
  838.     status = HAL_ERROR;
  839.   }
  840.   else
  841.   {
  842.     /* Check the parameter */
  843.     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  844.  
  845.     /* Set lock flag */
  846.     hcomp->State |= COMP_STATE_BIT_LOCK;
  847.  
  848.     /* Set the lock bit corresponding to selected comparator */
  849.     if(hcomp->Instance == COMP2)
  850.     {
  851.       regshift = COMP_CSR_COMP2_SHIFT;
  852.     }
  853.     SET_BIT(COMP->CSR, COMP_CSR_COMPxLOCK << regshift);
  854.   }
  855.  
  856.   return status;
  857. }
  858.  
  859. /**
  860.   * @brief  Return the output level (high or low) of the selected comparator.
  861.   *         The output level depends on the selected polarity.
  862.   *         If the polarity is not inverted:
  863.   *           - Comparator output is low when the non-inverting input is at a lower
  864.   *             voltage than the inverting input
  865.   *           - Comparator output is high when the non-inverting input is at a higher
  866.   *             voltage than the inverting input
  867.   *         If the polarity is inverted:
  868.   *           - Comparator output is high when the non-inverting input is at a lower
  869.   *             voltage than the inverting input
  870.   *           - Comparator output is low when the non-inverting input is at a higher
  871.   *             voltage than the inverting input
  872.   * @param  hcomp COMP handle
  873.   * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
  874.   *      
  875.   */
  876. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  877. {
  878.   uint32_t level=0;
  879.   uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  880.  
  881.   /* Check the parameter */
  882.   assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  883.  
  884.   if(hcomp->Instance == COMP2)
  885.   {
  886.     regshift = COMP_CSR_COMP2_SHIFT;
  887.   }
  888.   level = READ_BIT(COMP->CSR, COMP_CSR_COMPxOUT << regshift);
  889.  
  890.   if(level != 0U)
  891.   {
  892.     return(COMP_OUTPUTLEVEL_HIGH);
  893.   }
  894.   return(COMP_OUTPUTLEVEL_LOW);
  895. }
  896.  
  897. /**
  898.   * @brief  Comparator trigger callback.
  899.   * @param  hcomp COMP handle
  900.   * @retval None
  901.   */
  902. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  903. {
  904.   /* Prevent unused argument(s) compilation warning */
  905.   UNUSED(hcomp);
  906.  
  907.   /* NOTE : This function should not be modified, when the callback is needed,
  908.             the HAL_COMP_TriggerCallback should be implemented in the user file
  909.    */
  910. }
  911.  
  912.  
  913. /**
  914.   * @}
  915.   */
  916.  
  917. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  918.  *  @brief   Peripheral State functions
  919.  *
  920. @verbatim  
  921.  ===============================================================================
  922.                       ##### Peripheral State functions #####
  923.  ===============================================================================  
  924.     [..]
  925.     This subsection permit to get in run-time the status of the peripheral
  926.     and the data flow.
  927.  
  928. @endverbatim
  929.   * @{
  930.   */
  931.  
  932. /**
  933.   * @brief  Return the COMP state
  934.   * @param  hcomp COMP handle
  935.   * @retval HAL state
  936.   */
  937. uint32_t HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  938. {
  939.   /* Check the COMP handle allocation */
  940.   if(hcomp == NULL)
  941.   {
  942.     return HAL_COMP_STATE_RESET;
  943.   }
  944.  
  945.   /* Check the parameter */
  946.   assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  947.  
  948.   return hcomp->State;
  949. }
  950.  
  951. /**
  952.   * @brief  Return the COMP error code.
  953.   * @param hcomp COMP handle
  954.   * @retval COMP error code
  955.   */
  956. uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
  957. {
  958.   /* Check the parameters */
  959.   assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  960.  
  961.   return hcomp->ErrorCode;
  962. }
  963.  
  964. /**
  965.   * @}
  966.   */
  967.  
  968. /**
  969.   * @}
  970.   */
  971.  
  972. /**
  973.   * @}
  974.   */
  975.  
  976. /**
  977.   * @}
  978.   */
  979.  
  980. #endif /* COMP1 || COMP2 */
  981.  
  982. #endif /* HAL_COMP_MODULE_ENABLED */
  983.  
  984. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  985.