STM32L4xx_HAL_Driver  1.14.0
Initialization/de-initialization functions

Initialization and de-initialization functions. More...

Functions

HAL_StatusTypeDef HAL_COMP_Init (COMP_HandleTypeDef *hcomp)
 Initialize the COMP according to the specified parameters in the COMP_InitTypeDef and initialize the associated handle. More...
 
HAL_StatusTypeDef HAL_COMP_DeInit (COMP_HandleTypeDef *hcomp)
 DeInitialize the COMP peripheral. More...
 
void HAL_COMP_MspInit (COMP_HandleTypeDef *hcomp)
 Initialize the COMP MSP. More...
 
void HAL_COMP_MspDeInit (COMP_HandleTypeDef *hcomp)
 DeInitialize the COMP MSP. More...
 
HAL_StatusTypeDef HAL_COMP_RegisterCallback (COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
 Register a User COMP Callback To be used instead of the weak predefined callback. More...
 
HAL_StatusTypeDef HAL_COMP_UnRegisterCallback (COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
 Unregister a COMP Callback COMP callback is redirected to the weak predefined callback. More...
 

Detailed Description

Initialization and de-initialization functions.

 ===============================================================================
              ##### Initialization and de-initialization functions #####
 ===============================================================================
    [..]  This section provides functions to initialize and de-initialize comparators

Function Documentation

◆ HAL_COMP_DeInit()

HAL_StatusTypeDef HAL_COMP_DeInit ( COMP_HandleTypeDef hcomp)

DeInitialize the COMP peripheral.

Note
Deinitialization cannot be performed if the COMP configuration is locked. To unlock the configuration, perform a system reset.
Parameters
hcompCOMP handle
Return values
HALstatus

Definition at line 480 of file stm32l4xx_hal_comp.c.

481 {
482  HAL_StatusTypeDef status = HAL_OK;
483 
484  /* Check the COMP handle allocation and lock status */
485  if(hcomp == NULL)
486  {
487  status = HAL_ERROR;
488  }
489  else if(__HAL_COMP_IS_LOCKED(hcomp))
490  {
491  status = HAL_ERROR;
492  }
493  else
494  {
495  /* Check the parameter */
496  assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
497 
498  /* Set COMP_CSR register to reset value */
499  WRITE_REG(hcomp->Instance->CSR, 0x00000000UL);
500 
501 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
502  if (hcomp->MspDeInitCallback == NULL)
503  {
504  hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
505  }
506 
507  /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
508  hcomp->MspDeInitCallback(hcomp);
509 #else
510  /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
511  HAL_COMP_MspDeInit(hcomp);
512 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
513 
514  /* Set HAL COMP handle state */
515  hcomp->State = HAL_COMP_STATE_RESET;
516 
517  /* Release Lock */
518  __HAL_UNLOCK(hcomp);
519  }
520 
521  return status;
522 }
void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
DeInitialize the COMP MSP.
__HAL_UNLOCK(hrtc)
return HAL_OK
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_COMP_Init()

HAL_StatusTypeDef HAL_COMP_Init ( COMP_HandleTypeDef hcomp)

Initialize the COMP according to the specified parameters in the COMP_InitTypeDef and initialize the associated handle.

Note
If the selected comparator is locked, initialization can't be performed. To unlock the configuration, perform a system reset.
Parameters
hcompCOMP handle
Return values
HALstatus

Definition at line 265 of file stm32l4xx_hal_comp.c.

266 {
267  uint32_t tmp_csr;
268  uint32_t exti_line;
269  uint32_t comp_voltage_scaler_initialized; /* Value "0" if comparator voltage scaler is not initialized */
270  __IO uint32_t wait_loop_index = 0UL;
271  HAL_StatusTypeDef status = HAL_OK;
272 
273  /* Check the COMP handle allocation and lock status */
274  if(hcomp == NULL)
275  {
276  status = HAL_ERROR;
277  }
278  else if(__HAL_COMP_IS_LOCKED(hcomp))
279  {
280  status = HAL_ERROR;
281  }
282  else
283  {
284  /* Check the parameters */
285  assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
286  assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput));
287  assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput));
288  assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
289  assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode));
290  assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
291  assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
292  assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
293 #if defined(COMP2)
294  assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
295 #endif
296 
297  if(hcomp->State == HAL_COMP_STATE_RESET)
298  {
299  /* Allocate lock resource and initialize it */
300  hcomp->Lock = HAL_UNLOCKED;
301 
302  /* Set COMP error code to none */
303  COMP_CLEAR_ERRORCODE(hcomp);
304 
305  /* Init SYSCFG and the low level hardware to access comparators */
306  /* Note: HAL_COMP_Init() calls __HAL_RCC_SYSCFG_CLK_ENABLE() */
307  /* to enable internal control clock of the comparators. */
308  /* However, this is a legacy strategy. In future STM32 families, */
309  /* COMP clock enable must be implemented by user */
310  /* in "HAL_COMP_MspInit()". */
311  /* Therefore, for compatibility anticipation, it is recommended */
312  /* to implement __HAL_RCC_SYSCFG_CLK_ENABLE() */
313  /* in "HAL_COMP_MspInit()". */
314  __HAL_RCC_SYSCFG_CLK_ENABLE();
315 
316 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
317  /* Init the COMP Callback settings */
318  hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
319 
320  if (hcomp->MspInitCallback == NULL)
321  {
322  hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
323  }
324 
325  /* Init the low level hardware */
326  hcomp->MspInitCallback(hcomp);
327 #else
328  /* Init the low level hardware */
329  HAL_COMP_MspInit(hcomp);
330 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
331  }
332 
333  /* Memorize voltage scaler state before initialization */
334  comp_voltage_scaler_initialized = READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN);
335 
336  /* Set COMP parameters */
337  tmp_csr = ( hcomp->Init.NonInvertingInput
338  | hcomp->Init.InvertingInput
339  | hcomp->Init.BlankingSrce
340  | hcomp->Init.Hysteresis
341  | hcomp->Init.OutputPol
342  | hcomp->Init.Mode
343  );
344 
345  /* Set parameters in COMP register */
346  /* Note: Update all bits except read-only, lock and enable bits */
347 #if defined (COMP_CSR_INMESEL)
348 #if defined (COMP_CSR_WINMODE)
349  MODIFY_REG(hcomp->Instance->CSR,
350  COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
351  COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST |
352  COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL,
353  tmp_csr
354  );
355 #else
356  MODIFY_REG(hcomp->Instance->CSR,
357  COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
358  COMP_CSR_POLARITY | COMP_CSR_HYST |
359  COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL,
360  tmp_csr
361  );
362 #endif
363 #else
364  MODIFY_REG(hcomp->Instance->CSR,
365  COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
366  COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST |
367  COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN,
368  tmp_csr
369  );
370 #endif
371 
372 #if defined(COMP2)
373  /* Set window mode */
374  /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */
375  /* instances. Therefore, this function can update another COMP */
376  /* instance that the one currently selected. */
377  if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON)
378  {
379  SET_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
380  }
381  else
382  {
383  CLEAR_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
384  }
385 #endif /* COMP2 */
386 
387  /* Delay for COMP scaler bridge voltage stabilization */
388  /* Apply the delay if voltage scaler bridge is required and not already enabled */
389  if ((READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) != 0UL) &&
390  (comp_voltage_scaler_initialized == 0UL) )
391  {
392  /* Wait loop initialization and execution */
393  /* Note: Variable divided by 2 to compensate partially */
394  /* CPU processing cycles, scaling in us split to not */
395  /* exceed 32 bits register capacity and handle low frequency. */
396  wait_loop_index = ((COMP_DELAY_VOLTAGE_SCALER_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
397  while(wait_loop_index != 0UL)
398  {
399  wait_loop_index--;
400  }
401  }
402 
403  /* Get the EXTI line corresponding to the selected COMP instance */
404  exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
405 
406  /* Manage EXTI settings */
407  if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != 0UL)
408  {
409  /* Configure EXTI rising edge */
410  if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != 0UL)
411  {
413  }
414  else
415  {
417  }
418 
419  /* Configure EXTI falling edge */
420  if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != 0UL)
421  {
423  }
424  else
425  {
427  }
428 
429  /* Clear COMP EXTI pending bit (if any) */
430  LL_EXTI_ClearFlag_0_31(exti_line);
431 
432  /* Configure EXTI event mode */
433  if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != 0UL)
434  {
435  LL_EXTI_EnableEvent_0_31(exti_line);
436  }
437  else
438  {
439  LL_EXTI_DisableEvent_0_31(exti_line);
440  }
441 
442  /* Configure EXTI interrupt mode */
443  if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != 0UL)
444  {
445  LL_EXTI_EnableIT_0_31(exti_line);
446  }
447  else
448  {
449  LL_EXTI_DisableIT_0_31(exti_line);
450  }
451  }
452  else
453  {
454  /* Disable EXTI event mode */
455  LL_EXTI_DisableEvent_0_31(exti_line);
456 
457  /* Disable EXTI interrupt mode */
458  LL_EXTI_DisableIT_0_31(exti_line);
459  }
460 
461  /* Set HAL COMP handle state */
462  /* Note: Transition from state reset to state ready, */
463  /* otherwise (coming from state ready or busy) no state update. */
464  if (hcomp->State == HAL_COMP_STATE_RESET)
465  {
466  hcomp->State = HAL_COMP_STATE_READY;
467  }
468  }
469 
470  return status;
471 }
__STATIC_INLINE void LL_EXTI_DisableEvent_0_31(uint32_t ExtiLine)
Disable ExtiLine Event request for Lines in range 0 to 31 EMR1 EMx LL_EXTI_DisableEvent_0_31.
__STATIC_INLINE void LL_EXTI_DisableFallingTrig_0_31(uint32_t ExtiLine)
Disable ExtiLine Falling Edge Trigger for Lines in range 0 to 31.
__STATIC_INLINE void LL_EXTI_EnableFallingTrig_0_31(uint32_t ExtiLine)
Enable ExtiLine Falling Edge Trigger for Lines in range 0 to 31.
__STATIC_INLINE void LL_EXTI_EnableRisingTrig_0_31(uint32_t ExtiLine)
Enable ExtiLine Rising Edge Trigger for Lines in range 0 to 31.
__STATIC_INLINE void LL_EXTI_ClearFlag_0_31(uint32_t ExtiLine)
Clear ExtLine Flags for Lines in range 0 to 31.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__STATIC_INLINE void LL_EXTI_EnableEvent_0_31(uint32_t ExtiLine)
Enable ExtiLine Event request for Lines in range 0 to 31 EMR1 EMx LL_EXTI_EnableEvent_0_31.
return HAL_OK
__STATIC_INLINE void LL_EXTI_DisableRisingTrig_0_31(uint32_t ExtiLine)
Disable ExtiLine Rising Edge Trigger for Lines in range 0 to 31.
__STATIC_INLINE void LL_EXTI_DisableIT_0_31(uint32_t ExtiLine)
Disable ExtiLine Interrupt request for Lines in range 0 to 31.
void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
Initialize the COMP MSP.
__STATIC_INLINE void LL_EXTI_EnableIT_0_31(uint32_t ExtiLine)
Enable ExtiLine Interrupt request for Lines in range 0 to 31.
void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
Comparator trigger callback.
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_COMP_MspDeInit()

__weak void HAL_COMP_MspDeInit ( COMP_HandleTypeDef hcomp)

DeInitialize the COMP MSP.

Parameters
hcompCOMP handle
Return values
None

Definition at line 544 of file stm32l4xx_hal_comp.c.

545 {
546  /* Prevent unused argument(s) compilation warning */
547  UNUSED(hcomp);
548 
549  /* NOTE : This function should not be modified, when the callback is needed,
550  the HAL_COMP_MspDeInit could be implemented in the user file
551  */
552 }

◆ HAL_COMP_MspInit()

__weak void HAL_COMP_MspInit ( COMP_HandleTypeDef hcomp)

Initialize the COMP MSP.

Parameters
hcompCOMP handle
Return values
None

Definition at line 529 of file stm32l4xx_hal_comp.c.

530 {
531  /* Prevent unused argument(s) compilation warning */
532  UNUSED(hcomp);
533 
534  /* NOTE : This function should not be modified, when the callback is needed,
535  the HAL_COMP_MspInit could be implemented in the user file
536  */
537 }

◆ HAL_COMP_RegisterCallback()

HAL_StatusTypeDef HAL_COMP_RegisterCallback ( COMP_HandleTypeDef hcomp,
HAL_COMP_CallbackIDTypeDef  CallbackID,
pCOMP_CallbackTypeDef  pCallback 
)

Register a User COMP Callback To be used instead of the weak predefined callback.

Parameters
hcompPointer to a COMP_HandleTypeDef structure that contains the configuration information for the specified COMP.
CallbackIDID of the callback to be registered This parameter can be one of the following values:
pCallbackpointer to the Callback function
Return values
HALstatus

Definition at line 568 of file stm32l4xx_hal_comp.c.

569 {
570  HAL_StatusTypeDef status = HAL_OK;
571 
572  if (pCallback == NULL)
573  {
574  /* Update the error code */
575  hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
576 
577  return HAL_ERROR;
578  }
579 
580  if (HAL_COMP_STATE_READY == hcomp->State)
581  {
582  switch (CallbackID)
583  {
585  hcomp->TriggerCallback = pCallback;
586  break;
587 
589  hcomp->MspInitCallback = pCallback;
590  break;
591 
593  hcomp->MspDeInitCallback = pCallback;
594  break;
595 
596  default :
597  /* Update the error code */
598  hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
599 
600  /* Return error status */
601  status = HAL_ERROR;
602  break;
603  }
604  }
605  else if (HAL_COMP_STATE_RESET == hcomp->State)
606  {
607  switch (CallbackID)
608  {
610  hcomp->MspInitCallback = pCallback;
611  break;
612 
614  hcomp->MspDeInitCallback = pCallback;
615  break;
616 
617  default :
618  /* Update the error code */
619  hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
620 
621  /* Return error status */
622  status = HAL_ERROR;
623  break;
624  }
625  }
626  else
627  {
628  /* Update the error code */
629  hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
630 
631  /* Return error status */
632  status = HAL_ERROR;
633  }
634 
635  return status;
636 }
return HAL_OK

◆ HAL_COMP_UnRegisterCallback()

HAL_StatusTypeDef HAL_COMP_UnRegisterCallback ( COMP_HandleTypeDef hcomp,
HAL_COMP_CallbackIDTypeDef  CallbackID 
)

Unregister a COMP Callback COMP callback is redirected to the weak predefined callback.

Parameters
hcompPointer to a COMP_HandleTypeDef structure that contains the configuration information for the specified COMP.
CallbackIDID of the callback to be unregistered This parameter can be one of the following values:
Return values
HALstatus

Definition at line 650 of file stm32l4xx_hal_comp.c.

651 {
652  HAL_StatusTypeDef status = HAL_OK;
653 
654  if (HAL_COMP_STATE_READY == hcomp->State)
655  {
656  switch (CallbackID)
657  {
659  hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
660  break;
661 
663  hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
664  break;
665 
667  hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
668  break;
669 
670  default :
671  /* Update the error code */
672  hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
673 
674  /* Return error status */
675  status = HAL_ERROR;
676  break;
677  }
678  }
679  else if (HAL_COMP_STATE_RESET == hcomp->State)
680  {
681  switch (CallbackID)
682  {
684  hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
685  break;
686 
688  hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
689  break;
690 
691  default :
692  /* Update the error code */
693  hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
694 
695  /* Return error status */
696  status = HAL_ERROR;
697  break;
698  }
699  }
700  else
701  {
702  /* Update the error code */
703  hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
704 
705  /* Return error status */
706  status = HAL_ERROR;
707  }
708 
709  return status;
710 }
void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
DeInitialize the COMP MSP.
return HAL_OK
void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
Initialize the COMP MSP.
void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
Comparator trigger callback.