1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_comp_ex.c
4   * @author  MCD Application Team
5   * @brief   Extended COMP HAL module driver.
6   * @brief   This file provides firmware functions to manage voltage reference
7   *          VrefInt that must be specifically controlled for comparator
8   *          instance COMP2.
9   ******************************************************************************
10   * @attention
11   *
12   * Copyright (c) 2016 STMicroelectronics.
13   * All rights reserved.
14   *
15   * This software is licensed under terms that can be found in the LICENSE file
16   * in the root directory of this software component.
17   * If no LICENSE file comes with this software, it is provided AS-IS.
18   *
19   ******************************************************************************
20   @verbatim
21   ==============================================================================
22                ##### COMP peripheral Extended features  #####
23   ==============================================================================
24 
25   [..] Comparing to other previous devices, the COMP interface for STM32L0XX
26        devices contains the following additional features
27 
28        (+) Possibility to enable or disable the VREFINT which is used as input
29            to the comparator.
30 
31 
32   @endverbatim
33   ******************************************************************************
34   */
35 
36 #if !defined (STM32L010xB) && !defined (STM32L010x8) && !defined (STM32L010x6) && !defined (STM32L010x4)
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32l0xx_hal.h"
39 
40 /** @addtogroup STM32L0xx_HAL_Driver
41   * @{
42   */
43 
44 #ifdef HAL_COMP_MODULE_ENABLED
45 
46 /** @addtogroup COMPEx
47   * @brief Extended COMP HAL module driver
48   * @{
49   */
50 
51 /* Private define ------------------------------------------------------------*/
52 /** @addtogroup COMP_Private_Constants
53   * @{
54   */
55 
56 /* Delay for COMP voltage scaler stabilization time (voltage from VrefInt,    */
57 /* delay based on VrefInt startup time).                                      */
58 /* Literal set to maximum value (refer to device datasheet,                   */
59 /* parameter "TVREFINT").                                                     */
60 /* Unit: us                                                                   */
61 #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (3000U)  /*!< Delay for COMP voltage scaler stabilization time */
62 
63 /**
64   * @}
65   */
66 
67 /* Exported functions --------------------------------------------------------*/
68 /** @addtogroup COMPEx_Exported_Functions
69   * @{
70   */
71 
72 /** @addtogroup COMPEx_Exported_Functions_Group1
73   * @brief  Extended functions to manage VREFINT for the comparator
74   *
75   * @{
76   */
77 
78 /**
79   * @brief  Enable Vrefint and path to comparator, used by comparator
80   *         instance COMP2 input based on VrefInt or subdivision of VrefInt.
81   * @note   The equivalent of this function is managed automatically when
82   *         using function "HAL_COMP_Init()".
83   * @note   VrefInt requires a startup time
84   *         (refer to device datasheet, parameter "TVREFINT").
85   *         This function waits for the startup time
86   *         (alternative solution: poll for bit SYSCFG_CFGR3_VREFINT_RDYF set).
87   * @note   VrefInt must be disabled before entering in low-power mode.
88   *         Refer to description of bit EN_VREFINT in reference manual.
89   * @retval None
90   */
HAL_COMPEx_EnableVREFINT(void)91 void HAL_COMPEx_EnableVREFINT(void)
92 {
93   __IO uint32_t wait_loop_index = 0U;
94 
95   /* Enable VrefInt voltage reference and buffer */
96   SYSCFG->CFGR3 |= (SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP | SYSCFG_CFGR3_EN_VREFINT);
97 
98   /* Wait loop initialization and execution */
99   /* Note: Variable divided by 2 to compensate partially              */
100   /*       CPU processing cycles.                                     */
101   wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000U * 2U)));
102   while(wait_loop_index != 0U)
103   {
104     wait_loop_index--;
105   }
106 }
107 
108 /**
109   * @brief  Disable Vrefint and path to comparator, used by comparator
110   *         instance COMP2 input based on VrefInt or subdivision of VrefInt.
111   * @note   VrefInt must be disabled before entering in low-power mode.
112   *         Refer to description of bit EN_VREFINT in reference manual.
113   * @retval None
114   */
HAL_COMPEx_DisableVREFINT(void)115 void HAL_COMPEx_DisableVREFINT(void)
116 {
117   /* Disable VrefInt voltage reference and buffer */
118   SYSCFG->CFGR3 &= (uint32_t)~((uint32_t)(SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP | SYSCFG_CFGR3_EN_VREFINT));
119 }
120 
121 /**
122   * @}
123   */
124 
125 /**
126   * @}
127   */
128 
129 /**
130   * @}
131   */
132 
133 #endif /* HAL_COMP_MODULE_ENABLED */
134 
135 /**
136   * @}
137   */
138 #endif /* #if !defined (STM32L010xB) && !defined (STM32L010x8) && !defined (STM32L010x6) && !defined (STM32L010x4) */
139