1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_ll_comp.c
4   * @author  MCD Application Team
5   * @brief   COMP LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2017 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32l4xx_ll_comp.h"
23 
24 #ifdef  USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif /* USE_FULL_ASSERT */
29 
30 /** @addtogroup STM32L4xx_LL_Driver
31   * @{
32   */
33 
34 #if defined (COMP1) || defined (COMP2)
35 
36 /** @addtogroup COMP_LL COMP
37   * @{
38   */
39 
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
44 
45 /** @addtogroup COMP_LL_Private_Macros
46   * @{
47   */
48 
49 /* Check of parameters for configuration of COMP hierarchical scope:          */
50 /* COMP instance.                                                             */
51 
52 #define IS_LL_COMP_POWER_MODE(__POWER_MODE__)                                  \
53   (((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED)                           \
54    || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED)                      \
55    || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER)                    \
56   )
57 
58 /* Note: On this STM32 series, comparator input plus parameters are           */
59 /*       the same on all COMP instances.                                      */
60 /*       However, comparator instance kept as macro parameter for             */
61 /*       compatibility with other STM32 series.                               */
62 #if defined(COMP_CSR_INPSEL_1)
63 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
64   (((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                                \
65    || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2)                             \
66    || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3)                             \
67   )
68 #else
69 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
70   (((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                                \
71    || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2)                             \
72   )
73 #endif /* COMP_CSR_INPSEL_1 */
74 
75 /* Note: On this STM32 series, comparator input minus parameters are          */
76 /*       the same on all COMP instances.                                      */
77 /*       However, comparator instance kept as macro parameter for             */
78 /*       compatibility with other STM32 series.                               */
79 #if defined(COMP_CSR_INMESEL_1)
80 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
81   (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                       \
82    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
83    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
84    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
85    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                      \
86    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                      \
87    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
88    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2)                           \
89    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3)                           \
90    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO4)                           \
91    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO5)                           \
92   )
93 #else
94 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
95   (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                       \
96    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
97    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
98    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
99    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                      \
100    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                      \
101    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
102    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2)                           \
103   )
104 #endif /* COMP_CSR_INMESEL_1 */
105 
106 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__)                      \
107   (((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE)                         \
108    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW)                       \
109    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM)                    \
110    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH)                      \
111   )
112 
113 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__)                               \
114   (((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED)                           \
115    || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED)                           \
116   )
117 
118 #if defined(COMP2)
119 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__)       \
120   (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE)                                  \
121    || (((__COMP_INSTANCE__) == COMP1)                                                     \
122        ? (                                                                                    \
123            ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1)             \
124            || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1)             \
125            || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1)             \
126          )                                                                                    \
127        :                                                                                    \
128        (                                                                                    \
129            ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2)             \
130            || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2)             \
131            || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1_COMP2)            \
132        )                                                                                    \
133       )                                                                                        \
134   )
135 #else
136 #if defined(TIM3)
137 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__)       \
138   (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE)                                  \
139    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1)                \
140    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1)                \
141    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1)                \
142   )
143 #else
144 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__)       \
145   (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE)                                  \
146    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1)                \
147    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1)                \
148   )
149 #endif /* TIM3 */
150 #endif /* COMP2 */
151 /**
152   * @}
153   */
154 
155 
156 /* Private function prototypes -----------------------------------------------*/
157 
158 /* Exported functions --------------------------------------------------------*/
159 /** @addtogroup COMP_LL_Exported_Functions
160   * @{
161   */
162 
163 /** @addtogroup COMP_LL_EF_Init
164   * @{
165   */
166 
167 /**
168   * @brief  De-initialize registers of the selected COMP instance
169   *         to their default reset values.
170   * @note   If comparator is locked, de-initialization by software is
171   *         not possible.
172   *         The only way to unlock the comparator is a device hardware reset.
173   * @param  COMPx COMP instance
174   * @retval An ErrorStatus enumeration value:
175   *          - SUCCESS: COMP registers are de-initialized
176   *          - ERROR: COMP registers are not de-initialized
177   */
LL_COMP_DeInit(COMP_TypeDef * COMPx)178 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
179 {
180   ErrorStatus status = SUCCESS;
181 
182   /* Check the parameters */
183   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
184 
185   /* Note: Hardware constraint (refer to description of this function):       */
186   /*       COMP instance must not be locked.                                  */
187   if (LL_COMP_IsLocked(COMPx) == 0UL)
188   {
189     LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL);
190 
191   }
192   else
193   {
194     /* Comparator instance is locked: de-initialization by software is         */
195     /* not possible.                                                           */
196     /* The only way to unlock the comparator is a device hardware reset.       */
197     status = ERROR;
198   }
199 
200   return status;
201 }
202 
203 /**
204   * @brief  Initialize some features of COMP instance.
205   * @note   This function configures features of the selected COMP instance.
206   *         Some features are also available at scope COMP common instance
207   *         (common to several COMP instances).
208   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
209   * @param  COMPx COMP instance
210   * @param  COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
211   * @retval An ErrorStatus enumeration value:
212   *          - SUCCESS: COMP registers are initialized
213   *          - ERROR: COMP registers are not initialized
214   */
LL_COMP_Init(COMP_TypeDef * COMPx,const LL_COMP_InitTypeDef * COMP_InitStruct)215 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, const LL_COMP_InitTypeDef *COMP_InitStruct)
216 {
217   ErrorStatus status = SUCCESS;
218 
219   /* Check the parameters */
220   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
221   assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
222   assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
223   assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
224   assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
225   assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
226   assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMPx, COMP_InitStruct->OutputBlankingSource));
227 
228   /* Note: Hardware constraint (refer to description of this function)        */
229   /*       COMP instance must not be locked.                                  */
230   if (LL_COMP_IsLocked(COMPx) == 0UL)
231   {
232     /* Configuration of comparator instance :                                 */
233     /*  - PowerMode                                                           */
234     /*  - InputPlus                                                           */
235     /*  - InputMinus                                                          */
236     /*  - InputHysteresis                                                     */
237     /*  - OutputPolarity                                                      */
238     /*  - OutputBlankingSource                                                */
239 #if defined(COMP_CSR_INMESEL_1)
240     MODIFY_REG(COMPx->CSR,
241                COMP_CSR_PWRMODE
242                | COMP_CSR_INPSEL
243                | COMP_CSR_SCALEN
244                | COMP_CSR_BRGEN
245                | COMP_CSR_INMESEL
246                | COMP_CSR_INMSEL
247                | COMP_CSR_HYST
248                | COMP_CSR_POLARITY
249                | COMP_CSR_BLANKING
250                ,
251                COMP_InitStruct->PowerMode
252                | COMP_InitStruct->InputPlus
253                | COMP_InitStruct->InputMinus
254                | COMP_InitStruct->InputHysteresis
255                | COMP_InitStruct->OutputPolarity
256                | COMP_InitStruct->OutputBlankingSource
257               );
258 #else
259     MODIFY_REG(COMPx->CSR,
260                COMP_CSR_PWRMODE
261                | COMP_CSR_INPSEL
262                | COMP_CSR_SCALEN
263                | COMP_CSR_BRGEN
264                | COMP_CSR_INMSEL
265                | COMP_CSR_HYST
266                | COMP_CSR_POLARITY
267                | COMP_CSR_BLANKING
268                ,
269                COMP_InitStruct->PowerMode
270                | COMP_InitStruct->InputPlus
271                | COMP_InitStruct->InputMinus
272                | COMP_InitStruct->InputHysteresis
273                | COMP_InitStruct->OutputPolarity
274                | COMP_InitStruct->OutputBlankingSource
275               );
276 #endif /* COMP_CSR_INMESEL_1 */
277 
278   }
279   else
280   {
281     /* Initialization error: COMP instance is locked.                         */
282     status = ERROR;
283   }
284 
285   return status;
286 }
287 
288 /**
289   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
290   * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
291   *                        whose fields will be set to default values.
292   * @retval None
293   */
LL_COMP_StructInit(LL_COMP_InitTypeDef * COMP_InitStruct)294 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
295 {
296   /* Set COMP_InitStruct fields to default values */
297   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_ULTRALOWPOWER;
298   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
299   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
300   COMP_InitStruct->InputHysteresis      = LL_COMP_HYSTERESIS_NONE;
301   COMP_InitStruct->OutputPolarity       = LL_COMP_OUTPUTPOL_NONINVERTED;
302   COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
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