1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_ll_comp.c
4   * @author  MCD Application Team
5   * @brief   COMP LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2016 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 "stm32l0xx_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
28 
29 /** @addtogroup STM32L0xx_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_MEDIUMSPEED)                      \
53    || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER)                    \
54   )
55 
56 #if defined (STM32L011xx) || defined (STM32L021xx)
57 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
58   (((__COMP_INSTANCE__) == COMP1)                                              \
59     ? (                                                                        \
60        (__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1                              \
61       )                                                                        \
62       :                                                                        \
63       (                                                                        \
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        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO4)                         \
68        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5)                         \
69        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO6)                         \
70       )                                                                        \
71   )
72 #else
73 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
74   (((__COMP_INSTANCE__) == COMP1)                                              \
75     ? (                                                                        \
76        (__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1                              \
77       )                                                                        \
78       :                                                                        \
79       (                                                                        \
80           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                         \
81        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2)                         \
82        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3)                         \
83        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO4)                         \
84        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5)                         \
85       )                                                                        \
86   )
87 #endif
88 
89 /* Note: On this STM32 series, comparator input minus parameters are           */
90 /*       the different depending on COMP instances.                           */
91 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
92   (((__COMP_INSTANCE__) == COMP1)                                              \
93     ? (                                                                        \
94           ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                   \
95        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                  \
96        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                  \
97        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                       \
98       )                                                                        \
99       :                                                                        \
100       (                                                                        \
101           ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                \
102        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                \
103        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                \
104        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                   \
105        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                  \
106        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                  \
107        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                       \
108        || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2)                       \
109       )                                                                        \
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 /**
118   * @}
119   */
120 
121 
122 /* Private function prototypes -----------------------------------------------*/
123 
124 /* Exported functions --------------------------------------------------------*/
125 /** @addtogroup COMP_LL_Exported_Functions
126   * @{
127   */
128 
129 /** @addtogroup COMP_LL_EF_Init
130   * @{
131   */
132 
133 /**
134   * @brief  De-initialize registers of the selected COMP instance
135   *         to their default reset values.
136   * @note   If comparator is locked, de-initialization by software is
137   *         not possible.
138   *         The only way to unlock the comparator is a device hardware reset.
139   * @param  COMPx COMP instance
140   * @retval An ErrorStatus enumeration value:
141   *          - SUCCESS: COMP registers are de-initialized
142   *          - ERROR: COMP registers are not de-initialized
143   */
LL_COMP_DeInit(COMP_TypeDef * COMPx)144 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
145 {
146   ErrorStatus status = SUCCESS;
147 
148   /* Check the parameters */
149   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
150 
151   /* Note: Hardware constraint (refer to description of this function):       */
152   /*       COMP instance must not be locked.                                  */
153   if(LL_COMP_IsLocked(COMPx) == 0U)
154   {
155     if(COMPx == COMP1)
156     {
157       CLEAR_BIT(COMPx->CSR,
158                 (  COMP_CSR_COMP1EN
159                  | COMP_CSR_COMP1INNSEL
160                  | COMP_CSR_COMP1WM
161                  | COMP_CSR_COMP1LPTIM1IN1
162                  | COMP_CSR_COMP1POLARITY
163                  | COMP_CSR_COMP1LOCK
164                 )
165                );
166     }
167     else
168     {
169       CLEAR_BIT(COMPx->CSR,
170                 (  COMP_CSR_COMP2EN
171                  | COMP_CSR_COMP2SPEED
172                  | COMP_CSR_COMP2INNSEL
173                  | COMP_CSR_COMP2INPSEL
174                  | COMP_CSR_COMP2LPTIM1IN2
175                  | COMP_CSR_COMP2LPTIM1IN1
176                  | COMP_CSR_COMP2POLARITY
177                  | COMP_CSR_COMP2LOCK
178                 )
179                );
180     }
181 
182   }
183   else
184   {
185     /* Comparator instance is locked: de-initialization by software is         */
186     /* not possible.                                                           */
187     /* The only way to unlock the comparator is a device hardware reset.       */
188     status = ERROR;
189   }
190 
191   return status;
192 }
193 
194 /**
195   * @brief  Initialize some features of COMP instance.
196   * @note   This function configures features of the selected COMP instance.
197   *         Some features are also available at scope COMP common instance
198   *         (common to several COMP instances).
199   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
200   * @param  COMPx COMP instance
201   * @param  COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
202   * @retval An ErrorStatus enumeration value:
203   *          - SUCCESS: COMP registers are initialized
204   *          - ERROR: COMP registers are not initialized
205   */
LL_COMP_Init(COMP_TypeDef * COMPx,LL_COMP_InitTypeDef * COMP_InitStruct)206 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
207 {
208   ErrorStatus status = SUCCESS;
209 
210   /* Check the parameters */
211   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
212   if(COMPx == COMP2)
213   {
214     assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
215     assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
216   }
217   assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
218   assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
219 
220   /* Note: Hardware constraint (refer to description of this function)        */
221   /*       COMP instance must not be locked.                                  */
222   if(LL_COMP_IsLocked(COMPx) == 0U)
223   {
224     /* Configuration of comparator instance :                                 */
225     /*  - PowerMode                                                           */
226     /*  - InputPlus                                                           */
227     /*  - InputMinus                                                          */
228     /*  - OutputPolarity                                                      */
229     /* Note: Connection switch is applicable only to COMP instance COMP1,     */
230     /*       therefore is COMP2 is selected the equivalent bit is             */
231     /*       kept unmodified.                                                 */
232     if(COMPx == COMP1)
233     {
234       MODIFY_REG(COMPx->CSR,
235                  ( COMP_CSR_COMP1INNSEL
236                   | COMP_CSR_COMP1POLARITY
237                  )
238                 ,
239                  (  COMP_InitStruct->InputMinus
240                   | COMP_InitStruct->OutputPolarity
241                  )
242                 );
243     }
244     else
245     {
246       MODIFY_REG(COMPx->CSR,
247                  (  COMP_CSR_COMP2SPEED
248                   | COMP_CSR_COMP2INPSEL
249                   | COMP_CSR_COMP2INNSEL
250                   | COMP_CSR_COMP2POLARITY
251                  )
252                 ,
253                  (  COMP_InitStruct->PowerMode
254                   | COMP_InitStruct->InputPlus
255                   | COMP_InitStruct->InputMinus
256                   | COMP_InitStruct->OutputPolarity
257                  )
258                 );
259     }
260 
261   }
262   else
263   {
264     /* Initialization error: COMP instance is locked.                         */
265     status = ERROR;
266   }
267 
268   return status;
269 }
270 
271 /**
272   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
273   * @param COMP_InitStruct: pointer to a @ref LL_COMP_InitTypeDef structure
274   *                         whose fields will be set to default values.
275   * @retval None
276   */
LL_COMP_StructInit(LL_COMP_InitTypeDef * COMP_InitStruct)277 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
278 {
279   /* Set COMP_InitStruct fields to default values */
280   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_MEDIUMSPEED;
281   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
282   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
283   COMP_InitStruct->OutputPolarity       = LL_COMP_OUTPUTPOL_NONINVERTED;
284 }
285 
286 /**
287   * @}
288   */
289 
290 /**
291   * @}
292   */
293 
294 /**
295   * @}
296   */
297 
298 #endif /* COMP1 || COMP2 */
299 
300 /**
301   * @}
302   */
303 
304 #endif /* USE_FULL_LL_DRIVER */
305 
306