STM32L486xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_hal_comp.c 00004 * @author MCD Application Team 00005 * @brief COMP HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the COMP peripheral: 00008 * + Initialization and de-initialization functions 00009 * + Start/Stop operation functions in polling mode 00010 * + Start/Stop operation functions in interrupt mode (through EXTI interrupt) 00011 * + Peripheral control functions 00012 * + Peripheral state functions 00013 * 00014 @verbatim 00015 ================================================================================ 00016 ##### COMP Peripheral features ##### 00017 ================================================================================ 00018 00019 [..] 00020 The STM32L4xx device family integrates two analog comparators instances: 00021 COMP1, COMP2. 00022 (#) Comparators input minus (inverting input) and input plus (non inverting input) 00023 can be set to internal references or to GPIO pins 00024 (refer to GPIO list in reference manual). 00025 00026 (#) Comparators output level is available using HAL_COMP_GetOutputLevel() 00027 and can be redirected to other peripherals: GPIO pins (in mode 00028 alternate functions for comparator), timers. 00029 (refer to GPIO list in reference manual). 00030 00031 (#) The comparators have interrupt capability through the EXTI controller 00032 with wake-up from sleep and stop modes. 00033 00034 (#) Pairs of comparators instances can be combined in window mode 00035 (2 consecutive instances odd and even COMP<x> and COMP<x+1>). 00036 00037 From the corresponding IRQ handler, the right interrupt source can be retrieved 00038 using macro __HAL_COMP_COMPx_EXTI_GET_FLAG(). 00039 00040 ##### How to use this driver ##### 00041 ================================================================================ 00042 [..] 00043 This driver provides functions to configure and program the comparator instances 00044 of STM32L4xx devices. 00045 00046 To use the comparator, perform the following steps: 00047 00048 (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit(): 00049 (++) Configure the GPIO connected to comparator inputs plus and minus in analog mode 00050 using HAL_GPIO_Init(). 00051 (++) If needed, configure the GPIO connected to comparator output in alternate function mode 00052 using HAL_GPIO_Init(). 00053 (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and 00054 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator 00055 interrupt vector using HAL_NVIC_EnableIRQ() function. 00056 00057 (#) Configure the comparator using HAL_COMP_Init() function: 00058 (++) Select the input minus (inverting input) 00059 (++) Select the input plus (non-inverting input) 00060 (++) Select the hysteresis 00061 (++) Select the blanking source 00062 (++) Select the output polarity 00063 (++) Select the power mode 00064 (++) Select the window mode 00065 00066 -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() 00067 to enable internal control clock of the comparators. 00068 However, this is a legacy strategy. In future STM32 families, 00069 COMP clock enable must be implemented by user in "HAL_COMP_MspInit()". 00070 Therefore, for compatibility anticipation, it is recommended to 00071 implement __HAL_RCC_SYSCFG_CLK_ENABLE() in "HAL_COMP_MspInit()". 00072 00073 (#) Reconfiguration on-the-fly of comparator can be done by calling again 00074 function HAL_COMP_Init() with new input structure parameters values. 00075 00076 (#) Enable the comparator using HAL_COMP_Start() function. 00077 00078 (#) Use HAL_COMP_TriggerCallback() or HAL_COMP_GetOutputLevel() functions 00079 to manage comparator outputs (events and output level). 00080 00081 (#) Disable the comparator using HAL_COMP_Stop() function. 00082 00083 (#) De-initialize the comparator using HAL_COMP_DeInit() function. 00084 00085 (#) For safety purpose, comparator configuration can be locked using HAL_COMP_Lock() function. 00086 The only way to unlock the comparator is a device hardware reset. 00087 00088 *** Callback registration *** 00089 ============================================= 00090 [..] 00091 00092 The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1, 00093 allows the user to configure dynamically the driver callbacks. 00094 Use Functions @ref HAL_COMP_RegisterCallback() 00095 to register an interrupt callback. 00096 [..] 00097 00098 Function @ref HAL_COMP_RegisterCallback() allows to register following callbacks: 00099 (+) TriggerCallback : callback for COMP trigger. 00100 (+) MspInitCallback : callback for Msp Init. 00101 (+) MspDeInitCallback : callback for Msp DeInit. 00102 This function takes as parameters the HAL peripheral handle, the Callback ID 00103 and a pointer to the user callback function. 00104 [..] 00105 00106 Use function @ref HAL_COMP_UnRegisterCallback to reset a callback to the default 00107 weak function. 00108 [..] 00109 00110 @ref HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle, 00111 and the Callback ID. 00112 This function allows to reset following callbacks: 00113 (+) TriggerCallback : callback for COMP trigger. 00114 (+) MspInitCallback : callback for Msp Init. 00115 (+) MspDeInitCallback : callback for Msp DeInit. 00116 [..] 00117 00118 By default, after the @ref HAL_COMP_Init() and when the state is @ref HAL_COMP_STATE_RESET 00119 all callbacks are set to the corresponding weak functions: 00120 example @ref HAL_COMP_TriggerCallback(). 00121 Exception done for MspInit and MspDeInit functions that are 00122 reset to the legacy weak functions in the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit() only when 00123 these callbacks are null (not registered beforehand). 00124 [..] 00125 00126 If MspInit or MspDeInit are not null, the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit() 00127 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state. 00128 [..] 00129 00130 Callbacks can be registered/unregistered in @ref HAL_COMP_STATE_READY state only. 00131 Exception done MspInit/MspDeInit functions that can be registered/unregistered 00132 in @ref HAL_COMP_STATE_READY or @ref HAL_COMP_STATE_RESET state, 00133 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit. 00134 [..] 00135 00136 Then, the user first registers the MspInit/MspDeInit user callbacks 00137 using @ref HAL_COMP_RegisterCallback() before calling @ref HAL_COMP_DeInit() 00138 or @ref HAL_COMP_Init() function. 00139 [..] 00140 00141 When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or 00142 not defined, the callback registration feature is not available and all callbacks 00143 are set to the corresponding weak functions. 00144 00145 @endverbatim 00146 ****************************************************************************** 00147 00148 Table 1. COMP inputs and output for STM32L4xx devices 00149 +---------------------------------------------------------+ 00150 | | | COMP1 | COMP2 | 00151 |----------------|----------------|-----------|-----------| 00152 | | IO1 | PC5 | PB4 | 00153 | Input plus | IO2 | PB2 | PB6 | 00154 | | IO3 (3) | PA1 | PA3 | 00155 |----------------|----------------|-----------------------| 00156 | | 1/4 VrefInt | Available | Available | 00157 | | 1/2 VrefInt | Available | Available | 00158 | | 3/4 VrefInt | Available | Available | 00159 | Input minus | VrefInt | Available | Available | 00160 | | DAC1 channel 1 | Available | Available | 00161 | | DAC1 channel 2 | Available | Available | 00162 | | IO1 | PB1 | PB3 | 00163 | | IO2 | PC4 | PB7 | 00164 | | IO3 (3) | PA0 | PA2 | 00165 | | IO4 (3) | PA4 | PA4 | 00166 | | IO5 (3) | PA5 | PA5 | 00167 +---------------------------------------------------------+ 00168 | Output | | PB0 (1) | PB5 (1) | 00169 | | | PB10 (1) | PB11 (1) | 00170 | | | TIM (2) | TIM (2) | 00171 +---------------------------------------------------------+ 00172 (1) GPIO must be set to alternate function for comparator 00173 (2) Comparators output to timers is set in timers instances. 00174 (3) Only STM32L43x/L44x 00175 00176 ****************************************************************************** 00177 * @attention 00178 * 00179 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00180 * 00181 * Redistribution and use in source and binary forms, with or without modification, 00182 * are permitted provided that the following conditions are met: 00183 * 1. Redistributions of source code must retain the above copyright notice, 00184 * this list of conditions and the following disclaimer. 00185 * 2. Redistributions in binary form must reproduce the above copyright notice, 00186 * this list of conditions and the following disclaimer in the documentation 00187 * and/or other materials provided with the distribution. 00188 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00189 * may be used to endorse or promote products derived from this software 00190 * without specific prior written permission. 00191 * 00192 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00193 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00194 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00195 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00196 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00197 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00198 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00199 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00200 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00201 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00202 * 00203 ****************************************************************************** 00204 */ 00205 00206 /* Includes ------------------------------------------------------------------*/ 00207 #include "stm32l4xx_hal.h" 00208 00209 /** @addtogroup STM32L4xx_HAL_Driver 00210 * @{ 00211 */ 00212 00213 #ifdef HAL_COMP_MODULE_ENABLED 00214 00215 #if defined (COMP1) || defined (COMP2) 00216 00217 /** @defgroup COMP COMP 00218 * @brief COMP HAL module driver 00219 * @{ 00220 */ 00221 00222 /* Private typedef -----------------------------------------------------------*/ 00223 /* Private define ------------------------------------------------------------*/ 00224 /** @addtogroup COMP_Private_Constants 00225 * @{ 00226 */ 00227 00228 /* Delay for COMP startup time. */ 00229 /* Note: Delay required to reach propagation delay specification. */ 00230 /* Literal set to maximum value (refer to device datasheet, */ 00231 /* parameter "tSTART"). */ 00232 /* Unit: us */ 00233 #define COMP_DELAY_STARTUP_US (80U) /*!< Delay for COMP startup time */ 00234 00235 /* Delay for COMP voltage scaler stabilization time. */ 00236 /* Literal set to maximum value (refer to device datasheet, */ 00237 /* parameter "tSTART_SCALER"). */ 00238 /* Unit: us */ 00239 #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200U) /*!< Delay for COMP voltage scaler stabilization time */ 00240 00241 #define COMP_OUTPUT_LEVEL_BITOFFSET_POS (30U) 00242 00243 /** 00244 * @} 00245 */ 00246 00247 /* Private macro -------------------------------------------------------------*/ 00248 /* Private variables ---------------------------------------------------------*/ 00249 /* Private function prototypes -----------------------------------------------*/ 00250 /* Exported functions --------------------------------------------------------*/ 00251 00252 /** @defgroup COMP_Exported_Functions COMP Exported Functions 00253 * @{ 00254 */ 00255 00256 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions 00257 * @brief Initialization and de-initialization functions. 00258 * 00259 @verbatim 00260 =============================================================================== 00261 ##### Initialization and de-initialization functions ##### 00262 =============================================================================== 00263 [..] This section provides functions to initialize and de-initialize comparators 00264 00265 @endverbatim 00266 * @{ 00267 */ 00268 00269 /** 00270 * @brief Initialize the COMP according to the specified 00271 * parameters in the COMP_InitTypeDef and initialize the associated handle. 00272 * @note If the selected comparator is locked, initialization can't be performed. 00273 * To unlock the configuration, perform a system reset. 00274 * @param hcomp COMP handle 00275 * @retval HAL status 00276 */ 00277 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp) 00278 { 00279 uint32_t tmp_csr = 0U; 00280 uint32_t exti_line = 0U; 00281 uint32_t comp_voltage_scaler_not_initialized = 0U; 00282 __IO uint32_t wait_loop_index = 0U; 00283 HAL_StatusTypeDef status = HAL_OK; 00284 00285 /* Check the COMP handle allocation and lock status */ 00286 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00287 { 00288 status = HAL_ERROR; 00289 } 00290 else 00291 { 00292 /* Check the parameters */ 00293 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00294 assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput)); 00295 assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput)); 00296 assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol)); 00297 assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode)); 00298 assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis)); 00299 assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce)); 00300 assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode)); 00301 assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode)); 00302 00303 if(hcomp->State == HAL_COMP_STATE_RESET) 00304 { 00305 /* Allocate lock resource and initialize it */ 00306 hcomp->Lock = HAL_UNLOCKED; 00307 00308 /* Set COMP error code to none */ 00309 COMP_CLEAR_ERRORCODE(hcomp); 00310 00311 /* Init SYSCFG and the low level hardware to access comparators */ 00312 /* Note: HAL_COMP_Init() calls __HAL_RCC_SYSCFG_CLK_ENABLE() */ 00313 /* to enable internal control clock of the comparators. */ 00314 /* However, this is a legacy strategy. In future STM32 families, */ 00315 /* COMP clock enable must be implemented by user */ 00316 /* in "HAL_COMP_MspInit()". */ 00317 /* Therefore, for compatibility anticipation, it is recommended */ 00318 /* to implement __HAL_RCC_SYSCFG_CLK_ENABLE() */ 00319 /* in "HAL_COMP_MspInit()". */ 00320 __HAL_RCC_SYSCFG_CLK_ENABLE(); 00321 00322 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1) 00323 /* Init the COMP Callback settings */ 00324 hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */ 00325 00326 if (hcomp->MspInitCallback == NULL) 00327 { 00328 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */ 00329 } 00330 00331 /* Init the low level hardware */ 00332 hcomp->MspInitCallback(hcomp); 00333 #else 00334 /* Init the low level hardware */ 00335 HAL_COMP_MspInit(hcomp); 00336 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */ 00337 } 00338 00339 /* Memorize voltage scaler state before initialization */ 00340 comp_voltage_scaler_not_initialized = (READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) == 0); 00341 00342 /* Set COMP parameters */ 00343 tmp_csr = ( hcomp->Init.NonInvertingInput 00344 | hcomp->Init.InvertingInput 00345 | hcomp->Init.BlankingSrce 00346 | hcomp->Init.Hysteresis 00347 | hcomp->Init.OutputPol 00348 | hcomp->Init.Mode 00349 ); 00350 00351 /* Set parameters in COMP register */ 00352 /* Note: Update all bits except read-only, lock and enable bits */ 00353 #if defined (COMP_CSR_INMESEL) 00354 MODIFY_REG(hcomp->Instance->CSR, 00355 COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL | 00356 COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST | 00357 COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL, 00358 tmp_csr 00359 ); 00360 #else 00361 MODIFY_REG(hcomp->Instance->CSR, 00362 COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL | 00363 COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST | 00364 COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN, 00365 tmp_csr 00366 ); 00367 #endif 00368 00369 /* Set window mode */ 00370 /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */ 00371 /* instances. Therefore, this function can update another COMP */ 00372 /* instance that the one currently selected. */ 00373 if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON) 00374 { 00375 SET_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE); 00376 } 00377 else 00378 { 00379 CLEAR_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE); 00380 } 00381 00382 /* Delay for COMP scaler bridge voltage stabilization */ 00383 /* Apply the delay if voltage scaler bridge is enabled for the first time */ 00384 if ((READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) != 0U) && 00385 (comp_voltage_scaler_not_initialized != 0U) ) 00386 { 00387 /* Wait loop initialization and execution */ 00388 /* Note: Variable divided by 2 to compensate partially */ 00389 /* CPU processing cycles. */ 00390 wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000 * 2U))); 00391 while(wait_loop_index != 0U) 00392 { 00393 wait_loop_index--; 00394 } 00395 } 00396 00397 /* Get the EXTI line corresponding to the selected COMP instance */ 00398 exti_line = COMP_GET_EXTI_LINE(hcomp->Instance); 00399 00400 /* Manage EXTI settings */ 00401 if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != RESET) 00402 { 00403 /* Configure EXTI rising edge */ 00404 if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != RESET) 00405 { 00406 LL_EXTI_EnableRisingTrig_0_31(exti_line); 00407 } 00408 else 00409 { 00410 LL_EXTI_DisableRisingTrig_0_31(exti_line); 00411 } 00412 00413 /* Configure EXTI falling edge */ 00414 if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != RESET) 00415 { 00416 LL_EXTI_EnableFallingTrig_0_31(exti_line); 00417 } 00418 else 00419 { 00420 LL_EXTI_DisableFallingTrig_0_31(exti_line); 00421 } 00422 00423 /* Clear COMP EXTI pending bit (if any) */ 00424 LL_EXTI_ClearFlag_0_31(exti_line); 00425 00426 /* Configure EXTI event mode */ 00427 if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != RESET) 00428 { 00429 LL_EXTI_EnableEvent_0_31(exti_line); 00430 } 00431 else 00432 { 00433 LL_EXTI_DisableEvent_0_31(exti_line); 00434 } 00435 00436 /* Configure EXTI interrupt mode */ 00437 if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != RESET) 00438 { 00439 LL_EXTI_EnableIT_0_31(exti_line); 00440 } 00441 else 00442 { 00443 LL_EXTI_DisableIT_0_31(exti_line); 00444 } 00445 } 00446 else 00447 { 00448 /* Disable EXTI event mode */ 00449 LL_EXTI_DisableEvent_0_31(exti_line); 00450 00451 /* Disable EXTI interrupt mode */ 00452 LL_EXTI_DisableIT_0_31(exti_line); 00453 } 00454 00455 /* Set HAL COMP handle state */ 00456 /* Note: Transition from state reset to state ready, */ 00457 /* otherwise (coming from state ready or busy) no state update. */ 00458 if (hcomp->State == HAL_COMP_STATE_RESET) 00459 { 00460 hcomp->State = HAL_COMP_STATE_READY; 00461 } 00462 } 00463 00464 return status; 00465 } 00466 00467 /** 00468 * @brief DeInitialize the COMP peripheral. 00469 * @note Deinitialization cannot be performed if the COMP configuration is locked. 00470 * To unlock the configuration, perform a system reset. 00471 * @param hcomp COMP handle 00472 * @retval HAL status 00473 */ 00474 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp) 00475 { 00476 HAL_StatusTypeDef status = HAL_OK; 00477 00478 /* Check the COMP handle allocation and lock status */ 00479 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00480 { 00481 status = HAL_ERROR; 00482 } 00483 else 00484 { 00485 /* Check the parameter */ 00486 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00487 00488 /* Set COMP_CSR register to reset value */ 00489 WRITE_REG(hcomp->Instance->CSR, 0x00000000U); 00490 00491 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1) 00492 if (hcomp->MspDeInitCallback == NULL) 00493 { 00494 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */ 00495 } 00496 00497 /* DeInit the low level hardware: GPIO, RCC clock, NVIC */ 00498 hcomp->MspDeInitCallback(hcomp); 00499 #else 00500 /* DeInit the low level hardware: GPIO, RCC clock, NVIC */ 00501 HAL_COMP_MspDeInit(hcomp); 00502 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */ 00503 00504 /* Set HAL COMP handle state */ 00505 hcomp->State = HAL_COMP_STATE_RESET; 00506 00507 /* Release Lock */ 00508 __HAL_UNLOCK(hcomp); 00509 } 00510 00511 return status; 00512 } 00513 00514 /** 00515 * @brief Initialize the COMP MSP. 00516 * @param hcomp COMP handle 00517 * @retval None 00518 */ 00519 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp) 00520 { 00521 /* Prevent unused argument(s) compilation warning */ 00522 UNUSED(hcomp); 00523 00524 /* NOTE : This function should not be modified, when the callback is needed, 00525 the HAL_COMP_MspInit could be implemented in the user file 00526 */ 00527 } 00528 00529 /** 00530 * @brief DeInitialize the COMP MSP. 00531 * @param hcomp COMP handle 00532 * @retval None 00533 */ 00534 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp) 00535 { 00536 /* Prevent unused argument(s) compilation warning */ 00537 UNUSED(hcomp); 00538 00539 /* NOTE : This function should not be modified, when the callback is needed, 00540 the HAL_COMP_MspDeInit could be implemented in the user file 00541 */ 00542 } 00543 00544 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1) 00545 /** 00546 * @brief Register a User COMP Callback 00547 * To be used instead of the weak predefined callback 00548 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains 00549 * the configuration information for the specified COMP. 00550 * @param CallbackID ID of the callback to be registered 00551 * This parameter can be one of the following values: 00552 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID 00553 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID 00554 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID 00555 * @param pCallback pointer to the Callback function 00556 * @retval HAL status 00557 */ 00558 HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback) 00559 { 00560 HAL_StatusTypeDef status = HAL_OK; 00561 00562 if (pCallback == NULL) 00563 { 00564 /* Update the error code */ 00565 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK; 00566 00567 return HAL_ERROR; 00568 } 00569 00570 if (HAL_COMP_STATE_READY == hcomp->State) 00571 { 00572 switch (CallbackID) 00573 { 00574 case HAL_COMP_TRIGGER_CB_ID : 00575 hcomp->TriggerCallback = pCallback; 00576 break; 00577 00578 case HAL_COMP_MSPINIT_CB_ID : 00579 hcomp->MspInitCallback = pCallback; 00580 break; 00581 00582 case HAL_COMP_MSPDEINIT_CB_ID : 00583 hcomp->MspDeInitCallback = pCallback; 00584 break; 00585 00586 default : 00587 /* Update the error code */ 00588 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK; 00589 00590 /* Return error status */ 00591 status = HAL_ERROR; 00592 break; 00593 } 00594 } 00595 else if (HAL_COMP_STATE_RESET == hcomp->State) 00596 { 00597 switch (CallbackID) 00598 { 00599 case HAL_COMP_MSPINIT_CB_ID : 00600 hcomp->MspInitCallback = pCallback; 00601 break; 00602 00603 case HAL_COMP_MSPDEINIT_CB_ID : 00604 hcomp->MspDeInitCallback = pCallback; 00605 break; 00606 00607 default : 00608 /* Update the error code */ 00609 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK; 00610 00611 /* Return error status */ 00612 status = HAL_ERROR; 00613 break; 00614 } 00615 } 00616 else 00617 { 00618 /* Update the error code */ 00619 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK; 00620 00621 /* Return error status */ 00622 status = HAL_ERROR; 00623 } 00624 00625 return status; 00626 } 00627 00628 /** 00629 * @brief Unregister a COMP Callback 00630 * COMP callback is redirected to the weak predefined callback 00631 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains 00632 * the configuration information for the specified COMP. 00633 * @param CallbackID ID of the callback to be unregistered 00634 * This parameter can be one of the following values: 00635 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID 00636 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID 00637 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID 00638 * @retval HAL status 00639 */ 00640 HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID) 00641 { 00642 HAL_StatusTypeDef status = HAL_OK; 00643 00644 if (HAL_COMP_STATE_READY == hcomp->State) 00645 { 00646 switch (CallbackID) 00647 { 00648 case HAL_COMP_TRIGGER_CB_ID : 00649 hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */ 00650 break; 00651 00652 case HAL_COMP_MSPINIT_CB_ID : 00653 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */ 00654 break; 00655 00656 case HAL_COMP_MSPDEINIT_CB_ID : 00657 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */ 00658 break; 00659 00660 default : 00661 /* Update the error code */ 00662 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK; 00663 00664 /* Return error status */ 00665 status = HAL_ERROR; 00666 break; 00667 } 00668 } 00669 else if (HAL_COMP_STATE_RESET == hcomp->State) 00670 { 00671 switch (CallbackID) 00672 { 00673 case HAL_COMP_MSPINIT_CB_ID : 00674 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */ 00675 break; 00676 00677 case HAL_COMP_MSPDEINIT_CB_ID : 00678 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */ 00679 break; 00680 00681 default : 00682 /* Update the error code */ 00683 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK; 00684 00685 /* Return error status */ 00686 status = HAL_ERROR; 00687 break; 00688 } 00689 } 00690 else 00691 { 00692 /* Update the error code */ 00693 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK; 00694 00695 /* Return error status */ 00696 status = HAL_ERROR; 00697 } 00698 00699 return status; 00700 } 00701 00702 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */ 00703 00704 /** 00705 * @} 00706 */ 00707 00708 /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions 00709 * @brief Start-Stop operation functions. 00710 * 00711 @verbatim 00712 =============================================================================== 00713 ##### IO operation functions ##### 00714 =============================================================================== 00715 [..] This section provides functions allowing to: 00716 (+) Start a comparator instance. 00717 (+) Stop a comparator instance. 00718 00719 @endverbatim 00720 * @{ 00721 */ 00722 00723 /** 00724 * @brief Start the comparator. 00725 * @param hcomp COMP handle 00726 * @retval HAL status 00727 */ 00728 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp) 00729 { 00730 __IO uint32_t wait_loop_index = 0U; 00731 HAL_StatusTypeDef status = HAL_OK; 00732 00733 /* Check the COMP handle allocation and lock status */ 00734 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00735 { 00736 status = HAL_ERROR; 00737 } 00738 else 00739 { 00740 /* Check the parameter */ 00741 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00742 00743 if(hcomp->State == HAL_COMP_STATE_READY) 00744 { 00745 /* Enable the selected comparator */ 00746 SET_BIT(hcomp->Instance->CSR, COMP_CSR_EN); 00747 00748 /* Set HAL COMP handle state */ 00749 hcomp->State = HAL_COMP_STATE_BUSY; 00750 00751 /* Delay for COMP startup time */ 00752 /* Wait loop initialization and execution */ 00753 /* Note: Variable divided by 2 to compensate partially */ 00754 /* CPU processing cycles. */ 00755 wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / (1000000U * 2U))); 00756 while(wait_loop_index != 0U) 00757 { 00758 wait_loop_index--; 00759 } 00760 } 00761 else 00762 { 00763 status = HAL_ERROR; 00764 } 00765 } 00766 00767 return status; 00768 } 00769 00770 /** 00771 * @brief Stop the comparator. 00772 * @param hcomp COMP handle 00773 * @retval HAL status 00774 */ 00775 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp) 00776 { 00777 HAL_StatusTypeDef status = HAL_OK; 00778 00779 /* Check the COMP handle allocation and lock status */ 00780 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00781 { 00782 status = HAL_ERROR; 00783 } 00784 else 00785 { 00786 /* Check the parameter */ 00787 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00788 00789 if((hcomp->State == HAL_COMP_STATE_BUSY) || 00790 (hcomp->State == HAL_COMP_STATE_READY) ) 00791 { 00792 /* Disable the selected comparator */ 00793 CLEAR_BIT(hcomp->Instance->CSR, COMP_CSR_EN); 00794 00795 /* Set HAL COMP handle state */ 00796 hcomp->State = HAL_COMP_STATE_READY; 00797 } 00798 else 00799 { 00800 status = HAL_ERROR; 00801 } 00802 } 00803 00804 return status; 00805 } 00806 00807 /** 00808 * @brief Comparator IRQ handler. 00809 * @param hcomp COMP handle 00810 * @retval None 00811 */ 00812 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp) 00813 { 00814 /* Get the EXTI line corresponding to the selected COMP instance */ 00815 uint32_t exti_line = COMP_GET_EXTI_LINE(hcomp->Instance); 00816 00817 /* Check COMP EXTI flag */ 00818 if(LL_EXTI_IsActiveFlag_0_31(exti_line) != RESET) 00819 { 00820 /* Check whether comparator is in independent or window mode */ 00821 if(READ_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE) != RESET) 00822 { 00823 /* Clear COMP EXTI line pending bit of the pair of comparators */ 00824 /* in window mode. */ 00825 /* Note: Pair of comparators in window mode can both trig IRQ when */ 00826 /* input voltage is changing from "out of window" area */ 00827 /* (low or high ) to the other "out of window" area (high or low).*/ 00828 /* Both flags must be cleared to call comparator trigger */ 00829 /* callback is called once. */ 00830 LL_EXTI_ClearFlag_0_31((COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2)); 00831 } 00832 else 00833 { 00834 /* Clear COMP EXTI line pending bit */ 00835 LL_EXTI_ClearFlag_0_31(exti_line); 00836 } 00837 00838 /* COMP trigger user callback */ 00839 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1) 00840 hcomp->TriggerCallback(hcomp); 00841 #else 00842 HAL_COMP_TriggerCallback(hcomp); 00843 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */ 00844 } 00845 } 00846 00847 /** 00848 * @} 00849 */ 00850 00851 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions 00852 * @brief Management functions. 00853 * 00854 @verbatim 00855 =============================================================================== 00856 ##### Peripheral Control functions ##### 00857 =============================================================================== 00858 [..] 00859 This subsection provides a set of functions allowing to control the comparators. 00860 00861 @endverbatim 00862 * @{ 00863 */ 00864 00865 /** 00866 * @brief Lock the selected comparator configuration. 00867 * @note A system reset is required to unlock the comparator configuration. 00868 * @note Locking the comparator from reset state is possible 00869 * if __HAL_RCC_SYSCFG_CLK_ENABLE() is being called before. 00870 * @param hcomp COMP handle 00871 * @retval HAL status 00872 */ 00873 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp) 00874 { 00875 HAL_StatusTypeDef status = HAL_OK; 00876 00877 /* Check the COMP handle allocation and lock status */ 00878 if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) 00879 { 00880 status = HAL_ERROR; 00881 } 00882 else 00883 { 00884 /* Check the parameter */ 00885 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00886 00887 /* Set HAL COMP handle state */ 00888 hcomp->State = ((HAL_COMP_StateTypeDef)(hcomp->State | COMP_STATE_BITFIELD_LOCK)); 00889 } 00890 00891 if(status == HAL_OK) 00892 { 00893 /* Set the lock bit corresponding to selected comparator */ 00894 __HAL_COMP_LOCK(hcomp); 00895 } 00896 00897 return status; 00898 } 00899 00900 /** 00901 * @brief Return the output level (high or low) of the selected comparator. 00902 * The output level depends on the selected polarity. 00903 * If the polarity is not inverted: 00904 * - Comparator output is low when the input plus is at a lower 00905 * voltage than the input minus 00906 * - Comparator output is high when the input plus is at a higher 00907 * voltage than the input minus 00908 * If the polarity is inverted: 00909 * - Comparator output is high when the input plus is at a lower 00910 * voltage than the input minus 00911 * - Comparator output is low when the input plus is at a higher 00912 * voltage than the input minus 00913 * @param hcomp COMP handle 00914 * @retval Returns the selected comparator output level: 00915 * @arg COMP_OUTPUT_LEVEL_LOW 00916 * @arg COMP_OUTPUT_LEVEL_HIGH 00917 * 00918 */ 00919 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp) 00920 { 00921 /* Check the parameter */ 00922 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00923 00924 return (uint32_t)(READ_BIT(hcomp->Instance->CSR, COMP_CSR_VALUE) 00925 >> COMP_OUTPUT_LEVEL_BITOFFSET_POS); 00926 } 00927 00928 /** 00929 * @brief Comparator trigger callback. 00930 * @param hcomp COMP handle 00931 * @retval None 00932 */ 00933 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) 00934 { 00935 /* Prevent unused argument(s) compilation warning */ 00936 UNUSED(hcomp); 00937 00938 /* NOTE : This function should not be modified, when the callback is needed, 00939 the HAL_COMP_TriggerCallback should be implemented in the user file 00940 */ 00941 } 00942 00943 00944 /** 00945 * @} 00946 */ 00947 00948 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions 00949 * @brief Peripheral State functions. 00950 * 00951 @verbatim 00952 =============================================================================== 00953 ##### Peripheral State functions ##### 00954 =============================================================================== 00955 [..] 00956 This subsection permit to get in run-time the status of the peripheral. 00957 00958 @endverbatim 00959 * @{ 00960 */ 00961 00962 /** 00963 * @brief Return the COMP handle state. 00964 * @param hcomp COMP handle 00965 * @retval HAL state 00966 */ 00967 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp) 00968 { 00969 /* Check the COMP handle allocation */ 00970 if(hcomp == NULL) 00971 { 00972 return HAL_COMP_STATE_RESET; 00973 } 00974 00975 /* Check the parameter */ 00976 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00977 00978 /* Return HAL COMP handle state */ 00979 return hcomp->State; 00980 } 00981 00982 /** 00983 * @brief Return the COMP error code. 00984 * @param hcomp COMP handle 00985 * @retval COMP error code 00986 */ 00987 uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp) 00988 { 00989 /* Check the parameters */ 00990 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); 00991 00992 return hcomp->ErrorCode; 00993 } 00994 00995 /** 00996 * @} 00997 */ 00998 00999 /** 01000 * @} 01001 */ 01002 01003 /** 01004 * @} 01005 */ 01006 01007 #endif /* COMP1 || COMP2 */ 01008 01009 #endif /* HAL_COMP_MODULE_ENABLED */ 01010 01011 /** 01012 * @} 01013 */ 01014 01015 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/