|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file stm32l0xx_ll_comp.c |
| 4 | + * @author MCD Application Team |
| 5 | + * @version V1.7.0 |
| 6 | + * @date 31-May-2016 |
| 7 | + * @brief COMP LL module driver |
| 8 | + ****************************************************************************** |
| 9 | + * @attention |
| 10 | + * |
| 11 | + * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
| 12 | + * |
| 13 | + * Redistribution and use in source and binary forms, with or without modification, |
| 14 | + * are permitted provided that the following conditions are met: |
| 15 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer. |
| 17 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 18 | + * this list of conditions and the following disclaimer in the documentation |
| 19 | + * and/or other materials provided with the distribution. |
| 20 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 21 | + * may be used to endorse or promote products derived from this software |
| 22 | + * without specific prior written permission. |
| 23 | + * |
| 24 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 28 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 30 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 31 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 32 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 33 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | + * |
| 35 | + ****************************************************************************** |
| 36 | + */ |
| 37 | +#if defined(USE_FULL_LL_DRIVER) |
| 38 | + |
| 39 | +/* Includes ------------------------------------------------------------------*/ |
| 40 | +#include "stm32l0xx_ll_comp.h" |
| 41 | + |
| 42 | +#ifdef USE_FULL_ASSERT |
| 43 | + #include "stm32_assert.h" |
| 44 | +#else |
| 45 | + #define assert_param(expr) ((void)0U) |
| 46 | +#endif |
| 47 | + |
| 48 | +/** @addtogroup STM32L0xx_LL_Driver |
| 49 | + * @{ |
| 50 | + */ |
| 51 | + |
| 52 | +#if defined (COMP1) || defined (COMP2) |
| 53 | + |
| 54 | +/** @addtogroup COMP_LL COMP |
| 55 | + * @{ |
| 56 | + */ |
| 57 | + |
| 58 | +/* Private types -------------------------------------------------------------*/ |
| 59 | +/* Private variables ---------------------------------------------------------*/ |
| 60 | +/* Private constants ---------------------------------------------------------*/ |
| 61 | +/* Private macros ------------------------------------------------------------*/ |
| 62 | + |
| 63 | +/** @addtogroup COMP_LL_Private_Macros |
| 64 | + * @{ |
| 65 | + */ |
| 66 | + |
| 67 | +/* Check of parameters for configuration of COMP hierarchical scope: */ |
| 68 | +/* COMP instance. */ |
| 69 | + |
| 70 | +#define IS_LL_COMP_POWER_MODE(__POWER_MODE__) \ |
| 71 | + ( ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \ |
| 72 | + || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER) \ |
| 73 | + ) |
| 74 | + |
| 75 | +#if defined (STM32L011xx) || defined (STM32L021xx) |
| 76 | +#define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \ |
| 77 | + (((__COMP_INSTANCE__) == COMP1) \ |
| 78 | + ? ( \ |
| 79 | + (__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1 \ |
| 80 | + ) \ |
| 81 | + : \ |
| 82 | + ( \ |
| 83 | + ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \ |
| 84 | + || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \ |
| 85 | + || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \ |
| 86 | + || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO4) \ |
| 87 | + || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5) \ |
| 88 | + || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO6) \ |
| 89 | + ) \ |
| 90 | + ) |
| 91 | +#else |
| 92 | +#define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \ |
| 93 | + (((__COMP_INSTANCE__) == COMP1) \ |
| 94 | + ? ( \ |
| 95 | + (__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1 \ |
| 96 | + ) \ |
| 97 | + : \ |
| 98 | + ( \ |
| 99 | + ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \ |
| 100 | + || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \ |
| 101 | + || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \ |
| 102 | + || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO4) \ |
| 103 | + || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5) \ |
| 104 | + ) \ |
| 105 | + ) |
| 106 | +#endif |
| 107 | + |
| 108 | +/* Note: On this STM32 serie, comparator input minus parameters are */ |
| 109 | +/* the different depending on COMP instances. */ |
| 110 | +#define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \ |
| 111 | + (((__COMP_INSTANCE__) == COMP1) \ |
| 112 | + ? ( \ |
| 113 | + ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \ |
| 114 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \ |
| 115 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \ |
| 116 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \ |
| 117 | + ) \ |
| 118 | + : \ |
| 119 | + ( \ |
| 120 | + ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \ |
| 121 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \ |
| 122 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \ |
| 123 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \ |
| 124 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \ |
| 125 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \ |
| 126 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \ |
| 127 | + || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \ |
| 128 | + ) \ |
| 129 | + ) |
| 130 | + |
| 131 | +#define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \ |
| 132 | + ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \ |
| 133 | + || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \ |
| 134 | + ) |
| 135 | + |
| 136 | +/** |
| 137 | + * @} |
| 138 | + */ |
| 139 | + |
| 140 | + |
| 141 | +/* Private function prototypes -----------------------------------------------*/ |
| 142 | + |
| 143 | +/* Exported functions --------------------------------------------------------*/ |
| 144 | +/** @addtogroup COMP_LL_Exported_Functions |
| 145 | + * @{ |
| 146 | + */ |
| 147 | + |
| 148 | +/** @addtogroup COMP_LL_EF_Init |
| 149 | + * @{ |
| 150 | + */ |
| 151 | + |
| 152 | +/** |
| 153 | + * @brief De-initialize registers of the selected COMP instance |
| 154 | + * to their default reset values. |
| 155 | + * @note If comparator is locked, de-initialization by software is |
| 156 | + * not possible. |
| 157 | + * The only way to unlock the comparator is a device hardware reset. |
| 158 | + * @param COMPx COMP instance |
| 159 | + * @retval An ErrorStatus enumeration value: |
| 160 | + * - SUCCESS: COMP registers are de-initialized |
| 161 | + * - ERROR: COMP registers are not de-initialized |
| 162 | + */ |
| 163 | +ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx) |
| 164 | +{ |
| 165 | + ErrorStatus status = SUCCESS; |
| 166 | + |
| 167 | + /* Check the parameters */ |
| 168 | + assert_param(IS_COMP_ALL_INSTANCE(COMPx)); |
| 169 | + |
| 170 | + /* Note: Hardware constraint (refer to description of this function): */ |
| 171 | + /* COMP instance must not be locked. */ |
| 172 | + if(LL_COMP_IsLocked(COMPx) == 0U) |
| 173 | + { |
| 174 | + if(COMPx == COMP1) |
| 175 | + { |
| 176 | + CLEAR_BIT(COMPx->CSR, |
| 177 | + ( COMP_CSR_COMP1EN |
| 178 | + | COMP_CSR_COMP1INNSEL |
| 179 | + | COMP_CSR_COMP1WM |
| 180 | + | COMP_CSR_COMP1LPTIM1IN1 |
| 181 | + | COMP_CSR_COMP1POLARITY |
| 182 | + | COMP_CSR_COMP1LOCK |
| 183 | + ) |
| 184 | + ); |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + CLEAR_BIT(COMPx->CSR, |
| 189 | + ( COMP_CSR_COMP2EN |
| 190 | + | COMP_CSR_COMP2SPEED |
| 191 | + | COMP_CSR_COMP2INNSEL |
| 192 | + | COMP_CSR_COMP2INPSEL |
| 193 | + | COMP_CSR_COMP2LPTIM1IN2 |
| 194 | + | COMP_CSR_COMP2LPTIM1IN1 |
| 195 | + | COMP_CSR_COMP2POLARITY |
| 196 | + | COMP_CSR_COMP2LOCK |
| 197 | + ) |
| 198 | + ); |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + /* Comparator instance is locked: de-initialization by software is */ |
| 205 | + /* not possible. */ |
| 206 | + /* The only way to unlock the comparator is a device hardware reset. */ |
| 207 | + status = ERROR; |
| 208 | + } |
| 209 | + |
| 210 | + return status; |
| 211 | +} |
| 212 | + |
| 213 | +/** |
| 214 | + * @brief Initialize some features of COMP instance. |
| 215 | + * @note This function configures features of the selected COMP instance. |
| 216 | + * Some features are also available at scope COMP common instance |
| 217 | + * (common to several COMP instances). |
| 218 | + * Refer to functions having argument "COMPxy_COMMON" as parameter. |
| 219 | + * @param COMPx COMP instance |
| 220 | + * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure |
| 221 | + * @retval An ErrorStatus enumeration value: |
| 222 | + * - SUCCESS: COMP registers are initialized |
| 223 | + * - ERROR: COMP registers are not initialized |
| 224 | + */ |
| 225 | +ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct) |
| 226 | +{ |
| 227 | + ErrorStatus status = SUCCESS; |
| 228 | + |
| 229 | + /* Check the parameters */ |
| 230 | + assert_param(IS_COMP_ALL_INSTANCE(COMPx)); |
| 231 | + if(COMPx == COMP2) |
| 232 | + { |
| 233 | + assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode)); |
| 234 | + assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus)); |
| 235 | + } |
| 236 | + assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus)); |
| 237 | + assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity)); |
| 238 | + |
| 239 | + /* Note: Hardware constraint (refer to description of this function) */ |
| 240 | + /* COMP instance must not be locked. */ |
| 241 | + if(LL_COMP_IsLocked(COMPx) == 0U) |
| 242 | + { |
| 243 | + /* Configuration of comparator instance : */ |
| 244 | + /* - PowerMode */ |
| 245 | + /* - InputPlus */ |
| 246 | + /* - InputMinus */ |
| 247 | + /* - OutputPolarity */ |
| 248 | + /* Note: Connection switch is applicable only to COMP instance COMP1, */ |
| 249 | + /* therefore is COMP2 is selected the equivalent bit is */ |
| 250 | + /* kept unmodified. */ |
| 251 | + if(COMPx == COMP1) |
| 252 | + { |
| 253 | + MODIFY_REG(COMPx->CSR, |
| 254 | + ( COMP_CSR_COMP1INNSEL |
| 255 | + | COMP_CSR_COMP1POLARITY |
| 256 | + ) |
| 257 | + , |
| 258 | + ( COMP_InitStruct->InputMinus |
| 259 | + | COMP_InitStruct->OutputPolarity |
| 260 | + ) |
| 261 | + ); |
| 262 | + } |
| 263 | + else |
| 264 | + { |
| 265 | + MODIFY_REG(COMPx->CSR, |
| 266 | + ( COMP_CSR_COMP2SPEED |
| 267 | + | COMP_CSR_COMP2INPSEL |
| 268 | + | COMP_CSR_COMP2INNSEL |
| 269 | + | COMP_CSR_COMP2POLARITY |
| 270 | + ) |
| 271 | + , |
| 272 | + ( COMP_InitStruct->PowerMode |
| 273 | + | COMP_InitStruct->InputPlus |
| 274 | + | COMP_InitStruct->InputMinus |
| 275 | + | COMP_InitStruct->OutputPolarity |
| 276 | + ) |
| 277 | + ); |
| 278 | + } |
| 279 | + |
| 280 | + } |
| 281 | + else |
| 282 | + { |
| 283 | + /* Initialization error: COMP instance is locked. */ |
| 284 | + status = ERROR; |
| 285 | + } |
| 286 | + |
| 287 | + return status; |
| 288 | +} |
| 289 | + |
| 290 | +/** |
| 291 | + * @brief Set each @ref LL_COMP_InitTypeDef field to default value. |
| 292 | + * @param COMP_InitStruct: pointer to a @ref LL_COMP_InitTypeDef structure |
| 293 | + * whose fields will be set to default values. |
| 294 | + * @retval None |
| 295 | + */ |
| 296 | +void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct) |
| 297 | +{ |
| 298 | + /* Set COMP_InitStruct fields to default values */ |
| 299 | + COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_MEDIUMSPEED; |
| 300 | + COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1; |
| 301 | + COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT; |
| 302 | + COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED; |
| 303 | +} |
| 304 | + |
| 305 | +/** |
| 306 | + * @} |
| 307 | + */ |
| 308 | + |
| 309 | +/** |
| 310 | + * @} |
| 311 | + */ |
| 312 | + |
| 313 | +/** |
| 314 | + * @} |
| 315 | + */ |
| 316 | + |
| 317 | +#endif /* COMP1 || COMP2 */ |
| 318 | + |
| 319 | +/** |
| 320 | + * @} |
| 321 | + */ |
| 322 | + |
| 323 | +#endif /* USE_FULL_LL_DRIVER */ |
| 324 | + |
| 325 | +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
0 commit comments