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