mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Parent:
394:83f921546702
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

Full URL: https://github.com/mbedmicro/mbed/commit/d5b4d2ab9c47edb4dc5776e7177b0c2263459081/

Initial version of drivers for SAMR21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 354:e67efb2aab0e 1 /**
mbed_official 354:e67efb2aab0e 2 ******************************************************************************
mbed_official 354:e67efb2aab0e 3 * @file stm32l1xx_hal_comp.c
mbed_official 354:e67efb2aab0e 4 * @author MCD Application Team
mbed_official 354:e67efb2aab0e 5 * @version V1.0.0
mbed_official 354:e67efb2aab0e 6 * @date 5-September-2014
mbed_official 354:e67efb2aab0e 7 * @brief COMP HAL module driver.
mbed_official 354:e67efb2aab0e 8 *
mbed_official 354:e67efb2aab0e 9 * This file provides firmware functions to manage the following
mbed_official 354:e67efb2aab0e 10 * functionalities of the COMP peripheral:
mbed_official 354:e67efb2aab0e 11 * + Initialization and de-initialization functions
mbed_official 354:e67efb2aab0e 12 * + I/O operation functions
mbed_official 354:e67efb2aab0e 13 * + Peripheral Control functions
mbed_official 354:e67efb2aab0e 14 * + Peripheral State functions
mbed_official 354:e67efb2aab0e 15 *
mbed_official 354:e67efb2aab0e 16 @verbatim
mbed_official 354:e67efb2aab0e 17 ================================================================================
mbed_official 354:e67efb2aab0e 18 ##### COMP Peripheral features #####
mbed_official 354:e67efb2aab0e 19 ================================================================================
mbed_official 354:e67efb2aab0e 20
mbed_official 354:e67efb2aab0e 21 [..]
mbed_official 354:e67efb2aab0e 22 The STM32L1xx device family integrates 2 analog comparators COMP1 and
mbed_official 354:e67efb2aab0e 23 COMP2:
mbed_official 354:e67efb2aab0e 24 (#) The non inverting input and inverting input can be set to GPIO pins.
mbed_official 354:e67efb2aab0e 25 Refer to "table1. COMP Inputs" below.
mbed_official 354:e67efb2aab0e 26 HAL COMP driver configures the Routing Interface (RI) to connect the
mbed_official 354:e67efb2aab0e 27 selected I/O pins to comparator input.
mbed_official 354:e67efb2aab0e 28 Caution: Comparator COMP1 and ADC cannot be used at the same time as
mbed_official 354:e67efb2aab0e 29 ADC since they share the ADC switch matrix: COMP1 non-inverting
mbed_official 354:e67efb2aab0e 30 input is routed through ADC switch matrix. Except if ADC is intented
mbed_official 354:e67efb2aab0e 31 to measure voltage on COMP1 non-inverting input: it can be performed
mbed_official 354:e67efb2aab0e 32 on ADC channel VCOMP.
mbed_official 354:e67efb2aab0e 33
mbed_official 354:e67efb2aab0e 34 (#) The COMP output is available using HAL_COMP_GetOutputLevel().
mbed_official 354:e67efb2aab0e 35
mbed_official 354:e67efb2aab0e 36 (#) The COMP output can be redirected to embedded timers (TIM2, TIM3,
mbed_official 354:e67efb2aab0e 37 TIM4, TIM10).
mbed_official 354:e67efb2aab0e 38 COMP output cannot be redirected to any I/O pin.
mbed_official 354:e67efb2aab0e 39 Refer to "table 2. COMP Outputs redirection to embedded timers" below.
mbed_official 354:e67efb2aab0e 40
mbed_official 354:e67efb2aab0e 41 (#) The comparators COMP1 and COMP2 can be combined in window mode.
mbed_official 354:e67efb2aab0e 42 In this mode, COMP2 non inverting input is used as common
mbed_official 354:e67efb2aab0e 43 non-inverting input.
mbed_official 354:e67efb2aab0e 44
mbed_official 354:e67efb2aab0e 45 (#) The 2 comparators have interrupt capability with wake-up
mbed_official 354:e67efb2aab0e 46 from Sleep and Stop modes (through the EXTI controller):
mbed_official 354:e67efb2aab0e 47 (++) COMP1 is internally connected to EXTI Line 21
mbed_official 354:e67efb2aab0e 48 (++) COMP2 is internally connected to EXTI Line 22
mbed_official 354:e67efb2aab0e 49
mbed_official 354:e67efb2aab0e 50 From the corresponding IRQ handler, the right interrupt source can be
mbed_official 354:e67efb2aab0e 51 retrieved with macro __HAL_COMP_EXTI_GET_FLAG(). Possible values are:
mbed_official 354:e67efb2aab0e 52 (++) COMP_EXTI_LINE_COMP1_EVENT
mbed_official 354:e67efb2aab0e 53 (++) COMP_EXTI_LINE_COMP2_EVENT
mbed_official 354:e67efb2aab0e 54
mbed_official 354:e67efb2aab0e 55 (#) The comparators also offer the possibility to ouput the voltage
mbed_official 354:e67efb2aab0e 56 reference (VrefInt), used on inverting inputs, on I/O pin through
mbed_official 354:e67efb2aab0e 57 a buffer. To use it, refer to macro "__HAL_VREFINT_OUT_ENABLE()".
mbed_official 354:e67efb2aab0e 58
mbed_official 354:e67efb2aab0e 59
mbed_official 354:e67efb2aab0e 60 [..] Table 1. COMP Inputs for the STM32L1xx devices
mbed_official 354:e67efb2aab0e 61 +----------------------------------------------------------------------+
mbed_official 354:e67efb2aab0e 62 | | | COMP1 | COMP2 |
mbed_official 354:e67efb2aab0e 63 |-----------------|--------------------------------|---------|---------|
mbed_official 354:e67efb2aab0e 64 | | 1/4 VREFINT | -- | OK |
mbed_official 354:e67efb2aab0e 65 | | 1/2 VREFINT | -- | OK |
mbed_official 354:e67efb2aab0e 66 | | 3/4 VREFINT | -- | OK |
mbed_official 354:e67efb2aab0e 67 | Inverting | VREFINT | OK | OK |
mbed_official 354:e67efb2aab0e 68 | input | DAC Ch1 OUT (PA4) | -- | OK |
mbed_official 354:e67efb2aab0e 69 | | DAC Ch2 OUT (PA5) | -- | OK |
mbed_official 354:e67efb2aab0e 70 | | IO: PB3 | -- | OK |
mbed_official 354:e67efb2aab0e 71 |-----------------|--------------------------------|---------|---------|
mbed_official 354:e67efb2aab0e 72 | | IO: | | |
mbed_official 354:e67efb2aab0e 73 | | PB4, 5, 6*, 7* | --- | OK |
mbed_official 354:e67efb2aab0e 74 | Non-inverting | PA0*, 1*, 2*, 3*, 4, 5, 6, 7 | OK | --- |
mbed_official 354:e67efb2aab0e 75 | input | PB0, 1, 12, 13, 14, 15 | OK | --- |
mbed_official 354:e67efb2aab0e 76 | | PC0, 1, 2, 3, 4, 5 | OK | --- |
mbed_official 354:e67efb2aab0e 77 | | PE7, 8, 9, 10 | OK | --- |
mbed_official 354:e67efb2aab0e 78 | | PF6, 7, 8, 9, 10 | OK | --- |
mbed_official 354:e67efb2aab0e 79 | | OPAMP1 output | OK | --- |
mbed_official 354:e67efb2aab0e 80 | | OPAMP2 output | OK | --- |
mbed_official 354:e67efb2aab0e 81 | | OPAMP3 output** | OK | --- |
mbed_official 354:e67efb2aab0e 82 +----------------------------------------------------------------------+
mbed_official 354:e67efb2aab0e 83 *: Available on devices category Cat.3, Cat.4, Cat.5 only.
mbed_official 354:e67efb2aab0e 84 **: Available on devices category Cat.4 only.
mbed_official 354:e67efb2aab0e 85
mbed_official 354:e67efb2aab0e 86 [..] Table 2. COMP Outputs redirection to embedded timers
mbed_official 354:e67efb2aab0e 87 +-----------------------------------+
mbed_official 354:e67efb2aab0e 88 | COMP1 | COMP2 |
mbed_official 354:e67efb2aab0e 89 |-----------------|-----------------|
mbed_official 354:e67efb2aab0e 90 | | TIM2 IC4 |
mbed_official 354:e67efb2aab0e 91 | | TIM2 OCREF CLR |
mbed_official 354:e67efb2aab0e 92 | (no redirection | TIM3 IC4 |
mbed_official 354:e67efb2aab0e 93 | to timers) | TIM3 OCREF CLR |
mbed_official 354:e67efb2aab0e 94 | | TIM4 IC4 |
mbed_official 354:e67efb2aab0e 95 | | TIM4 OCREF CLR |
mbed_official 354:e67efb2aab0e 96 | | TIM10 IC1 |
mbed_official 354:e67efb2aab0e 97 +-----------------------------------+
mbed_official 354:e67efb2aab0e 98
mbed_official 354:e67efb2aab0e 99
mbed_official 354:e67efb2aab0e 100 ##### How to use this driver #####
mbed_official 354:e67efb2aab0e 101 ================================================================================
mbed_official 354:e67efb2aab0e 102 [..]
mbed_official 354:e67efb2aab0e 103 This driver provides functions to configure and program the Comparators of all STM32L1xx devices.
mbed_official 354:e67efb2aab0e 104
mbed_official 354:e67efb2aab0e 105 To use the comparator, perform the following steps:
mbed_official 354:e67efb2aab0e 106
mbed_official 354:e67efb2aab0e 107 (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit().
mbed_official 354:e67efb2aab0e 108 (++) Configure the comparator input I/O pin using HAL_GPIO_Init():
mbed_official 354:e67efb2aab0e 109 - For all inputs: I/O pin in analog mode (Schmitt trigger disabled)
mbed_official 354:e67efb2aab0e 110 - Possible alternate configuration, for non-inverting inputs of comparator 2: I/O pin in floating mode (Schmitt trigger enabled).
mbed_official 354:e67efb2aab0e 111 It is recommended to use analog configuration to avoid any overconsumption around VDD/2.
mbed_official 354:e67efb2aab0e 112 (++) Enable COMP Peripheral clock using macro __COMP_CLK_ENABLE()
mbed_official 354:e67efb2aab0e 113 (++) If required enable the COMP interrupt (EXTI line Interrupt): enable
mbed_official 354:e67efb2aab0e 114 the comparator interrupt vector using HAL_NVIC_EnableIRQ(COMP_IRQn)
mbed_official 354:e67efb2aab0e 115 and HAL_NVIC_SetPriority(COMP_IRQn, xxx, xxx) functions.
mbed_official 354:e67efb2aab0e 116
mbed_official 354:e67efb2aab0e 117 (#) Configure the comparator using HAL_COMP_Init() function:
mbed_official 354:e67efb2aab0e 118 (++) Select the inverting input (COMP2 only)
mbed_official 354:e67efb2aab0e 119 (++) Select the non-inverting input
mbed_official 354:e67efb2aab0e 120 (++) Select the output redirection to timers (COMP2 only)
mbed_official 354:e67efb2aab0e 121 (++) Select the speed mode (COMP2 only)
mbed_official 354:e67efb2aab0e 122 (++) Select the window mode (related to COMP1 and COMP2, but selected
mbed_official 354:e67efb2aab0e 123 by COMP2 only)
mbed_official 354:e67efb2aab0e 124 (++) Select the pull-up/down resistors on non-inverting input (COMP1 only)
mbed_official 354:e67efb2aab0e 125
mbed_official 354:e67efb2aab0e 126 (#) Enable the comparator using HAL_COMP_Start() or HAL_COMP_Start_IT()
mbed_official 354:e67efb2aab0e 127 function
mbed_official 354:e67efb2aab0e 128
mbed_official 354:e67efb2aab0e 129 (#) If needed, use HAL_COMP_GetOutputLevel() or HAL_COMP_TriggerCallback()
mbed_official 354:e67efb2aab0e 130 functions to manage comparator actions (output level or events)
mbed_official 354:e67efb2aab0e 131
mbed_official 354:e67efb2aab0e 132 (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
mbed_official 354:e67efb2aab0e 133 function
mbed_official 354:e67efb2aab0e 134
mbed_official 354:e67efb2aab0e 135 (#) De-initialize the comparator using HAL_COMP_DeInit() function
mbed_official 354:e67efb2aab0e 136
mbed_official 354:e67efb2aab0e 137
mbed_official 354:e67efb2aab0e 138 @endverbatim
mbed_official 354:e67efb2aab0e 139 ******************************************************************************
mbed_official 354:e67efb2aab0e 140 * @attention
mbed_official 354:e67efb2aab0e 141 *
mbed_official 354:e67efb2aab0e 142 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 354:e67efb2aab0e 143 *
mbed_official 354:e67efb2aab0e 144 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 354:e67efb2aab0e 145 * are permitted provided that the following conditions are met:
mbed_official 354:e67efb2aab0e 146 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 354:e67efb2aab0e 147 * this list of conditions and the following disclaimer.
mbed_official 354:e67efb2aab0e 148 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 354:e67efb2aab0e 149 * this list of conditions and the following disclaimer in the documentation
mbed_official 354:e67efb2aab0e 150 * and/or other materials provided with the distribution.
mbed_official 354:e67efb2aab0e 151 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 354:e67efb2aab0e 152 * may be used to endorse or promote products derived from this software
mbed_official 354:e67efb2aab0e 153 * without specific prior written permission.
mbed_official 354:e67efb2aab0e 154 *
mbed_official 354:e67efb2aab0e 155 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 354:e67efb2aab0e 156 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 354:e67efb2aab0e 157 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 354:e67efb2aab0e 158 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 354:e67efb2aab0e 159 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 354:e67efb2aab0e 160 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 354:e67efb2aab0e 161 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 354:e67efb2aab0e 162 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 354:e67efb2aab0e 163 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 354:e67efb2aab0e 164 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 354:e67efb2aab0e 165 *
mbed_official 354:e67efb2aab0e 166 ******************************************************************************
mbed_official 354:e67efb2aab0e 167 */
mbed_official 354:e67efb2aab0e 168
mbed_official 354:e67efb2aab0e 169 /* Includes ------------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 170 #include "stm32l1xx_hal.h"
mbed_official 354:e67efb2aab0e 171
mbed_official 354:e67efb2aab0e 172 /** @addtogroup STM32L1xx_HAL_Driver
mbed_official 354:e67efb2aab0e 173 * @{
mbed_official 354:e67efb2aab0e 174 */
mbed_official 354:e67efb2aab0e 175
mbed_official 354:e67efb2aab0e 176 /** @defgroup COMP COMP
mbed_official 354:e67efb2aab0e 177 * @brief COMP HAL module driver
mbed_official 354:e67efb2aab0e 178 * @{
mbed_official 354:e67efb2aab0e 179 */
mbed_official 354:e67efb2aab0e 180
mbed_official 354:e67efb2aab0e 181 #ifdef HAL_COMP_MODULE_ENABLED
mbed_official 354:e67efb2aab0e 182
mbed_official 354:e67efb2aab0e 183 /* Private typedef -----------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 184 /* Private define ------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 185
mbed_official 354:e67efb2aab0e 186 /** @defgroup COMP_Private_Constants COMP Private Constants
mbed_official 354:e67efb2aab0e 187 * @{
mbed_official 354:e67efb2aab0e 188 */
mbed_official 354:e67efb2aab0e 189 /* Delay for COMP startup time. */
mbed_official 354:e67efb2aab0e 190 /* Maximum delay is 10us for comparator 1 and 25us for comparator 2 in slow */
mbed_official 354:e67efb2aab0e 191 /* mode (refer to device datasheet, parameter tSTART). */
mbed_official 354:e67efb2aab0e 192 /* Delay in CPU cycles, fixed to worst case: maximum CPU frequency 32MHz to */
mbed_official 354:e67efb2aab0e 193 /* have the minimum number of CPU cycles to fulfill this delay. */
mbed_official 354:e67efb2aab0e 194 /* - Comparator 1: delay minimum of 320 CPU cyles. Wait loop takes 3 CPU */
mbed_official 354:e67efb2aab0e 195 /* cycles per iteration, therefore total wait iterations */
mbed_official 354:e67efb2aab0e 196 /* number must be initialized at 106 iterations. */
mbed_official 354:e67efb2aab0e 197 /* - Comparator 2: delay minimum of 800 CPU cyles. Wait loop takes 3 CPU */
mbed_official 354:e67efb2aab0e 198 /* cycles per iteration, therefore total wait iterations */
mbed_official 354:e67efb2aab0e 199 /* number must be initialized at 266 iterations. */
mbed_official 354:e67efb2aab0e 200 #define COMP1_START_DELAY_CPU_CYCLES ((uint32_t)106)
mbed_official 354:e67efb2aab0e 201 #define COMP2_START_DELAY_CPU_CYCLES ((uint32_t)266)
mbed_official 354:e67efb2aab0e 202
mbed_official 354:e67efb2aab0e 203 /* Comparator status "locked": to update COMP handle state (software lock */
mbed_official 354:e67efb2aab0e 204 /* only on COMP of STM32L1xx devices) by bitfield: */
mbed_official 354:e67efb2aab0e 205 /* states HAL_COMP_STATE_READY_LOCKED, HAL_COMP_STATE_BUSY_LOCKED. */
mbed_official 354:e67efb2aab0e 206 #define COMP_STATE_BIT_LOCK ((uint32_t) 0x00000010)
mbed_official 354:e67efb2aab0e 207
mbed_official 354:e67efb2aab0e 208 /**
mbed_official 354:e67efb2aab0e 209 * @}
mbed_official 354:e67efb2aab0e 210 */
mbed_official 354:e67efb2aab0e 211
mbed_official 354:e67efb2aab0e 212
mbed_official 354:e67efb2aab0e 213 /* Private macro -------------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 214 /* Private variables ---------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 215 /* Private function prototypes -----------------------------------------------*/
mbed_official 354:e67efb2aab0e 216 /* Private functions ---------------------------------------------------------*/
mbed_official 354:e67efb2aab0e 217
mbed_official 354:e67efb2aab0e 218 /** @defgroup COMP_Exported_Functions COMP Exported Functions
mbed_official 354:e67efb2aab0e 219 * @{
mbed_official 354:e67efb2aab0e 220 */
mbed_official 354:e67efb2aab0e 221
mbed_official 354:e67efb2aab0e 222 /** @defgroup COMP_Exported_Functions_Group1 Initialization and de-initialization functions
mbed_official 354:e67efb2aab0e 223 * @brief Initialization and Configuration functions
mbed_official 354:e67efb2aab0e 224 *
mbed_official 354:e67efb2aab0e 225 @verbatim
mbed_official 354:e67efb2aab0e 226 ===============================================================================
mbed_official 354:e67efb2aab0e 227 ##### Initialization and de-initialization functions #####
mbed_official 354:e67efb2aab0e 228 ===============================================================================
mbed_official 354:e67efb2aab0e 229 [..] This section provides functions to initialize and de-initialize comparators
mbed_official 354:e67efb2aab0e 230
mbed_official 354:e67efb2aab0e 231 @endverbatim
mbed_official 354:e67efb2aab0e 232 * @{
mbed_official 354:e67efb2aab0e 233 */
mbed_official 354:e67efb2aab0e 234
mbed_official 354:e67efb2aab0e 235 /**
mbed_official 354:e67efb2aab0e 236 * @brief Initializes the COMP according to the specified
mbed_official 354:e67efb2aab0e 237 * parameters in the COMP_InitTypeDef and create the associated handle.
mbed_official 354:e67efb2aab0e 238 * @note If the selected comparator is locked, initialization can't be performed.
mbed_official 354:e67efb2aab0e 239 * To unlock the configuration, perform a system reset.
mbed_official 354:e67efb2aab0e 240 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 241 * @retval HAL status
mbed_official 354:e67efb2aab0e 242 */
mbed_official 354:e67efb2aab0e 243 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 244 {
mbed_official 354:e67efb2aab0e 245 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 246
mbed_official 354:e67efb2aab0e 247 /* Check the COMP handle allocation and lock status */
mbed_official 354:e67efb2aab0e 248 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
mbed_official 354:e67efb2aab0e 249 {
mbed_official 354:e67efb2aab0e 250 status = HAL_ERROR;
mbed_official 354:e67efb2aab0e 251 }
mbed_official 354:e67efb2aab0e 252 else
mbed_official 354:e67efb2aab0e 253 {
mbed_official 354:e67efb2aab0e 254 /* Check the parameter */
mbed_official 354:e67efb2aab0e 255 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
mbed_official 354:e67efb2aab0e 256
mbed_official 354:e67efb2aab0e 257 if (hcomp->Instance == COMP1)
mbed_official 354:e67efb2aab0e 258 {
mbed_official 354:e67efb2aab0e 259 assert_param(IS_COMP_NONINVERTINGINPUTPULL(hcomp->Init.NonInvertingInputPull));
mbed_official 354:e67efb2aab0e 260 }
mbed_official 354:e67efb2aab0e 261 else /* if (hcomp->Instance == COMP2) */
mbed_official 354:e67efb2aab0e 262 {
mbed_official 354:e67efb2aab0e 263 assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
mbed_official 354:e67efb2aab0e 264 assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
mbed_official 354:e67efb2aab0e 265 assert_param(IS_COMP_MODE(hcomp->Init.Mode));
mbed_official 354:e67efb2aab0e 266 assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
mbed_official 354:e67efb2aab0e 267 }
mbed_official 354:e67efb2aab0e 268
mbed_official 354:e67efb2aab0e 269 /* In window mode, non-inverting inputs of the 2 comparators are */
mbed_official 354:e67efb2aab0e 270 /* connected together and are using inputs of COMP2 only. If COMP1 is */
mbed_official 354:e67efb2aab0e 271 /* selected, this parameter is discarded. */
mbed_official 354:e67efb2aab0e 272 if ((hcomp->Init.WindowMode == COMP_WINDOWMODE_DISABLED) ||
mbed_official 354:e67efb2aab0e 273 (hcomp->Instance == COMP2) )
mbed_official 354:e67efb2aab0e 274 {
mbed_official 354:e67efb2aab0e 275 assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
mbed_official 354:e67efb2aab0e 276 }
mbed_official 354:e67efb2aab0e 277
mbed_official 354:e67efb2aab0e 278
mbed_official 354:e67efb2aab0e 279 /* Enable SYSCFG clock and the low level hardware to access comparators */
mbed_official 354:e67efb2aab0e 280 if(hcomp->State == HAL_COMP_STATE_RESET)
mbed_official 354:e67efb2aab0e 281 {
mbed_official 354:e67efb2aab0e 282 /* Enable SYSCFG clock to control the routing Interface (RI) */
mbed_official 354:e67efb2aab0e 283 __SYSCFG_CLK_ENABLE();
mbed_official 354:e67efb2aab0e 284
mbed_official 354:e67efb2aab0e 285 /* Init the low level hardware */
mbed_official 354:e67efb2aab0e 286 HAL_COMP_MspInit(hcomp);
mbed_official 354:e67efb2aab0e 287 }
mbed_official 354:e67efb2aab0e 288
mbed_official 354:e67efb2aab0e 289 /* Configuration of comparator: */
mbed_official 354:e67efb2aab0e 290 /* - Output selection */
mbed_official 354:e67efb2aab0e 291 /* - Inverting input selection */
mbed_official 354:e67efb2aab0e 292 /* - Window mode */
mbed_official 354:e67efb2aab0e 293 /* - Mode fast/slow speed */
mbed_official 354:e67efb2aab0e 294 /* - Inverting input pull-up/down resistors */
mbed_official 354:e67efb2aab0e 295
mbed_official 354:e67efb2aab0e 296 /* Configuration depending on comparator instance */
mbed_official 354:e67efb2aab0e 297 if (hcomp->Instance == COMP1)
mbed_official 354:e67efb2aab0e 298 {
mbed_official 354:e67efb2aab0e 299 MODIFY_REG(COMP->CSR, COMP_CSR_400KPD | COMP_CSR_10KPD | COMP_CSR_400KPU | COMP_CSR_10KPU,
mbed_official 354:e67efb2aab0e 300 hcomp->Init.NonInvertingInputPull );
mbed_official 354:e67efb2aab0e 301 }
mbed_official 354:e67efb2aab0e 302 else /* if (hcomp->Instance == COMP2) */
mbed_official 354:e67efb2aab0e 303 {
mbed_official 354:e67efb2aab0e 304 /* Note: If comparator 2 is not enabled, inverting input (parameter */
mbed_official 354:e67efb2aab0e 305 /* "hcomp->Init.InvertingInput") is configured into function */
mbed_official 354:e67efb2aab0e 306 /* "HAL_COMP_Start()" since inverting input selection also */
mbed_official 354:e67efb2aab0e 307 /* enables the comparator 2. */
mbed_official 354:e67efb2aab0e 308 /* If comparator 2 is already enabled, inverting input is */
mbed_official 354:e67efb2aab0e 309 /* reconfigured on the fly. */
mbed_official 354:e67efb2aab0e 310 if (__COMP_IS_ENABLED(hcomp) == RESET)
mbed_official 354:e67efb2aab0e 311 {
mbed_official 354:e67efb2aab0e 312 MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL |
mbed_official 354:e67efb2aab0e 313 COMP_CSR_WNDWE |
mbed_official 354:e67efb2aab0e 314 COMP_CSR_SPEED ,
mbed_official 354:e67efb2aab0e 315 hcomp->Init.Output |
mbed_official 354:e67efb2aab0e 316 hcomp->Init.WindowMode |
mbed_official 354:e67efb2aab0e 317 hcomp->Init.Mode );
mbed_official 354:e67efb2aab0e 318 }
mbed_official 354:e67efb2aab0e 319 else
mbed_official 354:e67efb2aab0e 320 {
mbed_official 354:e67efb2aab0e 321 MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL |
mbed_official 354:e67efb2aab0e 322 COMP_CSR_INSEL |
mbed_official 354:e67efb2aab0e 323 COMP_CSR_WNDWE |
mbed_official 354:e67efb2aab0e 324 COMP_CSR_SPEED ,
mbed_official 354:e67efb2aab0e 325 hcomp->Init.Output |
mbed_official 354:e67efb2aab0e 326 hcomp->Init.InvertingInput |
mbed_official 354:e67efb2aab0e 327 hcomp->Init.WindowMode |
mbed_official 354:e67efb2aab0e 328 hcomp->Init.Mode );
mbed_official 354:e67efb2aab0e 329 }
mbed_official 354:e67efb2aab0e 330 }
mbed_official 354:e67efb2aab0e 331
mbed_official 354:e67efb2aab0e 332 /* Configure Routing Interface (RI) switches for comparator non-inverting */
mbed_official 354:e67efb2aab0e 333 /* input. */
mbed_official 354:e67efb2aab0e 334 /* Except in 2 cases: */
mbed_official 354:e67efb2aab0e 335 /* - if non-inverting input has no selection: it can be the case for */
mbed_official 354:e67efb2aab0e 336 /* COMP1 in window mode. */
mbed_official 354:e67efb2aab0e 337 /* - particular case for PC3: if switch COMP1_SW1 is closed */
mbed_official 354:e67efb2aab0e 338 /* (by macro "__HAL_OPAMP_OPAMP3OUT_CONNECT_ADC_COMP1()" or */
mbed_official 354:e67efb2aab0e 339 /* "__HAL_RI_SWITCH_COMP1_SW1_CLOSE()"), connection between pin PC3 */
mbed_official 354:e67efb2aab0e 340 /* (or OPAMP3, if available) and COMP1 is done directly, without going */
mbed_official 354:e67efb2aab0e 341 /* through ADC switch matrix. */
mbed_official 354:e67efb2aab0e 342 if (__COMP_ROUTING_INTERFACE_TOBECONFIGURED(hcomp))
mbed_official 354:e67efb2aab0e 343 {
mbed_official 354:e67efb2aab0e 344 if (hcomp->Instance == COMP1)
mbed_official 354:e67efb2aab0e 345 {
mbed_official 354:e67efb2aab0e 346 /* Enable the switch control mode */
mbed_official 354:e67efb2aab0e 347 __HAL_RI_SWITCHCONTROLMODE_ENABLE();
mbed_official 354:e67efb2aab0e 348
mbed_official 354:e67efb2aab0e 349 /* Close the analog switch of ADC switch matrix to COMP1 (ADC */
mbed_official 354:e67efb2aab0e 350 /* channel 26: Vcomp) */
mbed_official 354:e67efb2aab0e 351 __HAL_RI_IOSWITCH_CLOSE(RI_IOSWITCH_VCOMP);
mbed_official 354:e67efb2aab0e 352 }
mbed_official 354:e67efb2aab0e 353
mbed_official 354:e67efb2aab0e 354 /* Close the I/O analog switch corresponding to comparator */
mbed_official 354:e67efb2aab0e 355 /* non-inverting input selected. */
mbed_official 354:e67efb2aab0e 356 __HAL_RI_IOSWITCH_CLOSE(hcomp->Init.NonInvertingInput);
mbed_official 354:e67efb2aab0e 357 }
mbed_official 354:e67efb2aab0e 358
mbed_official 354:e67efb2aab0e 359
mbed_official 354:e67efb2aab0e 360 /* Initialize the COMP state*/
mbed_official 354:e67efb2aab0e 361 if(hcomp->State == HAL_COMP_STATE_RESET)
mbed_official 354:e67efb2aab0e 362 {
mbed_official 354:e67efb2aab0e 363 hcomp->State = HAL_COMP_STATE_READY;
mbed_official 354:e67efb2aab0e 364 }
mbed_official 354:e67efb2aab0e 365 }
mbed_official 354:e67efb2aab0e 366
mbed_official 354:e67efb2aab0e 367 return status;
mbed_official 354:e67efb2aab0e 368 }
mbed_official 354:e67efb2aab0e 369
mbed_official 354:e67efb2aab0e 370
mbed_official 354:e67efb2aab0e 371 /**
mbed_official 354:e67efb2aab0e 372 * @brief DeInitializes the COMP peripheral
mbed_official 354:e67efb2aab0e 373 * @note Deinitialization can't be performed if the COMP configuration is locked.
mbed_official 354:e67efb2aab0e 374 * To unlock the configuration, perform a system reset.
mbed_official 354:e67efb2aab0e 375 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 376 * @retval HAL status
mbed_official 354:e67efb2aab0e 377 */
mbed_official 354:e67efb2aab0e 378 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 379 {
mbed_official 354:e67efb2aab0e 380 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 381
mbed_official 354:e67efb2aab0e 382 /* Check the COMP handle allocation and lock status */
mbed_official 354:e67efb2aab0e 383 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
mbed_official 354:e67efb2aab0e 384 {
mbed_official 354:e67efb2aab0e 385 status = HAL_ERROR;
mbed_official 354:e67efb2aab0e 386 }
mbed_official 354:e67efb2aab0e 387 else
mbed_official 354:e67efb2aab0e 388 {
mbed_official 354:e67efb2aab0e 389 /* Check the parameter */
mbed_official 354:e67efb2aab0e 390 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
mbed_official 354:e67efb2aab0e 391
mbed_official 354:e67efb2aab0e 392 /* Reset configuration depending on comparator instance */
mbed_official 354:e67efb2aab0e 393 if (hcomp->Instance == COMP1)
mbed_official 354:e67efb2aab0e 394 {
mbed_official 354:e67efb2aab0e 395 CLEAR_BIT(COMP->CSR , COMP_CSR_400KPD | COMP_CSR_10KPD | COMP_CSR_400KPU | COMP_CSR_10KPU);
mbed_official 354:e67efb2aab0e 396 }
mbed_official 354:e67efb2aab0e 397 else /* if (hcomp->Instance == COMP2) */
mbed_official 354:e67efb2aab0e 398 {
mbed_official 354:e67efb2aab0e 399 CLEAR_BIT(COMP->CSR , COMP_CSR_OUTSEL |
mbed_official 354:e67efb2aab0e 400 COMP_CSR_WNDWE |
mbed_official 354:e67efb2aab0e 401 COMP_CSR_INSEL |
mbed_official 354:e67efb2aab0e 402 COMP_CSR_SPEED );
mbed_official 354:e67efb2aab0e 403 }
mbed_official 354:e67efb2aab0e 404
mbed_official 354:e67efb2aab0e 405
mbed_official 354:e67efb2aab0e 406 /* Restore default state of Routing Interface (RI) switches for */
mbed_official 354:e67efb2aab0e 407 /* comparator non-inverting input. */
mbed_official 354:e67efb2aab0e 408 if (hcomp->Init.NonInvertingInput != COMP_NONINVERTINGINPUT_NONE)
mbed_official 354:e67efb2aab0e 409 {
mbed_official 354:e67efb2aab0e 410 /* Open the I/O analog switch corresponding to comparator */
mbed_official 354:e67efb2aab0e 411 /* non-inverting input selected. */
mbed_official 354:e67efb2aab0e 412 __HAL_RI_IOSWITCH_OPEN(hcomp->Init.NonInvertingInput);
mbed_official 354:e67efb2aab0e 413 }
mbed_official 354:e67efb2aab0e 414 if (hcomp->Instance == COMP1)
mbed_official 354:e67efb2aab0e 415 {
mbed_official 354:e67efb2aab0e 416 /* Open the analog switch of ADC switch matrix to COMP1 (ADC */
mbed_official 354:e67efb2aab0e 417 /* channel 26: Vcomp) */
mbed_official 354:e67efb2aab0e 418 __HAL_RI_IOSWITCH_OPEN(RI_IOSWITCH_VCOMP);
mbed_official 354:e67efb2aab0e 419
mbed_official 354:e67efb2aab0e 420 /* Disable the switch control mode */
mbed_official 354:e67efb2aab0e 421 __HAL_RI_SWITCHCONTROLMODE_DISABLE();
mbed_official 354:e67efb2aab0e 422 }
mbed_official 354:e67efb2aab0e 423
mbed_official 354:e67efb2aab0e 424
mbed_official 354:e67efb2aab0e 425 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
mbed_official 354:e67efb2aab0e 426 HAL_COMP_MspDeInit(hcomp);
mbed_official 354:e67efb2aab0e 427
mbed_official 354:e67efb2aab0e 428 hcomp->State = HAL_COMP_STATE_RESET;
mbed_official 354:e67efb2aab0e 429
mbed_official 354:e67efb2aab0e 430 /* Process unlocked */
mbed_official 354:e67efb2aab0e 431 __HAL_UNLOCK(hcomp);
mbed_official 354:e67efb2aab0e 432 }
mbed_official 354:e67efb2aab0e 433
mbed_official 354:e67efb2aab0e 434 return status;
mbed_official 354:e67efb2aab0e 435 }
mbed_official 354:e67efb2aab0e 436
mbed_official 354:e67efb2aab0e 437 /**
mbed_official 354:e67efb2aab0e 438 * @brief Initializes the COMP MSP.
mbed_official 354:e67efb2aab0e 439 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 440 * @retval None
mbed_official 354:e67efb2aab0e 441 */
mbed_official 354:e67efb2aab0e 442 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 443 {
mbed_official 354:e67efb2aab0e 444 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 445 the HAL_COMP_MspInit could be implenetd in the user file
mbed_official 354:e67efb2aab0e 446 */
mbed_official 354:e67efb2aab0e 447 }
mbed_official 354:e67efb2aab0e 448
mbed_official 354:e67efb2aab0e 449 /**
mbed_official 354:e67efb2aab0e 450 * @brief DeInitializes COMP MSP.
mbed_official 354:e67efb2aab0e 451 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 452 * @retval None
mbed_official 354:e67efb2aab0e 453 */
mbed_official 354:e67efb2aab0e 454 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 455 {
mbed_official 354:e67efb2aab0e 456 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 457 the HAL_COMP_MspDeInit could be implenetd in the user file
mbed_official 354:e67efb2aab0e 458 */
mbed_official 354:e67efb2aab0e 459 }
mbed_official 354:e67efb2aab0e 460
mbed_official 354:e67efb2aab0e 461 /**
mbed_official 354:e67efb2aab0e 462 * @}
mbed_official 354:e67efb2aab0e 463 */
mbed_official 354:e67efb2aab0e 464
mbed_official 354:e67efb2aab0e 465 /** @defgroup COMP_Exported_Functions_Group2 I/O operation functions
mbed_official 354:e67efb2aab0e 466 * @brief I/O operation functions
mbed_official 354:e67efb2aab0e 467 *
mbed_official 354:e67efb2aab0e 468 @verbatim
mbed_official 354:e67efb2aab0e 469 ===============================================================================
mbed_official 354:e67efb2aab0e 470 ##### IO operation functions #####
mbed_official 354:e67efb2aab0e 471 ===============================================================================
mbed_official 354:e67efb2aab0e 472 [..]
mbed_official 354:e67efb2aab0e 473 This subsection provides a set of functions allowing to manage the COMP
mbed_official 354:e67efb2aab0e 474 start and stop actions with or without interruption on ExtI line.
mbed_official 354:e67efb2aab0e 475
mbed_official 354:e67efb2aab0e 476 @endverbatim
mbed_official 354:e67efb2aab0e 477 * @{
mbed_official 354:e67efb2aab0e 478 */
mbed_official 354:e67efb2aab0e 479
mbed_official 354:e67efb2aab0e 480 /**
mbed_official 354:e67efb2aab0e 481 * @brief Start the comparator
mbed_official 354:e67efb2aab0e 482 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 483 * @retval HAL status
mbed_official 354:e67efb2aab0e 484 */
mbed_official 354:e67efb2aab0e 485 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 486 {
mbed_official 354:e67efb2aab0e 487 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 488 uint32_t wait_loop_cycles = 0;
mbed_official 354:e67efb2aab0e 489 __IO uint32_t wait_loop_index = 0;
mbed_official 354:e67efb2aab0e 490
mbed_official 354:e67efb2aab0e 491 /* Check the COMP handle allocation and lock status */
mbed_official 354:e67efb2aab0e 492 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
mbed_official 354:e67efb2aab0e 493 {
mbed_official 354:e67efb2aab0e 494 status = HAL_ERROR;
mbed_official 354:e67efb2aab0e 495 }
mbed_official 354:e67efb2aab0e 496 else
mbed_official 354:e67efb2aab0e 497 {
mbed_official 354:e67efb2aab0e 498 /* Check the parameter */
mbed_official 354:e67efb2aab0e 499 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
mbed_official 354:e67efb2aab0e 500
mbed_official 354:e67efb2aab0e 501 if(hcomp->State == HAL_COMP_STATE_READY)
mbed_official 354:e67efb2aab0e 502 {
mbed_official 354:e67efb2aab0e 503
mbed_official 354:e67efb2aab0e 504 /* Note: For comparator 2, inverting input (parameter */
mbed_official 354:e67efb2aab0e 505 /* "hcomp->Init.InvertingInput") is configured into this */
mbed_official 354:e67efb2aab0e 506 /* function instead of function "HAL_COMP_Init()" since */
mbed_official 354:e67efb2aab0e 507 /* inverting input selection also enables the comparator 2. */
mbed_official 354:e67efb2aab0e 508 __HAL_COMP_ENABLE(hcomp);
mbed_official 354:e67efb2aab0e 509
mbed_official 354:e67efb2aab0e 510 /* Set delay for COMP startup time */
mbed_official 354:e67efb2aab0e 511 if (hcomp->Instance == COMP1)
mbed_official 354:e67efb2aab0e 512 {
mbed_official 354:e67efb2aab0e 513 wait_loop_cycles = COMP1_START_DELAY_CPU_CYCLES;
mbed_official 354:e67efb2aab0e 514 }
mbed_official 354:e67efb2aab0e 515 else /* if (hcomp->Instance == COMP2) */
mbed_official 354:e67efb2aab0e 516 {
mbed_official 354:e67efb2aab0e 517 wait_loop_cycles = COMP2_START_DELAY_CPU_CYCLES;
mbed_official 354:e67efb2aab0e 518 }
mbed_official 354:e67efb2aab0e 519
mbed_official 354:e67efb2aab0e 520 /* Delay for COMP startup time. */
mbed_official 354:e67efb2aab0e 521 /* Delay fixed to worst case: maximum CPU frequency */
mbed_official 354:e67efb2aab0e 522 while(wait_loop_index < wait_loop_cycles)
mbed_official 354:e67efb2aab0e 523 {
mbed_official 354:e67efb2aab0e 524 wait_loop_index++;
mbed_official 354:e67efb2aab0e 525 }
mbed_official 354:e67efb2aab0e 526
mbed_official 354:e67efb2aab0e 527 /* Update COMP state */
mbed_official 354:e67efb2aab0e 528 hcomp->State = HAL_COMP_STATE_BUSY;
mbed_official 354:e67efb2aab0e 529
mbed_official 354:e67efb2aab0e 530 }
mbed_official 354:e67efb2aab0e 531 else
mbed_official 354:e67efb2aab0e 532 {
mbed_official 354:e67efb2aab0e 533 status = HAL_ERROR;
mbed_official 354:e67efb2aab0e 534 }
mbed_official 354:e67efb2aab0e 535 }
mbed_official 354:e67efb2aab0e 536
mbed_official 354:e67efb2aab0e 537 return status;
mbed_official 354:e67efb2aab0e 538 }
mbed_official 354:e67efb2aab0e 539
mbed_official 354:e67efb2aab0e 540 /**
mbed_official 354:e67efb2aab0e 541 * @brief Stop the comparator
mbed_official 354:e67efb2aab0e 542 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 543 * @retval HAL status
mbed_official 354:e67efb2aab0e 544 */
mbed_official 354:e67efb2aab0e 545 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 546 {
mbed_official 354:e67efb2aab0e 547 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 548
mbed_official 354:e67efb2aab0e 549 /* Check the COMP handle allocation and lock status */
mbed_official 354:e67efb2aab0e 550 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
mbed_official 354:e67efb2aab0e 551 {
mbed_official 354:e67efb2aab0e 552 status = HAL_ERROR;
mbed_official 354:e67efb2aab0e 553 }
mbed_official 354:e67efb2aab0e 554 else
mbed_official 354:e67efb2aab0e 555 {
mbed_official 354:e67efb2aab0e 556 /* Check the parameter */
mbed_official 354:e67efb2aab0e 557 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
mbed_official 354:e67efb2aab0e 558
mbed_official 354:e67efb2aab0e 559 if(hcomp->State == HAL_COMP_STATE_BUSY)
mbed_official 354:e67efb2aab0e 560 {
mbed_official 354:e67efb2aab0e 561 /* Disable the selected comparator */
mbed_official 354:e67efb2aab0e 562 __HAL_COMP_DISABLE(hcomp);
mbed_official 354:e67efb2aab0e 563
mbed_official 354:e67efb2aab0e 564 /* Update COMP state */
mbed_official 354:e67efb2aab0e 565 hcomp->State = HAL_COMP_STATE_READY;
mbed_official 354:e67efb2aab0e 566 }
mbed_official 354:e67efb2aab0e 567 else
mbed_official 354:e67efb2aab0e 568 {
mbed_official 354:e67efb2aab0e 569 status = HAL_ERROR;
mbed_official 354:e67efb2aab0e 570 }
mbed_official 354:e67efb2aab0e 571 }
mbed_official 354:e67efb2aab0e 572
mbed_official 354:e67efb2aab0e 573 return status;
mbed_official 354:e67efb2aab0e 574 }
mbed_official 354:e67efb2aab0e 575
mbed_official 354:e67efb2aab0e 576 /**
mbed_official 354:e67efb2aab0e 577 * @brief Enables the interrupt and starts the comparator
mbed_official 354:e67efb2aab0e 578 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 579 * @retval HAL status.
mbed_official 354:e67efb2aab0e 580 */
mbed_official 354:e67efb2aab0e 581 HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 582 {
mbed_official 354:e67efb2aab0e 583 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 584 uint32_t extiline = 0;
mbed_official 354:e67efb2aab0e 585
mbed_official 354:e67efb2aab0e 586 status = HAL_COMP_Start(hcomp);
mbed_official 354:e67efb2aab0e 587 if(status == HAL_OK)
mbed_official 354:e67efb2aab0e 588 {
mbed_official 354:e67efb2aab0e 589 /* Check the parameter */
mbed_official 354:e67efb2aab0e 590 assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
mbed_official 354:e67efb2aab0e 591
mbed_official 354:e67efb2aab0e 592 /* Get the Exti Line output configuration */
mbed_official 354:e67efb2aab0e 593 extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
mbed_official 354:e67efb2aab0e 594
mbed_official 354:e67efb2aab0e 595 /* Configure the rising edge */
mbed_official 354:e67efb2aab0e 596 /* COMP TriggerMode set to COMP_TRIGGERMODE_IT_RISING or */
mbed_official 354:e67efb2aab0e 597 /* COMP_TRIGGERMODE_IT_RISING_FALLING. */
mbed_official 354:e67efb2aab0e 598 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
mbed_official 354:e67efb2aab0e 599 {
mbed_official 354:e67efb2aab0e 600 __HAL_COMP_EXTI_RISING_IT_ENABLE(extiline);
mbed_official 354:e67efb2aab0e 601 }
mbed_official 354:e67efb2aab0e 602 else
mbed_official 354:e67efb2aab0e 603 {
mbed_official 354:e67efb2aab0e 604 __HAL_COMP_EXTI_RISING_IT_DISABLE(extiline);
mbed_official 354:e67efb2aab0e 605 }
mbed_official 354:e67efb2aab0e 606
mbed_official 354:e67efb2aab0e 607 /* Configure the falling edge */
mbed_official 354:e67efb2aab0e 608 /* COMP TriggerMode set to COMP_TRIGGERMODE_IT_FALLING or */
mbed_official 354:e67efb2aab0e 609 /* COMP_TRIGGERMODE_IT_RISING_FALLING. */
mbed_official 354:e67efb2aab0e 610 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
mbed_official 354:e67efb2aab0e 611 {
mbed_official 354:e67efb2aab0e 612 __HAL_COMP_EXTI_FALLING_IT_ENABLE(extiline);
mbed_official 354:e67efb2aab0e 613 }
mbed_official 354:e67efb2aab0e 614 else
mbed_official 354:e67efb2aab0e 615 {
mbed_official 354:e67efb2aab0e 616 __HAL_COMP_EXTI_FALLING_IT_DISABLE(extiline);
mbed_official 354:e67efb2aab0e 617 }
mbed_official 354:e67efb2aab0e 618
mbed_official 354:e67efb2aab0e 619 /* Enable Exti interrupt mode */
mbed_official 354:e67efb2aab0e 620 __HAL_COMP_EXTI_ENABLE_IT(extiline);
mbed_official 354:e67efb2aab0e 621 /* Clear COMP Exti pending bit */
mbed_official 354:e67efb2aab0e 622 __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
mbed_official 354:e67efb2aab0e 623 }
mbed_official 354:e67efb2aab0e 624
mbed_official 354:e67efb2aab0e 625 return status;
mbed_official 354:e67efb2aab0e 626 }
mbed_official 354:e67efb2aab0e 627
mbed_official 354:e67efb2aab0e 628 /**
mbed_official 354:e67efb2aab0e 629 * @brief Disable the interrupt and Stop the comparator
mbed_official 354:e67efb2aab0e 630 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 631 * @retval HAL status
mbed_official 354:e67efb2aab0e 632 */
mbed_official 354:e67efb2aab0e 633 HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 634 {
mbed_official 354:e67efb2aab0e 635 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 636
mbed_official 354:e67efb2aab0e 637 /* Disable the Exti Line interrupt mode */
mbed_official 354:e67efb2aab0e 638 __HAL_COMP_EXTI_DISABLE_IT(__HAL_COMP_GET_EXTI_LINE(hcomp->Instance));
mbed_official 354:e67efb2aab0e 639
mbed_official 354:e67efb2aab0e 640 status = HAL_COMP_Stop(hcomp);
mbed_official 354:e67efb2aab0e 641
mbed_official 354:e67efb2aab0e 642 return status;
mbed_official 354:e67efb2aab0e 643 }
mbed_official 354:e67efb2aab0e 644
mbed_official 354:e67efb2aab0e 645 /**
mbed_official 354:e67efb2aab0e 646 * @brief Comparator IRQ Handler
mbed_official 354:e67efb2aab0e 647 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 648 * @retval HAL status
mbed_official 354:e67efb2aab0e 649 */
mbed_official 354:e67efb2aab0e 650 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 651 {
mbed_official 354:e67efb2aab0e 652 uint32_t extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
mbed_official 354:e67efb2aab0e 653
mbed_official 354:e67efb2aab0e 654 /* Check COMP Exti flag */
mbed_official 354:e67efb2aab0e 655 if(__HAL_COMP_EXTI_GET_FLAG(extiline) != RESET)
mbed_official 354:e67efb2aab0e 656 {
mbed_official 354:e67efb2aab0e 657 /* Clear COMP Exti pending bit */
mbed_official 354:e67efb2aab0e 658 __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
mbed_official 354:e67efb2aab0e 659
mbed_official 354:e67efb2aab0e 660 /* COMP trigger user callback */
mbed_official 354:e67efb2aab0e 661 HAL_COMP_TriggerCallback(hcomp);
mbed_official 354:e67efb2aab0e 662 }
mbed_official 354:e67efb2aab0e 663 }
mbed_official 354:e67efb2aab0e 664
mbed_official 354:e67efb2aab0e 665 /**
mbed_official 354:e67efb2aab0e 666 * @}
mbed_official 354:e67efb2aab0e 667 */
mbed_official 354:e67efb2aab0e 668
mbed_official 354:e67efb2aab0e 669 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
mbed_official 354:e67efb2aab0e 670 * @brief Peripheral Control functions
mbed_official 354:e67efb2aab0e 671 *
mbed_official 354:e67efb2aab0e 672 @verbatim
mbed_official 354:e67efb2aab0e 673 ===============================================================================
mbed_official 354:e67efb2aab0e 674 ##### Peripheral Control functions #####
mbed_official 354:e67efb2aab0e 675 ===============================================================================
mbed_official 354:e67efb2aab0e 676 [..]
mbed_official 354:e67efb2aab0e 677 This subsection provides a set of functions allowing to control the COMP
mbed_official 354:e67efb2aab0e 678 management functions: Lock status, comparator output level check, IRQ
mbed_official 354:e67efb2aab0e 679 callback (in case of usage of comparator with interruption on ExtI line).
mbed_official 354:e67efb2aab0e 680
mbed_official 354:e67efb2aab0e 681 @endverbatim
mbed_official 354:e67efb2aab0e 682 * @{
mbed_official 354:e67efb2aab0e 683 */
mbed_official 354:e67efb2aab0e 684
mbed_official 354:e67efb2aab0e 685 /**
mbed_official 354:e67efb2aab0e 686 * @brief Lock the selected comparator configuration.
mbed_official 354:e67efb2aab0e 687 * Caution: On STM32L1, HAL COMP lock is software lock only (not
mbed_official 354:e67efb2aab0e 688 * hardware lock as on some other STM32 devices)
mbed_official 354:e67efb2aab0e 689 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 690 * @retval HAL status
mbed_official 354:e67efb2aab0e 691 */
mbed_official 354:e67efb2aab0e 692 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 693 {
mbed_official 354:e67efb2aab0e 694 HAL_StatusTypeDef status = HAL_OK;
mbed_official 354:e67efb2aab0e 695
mbed_official 354:e67efb2aab0e 696 /* Check the COMP handle allocation and lock status */
mbed_official 354:e67efb2aab0e 697 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
mbed_official 354:e67efb2aab0e 698 {
mbed_official 354:e67efb2aab0e 699 status = HAL_ERROR;
mbed_official 354:e67efb2aab0e 700 }
mbed_official 354:e67efb2aab0e 701 else
mbed_official 354:e67efb2aab0e 702 {
mbed_official 354:e67efb2aab0e 703 /* Check the parameter */
mbed_official 354:e67efb2aab0e 704 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
mbed_official 354:e67efb2aab0e 705
mbed_official 354:e67efb2aab0e 706 /* Set lock flag */
mbed_official 354:e67efb2aab0e 707 hcomp->State |= COMP_STATE_BIT_LOCK;
mbed_official 354:e67efb2aab0e 708 }
mbed_official 354:e67efb2aab0e 709
mbed_official 354:e67efb2aab0e 710 return status;
mbed_official 354:e67efb2aab0e 711 }
mbed_official 354:e67efb2aab0e 712
mbed_official 354:e67efb2aab0e 713 /**
mbed_official 354:e67efb2aab0e 714 * @brief Return the output level (high or low) of the selected comparator.
mbed_official 354:e67efb2aab0e 715 * The output level depends on the selected polarity.
mbed_official 354:e67efb2aab0e 716 * - Comparator output is low when the non-inverting input is at a lower
mbed_official 354:e67efb2aab0e 717 * voltage than the inverting input
mbed_official 354:e67efb2aab0e 718 * - Comparator output is high when the non-inverting input is at a higher
mbed_official 354:e67efb2aab0e 719 * voltage than the inverting input
mbed_official 354:e67efb2aab0e 720 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 721 * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
mbed_official 354:e67efb2aab0e 722 *
mbed_official 354:e67efb2aab0e 723 */
mbed_official 354:e67efb2aab0e 724 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 725 {
mbed_official 354:e67efb2aab0e 726 uint32_t level = 0;
mbed_official 354:e67efb2aab0e 727
mbed_official 354:e67efb2aab0e 728 /* Check the parameter */
mbed_official 354:e67efb2aab0e 729 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
mbed_official 354:e67efb2aab0e 730
mbed_official 354:e67efb2aab0e 731 /* Read output level of the selected comparator */
mbed_official 354:e67efb2aab0e 732 if(READ_BIT(COMP->CSR, __COMP_CSR_CMPXOUT(hcomp)) == RESET)
mbed_official 354:e67efb2aab0e 733 {
mbed_official 354:e67efb2aab0e 734 level = COMP_OUTPUTLEVEL_LOW;
mbed_official 354:e67efb2aab0e 735 }
mbed_official 354:e67efb2aab0e 736 else
mbed_official 354:e67efb2aab0e 737 {
mbed_official 354:e67efb2aab0e 738 level = COMP_OUTPUTLEVEL_HIGH;
mbed_official 354:e67efb2aab0e 739 }
mbed_official 354:e67efb2aab0e 740
mbed_official 354:e67efb2aab0e 741 return(level);
mbed_official 354:e67efb2aab0e 742 }
mbed_official 354:e67efb2aab0e 743
mbed_official 354:e67efb2aab0e 744 /**
mbed_official 354:e67efb2aab0e 745 * @brief Comparator callback.
mbed_official 354:e67efb2aab0e 746 * @param hcomp: COMP handle
mbed_official 354:e67efb2aab0e 747 * @retval None
mbed_official 354:e67efb2aab0e 748 */
mbed_official 354:e67efb2aab0e 749 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 750 {
mbed_official 354:e67efb2aab0e 751 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 354:e67efb2aab0e 752 the HAL_COMP_TriggerCallback should be implemented in the user file
mbed_official 354:e67efb2aab0e 753 */
mbed_official 354:e67efb2aab0e 754 }
mbed_official 354:e67efb2aab0e 755
mbed_official 354:e67efb2aab0e 756
mbed_official 354:e67efb2aab0e 757 /**
mbed_official 354:e67efb2aab0e 758 * @}
mbed_official 354:e67efb2aab0e 759 */
mbed_official 354:e67efb2aab0e 760
mbed_official 354:e67efb2aab0e 761 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
mbed_official 354:e67efb2aab0e 762 * @brief Peripheral State functions
mbed_official 354:e67efb2aab0e 763 *
mbed_official 354:e67efb2aab0e 764 @verbatim
mbed_official 354:e67efb2aab0e 765 ===============================================================================
mbed_official 354:e67efb2aab0e 766 ##### Peripheral State functions #####
mbed_official 354:e67efb2aab0e 767 ===============================================================================
mbed_official 354:e67efb2aab0e 768 [..]
mbed_official 354:e67efb2aab0e 769 This subsection permit to get in run-time the status of the peripheral.
mbed_official 354:e67efb2aab0e 770
mbed_official 354:e67efb2aab0e 771 @endverbatim
mbed_official 354:e67efb2aab0e 772 * @{
mbed_official 354:e67efb2aab0e 773 */
mbed_official 354:e67efb2aab0e 774
mbed_official 354:e67efb2aab0e 775 /**
mbed_official 354:e67efb2aab0e 776 * @brief Return the COMP state
mbed_official 354:e67efb2aab0e 777 * @param hcomp : COMP handle
mbed_official 354:e67efb2aab0e 778 * @retval HAL state
mbed_official 354:e67efb2aab0e 779 */
mbed_official 354:e67efb2aab0e 780 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
mbed_official 354:e67efb2aab0e 781 {
mbed_official 354:e67efb2aab0e 782 /* Check the COMP handle allocation */
mbed_official 354:e67efb2aab0e 783 if(hcomp == HAL_NULL)
mbed_official 354:e67efb2aab0e 784 {
mbed_official 354:e67efb2aab0e 785 return HAL_COMP_STATE_RESET;
mbed_official 354:e67efb2aab0e 786 }
mbed_official 354:e67efb2aab0e 787
mbed_official 354:e67efb2aab0e 788 /* Check the parameter */
mbed_official 354:e67efb2aab0e 789 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
mbed_official 354:e67efb2aab0e 790
mbed_official 354:e67efb2aab0e 791 return hcomp->State;
mbed_official 354:e67efb2aab0e 792 }
mbed_official 354:e67efb2aab0e 793 /**
mbed_official 354:e67efb2aab0e 794 * @}
mbed_official 354:e67efb2aab0e 795 */
mbed_official 354:e67efb2aab0e 796
mbed_official 354:e67efb2aab0e 797 /**
mbed_official 354:e67efb2aab0e 798 * @}
mbed_official 354:e67efb2aab0e 799 */
mbed_official 354:e67efb2aab0e 800
mbed_official 354:e67efb2aab0e 801 #endif /* HAL_COMP_MODULE_ENABLED */
mbed_official 354:e67efb2aab0e 802 /**
mbed_official 354:e67efb2aab0e 803 * @}
mbed_official 354:e67efb2aab0e 804 */
mbed_official 354:e67efb2aab0e 805
mbed_official 354:e67efb2aab0e 806 /**
mbed_official 354:e67efb2aab0e 807 * @}
mbed_official 354:e67efb2aab0e 808 */
mbed_official 354:e67efb2aab0e 809
mbed_official 354:e67efb2aab0e 810 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/