TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elmot 1:d0dfbce63a89 1 /**
elmot 1:d0dfbce63a89 2 ******************************************************************************
elmot 1:d0dfbce63a89 3 * @file stm32l4xx_hal_comp.c
elmot 1:d0dfbce63a89 4 * @author MCD Application Team
elmot 1:d0dfbce63a89 5 * @version V1.5.1
elmot 1:d0dfbce63a89 6 * @date 31-May-2016
elmot 1:d0dfbce63a89 7 * @brief COMP HAL module driver.
elmot 1:d0dfbce63a89 8 * This file provides firmware functions to manage the following
elmot 1:d0dfbce63a89 9 * functionalities of the COMP peripheral:
elmot 1:d0dfbce63a89 10 * + Initialization and de-initialization functions
elmot 1:d0dfbce63a89 11 * + Start/Stop operation functions in polling mode
elmot 1:d0dfbce63a89 12 * + Start/Stop operation functions in interrupt mode (through EXTI interrupt)
elmot 1:d0dfbce63a89 13 * + Peripheral control functions
elmot 1:d0dfbce63a89 14 * + Peripheral state functions
elmot 1:d0dfbce63a89 15 *
elmot 1:d0dfbce63a89 16 @verbatim
elmot 1:d0dfbce63a89 17 ================================================================================
elmot 1:d0dfbce63a89 18 ##### COMP Peripheral features #####
elmot 1:d0dfbce63a89 19 ================================================================================
elmot 1:d0dfbce63a89 20
elmot 1:d0dfbce63a89 21 [..]
elmot 1:d0dfbce63a89 22 The STM32L4xx device family integrates two analog comparators instances
elmot 1:d0dfbce63a89 23 COMP1 and COMP2:
elmot 1:d0dfbce63a89 24 (#) The COMP input minus (inverting input) and input plus (non inverting input)
elmot 1:d0dfbce63a89 25 can be set to internal references or to GPIO pins
elmot 1:d0dfbce63a89 26 (refer to GPIO list in reference manual).
elmot 1:d0dfbce63a89 27
elmot 1:d0dfbce63a89 28 (#) The COMP output level is available using HAL_COMP_GetOutputLevel()
elmot 1:d0dfbce63a89 29 and can be redirected to other peripherals: GPIO pins (in mode
elmot 1:d0dfbce63a89 30 alternate functions for comparator), timers.
elmot 1:d0dfbce63a89 31 (refer to GPIO list in reference manual).
elmot 1:d0dfbce63a89 32
elmot 1:d0dfbce63a89 33 (#) Pairs of comparators instances can be combined in window mode
elmot 1:d0dfbce63a89 34 (2 consecutive instances odd and even COMP<x> and COMP<x+1>).
elmot 1:d0dfbce63a89 35
elmot 1:d0dfbce63a89 36 (#) The comparators have interrupt capability through the EXTI controller
elmot 1:d0dfbce63a89 37 with wake-up from sleep and stop modes:
elmot 1:d0dfbce63a89 38 (++) COMP1 is internally connected to EXTI Line 21
elmot 1:d0dfbce63a89 39 (++) COMP2 is internally connected to EXTI Line 22
elmot 1:d0dfbce63a89 40
elmot 1:d0dfbce63a89 41 From the corresponding IRQ handler, the right interrupt source can be retrieved
elmot 1:d0dfbce63a89 42 using macro __HAL_COMP_COMP1_EXTI_GET_FLAG() and __HAL_COMP_COMP2_EXTI_GET_FLAG().
elmot 1:d0dfbce63a89 43
elmot 1:d0dfbce63a89 44 ##### How to use this driver #####
elmot 1:d0dfbce63a89 45 ================================================================================
elmot 1:d0dfbce63a89 46 [..]
elmot 1:d0dfbce63a89 47 This driver provides functions to configure and program the comparator instances
elmot 1:d0dfbce63a89 48 of STM32L4xx devices.
elmot 1:d0dfbce63a89 49
elmot 1:d0dfbce63a89 50 To use the comparator, perform the following steps:
elmot 1:d0dfbce63a89 51
elmot 1:d0dfbce63a89 52 (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit():
elmot 1:d0dfbce63a89 53 (++) Configure the GPIO connected to comparator inputs plus and minus in analog mode
elmot 1:d0dfbce63a89 54 using HAL_GPIO_Init().
elmot 1:d0dfbce63a89 55 (++) If needed, configure the GPIO connected to comparator output in alternate function mode
elmot 1:d0dfbce63a89 56 using HAL_GPIO_Init().
elmot 1:d0dfbce63a89 57 (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
elmot 1:d0dfbce63a89 58 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
elmot 1:d0dfbce63a89 59 interrupt vector using HAL_NVIC_EnableIRQ() function.
elmot 1:d0dfbce63a89 60
elmot 1:d0dfbce63a89 61 (#) Configure the comparator using HAL_COMP_Init() function:
elmot 1:d0dfbce63a89 62 (++) Select the input minus (inverting input)
elmot 1:d0dfbce63a89 63 (++) Select the input plus (non-inverting input)
elmot 1:d0dfbce63a89 64 (++) Select the hysteresis
elmot 1:d0dfbce63a89 65 (++) Select the blanking source
elmot 1:d0dfbce63a89 66 (++) Select the output polarity
elmot 1:d0dfbce63a89 67 (++) Select the power mode
elmot 1:d0dfbce63a89 68 (++) Select the window mode
elmot 1:d0dfbce63a89 69
elmot 1:d0dfbce63a89 70 -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE()
elmot 1:d0dfbce63a89 71 to enable internal control clock of the comparators.
elmot 1:d0dfbce63a89 72 However, this is a legacy strategy. In future STM32 families,
elmot 1:d0dfbce63a89 73 COMP clock enable must be implemented by user in "HAL_COMP_MspInit()".
elmot 1:d0dfbce63a89 74 Therefore, for compatibility anticipation, it is recommended to
elmot 1:d0dfbce63a89 75 implement __HAL_RCC_SYSCFG_CLK_ENABLE() in "HAL_COMP_MspInit()".
elmot 1:d0dfbce63a89 76
elmot 1:d0dfbce63a89 77 (#) Reconfiguration on-the-fly of comparator can be done by calling again
elmot 1:d0dfbce63a89 78 function HAL_COMP_Init() with new input structure parameters values.
elmot 1:d0dfbce63a89 79
elmot 1:d0dfbce63a89 80 (#) Enable the comparator using HAL_COMP_Start() function.
elmot 1:d0dfbce63a89 81
elmot 1:d0dfbce63a89 82 (#) Use HAL_COMP_TriggerCallback() or HAL_COMP_GetOutputLevel() functions
elmot 1:d0dfbce63a89 83 to manage comparator outputs (events and output level).
elmot 1:d0dfbce63a89 84
elmot 1:d0dfbce63a89 85 (#) Disable the comparator using HAL_COMP_Stop() function.
elmot 1:d0dfbce63a89 86
elmot 1:d0dfbce63a89 87 (#) De-initialize the comparator using HAL_COMP_DeInit() function.
elmot 1:d0dfbce63a89 88
elmot 1:d0dfbce63a89 89 (#) For safety purpose, comparator configuration can be locked using HAL_COMP_Lock() function.
elmot 1:d0dfbce63a89 90 The only way to unlock the comparator is a device hardware reset.
elmot 1:d0dfbce63a89 91
elmot 1:d0dfbce63a89 92 @endverbatim
elmot 1:d0dfbce63a89 93 ******************************************************************************
elmot 1:d0dfbce63a89 94
elmot 1:d0dfbce63a89 95 Table 1. COMP inputs and output for STM32L4xx devices
elmot 1:d0dfbce63a89 96 +---------------------------------------------------------+
elmot 1:d0dfbce63a89 97 | | | COMP1 | COMP2 |
elmot 1:d0dfbce63a89 98 |----------------|----------------|-----------|-----------|
elmot 1:d0dfbce63a89 99 | | IO1 | PC5 | PB4 |
elmot 1:d0dfbce63a89 100 | Input plus | IO2 | PB2 | PB6 |
elmot 1:d0dfbce63a89 101 | | IO3 (3) | PA1 | PA3 |
elmot 1:d0dfbce63a89 102 |----------------|----------------|-----------------------|
elmot 1:d0dfbce63a89 103 | | 1/4 VrefInt | Available | Available |
elmot 1:d0dfbce63a89 104 | | 1/2 VrefInt | Available | Available |
elmot 1:d0dfbce63a89 105 | | 3/4 VrefInt | Available | Available |
elmot 1:d0dfbce63a89 106 | Input minus | VrefInt | Available | Available |
elmot 1:d0dfbce63a89 107 | | DAC1 channel 1 | Available | Available |
elmot 1:d0dfbce63a89 108 | | DAC1 channel 2 | Available | Available |
elmot 1:d0dfbce63a89 109 | | IO1 | PB1 | PB3 |
elmot 1:d0dfbce63a89 110 | | IO2 | PC4 | PB7 |
elmot 1:d0dfbce63a89 111 | | IO3 (3) | PA0 | PA2 |
elmot 1:d0dfbce63a89 112 | | IO4 (3) | PA4 | PA4 |
elmot 1:d0dfbce63a89 113 | | IO5 (3) | PA5 | PA5 |
elmot 1:d0dfbce63a89 114 +---------------------------------------------------------+
elmot 1:d0dfbce63a89 115 | Output | | PB0 (1) | PB5 (1) |
elmot 1:d0dfbce63a89 116 | | | PB10 (1) | PB11 (1) |
elmot 1:d0dfbce63a89 117 | | | TIM (2) | TIM (2) |
elmot 1:d0dfbce63a89 118 +---------------------------------------------------------+
elmot 1:d0dfbce63a89 119 (1) GPIO must be set to alternate function for comparator
elmot 1:d0dfbce63a89 120 (2) Comparators output to timers is set in timers instances.
elmot 1:d0dfbce63a89 121 (3) Only STM32L43x/L44x
elmot 1:d0dfbce63a89 122
elmot 1:d0dfbce63a89 123 ******************************************************************************
elmot 1:d0dfbce63a89 124 * @attention
elmot 1:d0dfbce63a89 125 *
elmot 1:d0dfbce63a89 126 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
elmot 1:d0dfbce63a89 127 *
elmot 1:d0dfbce63a89 128 * Redistribution and use in source and binary forms, with or without modification,
elmot 1:d0dfbce63a89 129 * are permitted provided that the following conditions are met:
elmot 1:d0dfbce63a89 130 * 1. Redistributions of source code must retain the above copyright notice,
elmot 1:d0dfbce63a89 131 * this list of conditions and the following disclaimer.
elmot 1:d0dfbce63a89 132 * 2. Redistributions in binary form must reproduce the above copyright notice,
elmot 1:d0dfbce63a89 133 * this list of conditions and the following disclaimer in the documentation
elmot 1:d0dfbce63a89 134 * and/or other materials provided with the distribution.
elmot 1:d0dfbce63a89 135 * 3. Neither the name of STMicroelectronics nor the names of its contributors
elmot 1:d0dfbce63a89 136 * may be used to endorse or promote products derived from this software
elmot 1:d0dfbce63a89 137 * without specific prior written permission.
elmot 1:d0dfbce63a89 138 *
elmot 1:d0dfbce63a89 139 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
elmot 1:d0dfbce63a89 140 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
elmot 1:d0dfbce63a89 141 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
elmot 1:d0dfbce63a89 142 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
elmot 1:d0dfbce63a89 143 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
elmot 1:d0dfbce63a89 144 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
elmot 1:d0dfbce63a89 145 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
elmot 1:d0dfbce63a89 146 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
elmot 1:d0dfbce63a89 147 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
elmot 1:d0dfbce63a89 148 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
elmot 1:d0dfbce63a89 149 *
elmot 1:d0dfbce63a89 150 ******************************************************************************
elmot 1:d0dfbce63a89 151 */
elmot 1:d0dfbce63a89 152
elmot 1:d0dfbce63a89 153 /* Includes ------------------------------------------------------------------*/
elmot 1:d0dfbce63a89 154 #include "stm32l4xx_hal.h"
elmot 1:d0dfbce63a89 155
elmot 1:d0dfbce63a89 156 /** @addtogroup STM32L4xx_HAL_Driver
elmot 1:d0dfbce63a89 157 * @{
elmot 1:d0dfbce63a89 158 */
elmot 1:d0dfbce63a89 159
elmot 1:d0dfbce63a89 160 /** @defgroup COMP COMP
elmot 1:d0dfbce63a89 161 * @brief COMP HAL module driver
elmot 1:d0dfbce63a89 162 * @{
elmot 1:d0dfbce63a89 163 */
elmot 1:d0dfbce63a89 164
elmot 1:d0dfbce63a89 165 #ifdef HAL_COMP_MODULE_ENABLED
elmot 1:d0dfbce63a89 166
elmot 1:d0dfbce63a89 167 /* Private typedef -----------------------------------------------------------*/
elmot 1:d0dfbce63a89 168 /* Private define ------------------------------------------------------------*/
elmot 1:d0dfbce63a89 169 /** @addtogroup COMP_Private_Constants
elmot 1:d0dfbce63a89 170 * @{
elmot 1:d0dfbce63a89 171 */
elmot 1:d0dfbce63a89 172
elmot 1:d0dfbce63a89 173 /* Delay for COMP startup time. */
elmot 1:d0dfbce63a89 174 /* Note: Delay required to reach propagation delay specification. */
elmot 1:d0dfbce63a89 175 /* Literal set to maximum value (refer to device datasheet, */
elmot 1:d0dfbce63a89 176 /* parameter "tSTART"). */
elmot 1:d0dfbce63a89 177 /* Unit: us */
elmot 1:d0dfbce63a89 178 #define COMP_DELAY_STARTUP_US ((uint32_t) 80U) /*!< Delay for COMP startup time */
elmot 1:d0dfbce63a89 179
elmot 1:d0dfbce63a89 180 /* Delay for COMP voltage scaler stabilization time. */
elmot 1:d0dfbce63a89 181 /* Literal set to maximum value (refer to device datasheet, */
elmot 1:d0dfbce63a89 182 /* parameter "tSTART_SCALER"). */
elmot 1:d0dfbce63a89 183 /* Unit: us */
elmot 1:d0dfbce63a89 184 #define COMP_DELAY_VOLTAGE_SCALER_STAB_US ((uint32_t) 200U) /*!< Delay for COMP voltage scaler stabilization time */
elmot 1:d0dfbce63a89 185
elmot 1:d0dfbce63a89 186 #define COMP_OUTPUT_LEVEL_BITOFFSET_POS ((uint32_t) 30U)
elmot 1:d0dfbce63a89 187
elmot 1:d0dfbce63a89 188 /**
elmot 1:d0dfbce63a89 189 * @}
elmot 1:d0dfbce63a89 190 */
elmot 1:d0dfbce63a89 191
elmot 1:d0dfbce63a89 192 /* Private macro -------------------------------------------------------------*/
elmot 1:d0dfbce63a89 193 /* Private variables ---------------------------------------------------------*/
elmot 1:d0dfbce63a89 194 /* Private function prototypes -----------------------------------------------*/
elmot 1:d0dfbce63a89 195 /* Exported functions --------------------------------------------------------*/
elmot 1:d0dfbce63a89 196
elmot 1:d0dfbce63a89 197 /** @defgroup COMP_Exported_Functions COMP Exported Functions
elmot 1:d0dfbce63a89 198 * @{
elmot 1:d0dfbce63a89 199 */
elmot 1:d0dfbce63a89 200
elmot 1:d0dfbce63a89 201 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
elmot 1:d0dfbce63a89 202 * @brief Initialization and de-initialization functions.
elmot 1:d0dfbce63a89 203 *
elmot 1:d0dfbce63a89 204 @verbatim
elmot 1:d0dfbce63a89 205 ===============================================================================
elmot 1:d0dfbce63a89 206 ##### Initialization and de-initialization functions #####
elmot 1:d0dfbce63a89 207 ===============================================================================
elmot 1:d0dfbce63a89 208 [..] This section provides functions to initialize and de-initialize comparators
elmot 1:d0dfbce63a89 209
elmot 1:d0dfbce63a89 210 @endverbatim
elmot 1:d0dfbce63a89 211 * @{
elmot 1:d0dfbce63a89 212 */
elmot 1:d0dfbce63a89 213
elmot 1:d0dfbce63a89 214 /**
elmot 1:d0dfbce63a89 215 * @brief Initialize the COMP according to the specified
elmot 1:d0dfbce63a89 216 * parameters in the COMP_InitTypeDef and initialize the associated handle.
elmot 1:d0dfbce63a89 217 * @note If the selected comparator is locked, initialization can't be performed.
elmot 1:d0dfbce63a89 218 * To unlock the configuration, perform a system reset.
elmot 1:d0dfbce63a89 219 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 220 * @retval HAL status
elmot 1:d0dfbce63a89 221 */
elmot 1:d0dfbce63a89 222 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 223 {
elmot 1:d0dfbce63a89 224 uint32_t tmp_csr = 0;
elmot 1:d0dfbce63a89 225 uint32_t exti_line = 0;
elmot 1:d0dfbce63a89 226 uint32_t comp_voltage_scaler_not_initialized = 0;
elmot 1:d0dfbce63a89 227 __IO uint32_t wait_loop_index = 0;
elmot 1:d0dfbce63a89 228 HAL_StatusTypeDef status = HAL_OK;
elmot 1:d0dfbce63a89 229
elmot 1:d0dfbce63a89 230 /* Check the COMP handle allocation and lock status */
elmot 1:d0dfbce63a89 231 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
elmot 1:d0dfbce63a89 232 {
elmot 1:d0dfbce63a89 233 status = HAL_ERROR;
elmot 1:d0dfbce63a89 234 }
elmot 1:d0dfbce63a89 235 else
elmot 1:d0dfbce63a89 236 {
elmot 1:d0dfbce63a89 237 /* Check the parameters */
elmot 1:d0dfbce63a89 238 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
elmot 1:d0dfbce63a89 239 assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput));
elmot 1:d0dfbce63a89 240 assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput));
elmot 1:d0dfbce63a89 241 assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
elmot 1:d0dfbce63a89 242 assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode));
elmot 1:d0dfbce63a89 243 assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
elmot 1:d0dfbce63a89 244 assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
elmot 1:d0dfbce63a89 245 assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
elmot 1:d0dfbce63a89 246 assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
elmot 1:d0dfbce63a89 247
elmot 1:d0dfbce63a89 248 if(hcomp->State == HAL_COMP_STATE_RESET)
elmot 1:d0dfbce63a89 249 {
elmot 1:d0dfbce63a89 250 /* Allocate lock resource and initialize it */
elmot 1:d0dfbce63a89 251 hcomp->Lock = HAL_UNLOCKED;
elmot 1:d0dfbce63a89 252
elmot 1:d0dfbce63a89 253 /* Init SYSCFG and the low level hardware to access comparators */
elmot 1:d0dfbce63a89 254 /* Note: HAL_COMP_Init() calls __HAL_RCC_SYSCFG_CLK_ENABLE() */
elmot 1:d0dfbce63a89 255 /* to enable internal control clock of the comparators. */
elmot 1:d0dfbce63a89 256 /* However, this is a legacy strategy. In future STM32 families, */
elmot 1:d0dfbce63a89 257 /* COMP clock enable must be implemented by user */
elmot 1:d0dfbce63a89 258 /* in "HAL_COMP_MspInit()". */
elmot 1:d0dfbce63a89 259 /* Therefore, for compatibility anticipation, it is recommended */
elmot 1:d0dfbce63a89 260 /* to implement __HAL_RCC_SYSCFG_CLK_ENABLE() */
elmot 1:d0dfbce63a89 261 /* in "HAL_COMP_MspInit()". */
elmot 1:d0dfbce63a89 262 __HAL_RCC_SYSCFG_CLK_ENABLE();
elmot 1:d0dfbce63a89 263
elmot 1:d0dfbce63a89 264 /* Init the low level hardware */
elmot 1:d0dfbce63a89 265 HAL_COMP_MspInit(hcomp);
elmot 1:d0dfbce63a89 266 }
elmot 1:d0dfbce63a89 267
elmot 1:d0dfbce63a89 268 /* Memorize voltage scaler state before initialization */
elmot 1:d0dfbce63a89 269 comp_voltage_scaler_not_initialized = (READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) == 0);
elmot 1:d0dfbce63a89 270
elmot 1:d0dfbce63a89 271 /* Set COMP parameters */
elmot 1:d0dfbce63a89 272 tmp_csr = (hcomp->Init.InvertingInput |
elmot 1:d0dfbce63a89 273 hcomp->Init.NonInvertingInput |
elmot 1:d0dfbce63a89 274 hcomp->Init.BlankingSrce |
elmot 1:d0dfbce63a89 275 hcomp->Init.Hysteresis |
elmot 1:d0dfbce63a89 276 hcomp->Init.OutputPol |
elmot 1:d0dfbce63a89 277 hcomp->Init.Mode );
elmot 1:d0dfbce63a89 278
elmot 1:d0dfbce63a89 279 /* Set parameters in COMP register */
elmot 1:d0dfbce63a89 280 /* Note: Update all bits except read-only, lock and enable bits */
elmot 1:d0dfbce63a89 281 #if defined (COMP_CSR_INMESEL)
elmot 1:d0dfbce63a89 282 MODIFY_REG(hcomp->Instance->CSR,
elmot 1:d0dfbce63a89 283 COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
elmot 1:d0dfbce63a89 284 COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST |
elmot 1:d0dfbce63a89 285 COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL,
elmot 1:d0dfbce63a89 286 tmp_csr
elmot 1:d0dfbce63a89 287 );
elmot 1:d0dfbce63a89 288 #else
elmot 1:d0dfbce63a89 289 MODIFY_REG(hcomp->Instance->CSR,
elmot 1:d0dfbce63a89 290 COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
elmot 1:d0dfbce63a89 291 COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST |
elmot 1:d0dfbce63a89 292 COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN,
elmot 1:d0dfbce63a89 293 tmp_csr
elmot 1:d0dfbce63a89 294 );
elmot 1:d0dfbce63a89 295 #endif
elmot 1:d0dfbce63a89 296
elmot 1:d0dfbce63a89 297 /* Set window mode */
elmot 1:d0dfbce63a89 298 /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */
elmot 1:d0dfbce63a89 299 /* instances. Therefore, this function can update another COMP */
elmot 1:d0dfbce63a89 300 /* instance that the one currently selected. */
elmot 1:d0dfbce63a89 301 if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON)
elmot 1:d0dfbce63a89 302 {
elmot 1:d0dfbce63a89 303 SET_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
elmot 1:d0dfbce63a89 304 }
elmot 1:d0dfbce63a89 305 else
elmot 1:d0dfbce63a89 306 {
elmot 1:d0dfbce63a89 307 CLEAR_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
elmot 1:d0dfbce63a89 308 }
elmot 1:d0dfbce63a89 309
elmot 1:d0dfbce63a89 310 /* Delay for COMP scaler bridge voltage stabilization */
elmot 1:d0dfbce63a89 311 /* Apply the delay if voltage scaler bridge is enabled for the first time */
elmot 1:d0dfbce63a89 312 if ((READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) != 0) &&
elmot 1:d0dfbce63a89 313 (comp_voltage_scaler_not_initialized != 0) )
elmot 1:d0dfbce63a89 314 {
elmot 1:d0dfbce63a89 315 /* Wait loop initialization and execution */
elmot 1:d0dfbce63a89 316 /* Note: Variable divided by 2 to compensate partially */
elmot 1:d0dfbce63a89 317 /* CPU processing cycles. */
elmot 1:d0dfbce63a89 318 wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000 * 2)));
elmot 1:d0dfbce63a89 319 while(wait_loop_index != 0)
elmot 1:d0dfbce63a89 320 {
elmot 1:d0dfbce63a89 321 wait_loop_index--;
elmot 1:d0dfbce63a89 322 }
elmot 1:d0dfbce63a89 323 }
elmot 1:d0dfbce63a89 324
elmot 1:d0dfbce63a89 325 /* Get the EXTI line corresponding to the selected COMP instance */
elmot 1:d0dfbce63a89 326 exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
elmot 1:d0dfbce63a89 327
elmot 1:d0dfbce63a89 328 /* Manage EXTI settings */
elmot 1:d0dfbce63a89 329 if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != RESET)
elmot 1:d0dfbce63a89 330 {
elmot 1:d0dfbce63a89 331 /* Configure EXTI rising edge */
elmot 1:d0dfbce63a89 332 if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != RESET)
elmot 1:d0dfbce63a89 333 {
elmot 1:d0dfbce63a89 334 SET_BIT(EXTI->RTSR1, exti_line);
elmot 1:d0dfbce63a89 335 }
elmot 1:d0dfbce63a89 336 else
elmot 1:d0dfbce63a89 337 {
elmot 1:d0dfbce63a89 338 CLEAR_BIT(EXTI->RTSR1, exti_line);
elmot 1:d0dfbce63a89 339 }
elmot 1:d0dfbce63a89 340
elmot 1:d0dfbce63a89 341 /* Configure EXTI falling edge */
elmot 1:d0dfbce63a89 342 if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != RESET)
elmot 1:d0dfbce63a89 343 {
elmot 1:d0dfbce63a89 344 SET_BIT(EXTI->FTSR1, exti_line);
elmot 1:d0dfbce63a89 345 }
elmot 1:d0dfbce63a89 346 else
elmot 1:d0dfbce63a89 347 {
elmot 1:d0dfbce63a89 348 CLEAR_BIT(EXTI->FTSR1, exti_line);
elmot 1:d0dfbce63a89 349 }
elmot 1:d0dfbce63a89 350
elmot 1:d0dfbce63a89 351 /* Clear COMP EXTI pending bit (if any) */
elmot 1:d0dfbce63a89 352 WRITE_REG(EXTI->PR1, exti_line);
elmot 1:d0dfbce63a89 353
elmot 1:d0dfbce63a89 354 /* Configure EXTI event mode */
elmot 1:d0dfbce63a89 355 if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != RESET)
elmot 1:d0dfbce63a89 356 {
elmot 1:d0dfbce63a89 357 SET_BIT(EXTI->EMR1, exti_line);
elmot 1:d0dfbce63a89 358 }
elmot 1:d0dfbce63a89 359 else
elmot 1:d0dfbce63a89 360 {
elmot 1:d0dfbce63a89 361 CLEAR_BIT(EXTI->EMR1, exti_line);
elmot 1:d0dfbce63a89 362 }
elmot 1:d0dfbce63a89 363
elmot 1:d0dfbce63a89 364 /* Configure EXTI interrupt mode */
elmot 1:d0dfbce63a89 365 if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != RESET)
elmot 1:d0dfbce63a89 366 {
elmot 1:d0dfbce63a89 367 SET_BIT(EXTI->IMR1, exti_line);
elmot 1:d0dfbce63a89 368 }
elmot 1:d0dfbce63a89 369 else
elmot 1:d0dfbce63a89 370 {
elmot 1:d0dfbce63a89 371 CLEAR_BIT(EXTI->IMR1, exti_line);
elmot 1:d0dfbce63a89 372 }
elmot 1:d0dfbce63a89 373 }
elmot 1:d0dfbce63a89 374 else
elmot 1:d0dfbce63a89 375 {
elmot 1:d0dfbce63a89 376 /* Disable EXTI event mode */
elmot 1:d0dfbce63a89 377 CLEAR_BIT(EXTI->EMR1, exti_line);
elmot 1:d0dfbce63a89 378
elmot 1:d0dfbce63a89 379 /* Disable EXTI interrupt mode */
elmot 1:d0dfbce63a89 380 CLEAR_BIT(EXTI->IMR1, exti_line);
elmot 1:d0dfbce63a89 381 }
elmot 1:d0dfbce63a89 382
elmot 1:d0dfbce63a89 383 /* Set HAL COMP handle state */
elmot 1:d0dfbce63a89 384 /* Note: Transition from state reset to state ready, */
elmot 1:d0dfbce63a89 385 /* otherwise (coming from state ready or busy) no state update. */
elmot 1:d0dfbce63a89 386 if (hcomp->State == HAL_COMP_STATE_RESET)
elmot 1:d0dfbce63a89 387 {
elmot 1:d0dfbce63a89 388 hcomp->State = HAL_COMP_STATE_READY;
elmot 1:d0dfbce63a89 389 }
elmot 1:d0dfbce63a89 390 }
elmot 1:d0dfbce63a89 391
elmot 1:d0dfbce63a89 392 return status;
elmot 1:d0dfbce63a89 393 }
elmot 1:d0dfbce63a89 394
elmot 1:d0dfbce63a89 395 /**
elmot 1:d0dfbce63a89 396 * @brief DeInitialize the COMP peripheral.
elmot 1:d0dfbce63a89 397 * @note Deinitialization cannot be performed if the COMP configuration is locked.
elmot 1:d0dfbce63a89 398 * To unlock the configuration, perform a system reset.
elmot 1:d0dfbce63a89 399 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 400 * @retval HAL status
elmot 1:d0dfbce63a89 401 */
elmot 1:d0dfbce63a89 402 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 403 {
elmot 1:d0dfbce63a89 404 HAL_StatusTypeDef status = HAL_OK;
elmot 1:d0dfbce63a89 405
elmot 1:d0dfbce63a89 406 /* Check the COMP handle allocation and lock status */
elmot 1:d0dfbce63a89 407 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
elmot 1:d0dfbce63a89 408 {
elmot 1:d0dfbce63a89 409 status = HAL_ERROR;
elmot 1:d0dfbce63a89 410 }
elmot 1:d0dfbce63a89 411 else
elmot 1:d0dfbce63a89 412 {
elmot 1:d0dfbce63a89 413 /* Check the parameter */
elmot 1:d0dfbce63a89 414 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
elmot 1:d0dfbce63a89 415
elmot 1:d0dfbce63a89 416 /* Set COMP_CSR register to reset value */
elmot 1:d0dfbce63a89 417 WRITE_REG(hcomp->Instance->CSR, 0x00000000);
elmot 1:d0dfbce63a89 418
elmot 1:d0dfbce63a89 419 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
elmot 1:d0dfbce63a89 420 HAL_COMP_MspDeInit(hcomp);
elmot 1:d0dfbce63a89 421
elmot 1:d0dfbce63a89 422 /* Set HAL COMP handle state */
elmot 1:d0dfbce63a89 423 hcomp->State = HAL_COMP_STATE_RESET;
elmot 1:d0dfbce63a89 424
elmot 1:d0dfbce63a89 425 /* Release Lock */
elmot 1:d0dfbce63a89 426 __HAL_UNLOCK(hcomp);
elmot 1:d0dfbce63a89 427 }
elmot 1:d0dfbce63a89 428
elmot 1:d0dfbce63a89 429 return status;
elmot 1:d0dfbce63a89 430 }
elmot 1:d0dfbce63a89 431
elmot 1:d0dfbce63a89 432 /**
elmot 1:d0dfbce63a89 433 * @brief Initialize the COMP MSP.
elmot 1:d0dfbce63a89 434 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 435 * @retval None
elmot 1:d0dfbce63a89 436 */
elmot 1:d0dfbce63a89 437 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 438 {
elmot 1:d0dfbce63a89 439 /* Prevent unused argument(s) compilation warning */
elmot 1:d0dfbce63a89 440 UNUSED(hcomp);
elmot 1:d0dfbce63a89 441
elmot 1:d0dfbce63a89 442 /* NOTE : This function should not be modified, when the callback is needed,
elmot 1:d0dfbce63a89 443 the HAL_COMP_MspInit could be implemented in the user file
elmot 1:d0dfbce63a89 444 */
elmot 1:d0dfbce63a89 445 }
elmot 1:d0dfbce63a89 446
elmot 1:d0dfbce63a89 447 /**
elmot 1:d0dfbce63a89 448 * @brief DeInitialize the COMP MSP.
elmot 1:d0dfbce63a89 449 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 450 * @retval None
elmot 1:d0dfbce63a89 451 */
elmot 1:d0dfbce63a89 452 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 453 {
elmot 1:d0dfbce63a89 454 /* Prevent unused argument(s) compilation warning */
elmot 1:d0dfbce63a89 455 UNUSED(hcomp);
elmot 1:d0dfbce63a89 456
elmot 1:d0dfbce63a89 457 /* NOTE : This function should not be modified, when the callback is needed,
elmot 1:d0dfbce63a89 458 the HAL_COMP_MspDeInit could be implemented in the user file
elmot 1:d0dfbce63a89 459 */
elmot 1:d0dfbce63a89 460 }
elmot 1:d0dfbce63a89 461
elmot 1:d0dfbce63a89 462 /**
elmot 1:d0dfbce63a89 463 * @}
elmot 1:d0dfbce63a89 464 */
elmot 1:d0dfbce63a89 465
elmot 1:d0dfbce63a89 466 /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions
elmot 1:d0dfbce63a89 467 * @brief Start-Stop operation functions.
elmot 1:d0dfbce63a89 468 *
elmot 1:d0dfbce63a89 469 @verbatim
elmot 1:d0dfbce63a89 470 ===============================================================================
elmot 1:d0dfbce63a89 471 ##### IO operation functions #####
elmot 1:d0dfbce63a89 472 ===============================================================================
elmot 1:d0dfbce63a89 473 [..] This section provides functions allowing to:
elmot 1:d0dfbce63a89 474 (+) Start a comparator instance.
elmot 1:d0dfbce63a89 475 (+) Stop a comparator instance.
elmot 1:d0dfbce63a89 476
elmot 1:d0dfbce63a89 477 @endverbatim
elmot 1:d0dfbce63a89 478 * @{
elmot 1:d0dfbce63a89 479 */
elmot 1:d0dfbce63a89 480
elmot 1:d0dfbce63a89 481 /**
elmot 1:d0dfbce63a89 482 * @brief Start the comparator.
elmot 1:d0dfbce63a89 483 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 484 * @retval HAL status
elmot 1:d0dfbce63a89 485 */
elmot 1:d0dfbce63a89 486 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 487 {
elmot 1:d0dfbce63a89 488 __IO uint32_t wait_loop_index = 0;
elmot 1:d0dfbce63a89 489 HAL_StatusTypeDef status = HAL_OK;
elmot 1:d0dfbce63a89 490
elmot 1:d0dfbce63a89 491 /* Check the COMP handle allocation and lock status */
elmot 1:d0dfbce63a89 492 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
elmot 1:d0dfbce63a89 493 {
elmot 1:d0dfbce63a89 494 status = HAL_ERROR;
elmot 1:d0dfbce63a89 495 }
elmot 1:d0dfbce63a89 496 else
elmot 1:d0dfbce63a89 497 {
elmot 1:d0dfbce63a89 498 /* Check the parameter */
elmot 1:d0dfbce63a89 499 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
elmot 1:d0dfbce63a89 500
elmot 1:d0dfbce63a89 501 if(hcomp->State == HAL_COMP_STATE_READY)
elmot 1:d0dfbce63a89 502 {
elmot 1:d0dfbce63a89 503 /* Enable the selected comparator */
elmot 1:d0dfbce63a89 504 SET_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
elmot 1:d0dfbce63a89 505
elmot 1:d0dfbce63a89 506 /* Set HAL COMP handle state */
elmot 1:d0dfbce63a89 507 hcomp->State = HAL_COMP_STATE_BUSY;
elmot 1:d0dfbce63a89 508
elmot 1:d0dfbce63a89 509 /* Delay for COMP startup time */
elmot 1:d0dfbce63a89 510 /* Wait loop initialization and execution */
elmot 1:d0dfbce63a89 511 /* Note: Variable divided by 2 to compensate partially */
elmot 1:d0dfbce63a89 512 /* CPU processing cycles. */
elmot 1:d0dfbce63a89 513 wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / (1000000 * 2)));
elmot 1:d0dfbce63a89 514 while(wait_loop_index != 0)
elmot 1:d0dfbce63a89 515 {
elmot 1:d0dfbce63a89 516 wait_loop_index--;
elmot 1:d0dfbce63a89 517 }
elmot 1:d0dfbce63a89 518 }
elmot 1:d0dfbce63a89 519 else
elmot 1:d0dfbce63a89 520 {
elmot 1:d0dfbce63a89 521 status = HAL_ERROR;
elmot 1:d0dfbce63a89 522 }
elmot 1:d0dfbce63a89 523 }
elmot 1:d0dfbce63a89 524
elmot 1:d0dfbce63a89 525 return status;
elmot 1:d0dfbce63a89 526 }
elmot 1:d0dfbce63a89 527
elmot 1:d0dfbce63a89 528 /**
elmot 1:d0dfbce63a89 529 * @brief Stop the comparator.
elmot 1:d0dfbce63a89 530 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 531 * @retval HAL status
elmot 1:d0dfbce63a89 532 */
elmot 1:d0dfbce63a89 533 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 534 {
elmot 1:d0dfbce63a89 535 HAL_StatusTypeDef status = HAL_OK;
elmot 1:d0dfbce63a89 536
elmot 1:d0dfbce63a89 537 /* Check the COMP handle allocation and lock status */
elmot 1:d0dfbce63a89 538 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
elmot 1:d0dfbce63a89 539 {
elmot 1:d0dfbce63a89 540 status = HAL_ERROR;
elmot 1:d0dfbce63a89 541 }
elmot 1:d0dfbce63a89 542 else
elmot 1:d0dfbce63a89 543 {
elmot 1:d0dfbce63a89 544 /* Check the parameter */
elmot 1:d0dfbce63a89 545 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
elmot 1:d0dfbce63a89 546
elmot 1:d0dfbce63a89 547 if((hcomp->State == HAL_COMP_STATE_BUSY) ||
elmot 1:d0dfbce63a89 548 (hcomp->State == HAL_COMP_STATE_READY) )
elmot 1:d0dfbce63a89 549 {
elmot 1:d0dfbce63a89 550 /* Disable the selected comparator */
elmot 1:d0dfbce63a89 551 CLEAR_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
elmot 1:d0dfbce63a89 552
elmot 1:d0dfbce63a89 553 /* Set HAL COMP handle state */
elmot 1:d0dfbce63a89 554 hcomp->State = HAL_COMP_STATE_READY;
elmot 1:d0dfbce63a89 555 }
elmot 1:d0dfbce63a89 556 else
elmot 1:d0dfbce63a89 557 {
elmot 1:d0dfbce63a89 558 status = HAL_ERROR;
elmot 1:d0dfbce63a89 559 }
elmot 1:d0dfbce63a89 560 }
elmot 1:d0dfbce63a89 561
elmot 1:d0dfbce63a89 562 return status;
elmot 1:d0dfbce63a89 563 }
elmot 1:d0dfbce63a89 564
elmot 1:d0dfbce63a89 565 /**
elmot 1:d0dfbce63a89 566 * @brief Comparator IRQ handler.
elmot 1:d0dfbce63a89 567 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 568 * @retval None
elmot 1:d0dfbce63a89 569 */
elmot 1:d0dfbce63a89 570 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 571 {
elmot 1:d0dfbce63a89 572 /* Get the EXTI line corresponding to the selected COMP instance */
elmot 1:d0dfbce63a89 573 uint32_t exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
elmot 1:d0dfbce63a89 574
elmot 1:d0dfbce63a89 575 /* Check COMP EXTI flag */
elmot 1:d0dfbce63a89 576 if(READ_BIT(EXTI->PR1, exti_line) != RESET)
elmot 1:d0dfbce63a89 577 {
elmot 1:d0dfbce63a89 578 /* Check whether comparator is in independent or window mode */
elmot 1:d0dfbce63a89 579 if(READ_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE) != RESET)
elmot 1:d0dfbce63a89 580 {
elmot 1:d0dfbce63a89 581 /* Clear COMP EXTI line pending bit of the pair of comparators */
elmot 1:d0dfbce63a89 582 /* in window mode. */
elmot 1:d0dfbce63a89 583 /* Note: Pair of comparators in window mode can both trig IRQ when */
elmot 1:d0dfbce63a89 584 /* input voltage is changing from "out of window" area */
elmot 1:d0dfbce63a89 585 /* (low or high ) to the other "out of window" area (high or low).*/
elmot 1:d0dfbce63a89 586 /* Both flags must be cleared to call comparator trigger */
elmot 1:d0dfbce63a89 587 /* callback is called once. */
elmot 1:d0dfbce63a89 588 WRITE_REG(EXTI->PR1, (COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
elmot 1:d0dfbce63a89 589 }
elmot 1:d0dfbce63a89 590 else
elmot 1:d0dfbce63a89 591 {
elmot 1:d0dfbce63a89 592 /* Clear COMP EXTI line pending bit */
elmot 1:d0dfbce63a89 593 WRITE_REG(EXTI->PR1, exti_line);
elmot 1:d0dfbce63a89 594 }
elmot 1:d0dfbce63a89 595
elmot 1:d0dfbce63a89 596 /* COMP trigger user callback */
elmot 1:d0dfbce63a89 597 HAL_COMP_TriggerCallback(hcomp);
elmot 1:d0dfbce63a89 598 }
elmot 1:d0dfbce63a89 599 }
elmot 1:d0dfbce63a89 600
elmot 1:d0dfbce63a89 601 /**
elmot 1:d0dfbce63a89 602 * @}
elmot 1:d0dfbce63a89 603 */
elmot 1:d0dfbce63a89 604
elmot 1:d0dfbce63a89 605 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
elmot 1:d0dfbce63a89 606 * @brief Management functions.
elmot 1:d0dfbce63a89 607 *
elmot 1:d0dfbce63a89 608 @verbatim
elmot 1:d0dfbce63a89 609 ===============================================================================
elmot 1:d0dfbce63a89 610 ##### Peripheral Control functions #####
elmot 1:d0dfbce63a89 611 ===============================================================================
elmot 1:d0dfbce63a89 612 [..]
elmot 1:d0dfbce63a89 613 This subsection provides a set of functions allowing to control the comparators.
elmot 1:d0dfbce63a89 614
elmot 1:d0dfbce63a89 615 @endverbatim
elmot 1:d0dfbce63a89 616 * @{
elmot 1:d0dfbce63a89 617 */
elmot 1:d0dfbce63a89 618
elmot 1:d0dfbce63a89 619 /**
elmot 1:d0dfbce63a89 620 * @brief Lock the selected comparator configuration.
elmot 1:d0dfbce63a89 621 * @note A system reset is required to unlock the comparator configuration.
elmot 1:d0dfbce63a89 622 * @note Locking the comparator from reset state is possible
elmot 1:d0dfbce63a89 623 * if __HAL_RCC_SYSCFG_CLK_ENABLE() is being called before.
elmot 1:d0dfbce63a89 624 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 625 * @retval HAL status
elmot 1:d0dfbce63a89 626 */
elmot 1:d0dfbce63a89 627 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 628 {
elmot 1:d0dfbce63a89 629 HAL_StatusTypeDef status = HAL_OK;
elmot 1:d0dfbce63a89 630
elmot 1:d0dfbce63a89 631 /* Check the COMP handle allocation and lock status */
elmot 1:d0dfbce63a89 632 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
elmot 1:d0dfbce63a89 633 {
elmot 1:d0dfbce63a89 634 status = HAL_ERROR;
elmot 1:d0dfbce63a89 635 }
elmot 1:d0dfbce63a89 636 else
elmot 1:d0dfbce63a89 637 {
elmot 1:d0dfbce63a89 638 /* Check the parameter */
elmot 1:d0dfbce63a89 639 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
elmot 1:d0dfbce63a89 640
elmot 1:d0dfbce63a89 641 /* Set HAL COMP handle state */
elmot 1:d0dfbce63a89 642 hcomp->State = ((HAL_COMP_StateTypeDef)(hcomp->State | COMP_STATE_BITFIELD_LOCK));
elmot 1:d0dfbce63a89 643 }
elmot 1:d0dfbce63a89 644
elmot 1:d0dfbce63a89 645 if(status == HAL_OK)
elmot 1:d0dfbce63a89 646 {
elmot 1:d0dfbce63a89 647 /* Set the lock bit corresponding to selected comparator */
elmot 1:d0dfbce63a89 648 __HAL_COMP_LOCK(hcomp);
elmot 1:d0dfbce63a89 649 }
elmot 1:d0dfbce63a89 650
elmot 1:d0dfbce63a89 651 return status;
elmot 1:d0dfbce63a89 652 }
elmot 1:d0dfbce63a89 653
elmot 1:d0dfbce63a89 654 /**
elmot 1:d0dfbce63a89 655 * @brief Return the output level (high or low) of the selected comparator.
elmot 1:d0dfbce63a89 656 * The output level depends on the selected polarity.
elmot 1:d0dfbce63a89 657 * If the polarity is not inverted:
elmot 1:d0dfbce63a89 658 * - Comparator output is low when the input plus is at a lower
elmot 1:d0dfbce63a89 659 * voltage than the input minus
elmot 1:d0dfbce63a89 660 * - Comparator output is high when the input plus is at a higher
elmot 1:d0dfbce63a89 661 * voltage than the input minus
elmot 1:d0dfbce63a89 662 * If the polarity is inverted:
elmot 1:d0dfbce63a89 663 * - Comparator output is high when the input plus is at a lower
elmot 1:d0dfbce63a89 664 * voltage than the input minus
elmot 1:d0dfbce63a89 665 * - Comparator output is low when the input plus is at a higher
elmot 1:d0dfbce63a89 666 * voltage than the input minus
elmot 1:d0dfbce63a89 667 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 668 * @retval Returns the selected comparator output level:
elmot 1:d0dfbce63a89 669 * @arg @ref COMP_OUTPUT_LEVEL_LOW
elmot 1:d0dfbce63a89 670 * @arg @ref COMP_OUTPUT_LEVEL_HIGH
elmot 1:d0dfbce63a89 671 *
elmot 1:d0dfbce63a89 672 */
elmot 1:d0dfbce63a89 673 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 674 {
elmot 1:d0dfbce63a89 675 /* Check the parameter */
elmot 1:d0dfbce63a89 676 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
elmot 1:d0dfbce63a89 677
elmot 1:d0dfbce63a89 678 return (uint32_t)(READ_BIT(hcomp->Instance->CSR, COMP_CSR_VALUE)
elmot 1:d0dfbce63a89 679 >> COMP_OUTPUT_LEVEL_BITOFFSET_POS);
elmot 1:d0dfbce63a89 680 }
elmot 1:d0dfbce63a89 681
elmot 1:d0dfbce63a89 682 /**
elmot 1:d0dfbce63a89 683 * @brief Comparator callback.
elmot 1:d0dfbce63a89 684 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 685 * @retval None
elmot 1:d0dfbce63a89 686 */
elmot 1:d0dfbce63a89 687 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 688 {
elmot 1:d0dfbce63a89 689 /* Prevent unused argument(s) compilation warning */
elmot 1:d0dfbce63a89 690 UNUSED(hcomp);
elmot 1:d0dfbce63a89 691
elmot 1:d0dfbce63a89 692 /* NOTE : This function should not be modified, when the callback is needed,
elmot 1:d0dfbce63a89 693 the HAL_COMP_TriggerCallback should be implemented in the user file
elmot 1:d0dfbce63a89 694 */
elmot 1:d0dfbce63a89 695 }
elmot 1:d0dfbce63a89 696
elmot 1:d0dfbce63a89 697
elmot 1:d0dfbce63a89 698 /**
elmot 1:d0dfbce63a89 699 * @}
elmot 1:d0dfbce63a89 700 */
elmot 1:d0dfbce63a89 701
elmot 1:d0dfbce63a89 702 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
elmot 1:d0dfbce63a89 703 * @brief Peripheral State functions.
elmot 1:d0dfbce63a89 704 *
elmot 1:d0dfbce63a89 705 @verbatim
elmot 1:d0dfbce63a89 706 ===============================================================================
elmot 1:d0dfbce63a89 707 ##### Peripheral State functions #####
elmot 1:d0dfbce63a89 708 ===============================================================================
elmot 1:d0dfbce63a89 709 [..]
elmot 1:d0dfbce63a89 710 This subsection permit to get in run-time the status of the peripheral.
elmot 1:d0dfbce63a89 711
elmot 1:d0dfbce63a89 712 @endverbatim
elmot 1:d0dfbce63a89 713 * @{
elmot 1:d0dfbce63a89 714 */
elmot 1:d0dfbce63a89 715
elmot 1:d0dfbce63a89 716 /**
elmot 1:d0dfbce63a89 717 * @brief Return the COMP handle state.
elmot 1:d0dfbce63a89 718 * @param hcomp COMP handle
elmot 1:d0dfbce63a89 719 * @retval HAL state
elmot 1:d0dfbce63a89 720 */
elmot 1:d0dfbce63a89 721 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
elmot 1:d0dfbce63a89 722 {
elmot 1:d0dfbce63a89 723 /* Check the COMP handle allocation */
elmot 1:d0dfbce63a89 724 if(hcomp == NULL)
elmot 1:d0dfbce63a89 725 {
elmot 1:d0dfbce63a89 726 return HAL_COMP_STATE_RESET;
elmot 1:d0dfbce63a89 727 }
elmot 1:d0dfbce63a89 728
elmot 1:d0dfbce63a89 729 /* Check the parameter */
elmot 1:d0dfbce63a89 730 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
elmot 1:d0dfbce63a89 731
elmot 1:d0dfbce63a89 732 /* Return HAL COMP handle state */
elmot 1:d0dfbce63a89 733 return hcomp->State;
elmot 1:d0dfbce63a89 734 }
elmot 1:d0dfbce63a89 735
elmot 1:d0dfbce63a89 736 /**
elmot 1:d0dfbce63a89 737 * @}
elmot 1:d0dfbce63a89 738 */
elmot 1:d0dfbce63a89 739
elmot 1:d0dfbce63a89 740 /**
elmot 1:d0dfbce63a89 741 * @}
elmot 1:d0dfbce63a89 742 */
elmot 1:d0dfbce63a89 743
elmot 1:d0dfbce63a89 744 #endif /* HAL_COMP_MODULE_ENABLED */
elmot 1:d0dfbce63a89 745 /**
elmot 1:d0dfbce63a89 746 * @}
elmot 1:d0dfbce63a89 747 */
elmot 1:d0dfbce63a89 748
elmot 1:d0dfbce63a89 749 /**
elmot 1:d0dfbce63a89 750 * @}
elmot 1:d0dfbce63a89 751 */
elmot 1:d0dfbce63a89 752
elmot 1:d0dfbce63a89 753 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/