Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
stm32l4xx_hal_comp.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_hal_comp.c 00004 * @author MCD Application Team 00005 * @version V1.5.1 00006 * @date 31-May-2016 00007 * @brief COMP HAL module driver. 00008 * This file provides firmware functions to manage the following 00009 * functionalities of the COMP peripheral: 00010 * + Initialization and de-initialization functions 00011 * + Start/Stop operation functions in polling mode 00012 * + Start/Stop operation functions in interrupt mode (through EXTI interrupt) 00013 * + Peripheral control functions 00014 * + Peripheral state functions 00015 * 00016 @verbatim 00017 ================================================================================ 00018 ##### COMP Peripheral features ##### 00019 ================================================================================ 00020 00021 [..] 00022 The STM32L4xx device family integrates two analog comparators instances 00023 COMP1 and COMP2: 00024 (#) The COMP input minus (inverting input) and input plus (non inverting input) 00025 can be set to internal references or to GPIO pins 00026 (refer to GPIO list in reference manual). 00027 00028 (#) The COMP output level is available using HAL_COMP_GetOutputLevel() 00029 and can be redirected to other peripherals: GPIO pins (in mode 00030 alternate functions for comparator), timers. 00031 (refer to GPIO list in reference manual). 00032 00033 (#) Pairs of comparators instances can be combined in window mode 00034 (2 consecutive instances odd and even COMP<x> and COMP<x+1>). 00035 00036 (#) The comparators have interrupt capability through the EXTI controller 00037 with wake-up from sleep and stop modes: 00038 (++) COMP1 is internally connected to EXTI Line 21 00039 (++) COMP2 is internally connected to EXTI Line 22 00040 00041 From the corresponding IRQ handler, the right interrupt source can be retrieved 00042 using macro __HAL_COMP_COMP1_EXTI_GET_FLAG() and __HAL_COMP_COMP2_EXTI_GET_FLAG(). 00043 00044 ##### How to use this driver ##### 00045 ================================================================================ 00046 [..] 00047 This driver provides functions to configure and program the comparator instances 00048 of STM32L4xx devices. 00049 00050 To use the comparator, perform the following steps: 00051 00052 (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit(): 00053 (++) Configure the GPIO connected to comparator inputs plus and minus in analog mode 00054 using HAL_GPIO_Init(). 00055 (++) If needed, configure the GPIO connected to comparator output in alternate function mode 00056 using HAL_GPIO_Init(). 00057 (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and 00058 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator 00059 interrupt vector using HAL_NVIC_EnableIRQ() function. 00060 00061 (#) Configure the comparator using HAL_COMP_Init() function: 00062 (++) Select the input minus (inverting input) 00063 (++) Select the input plus (non-inverting input) 00064 (++) Select the hysteresis 00065 (++) Select the blanking source 00066 (++) Select the output polarity 00067 (++) Select the power mode 00068 (++) Select the window mode 00069 00070 -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() 00071 to enable internal control clock of the comparators. 00072 However, this is a legacy strategy. In future STM32 families, 00073 COMP clock enable must be implemented by user in "HAL_COMP_MspInit()". 00074 Therefore, for compatibility anticipation, it is recommended to 00075 implement __HAL_RCC_SYSCFG_CLK_ENABLE() in "HAL_COMP_MspInit()". 00076 00077 (#) Reconfiguration on-the-fly of comparator can be done by calling again 00078 function HAL_COMP_Init() with new input structure parameters values. 00079 00080 (#) Enable the comparator using HAL_COMP_Start() function. 00081 00082 (#) Use HAL_COMP_TriggerCallback() or HAL_COMP_GetOutputLevel() functions 00083 to manage comparator outputs (events and output level). 00084 00085 (#) Disable the comparator using HAL_COMP_Stop() function. 00086 00087 (#) De-initialize the comparator using HAL_COMP_DeInit() function. 00088 00089 (#) For safety purpose, comparator configuration can be locked using HAL_COMP_Lock() function. 00090 The only way to unlock the comparator is a device hardware reset. 00091 00092 @endverbatim 00093 ****************************************************************************** 00094 00095 Table 1. COMP inputs and output for STM32L4xx devices 00096 +---------------------------------------------------------+ 00097 | | | COMP1 | COMP2 | 00098 |----------------|----------------|-----------|-----------| 00099 | | IO1 | PC5 | PB4 | 00100 | Input plus | IO2 | PB2 | PB6 | 00101 | | IO3 (3) | PA1 | PA3 | 00102 |----------------|----------------|-----------------------| 00103 | | 1/4 VrefInt | Available | Available | 00104 | | 1/2 VrefInt | Available | Available | 00105 | | 3/4 VrefInt | Available | Available | 00106 | Input minus | VrefInt | Available | Available | 00107 | | DAC1 channel 1 | Available | Available | 00108 | | DAC1 channel 2 | Available | Available | 00109 | | IO1 | PB1 | PB3 | 00110 | | IO2 | PC4 | PB7 | 00111 | | IO3 (3) | PA0 | PA2 | 00112 | | IO4 (3) | PA4 | PA4 | 00113 | | IO5 (3) | PA5 | PA5 | 00114 +---------------------------------------------------------+ 00115 | Output | | PB0 (1) | PB5 (1) | 00116 | | | PB10 (1) | PB11 (1) | 00117 | | | TIM (2) | TIM (2) | 00118 +---------------------------------------------------------+ 00119 (1) GPIO must be set to alternate function for comparator 00120 (2) Comparators output to timers is set in timers instances. 00121 (3) Only STM32L43x/L44x 00122 00123 ****************************************************************************** 00124 * @attention 00125 * 00126 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00127 * 00128 * Redistribution and use in source and binary forms, with or without modification, 00129 * are permitted provided that the following conditions are met: 00130 * 1. Redistributions of source code must retain the above copyright notice, 00131 * this list of conditions and the following disclaimer. 00132 * 2. Redistributions in binary form must reproduce the above copyright notice, 00133 * this list of conditions and the following disclaimer in the documentation 00134 * and/or other materials provided with the distribution. 00135 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00136 * may be used to endorse or promote products derived from this software 00137 * without specific prior written permission. 00138 * 00139 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00140 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00141 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00142 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00143 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00144 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00145 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00146 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00147 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00148 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00149 * 00150 ****************************************************************************** 00151 */ 00152 00153 /* Includes ------------------------------------------------------------------*/ 00154 #include "stm32l4xx_hal.h" 00155 00156 /** @addtogroup STM32L4xx_HAL_Driver 00157 * @{ 00158 */ 00159 00160 /** @defgroup COMP COMP 00161 * @brief COMP HAL module driver 00162 * @{ 00163 */ 00164 00165 #ifdef HAL_COMP_MODULE_ENABLED 00166 00167 /* Private typedef -----------------------------------------------------------*/ 00168 /* Private define ------------------------------------------------------------*/ 00169 /** @addtogroup COMP_Private_Constants 00170 * @{ 00171 */ 00172 00173 /* Delay for COMP startup time. */ 00174 /* Note: Delay required to reach propagation delay specification. */ 00175 /* Literal set to maximum value (refer to device datasheet, */ 00176 /* parameter "tSTART"). */ 00177 /* Unit: us */ 00178 #define COMP_DELAY_STARTUP_US ((uint32_t) 80U) /*!< Delay for COMP startup time */ 00179 00180 /* Delay for COMP voltage scaler stabilization time. */ 00181 /* Literal set to maximum value (refer to device datasheet, */ 00182 /* parameter "tSTART_SCALER"). */ 00183 /* Unit: us */ 00184 #define COMP_DELAY_VOLTAGE_SCALER_STAB_US ((uint32_t) 200U) /*!< Delay for COMP voltage scaler stabilization time */ 00185 00186 #define COMP_OUTPUT_LEVEL_BITOFFSET_POS ((uint32_t) 30U) 00187 00188 /** 00189 * @} 00190 */ 00191 00192 /* Private macro -------------------------------------------------------------*/ 00193 /* Private variables ---------------------------------------------------------*/ 00194 /* Private function prototypes -----------------------------------------------*/ 00195 /* Exported functions --------------------------------------------------------*/ 00196 00197 /** @defgroup COMP_Exported_Functions COMP Exported Functions 00198 * @{ 00199 */ 00200 00201 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions 00202 * @brief Initialization and de-initialization functions. 00203 * 00204 @verbatim 00205 =============================================================================== 00206 ##### Initialization and de-initialization functions ##### 00207 =============================================================================== 00208 [..] This section provides functions to initialize and de-initialize comparators 00209 00210 @endverbatim 00211 * @{ 00212 */ 00213 00214 /** 00215 * @brief Initialize the COMP according to the specified 00216 * parameters in the COMP_InitTypeDef and initialize the associated handle. 00217 * @note If the selected comparator is locked, initialization can't be performed. 00218 * To unlock the configuration, perform a system reset. 00219 * @param hcomp COMP handle 00220 * @retval HAL status 00221 */ 00222 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp) 00223 { 00224 uint32_t tmp_csr = 0; 00225 uint32_t exti_line = 0; 00226 uint32_t comp_voltage_scaler_not_initialized = 0; 00227 __IO uint32_t wait_loop_index = 0; 00228 HAL_StatusTypeDef status = HAL_OK; 00229 00230 /* Check the COMP handle allocation and lock status */ 00231 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00232 { 00233 status = HAL_ERROR; 00234 } 00235 else 00236 { 00237 /* Check the parameters */ 00238 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00239 assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput)); 00240 assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput)); 00241 assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol)); 00242 assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode)); 00243 assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis)); 00244 assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce)); 00245 assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode)); 00246 assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode)); 00247 00248 if(hcomp->State == HAL_COMP_STATE_RESET) 00249 { 00250 /* Allocate lock resource and initialize it */ 00251 hcomp->Lock = HAL_UNLOCKED; 00252 00253 /* Init SYSCFG and the low level hardware to access comparators */ 00254 /* Note: HAL_COMP_Init() calls __HAL_RCC_SYSCFG_CLK_ENABLE() */ 00255 /* to enable internal control clock of the comparators. */ 00256 /* However, this is a legacy strategy. In future STM32 families, */ 00257 /* COMP clock enable must be implemented by user */ 00258 /* in "HAL_COMP_MspInit()". */ 00259 /* Therefore, for compatibility anticipation, it is recommended */ 00260 /* to implement __HAL_RCC_SYSCFG_CLK_ENABLE() */ 00261 /* in "HAL_COMP_MspInit()". */ 00262 __HAL_RCC_SYSCFG_CLK_ENABLE(); 00263 00264 /* Init the low level hardware */ 00265 HAL_COMP_MspInit(hcomp); 00266 } 00267 00268 /* Memorize voltage scaler state before initialization */ 00269 comp_voltage_scaler_not_initialized = (READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) == 0); 00270 00271 /* Set COMP parameters */ 00272 tmp_csr = (hcomp->Init.InvertingInput | 00273 hcomp->Init.NonInvertingInput | 00274 hcomp->Init.BlankingSrce | 00275 hcomp->Init.Hysteresis | 00276 hcomp->Init.OutputPol | 00277 hcomp->Init.Mode ); 00278 00279 /* Set parameters in COMP register */ 00280 /* Note: Update all bits except read-only, lock and enable bits */ 00281 #if defined (COMP_CSR_INMESEL) 00282 MODIFY_REG(hcomp->Instance->CSR, 00283 COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL | 00284 COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST | 00285 COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL, 00286 tmp_csr 00287 ); 00288 #else 00289 MODIFY_REG(hcomp->Instance->CSR, 00290 COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL | 00291 COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST | 00292 COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN, 00293 tmp_csr 00294 ); 00295 #endif 00296 00297 /* Set window mode */ 00298 /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */ 00299 /* instances. Therefore, this function can update another COMP */ 00300 /* instance that the one currently selected. */ 00301 if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON) 00302 { 00303 SET_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE); 00304 } 00305 else 00306 { 00307 CLEAR_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE); 00308 } 00309 00310 /* Delay for COMP scaler bridge voltage stabilization */ 00311 /* Apply the delay if voltage scaler bridge is enabled for the first time */ 00312 if ((READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) != 0) && 00313 (comp_voltage_scaler_not_initialized != 0) ) 00314 { 00315 /* Wait loop initialization and execution */ 00316 /* Note: Variable divided by 2 to compensate partially */ 00317 /* CPU processing cycles. */ 00318 wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000 * 2))); 00319 while(wait_loop_index != 0) 00320 { 00321 wait_loop_index--; 00322 } 00323 } 00324 00325 /* Get the EXTI line corresponding to the selected COMP instance */ 00326 exti_line = COMP_GET_EXTI_LINE(hcomp->Instance); 00327 00328 /* Manage EXTI settings */ 00329 if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != RESET) 00330 { 00331 /* Configure EXTI rising edge */ 00332 if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != RESET) 00333 { 00334 SET_BIT(EXTI->RTSR1, exti_line); 00335 } 00336 else 00337 { 00338 CLEAR_BIT(EXTI->RTSR1, exti_line); 00339 } 00340 00341 /* Configure EXTI falling edge */ 00342 if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != RESET) 00343 { 00344 SET_BIT(EXTI->FTSR1, exti_line); 00345 } 00346 else 00347 { 00348 CLEAR_BIT(EXTI->FTSR1, exti_line); 00349 } 00350 00351 /* Clear COMP EXTI pending bit (if any) */ 00352 WRITE_REG(EXTI->PR1, exti_line); 00353 00354 /* Configure EXTI event mode */ 00355 if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != RESET) 00356 { 00357 SET_BIT(EXTI->EMR1, exti_line); 00358 } 00359 else 00360 { 00361 CLEAR_BIT(EXTI->EMR1, exti_line); 00362 } 00363 00364 /* Configure EXTI interrupt mode */ 00365 if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != RESET) 00366 { 00367 SET_BIT(EXTI->IMR1, exti_line); 00368 } 00369 else 00370 { 00371 CLEAR_BIT(EXTI->IMR1, exti_line); 00372 } 00373 } 00374 else 00375 { 00376 /* Disable EXTI event mode */ 00377 CLEAR_BIT(EXTI->EMR1, exti_line); 00378 00379 /* Disable EXTI interrupt mode */ 00380 CLEAR_BIT(EXTI->IMR1, exti_line); 00381 } 00382 00383 /* Set HAL COMP handle state */ 00384 /* Note: Transition from state reset to state ready, */ 00385 /* otherwise (coming from state ready or busy) no state update. */ 00386 if (hcomp->State == HAL_COMP_STATE_RESET) 00387 { 00388 hcomp->State = HAL_COMP_STATE_READY; 00389 } 00390 } 00391 00392 return status; 00393 } 00394 00395 /** 00396 * @brief DeInitialize the COMP peripheral. 00397 * @note Deinitialization cannot be performed if the COMP configuration is locked. 00398 * To unlock the configuration, perform a system reset. 00399 * @param hcomp COMP handle 00400 * @retval HAL status 00401 */ 00402 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp) 00403 { 00404 HAL_StatusTypeDef status = HAL_OK; 00405 00406 /* Check the COMP handle allocation and lock status */ 00407 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00408 { 00409 status = HAL_ERROR; 00410 } 00411 else 00412 { 00413 /* Check the parameter */ 00414 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00415 00416 /* Set COMP_CSR register to reset value */ 00417 WRITE_REG(hcomp->Instance->CSR, 0x00000000); 00418 00419 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */ 00420 HAL_COMP_MspDeInit(hcomp); 00421 00422 /* Set HAL COMP handle state */ 00423 hcomp->State = HAL_COMP_STATE_RESET; 00424 00425 /* Release Lock */ 00426 __HAL_UNLOCK(hcomp); 00427 } 00428 00429 return status; 00430 } 00431 00432 /** 00433 * @brief Initialize the COMP MSP. 00434 * @param hcomp COMP handle 00435 * @retval None 00436 */ 00437 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp) 00438 { 00439 /* Prevent unused argument(s) compilation warning */ 00440 UNUSED(hcomp); 00441 00442 /* NOTE : This function should not be modified, when the callback is needed, 00443 the HAL_COMP_MspInit could be implemented in the user file 00444 */ 00445 } 00446 00447 /** 00448 * @brief DeInitialize the COMP MSP. 00449 * @param hcomp COMP handle 00450 * @retval None 00451 */ 00452 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp) 00453 { 00454 /* Prevent unused argument(s) compilation warning */ 00455 UNUSED(hcomp); 00456 00457 /* NOTE : This function should not be modified, when the callback is needed, 00458 the HAL_COMP_MspDeInit could be implemented in the user file 00459 */ 00460 } 00461 00462 /** 00463 * @} 00464 */ 00465 00466 /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions 00467 * @brief Start-Stop operation functions. 00468 * 00469 @verbatim 00470 =============================================================================== 00471 ##### IO operation functions ##### 00472 =============================================================================== 00473 [..] This section provides functions allowing to: 00474 (+) Start a comparator instance. 00475 (+) Stop a comparator instance. 00476 00477 @endverbatim 00478 * @{ 00479 */ 00480 00481 /** 00482 * @brief Start the comparator. 00483 * @param hcomp COMP handle 00484 * @retval HAL status 00485 */ 00486 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp) 00487 { 00488 __IO uint32_t wait_loop_index = 0; 00489 HAL_StatusTypeDef status = HAL_OK; 00490 00491 /* Check the COMP handle allocation and lock status */ 00492 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00493 { 00494 status = HAL_ERROR; 00495 } 00496 else 00497 { 00498 /* Check the parameter */ 00499 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00500 00501 if(hcomp->State == HAL_COMP_STATE_READY) 00502 { 00503 /* Enable the selected comparator */ 00504 SET_BIT(hcomp->Instance->CSR, COMP_CSR_EN); 00505 00506 /* Set HAL COMP handle state */ 00507 hcomp->State = HAL_COMP_STATE_BUSY; 00508 00509 /* Delay for COMP startup time */ 00510 /* Wait loop initialization and execution */ 00511 /* Note: Variable divided by 2 to compensate partially */ 00512 /* CPU processing cycles. */ 00513 wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / (1000000 * 2))); 00514 while(wait_loop_index != 0) 00515 { 00516 wait_loop_index--; 00517 } 00518 } 00519 else 00520 { 00521 status = HAL_ERROR; 00522 } 00523 } 00524 00525 return status; 00526 } 00527 00528 /** 00529 * @brief Stop the comparator. 00530 * @param hcomp COMP handle 00531 * @retval HAL status 00532 */ 00533 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp) 00534 { 00535 HAL_StatusTypeDef status = HAL_OK; 00536 00537 /* Check the COMP handle allocation and lock status */ 00538 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00539 { 00540 status = HAL_ERROR; 00541 } 00542 else 00543 { 00544 /* Check the parameter */ 00545 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00546 00547 if((hcomp->State == HAL_COMP_STATE_BUSY) || 00548 (hcomp->State == HAL_COMP_STATE_READY) ) 00549 { 00550 /* Disable the selected comparator */ 00551 CLEAR_BIT(hcomp->Instance->CSR, COMP_CSR_EN); 00552 00553 /* Set HAL COMP handle state */ 00554 hcomp->State = HAL_COMP_STATE_READY; 00555 } 00556 else 00557 { 00558 status = HAL_ERROR; 00559 } 00560 } 00561 00562 return status; 00563 } 00564 00565 /** 00566 * @brief Comparator IRQ handler. 00567 * @param hcomp COMP handle 00568 * @retval None 00569 */ 00570 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp) 00571 { 00572 /* Get the EXTI line corresponding to the selected COMP instance */ 00573 uint32_t exti_line = COMP_GET_EXTI_LINE(hcomp->Instance); 00574 00575 /* Check COMP EXTI flag */ 00576 if(READ_BIT(EXTI->PR1, exti_line) != RESET) 00577 { 00578 /* Check whether comparator is in independent or window mode */ 00579 if(READ_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE) != RESET) 00580 { 00581 /* Clear COMP EXTI line pending bit of the pair of comparators */ 00582 /* in window mode. */ 00583 /* Note: Pair of comparators in window mode can both trig IRQ when */ 00584 /* input voltage is changing from "out of window" area */ 00585 /* (low or high ) to the other "out of window" area (high or low).*/ 00586 /* Both flags must be cleared to call comparator trigger */ 00587 /* callback is called once. */ 00588 WRITE_REG(EXTI->PR1, (COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2)); 00589 } 00590 else 00591 { 00592 /* Clear COMP EXTI line pending bit */ 00593 WRITE_REG(EXTI->PR1, exti_line); 00594 } 00595 00596 /* COMP trigger user callback */ 00597 HAL_COMP_TriggerCallback(hcomp); 00598 } 00599 } 00600 00601 /** 00602 * @} 00603 */ 00604 00605 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions 00606 * @brief Management functions. 00607 * 00608 @verbatim 00609 =============================================================================== 00610 ##### Peripheral Control functions ##### 00611 =============================================================================== 00612 [..] 00613 This subsection provides a set of functions allowing to control the comparators. 00614 00615 @endverbatim 00616 * @{ 00617 */ 00618 00619 /** 00620 * @brief Lock the selected comparator configuration. 00621 * @note A system reset is required to unlock the comparator configuration. 00622 * @note Locking the comparator from reset state is possible 00623 * if __HAL_RCC_SYSCFG_CLK_ENABLE() is being called before. 00624 * @param hcomp COMP handle 00625 * @retval HAL status 00626 */ 00627 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp) 00628 { 00629 HAL_StatusTypeDef status = HAL_OK; 00630 00631 /* Check the COMP handle allocation and lock status */ 00632 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00633 { 00634 status = HAL_ERROR; 00635 } 00636 else 00637 { 00638 /* Check the parameter */ 00639 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00640 00641 /* Set HAL COMP handle state */ 00642 hcomp->State = ((HAL_COMP_StateTypeDef)(hcomp->State | COMP_STATE_BITFIELD_LOCK)); 00643 } 00644 00645 if(status == HAL_OK) 00646 { 00647 /* Set the lock bit corresponding to selected comparator */ 00648 __HAL_COMP_LOCK(hcomp); 00649 } 00650 00651 return status; 00652 } 00653 00654 /** 00655 * @brief Return the output level (high or low) of the selected comparator. 00656 * The output level depends on the selected polarity. 00657 * If the polarity is not inverted: 00658 * - Comparator output is low when the input plus is at a lower 00659 * voltage than the input minus 00660 * - Comparator output is high when the input plus is at a higher 00661 * voltage than the input minus 00662 * If the polarity is inverted: 00663 * - Comparator output is high when the input plus is at a lower 00664 * voltage than the input minus 00665 * - Comparator output is low when the input plus is at a higher 00666 * voltage than the input minus 00667 * @param hcomp COMP handle 00668 * @retval Returns the selected comparator output level: 00669 * @arg @ref COMP_OUTPUT_LEVEL_LOW 00670 * @arg @ref COMP_OUTPUT_LEVEL_HIGH 00671 * 00672 */ 00673 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp) 00674 { 00675 /* Check the parameter */ 00676 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00677 00678 return (uint32_t)(READ_BIT(hcomp->Instance->CSR, COMP_CSR_VALUE) 00679 >> COMP_OUTPUT_LEVEL_BITOFFSET_POS); 00680 } 00681 00682 /** 00683 * @brief Comparator callback. 00684 * @param hcomp COMP handle 00685 * @retval None 00686 */ 00687 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) 00688 { 00689 /* Prevent unused argument(s) compilation warning */ 00690 UNUSED(hcomp); 00691 00692 /* NOTE : This function should not be modified, when the callback is needed, 00693 the HAL_COMP_TriggerCallback should be implemented in the user file 00694 */ 00695 } 00696 00697 00698 /** 00699 * @} 00700 */ 00701 00702 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions 00703 * @brief Peripheral State functions. 00704 * 00705 @verbatim 00706 =============================================================================== 00707 ##### Peripheral State functions ##### 00708 =============================================================================== 00709 [..] 00710 This subsection permit to get in run-time the status of the peripheral. 00711 00712 @endverbatim 00713 * @{ 00714 */ 00715 00716 /** 00717 * @brief Return the COMP handle state. 00718 * @param hcomp COMP handle 00719 * @retval HAL state 00720 */ 00721 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp) 00722 { 00723 /* Check the COMP handle allocation */ 00724 if(hcomp == NULL) 00725 { 00726 return HAL_COMP_STATE_RESET; 00727 } 00728 00729 /* Check the parameter */ 00730 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00731 00732 /* Return HAL COMP handle state */ 00733 return hcomp->State; 00734 } 00735 00736 /** 00737 * @} 00738 */ 00739 00740 /** 00741 * @} 00742 */ 00743 00744 #endif /* HAL_COMP_MODULE_ENABLED */ 00745 /** 00746 * @} 00747 */ 00748 00749 /** 00750 * @} 00751 */ 00752 00753 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 10:59:57 by
