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