xref: /btstack/port/stm32-l451-miromico-sx1280/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_comp.c (revision 2fd737d36a1de5d778cacc671d4b4d8c4f3fed82)
1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_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 "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
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 serie, 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 families.                             */
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
74 
75 /* Note: On this STM32 serie, 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 families.                             */
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
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     ? (                                                                                        \
122        (1UL)                                                                                   \
123       )                                                                                        \
124       :                                                                                        \
125       (((__COMP_INSTANCE__) == COMP1)                                                          \
126         ? (                                                                                    \
127               ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1)             \
128            || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1)             \
129            || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1)             \
130           )                                                                                    \
131           :                                                                                    \
132           (                                                                                    \
133               ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2)             \
134            || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2)             \
135            || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1_COMP2)            \
136           )                                                                                    \
137       )                                                                                        \
138   )
139 #else
140 #if defined(TIM3)
141 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__)       \
142   (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE)                                  \
143     ? (                                                                                        \
144        (1UL)                                                                                   \
145       )                                                                                        \
146       :                                                                                        \
147       (                                                                                        \
148            ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1)                \
149         || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1)                \
150         || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1)                \
151       )                                                                                        \
152   )
153 #else
154 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__)       \
155   (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE)                                  \
156     ? (                                                                                        \
157        (1UL)                                                                                   \
158       )                                                                                        \
159       :                                                                                        \
160       (                                                                                        \
161            ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1)                \
162         || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1)                \
163       )                                                                                        \
164   )
165 #endif /* TIM3 */
166 #endif /* COMP2 */
167 /**
168   * @}
169   */
170 
171 
172 /* Private function prototypes -----------------------------------------------*/
173 
174 /* Exported functions --------------------------------------------------------*/
175 /** @addtogroup COMP_LL_Exported_Functions
176   * @{
177   */
178 
179 /** @addtogroup COMP_LL_EF_Init
180   * @{
181   */
182 
183 /**
184   * @brief  De-initialize registers of the selected COMP instance
185   *         to their default reset values.
186   * @note   If comparator is locked, de-initialization by software is
187   *         not possible.
188   *         The only way to unlock the comparator is a device hardware reset.
189   * @param  COMPx COMP instance
190   * @retval An ErrorStatus enumeration value:
191   *          - SUCCESS: COMP registers are de-initialized
192   *          - ERROR: COMP registers are not de-initialized
193   */
LL_COMP_DeInit(COMP_TypeDef * COMPx)194 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
195 {
196   ErrorStatus status = SUCCESS;
197 
198   /* Check the parameters */
199   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
200 
201   /* Note: Hardware constraint (refer to description of this function):       */
202   /*       COMP instance must not be locked.                                  */
203   if (LL_COMP_IsLocked(COMPx) == 0UL)
204   {
205     LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL);
206 
207   }
208   else
209   {
210     /* Comparator instance is locked: de-initialization by software is         */
211     /* not possible.                                                           */
212     /* The only way to unlock the comparator is a device hardware reset.       */
213     status = ERROR;
214   }
215 
216   return status;
217 }
218 
219 /**
220   * @brief  Initialize some features of COMP instance.
221   * @note   This function configures features of the selected COMP instance.
222   *         Some features are also available at scope COMP common instance
223   *         (common to several COMP instances).
224   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
225   * @param  COMPx COMP instance
226   * @param  COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
227   * @retval An ErrorStatus enumeration value:
228   *          - SUCCESS: COMP registers are initialized
229   *          - ERROR: COMP registers are not initialized
230   */
LL_COMP_Init(COMP_TypeDef * COMPx,LL_COMP_InitTypeDef * COMP_InitStruct)231 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
232 {
233   ErrorStatus status = SUCCESS;
234 
235   /* Check the parameters */
236   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
237   assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
238   assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
239   assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
240   assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
241   assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
242   assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMPx, COMP_InitStruct->OutputBlankingSource));
243 
244   /* Note: Hardware constraint (refer to description of this function)        */
245   /*       COMP instance must not be locked.                                  */
246   if (LL_COMP_IsLocked(COMPx) == 0UL)
247   {
248     /* Configuration of comparator instance :                                 */
249     /*  - PowerMode                                                           */
250     /*  - InputPlus                                                           */
251     /*  - InputMinus                                                          */
252     /*  - InputHysteresis                                                     */
253     /*  - OutputPolarity                                                      */
254     /*  - OutputBlankingSource                                                */
255 #if defined(COMP_CSR_INMESEL_1)
256     MODIFY_REG(COMPx->CSR,
257                COMP_CSR_PWRMODE
258                | COMP_CSR_INPSEL
259                | COMP_CSR_SCALEN
260                | COMP_CSR_BRGEN
261                | COMP_CSR_INMESEL
262                | COMP_CSR_INMSEL
263                | COMP_CSR_HYST
264                | COMP_CSR_POLARITY
265                | COMP_CSR_BLANKING
266                ,
267                COMP_InitStruct->PowerMode
268                | COMP_InitStruct->InputPlus
269                | COMP_InitStruct->InputMinus
270                | COMP_InitStruct->InputHysteresis
271                | COMP_InitStruct->OutputPolarity
272                | COMP_InitStruct->OutputBlankingSource
273               );
274 #else
275     MODIFY_REG(COMPx->CSR,
276                COMP_CSR_PWRMODE
277                | COMP_CSR_INPSEL
278                | COMP_CSR_SCALEN
279                | COMP_CSR_BRGEN
280                | COMP_CSR_INMSEL
281                | COMP_CSR_HYST
282                | COMP_CSR_POLARITY
283                | COMP_CSR_BLANKING
284                ,
285                COMP_InitStruct->PowerMode
286                | COMP_InitStruct->InputPlus
287                | COMP_InitStruct->InputMinus
288                | COMP_InitStruct->InputHysteresis
289                | COMP_InitStruct->OutputPolarity
290                | COMP_InitStruct->OutputBlankingSource
291               );
292 #endif
293 
294   }
295   else
296   {
297     /* Initialization error: COMP instance is locked.                         */
298     status = ERROR;
299   }
300 
301   return status;
302 }
303 
304 /**
305   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
306   * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
307   *                        whose fields will be set to default values.
308   * @retval None
309   */
LL_COMP_StructInit(LL_COMP_InitTypeDef * COMP_InitStruct)310 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
311 {
312   /* Set COMP_InitStruct fields to default values */
313   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_ULTRALOWPOWER;
314   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
315   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
316   COMP_InitStruct->InputHysteresis      = LL_COMP_HYSTERESIS_NONE;
317   COMP_InitStruct->OutputPolarity       = LL_COMP_OUTPUTPOL_NONINVERTED;
318   COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_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 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
342