STM32L443xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_ll_comp.c 00004 * @author MCD Application Team 00005 * @brief COMP LL module driver 00006 ****************************************************************************** 00007 * @attention 00008 * 00009 * Copyright (c) 2017 STMicroelectronics. 00010 * All rights reserved. 00011 * 00012 * This software is licensed under terms that can be found in the LICENSE file 00013 * in the root directory of this software component. 00014 * If no LICENSE file comes with this software, it is provided AS-IS. 00015 * 00016 ****************************************************************************** 00017 */ 00018 #if defined(USE_FULL_LL_DRIVER) 00019 00020 /* Includes ------------------------------------------------------------------*/ 00021 #include "stm32l4xx_ll_comp.h" 00022 00023 #ifdef USE_FULL_ASSERT 00024 #include "stm32_assert.h" 00025 #else 00026 #define assert_param(expr) ((void)0U) 00027 #endif /* USE_FULL_ASSERT */ 00028 00029 /** @addtogroup STM32L4xx_LL_Driver 00030 * @{ 00031 */ 00032 00033 #if defined (COMP1) || defined (COMP2) 00034 00035 /** @addtogroup COMP_LL COMP 00036 * @{ 00037 */ 00038 00039 /* Private types -------------------------------------------------------------*/ 00040 /* Private variables ---------------------------------------------------------*/ 00041 /* Private constants ---------------------------------------------------------*/ 00042 /* Private macros ------------------------------------------------------------*/ 00043 00044 /** @addtogroup COMP_LL_Private_Macros 00045 * @{ 00046 */ 00047 00048 /* Check of parameters for configuration of COMP hierarchical scope: */ 00049 /* COMP instance. */ 00050 00051 #define IS_LL_COMP_POWER_MODE(__POWER_MODE__) \ 00052 ( ((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED) \ 00053 || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \ 00054 || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER) \ 00055 ) 00056 00057 /* Note: On this STM32 series, comparator input plus parameters are */ 00058 /* the same on all COMP instances. */ 00059 /* However, comparator instance kept as macro parameter for */ 00060 /* compatibility with other STM32 families. */ 00061 #if defined(COMP_CSR_INPSEL_1) 00062 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \ 00063 ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \ 00064 || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \ 00065 || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \ 00066 ) 00067 #else 00068 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \ 00069 ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \ 00070 || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \ 00071 ) 00072 #endif 00073 00074 /* Note: On this STM32 series, comparator input minus parameters are */ 00075 /* the same on all COMP instances. */ 00076 /* However, comparator instance kept as macro parameter for */ 00077 /* compatibility with other STM32 families. */ 00078 #if defined(COMP_CSR_INMESEL_1) 00079 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \ 00080 ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \ 00081 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \ 00082 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \ 00083 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \ 00084 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \ 00085 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \ 00086 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \ 00087 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \ 00088 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3) \ 00089 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO4) \ 00090 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO5) \ 00091 ) 00092 #else 00093 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \ 00094 ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \ 00095 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \ 00096 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \ 00097 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \ 00098 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \ 00099 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \ 00100 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \ 00101 || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \ 00102 ) 00103 #endif 00104 00105 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \ 00106 ( ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \ 00107 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW) \ 00108 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM) \ 00109 || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH) \ 00110 ) 00111 00112 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \ 00113 ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \ 00114 || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \ 00115 ) 00116 00117 #if defined(COMP2) 00118 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \ 00119 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \ 00120 ? ( \ 00121 (1UL) \ 00122 ) \ 00123 : \ 00124 (((__COMP_INSTANCE__) == COMP1) \ 00125 ? ( \ 00126 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \ 00127 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \ 00128 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) \ 00129 ) \ 00130 : \ 00131 ( \ 00132 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2) \ 00133 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2) \ 00134 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1_COMP2) \ 00135 ) \ 00136 ) \ 00137 ) 00138 #else 00139 #if defined(TIM3) 00140 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \ 00141 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \ 00142 ? ( \ 00143 (1UL) \ 00144 ) \ 00145 : \ 00146 ( \ 00147 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \ 00148 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \ 00149 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) \ 00150 ) \ 00151 ) 00152 #else 00153 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \ 00154 (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \ 00155 ? ( \ 00156 (1UL) \ 00157 ) \ 00158 : \ 00159 ( \ 00160 ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \ 00161 || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \ 00162 ) \ 00163 ) 00164 #endif /* TIM3 */ 00165 #endif /* COMP2 */ 00166 /** 00167 * @} 00168 */ 00169 00170 00171 /* Private function prototypes -----------------------------------------------*/ 00172 00173 /* Exported functions --------------------------------------------------------*/ 00174 /** @addtogroup COMP_LL_Exported_Functions 00175 * @{ 00176 */ 00177 00178 /** @addtogroup COMP_LL_EF_Init 00179 * @{ 00180 */ 00181 00182 /** 00183 * @brief De-initialize registers of the selected COMP instance 00184 * to their default reset values. 00185 * @note If comparator is locked, de-initialization by software is 00186 * not possible. 00187 * The only way to unlock the comparator is a device hardware reset. 00188 * @param COMPx COMP instance 00189 * @retval An ErrorStatus enumeration value: 00190 * - SUCCESS: COMP registers are de-initialized 00191 * - ERROR: COMP registers are not de-initialized 00192 */ 00193 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx) 00194 { 00195 ErrorStatus status = SUCCESS; 00196 00197 /* Check the parameters */ 00198 assert_param(IS_COMP_ALL_INSTANCE(COMPx)); 00199 00200 /* Note: Hardware constraint (refer to description of this function): */ 00201 /* COMP instance must not be locked. */ 00202 if (LL_COMP_IsLocked(COMPx) == 0UL) 00203 { 00204 LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL); 00205 00206 } 00207 else 00208 { 00209 /* Comparator instance is locked: de-initialization by software is */ 00210 /* not possible. */ 00211 /* The only way to unlock the comparator is a device hardware reset. */ 00212 status = ERROR; 00213 } 00214 00215 return status; 00216 } 00217 00218 /** 00219 * @brief Initialize some features of COMP instance. 00220 * @note This function configures features of the selected COMP instance. 00221 * Some features are also available at scope COMP common instance 00222 * (common to several COMP instances). 00223 * Refer to functions having argument "COMPxy_COMMON" as parameter. 00224 * @param COMPx COMP instance 00225 * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure 00226 * @retval An ErrorStatus enumeration value: 00227 * - SUCCESS: COMP registers are initialized 00228 * - ERROR: COMP registers are not initialized 00229 */ 00230 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct) 00231 { 00232 ErrorStatus status = SUCCESS; 00233 00234 /* Check the parameters */ 00235 assert_param(IS_COMP_ALL_INSTANCE(COMPx)); 00236 assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode)); 00237 assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus)); 00238 assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus)); 00239 assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis)); 00240 assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity)); 00241 assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMPx, COMP_InitStruct->OutputBlankingSource)); 00242 00243 /* Note: Hardware constraint (refer to description of this function) */ 00244 /* COMP instance must not be locked. */ 00245 if (LL_COMP_IsLocked(COMPx) == 0UL) 00246 { 00247 /* Configuration of comparator instance : */ 00248 /* - PowerMode */ 00249 /* - InputPlus */ 00250 /* - InputMinus */ 00251 /* - InputHysteresis */ 00252 /* - OutputPolarity */ 00253 /* - OutputBlankingSource */ 00254 #if defined(COMP_CSR_INMESEL_1) 00255 MODIFY_REG(COMPx->CSR, 00256 COMP_CSR_PWRMODE 00257 | COMP_CSR_INPSEL 00258 | COMP_CSR_SCALEN 00259 | COMP_CSR_BRGEN 00260 | COMP_CSR_INMESEL 00261 | COMP_CSR_INMSEL 00262 | COMP_CSR_HYST 00263 | COMP_CSR_POLARITY 00264 | COMP_CSR_BLANKING 00265 , 00266 COMP_InitStruct->PowerMode 00267 | COMP_InitStruct->InputPlus 00268 | COMP_InitStruct->InputMinus 00269 | COMP_InitStruct->InputHysteresis 00270 | COMP_InitStruct->OutputPolarity 00271 | COMP_InitStruct->OutputBlankingSource 00272 ); 00273 #else 00274 MODIFY_REG(COMPx->CSR, 00275 COMP_CSR_PWRMODE 00276 | COMP_CSR_INPSEL 00277 | COMP_CSR_SCALEN 00278 | COMP_CSR_BRGEN 00279 | COMP_CSR_INMSEL 00280 | COMP_CSR_HYST 00281 | COMP_CSR_POLARITY 00282 | COMP_CSR_BLANKING 00283 , 00284 COMP_InitStruct->PowerMode 00285 | COMP_InitStruct->InputPlus 00286 | COMP_InitStruct->InputMinus 00287 | COMP_InitStruct->InputHysteresis 00288 | COMP_InitStruct->OutputPolarity 00289 | COMP_InitStruct->OutputBlankingSource 00290 ); 00291 #endif 00292 00293 } 00294 else 00295 { 00296 /* Initialization error: COMP instance is locked. */ 00297 status = ERROR; 00298 } 00299 00300 return status; 00301 } 00302 00303 /** 00304 * @brief Set each @ref LL_COMP_InitTypeDef field to default value. 00305 * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure 00306 * whose fields will be set to default values. 00307 * @retval None 00308 */ 00309 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct) 00310 { 00311 /* Set COMP_InitStruct fields to default values */ 00312 COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_ULTRALOWPOWER; 00313 COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1; 00314 COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT; 00315 COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE; 00316 COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED; 00317 COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE; 00318 } 00319 00320 /** 00321 * @} 00322 */ 00323 00324 /** 00325 * @} 00326 */ 00327 00328 /** 00329 * @} 00330 */ 00331 00332 #endif /* COMP1 || COMP2 */ 00333 00334 /** 00335 * @} 00336 */ 00337 00338 #endif /* USE_FULL_LL_DRIVER */