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 controled for comparator
8   *          instance COMP2.
9   @verbatim
10   ==============================================================================
11                ##### COMP peripheral Extended features  #####
12   ==============================================================================
13 
14   [..] Comparing to other previous devices, the COMP interface for STM32L0XX
15        devices contains the following additional features
16 
17        (+) Possibility to enable or disable the VREFINT which is used as input
18            to the comparator.
19 
20 
21   @endverbatim
22   ******************************************************************************
23   * @attention
24   *
25   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
26   *
27   * Redistribution and use in source and binary forms, with or without modification,
28   * are permitted provided that the following conditions are met:
29   *   1. Redistributions of source code must retain the above copyright notice,
30   *      this list of conditions and the following disclaimer.
31   *   2. Redistributions in binary form must reproduce the above copyright notice,
32   *      this list of conditions and the following disclaimer in the documentation
33   *      and/or other materials provided with the distribution.
34   *   3. Neither the name of STMicroelectronics nor the names of its contributors
35   *      may be used to endorse or promote products derived from this software
36   *      without specific prior written permission.
37   *
38   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
39   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
42   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
44   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
45   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48   *
49   ******************************************************************************
50   */
51 
52 /* Includes ------------------------------------------------------------------*/
53 #include "stm32l0xx_hal.h"
54 
55 /** @addtogroup STM32L0xx_HAL_Driver
56   * @{
57   */
58 
59 #ifdef HAL_COMP_MODULE_ENABLED
60 
61 /** @addtogroup COMPEx
62   * @brief Extended COMP HAL module driver
63   * @{
64   */
65 
66 /* Private define ------------------------------------------------------------*/
67 /** @addtogroup COMP_Private_Constants
68   * @{
69   */
70 
71 /* Delay for COMP voltage scaler stabilization time (voltage from VrefInt,    */
72 /* delay based on VrefInt startup time).                                      */
73 /* Literal set to maximum value (refer to device datasheet,                   */
74 /* parameter "TVREFINT").                                                     */
75 /* Unit: us                                                                   */
76 #define COMP_DELAY_VOLTAGE_SCALER_STAB_US ((uint32_t)3000U)  /*!< Delay for COMP voltage scaler stabilization time */
77 
78 /**
79   * @}
80   */
81 
82 /* Exported functions --------------------------------------------------------*/
83 /** @addtogroup COMPEx_Exported_Functions
84   * @{
85   */
86 
87 /** @addtogroup COMPEx_Exported_Functions_Group1
88   * @brief  Extended functions to manage VREFINT for the comparator
89   *
90   * @{
91   */
92 
93 /**
94   * @brief  Enable Vrefint and path to comparator, used by comparator
95   *         instance COMP2 input based on VrefInt or subdivision of VrefInt.
96   * @note   The equivalent of this function is managed automatically when
97   *         using function "HAL_COMP_Init()".
98   * @note   VrefInt requires a startup time
99   *         (refer to device datasheet, parameter "TVREFINT").
100   *         This function waits for the startup time
101   *         (alternative solution: poll for bit SYSCFG_CFGR3_VREFINT_RDYF set).
102   * @retval None
103   */
HAL_COMPEx_EnableVREFINT(void)104 void HAL_COMPEx_EnableVREFINT(void)
105 {
106   __IO uint32_t wait_loop_index = 0U;
107 
108   /* Enable the Buffer for the COMP by setting ENBUFLP_VREFINT_COMP bit in the CFGR3 register */
109   SYSCFG->CFGR3 |= (SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP);
110 
111   /* Wait loop initialization and execution */
112   /* Note: Variable divided by 2 to compensate partially              */
113   /*       CPU processing cycles.                                     */
114   wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000U * 2U)));
115   while(wait_loop_index != 0U)
116   {
117     wait_loop_index--;
118   }
119 }
120 
121 /**
122   * @brief  Disable Vrefint and path to comparator, used by comparator
123   *         instance COMP2 input based on VrefInt or subdivision of VrefInt.
124   * @retval None
125   */
HAL_COMPEx_DisableVREFINT(void)126 void HAL_COMPEx_DisableVREFINT(void)
127 {
128   /* Disable the Vrefint by resetting ENBUFLP_VREFINT_COMP bit in the CFGR3 register */
129   SYSCFG->CFGR3 &= (uint32_t)~((uint32_t)(SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP));
130 }
131 
132 /**
133   * @}
134   */
135 
136 /**
137   * @}
138   */
139 
140 /**
141   * @}
142   */
143 
144 #endif /* HAL_COMP_MODULE_ENABLED */
145 
146 /**
147   * @}
148   */
149 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
150