Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:09

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_ll_comp.c
0004   * @author  MCD Application Team
0005   * @brief   COMP LL module driver
0006   ******************************************************************************
0007   * @attention
0008   *
0009   * Copyright (c) 2017 STMicroelectronics.
0010   * All rights reserved.
0011   *
0012   * This software is licensed under terms that can be found in the LICENSE file
0013   * in the root directory of this software component.
0014   * If no LICENSE file comes with this software, it is provided AS-IS.
0015   *
0016   ******************************************************************************
0017   */
0018 #if defined(USE_FULL_LL_DRIVER) || defined(__rtems__)
0019 
0020 /* Includes ------------------------------------------------------------------*/
0021 #include "stm32h7xx_ll_comp.h"
0022 
0023 #ifdef  USE_FULL_ASSERT
0024  #include "stm32_assert.h"
0025 #else
0026  #define assert_param(expr) ((void)0U)
0027 #endif
0028 
0029 /** @addtogroup STM32H7xx_LL_Driver
0030   * @{
0031   */
0032 
0033 #if defined (COMP1) || defined (COMP2)
0034 
0035 /** @addtogroup COMP_LL COMP
0036   * @{
0037   */
0038 
0039 /* Private types -------------------------------------------------------------*/
0040 /* Private variables ---------------------------------------------------------*/
0041 /* Private constants ---------------------------------------------------------*/
0042 /* Private macros ------------------------------------------------------------*/
0043 
0044 /** @addtogroup COMP_LL_Private_Macros
0045   * @{
0046   */
0047 
0048 /* Check of parameters for configuration of COMP hierarchical scope:          */
0049 /* COMP instance.                                                             */
0050 
0051 #define IS_LL_COMP_POWER_MODE(__POWER_MODE__)                                  \
0052   (   ((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED)                        \
0053    || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED)                      \
0054    || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER)                    \
0055   )
0056 
0057 /* Note: On this STM32 series, comparator input plus parameters are            */
0058 /*       the same on all COMP instances.                                      */
0059 /*       However, comparator instance kept as macro parameter for             */
0060 /*       compatibility with other STM32 families.                             */
0061 #if defined (COMP_CFGRx_INP2SEL)
0062 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
0063   (   ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                             \
0064    || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2)                             \
0065    || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_DAC2_CH1))
0066 #else
0067 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
0068   (   ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                             \
0069    || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2))
0070 #endif
0071     
0072 /* Note: On this STM32 series, comparator input minus parameters are           */
0073 /*       the same on all COMP instances.                                      */
0074 /*       However, comparator instance kept as macro parameter for             */
0075 /*       compatibility with other STM32 families.                             */
0076 #if defined (COMP_CFGRx_INMSEL_3)
0077 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
0078   (   ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                    \
0079    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
0080    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
0081    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
0082    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                      \
0083    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                      \
0084    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
0085    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2)                           \
0086    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_TPSENS_DAC2CH1)                \
0087    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VBAT_VDDAP))
0088 #else
0089 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
0090   (   ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                    \
0091    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
0092    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
0093    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
0094    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                      \
0095    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                      \
0096    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
0097    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2))
0098 #endif
0099     
0100 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__)                      \
0101   (   ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE)                      \
0102    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW)                       \
0103    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM)                    \
0104    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH)                      \
0105   )
0106 
0107 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__)                               \
0108   (   ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED)                        \
0109    || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED)                           \
0110   )
0111 
0112 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__OUTPUT_BLANKING_SOURCE__)          \
0113   (   ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE)               \
0114    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5)           \
0115    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3)           \
0116    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3)           \
0117    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4)           \
0118    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5)           \
0119    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1)          \
0120   )
0121   
0122 /**
0123   * @}
0124   */
0125 
0126 
0127 /* Private function prototypes -----------------------------------------------*/
0128 
0129 /* Exported functions --------------------------------------------------------*/
0130 /** @addtogroup COMP_LL_Exported_Functions
0131   * @{
0132   */
0133 
0134 /** @addtogroup COMP_LL_EF_Init
0135   * @{
0136   */
0137 
0138 /**
0139   * @brief  De-initialize registers of the selected COMP instance
0140   *         to their default reset values.
0141   * @note   If comparator is locked, de-initialization by software is
0142   *         not possible.
0143   *         The only way to unlock the comparator is a device hardware reset.
0144   * @param  COMPx COMP instance
0145   * @retval An ErrorStatus enumeration value:
0146   *          - SUCCESS: COMP registers are de-initialized
0147   *          - ERROR: COMP registers are not de-initialized
0148   */
0149 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
0150 {
0151   ErrorStatus status = SUCCESS;
0152   
0153   /* Check the parameters */
0154   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
0155   
0156   /* Note: Hardware constraint (refer to description of this function):       */
0157   /*       COMP instance must not be locked.                                  */
0158   if(LL_COMP_IsLocked(COMPx) == 0UL)
0159   {
0160     LL_COMP_WriteReg((COMPx), CFGR, 0x00000000UL);
0161 
0162   }
0163   else
0164   {
0165     /* Comparator instance is locked: de-initialization by software is         */
0166     /* not possible.                                                           */
0167     /* The only way to unlock the comparator is a device hardware reset.       */
0168     status = ERROR;
0169   }
0170   
0171   return status;
0172 }
0173 
0174 /**
0175   * @brief  Initialize some features of COMP instance.
0176   * @note   This function configures features of the selected COMP instance.
0177   *         Some features are also available at scope COMP common instance
0178   *         (common to several COMP instances).
0179   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
0180   * @param  COMPx COMP instance
0181   * @param  COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
0182   * @retval An ErrorStatus enumeration value:
0183   *          - SUCCESS: COMP registers are initialized
0184   *          - ERROR: COMP registers are not initialized
0185   */
0186 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
0187 {
0188   ErrorStatus status = SUCCESS;
0189   
0190   /* Check the parameters */
0191   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
0192   assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
0193   assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
0194   assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
0195   assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
0196   assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
0197   assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMP_InitStruct->OutputBlankingSource));
0198   
0199   /* Note: Hardware constraint (refer to description of this function)        */
0200   /*       COMP instance must not be locked.                                  */
0201   if(LL_COMP_IsLocked(COMPx) == 0UL)
0202   {
0203     /* Configuration of comparator instance :                                 */
0204     /*  - PowerMode                                                           */
0205     /*  - InputPlus                                                           */
0206     /*  - InputMinus                                                          */
0207     /*  - InputHysteresis                                                     */
0208     /*  - OutputPolarity                                                      */
0209     /*  - OutputBlankingSource                                                */
0210 #if defined (COMP_CFGRx_INP2SEL)
0211     MODIFY_REG(COMPx->CFGR,
0212                  COMP_CFGRx_PWRMODE
0213                | COMP_CFGRx_INPSEL
0214                | COMP_CFGRx_INP2SEL
0215                | COMP_CFGRx_SCALEN
0216                | COMP_CFGRx_BRGEN
0217                | COMP_CFGRx_INMSEL
0218                | COMP_CFGRx_HYST
0219                | COMP_CFGRx_POLARITY
0220                | COMP_CFGRx_BLANKING
0221               ,
0222                  COMP_InitStruct->PowerMode
0223                | COMP_InitStruct->InputPlus
0224                | COMP_InitStruct->InputMinus
0225                | COMP_InitStruct->InputHysteresis
0226                | COMP_InitStruct->OutputPolarity
0227                | COMP_InitStruct->OutputBlankingSource
0228               );
0229 #else
0230     MODIFY_REG(COMPx->CFGR,
0231                  COMP_CFGRx_PWRMODE
0232                | COMP_CFGRx_INPSEL
0233                | COMP_CFGRx_SCALEN
0234                | COMP_CFGRx_BRGEN
0235                | COMP_CFGRx_INMSEL
0236                | COMP_CFGRx_HYST
0237                | COMP_CFGRx_POLARITY
0238                | COMP_CFGRx_BLANKING
0239               ,
0240                  COMP_InitStruct->PowerMode
0241                | COMP_InitStruct->InputPlus
0242                | COMP_InitStruct->InputMinus
0243                | COMP_InitStruct->InputHysteresis
0244                | COMP_InitStruct->OutputPolarity
0245                | COMP_InitStruct->OutputBlankingSource
0246               );
0247 #endif
0248   }
0249   else
0250   {
0251     /* Initialization error: COMP instance is locked.                         */
0252     status = ERROR;
0253   }
0254   
0255   return status;
0256 }
0257 
0258 /**
0259   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
0260   * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
0261   *                        whose fields will be set to default values.
0262   * @retval None
0263   */
0264 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
0265 {
0266   /* Set COMP_InitStruct fields to default values */
0267   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_ULTRALOWPOWER;
0268   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
0269   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
0270   COMP_InitStruct->InputHysteresis      = LL_COMP_HYSTERESIS_NONE;
0271   COMP_InitStruct->OutputPolarity       = LL_COMP_OUTPUTPOL_NONINVERTED;
0272   COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
0273 }
0274 
0275 /**
0276   * @}
0277   */
0278 
0279 /**
0280   * @}
0281   */
0282 
0283 /**
0284   * @}
0285   */
0286 
0287 #endif /* COMP1 || COMP2 */
0288 
0289 /**
0290   * @}
0291   */
0292 
0293 #endif /* USE_FULL_LL_DRIVER */
0294