1 /**
2   ******************************************************************************
3   * @file    stm32l1xx_ll_comp.c
4   * @author  MCD Application Team
5   * @brief   COMP LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
12   * This software component is licensed by ST under BSD 3-Clause license,
13   * the "License"; You may not use this file except in compliance with the
14   * License. You may obtain a copy of the License at:
15   *                        opensource.org/licenses/BSD-3-Clause
16   *
17   ******************************************************************************
18   */
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32l1xx_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
29 
30 /** @addtogroup STM32L1xx_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_MEDIUMSPEED)                      \
54    || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER)                    \
55   )
56 
57 /* Note: On this STM32 serie, comparator input plus parameters are            */
58 /*       the different depending on COMP instances.                           */
59 #if defined(RI_ASCR1_CH_31)
60 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
61   (((__COMP_INSTANCE__) == COMP1)                                              \
62     ? (                                                                        \
63           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5)                         \
64        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO6)                         \
65        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO7)                         \
66        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO8)                         \
67        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO9)                         \
68        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO10)                        \
69        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO11)                        \
70        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO12)                        \
71        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO13)                        \
72        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO14)                        \
73        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO15)                        \
74        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO16)                        \
75        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO17)                        \
76        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO18)                        \
77        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO19)                        \
78        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO20)                        \
79        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO21)                        \
80        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO22)                        \
81        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO23)                        \
82        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO24)                        \
83        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO25)                        \
84        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO26)                        \
85        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO27)                        \
86        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO28)                        \
87        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO29)                        \
88        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO30)                        \
89        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO31)                        \
90        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO32)                        \
91        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO33)                        \
92        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_NONE)                        \
93       )                                                                        \
94       :                                                                        \
95       (                                                                        \
96           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                         \
97        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2)                         \
98        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3)                         \
99        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO4)                         \
100        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_NONE)                        \
101       )                                                                        \
102   )
103 #else
104 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
105   (((__COMP_INSTANCE__) == COMP1)                                              \
106     ? (                                                                        \
107           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5)                         \
108        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO6)                         \
109        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO7)                         \
110        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO8)                         \
111        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO9)                         \
112        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO10)                        \
113        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO11)                        \
114        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO12)                        \
115        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO13)                        \
116        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO14)                        \
117        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO15)                        \
118        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO16)                        \
119        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO17)                        \
120        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO18)                        \
121        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO19)                        \
122        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO20)                        \
123        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO21)                        \
124        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO22)                        \
125        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO23)                        \
126        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO24)                        \
127        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO25)                        \
128        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO26)                        \
129        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO27)                        \
130        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO28)                        \
131       )                                                                        \
132       :                                                                        \
133       (                                                                        \
134           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                         \
135        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2)                         \
136       )                                                                        \
137   )
138 #endif
139 
140 /* Note: On this STM32 serie, comparator input minus parameters are           */
141 /*       the different depending on COMP instances.                           */
142 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
143   (((__COMP_INSTANCE__) == COMP1)                                              \
144     ? (                                                                        \
145           ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                   \
146       )                                                                        \
147       :                                                                        \
148       (                                                                        \
149           ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                \
150        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                \
151        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                \
152        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                   \
153        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                  \
154        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                  \
155        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                       \
156       )                                                                        \
157   )
158 
159 #define IS_LL_COMP_OUTPUT_SELECTION(__OUTPUT_SELECTION__)                      \
160   (   ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_NONE)                          \
161    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_IC4)                      \
162    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_OCREFCLR)                 \
163    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_IC4)                      \
164    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_OCREFCLR)                 \
165    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM4_IC4)                      \
166    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM4_OCREFCLR)                 \
167    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM10_IC1)                     \
168   )
169 
170 /**
171   * @}
172   */
173 
174 
175 /* Private function prototypes -----------------------------------------------*/
176 
177 /* Exported functions --------------------------------------------------------*/
178 /** @addtogroup COMP_LL_Exported_Functions
179   * @{
180   */
181 
182 /** @addtogroup COMP_LL_EF_Init
183   * @{
184   */
185 
186 /**
187   * @brief  De-initialize registers of the selected COMP instance
188   *         to their default reset values.
189   * @note   If comparator is locked, de-initialization by software is
190   *         not possible.
191   *         The only way to unlock the comparator is a device hardware reset.
192   * @param  COMPx COMP instance
193   * @retval An ErrorStatus enumeration value:
194   *          - SUCCESS: COMP registers are de-initialized
195   *          - ERROR: COMP registers are not de-initialized
196   */
LL_COMP_DeInit(COMP_TypeDef * COMPx)197 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
198 {
199   ErrorStatus status = SUCCESS;
200 
201   /* Check the parameters */
202   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
203 
204     /* Note: On this STM32 serie, only COMP instance COMP2 has                */
205     /*       features settables: power mode, input minus selection            */
206     /*       and output selection.                                            */
207     /* Note: On this STM32 serie, setting COMP instance COMP2 input minus     */
208     /*       is enabling the comparator.                                      */
209     /*       Reset COMP2 input minus also disable the comparator.             */
210     /* Note: In case of de-initialization of COMP instance COMP1:             */
211     /*       Switch COMP_CSR_SW1 is not modified because can be used          */
212     /*       to connect OPAMP3 to ADC.                                        */
213     /*       Switches RI_ASCR1_VCOMP, RI_ASCR1_SCM are reset: let routing     */
214     /*       interface under control of ADC.                                  */
215     if(COMPx == COMP1)
216     {
217       CLEAR_BIT(COMP->CSR,
218                 (  COMP_CSR_CMP1EN
219                  | COMP_CSR_10KPU
220                  | COMP_CSR_400KPU
221                  | COMP_CSR_10KPD
222                  | COMP_CSR_400KPD
223                 )
224                );
225     }
226     else
227     {
228       CLEAR_BIT(COMP->CSR,
229                 (  COMP_CSR_SPEED
230                  | COMP_CSR_INSEL
231                  | COMP_CSR_OUTSEL
232                 )
233                );
234     }
235 
236     /* Set comparator input plus */
237     LL_COMP_SetInputPlus(COMPx, LL_COMP_INPUT_PLUS_NONE);
238 
239   return status;
240 }
241 
242 /**
243   * @brief  Initialize some features of COMP instance.
244   * @note   This function configures features of the selected COMP instance.
245   *         Some features are also available at scope COMP common instance
246   *         (common to several COMP instances).
247   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
248   * @param  COMPx COMP instance
249   * @param  COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
250   * @retval An ErrorStatus enumeration value:
251   *          - SUCCESS: COMP registers are initialized
252   *          - ERROR: COMP registers are not initialized
253   */
LL_COMP_Init(COMP_TypeDef * COMPx,LL_COMP_InitTypeDef * COMP_InitStruct)254 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
255 {
256   ErrorStatus status = SUCCESS;
257 
258   /* Check the parameters */
259   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
260   if(COMPx == COMP2)
261   {
262     assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
263     assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
264     assert_param(IS_LL_COMP_OUTPUT_SELECTION(COMP_InitStruct->OutputSelection));
265   }
266   assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
267 
268   /* Configuration of comparator instance :                                 */
269   /*  - PowerMode                                                           */
270   /*  - InputPlus                                                           */
271   /*  - InputMinus                                                          */
272   /*  - OutputSelection                                                     */
273   /* Note: On this STM32 serie, only COMP instance COMP2 has                */
274   /*       features settables: power mode, input minus selection            */
275   /*       and output selection.                                            */
276   /* Note: On this STM32 serie, setting COMP instance COMP2 input minus     */
277   /*       is enabling the comparator.                                      */
278   if(COMPx == COMP2)
279   {
280     MODIFY_REG(COMP->CSR,
281                  COMP_CSR_SPEED
282                | COMP_CSR_INSEL
283                | COMP_CSR_OUTSEL
284               ,
285                  COMP_InitStruct->PowerMode
286                | COMP_InitStruct->InputMinus
287                | COMP_InitStruct->OutputSelection
288               );
289 
290     /* Set comparator input plus */
291     LL_COMP_SetInputPlus(COMPx, COMP_InitStruct->InputPlus);
292   }
293   else /* COMPx == COMP1 */
294   {
295     /* If window mode is enabled, COMP1 input plus is not used and therefore
296        not modified */
297     if(READ_BIT(COMP12_COMMON->CSR, COMP_CSR_WNDWE) == COMP_CSR_WNDWE)
298     {
299       /* Set comparator input plus */
300       LL_COMP_SetInputPlus(COMPx, COMP_InitStruct->InputPlus);
301     }
302   }
303 
304   return status;
305 }
306 
307 /**
308   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
309   * @param COMP_InitStruct: pointer to a @ref LL_COMP_InitTypeDef structure
310   *                         whose fields will be set to default values.
311   * @retval None
312   */
LL_COMP_StructInit(LL_COMP_InitTypeDef * COMP_InitStruct)313 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
314 {
315   /* Set COMP_InitStruct fields to default values */
316   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_ULTRALOWPOWER;
317   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
318   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
319   COMP_InitStruct->OutputSelection      = LL_COMP_OUTPUT_NONE;
320 }
321 
322 /**
323   * @}
324   */
325 
326 /**
327   * @}
328   */
329 
330 /**
331   * @}
332   */
333 
334 #endif /* COMP1 || COMP2 */
335 
336 /**
337   * @}
338   */
339 
340 #endif /* USE_FULL_LL_DRIVER */
341 
342 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
343