1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_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 
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32f0xx_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 STM32F0xx_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_LOWPOWER)                         \
56    || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER)                    \
57   )
58 
59 /* Note: On this STM32 series, comparator input plus parameters are            */
60 /*       the different depending on COMP instances.                           */
61 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
62   (((__COMP_INSTANCE__) == COMP1)                                              \
63     ? (                                                                        \
64           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                         \
65        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_DAC1_CH1)                    \
66       )                                                                        \
67       :                                                                        \
68       (                                                                        \
69           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                         \
70       )                                                                        \
71   )
72 
73 /* Note: On this STM32 series, comparator input minus parameters are           */
74 /*       the same on all COMP instances.                                      */
75 /*       However, comparator instance kept as macro parameter for             */
76 /*       compatibility with other STM32 families.                             */
77 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
78   (   ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                    \
79    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
80    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
81    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
82    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                      \
83    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                      \
84    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
85   )
86 
87 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__)                      \
88   (   ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE)                      \
89    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW)                       \
90    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM)                    \
91    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH)                      \
92   )
93 
94 #define IS_LL_COMP_OUTPUT_SELECTION(__OUTPUT_SELECTION__)                      \
95   (   ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_NONE)                          \
96    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_BKIN)                     \
97    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_IC1)                      \
98    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_OCCLR)                    \
99    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_IC4)                      \
100    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_OCCLR)                    \
101    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_IC1)                      \
102    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_OCCLR)                    \
103   )
104 
105 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__)                               \
106   (   ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED)                        \
107    || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED)                           \
108   )
109 
110 /**
111   * @}
112   */
113 
114 
115 /* Private function prototypes -----------------------------------------------*/
116 
117 /* Exported functions --------------------------------------------------------*/
118 /** @addtogroup COMP_LL_Exported_Functions
119   * @{
120   */
121 
122 /** @addtogroup COMP_LL_EF_Init
123   * @{
124   */
125 
126 /**
127   * @brief  De-initialize registers of the selected COMP instance
128   *         to their default reset values.
129   * @note   If comparator is locked, de-initialization by software is
130   *         not possible.
131   *         The only way to unlock the comparator is a device hardware reset.
132   * @param  COMPx COMP instance
133   * @retval An ErrorStatus enumeration value:
134   *          - SUCCESS: COMP registers are de-initialized
135   *          - ERROR: COMP registers are not de-initialized
136   */
LL_COMP_DeInit(COMP_TypeDef * COMPx)137 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
138 {
139   ErrorStatus status = SUCCESS;
140 
141   /* Check the parameters */
142   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
143 
144   /* Note: Hardware constraint (refer to description of this function):       */
145   /*       COMP instance must not be locked.                                  */
146   if(LL_COMP_IsLocked(COMPx) == 0U)
147   {
148     /* Note: Connection switch is applicable only to COMP instance COMP1,     */
149     /*       therefore is COMP2 is selected the equivalent bit is             */
150     /*       kept unmodified.                                                 */
151     if(COMPx == COMP1)
152     {
153       CLEAR_BIT(COMP->CSR,
154                 (  COMP_CSR_COMP1MODE
155                  | COMP_CSR_COMP1INSEL
156                  | COMP_CSR_COMP1SW1
157                  | COMP_CSR_COMP1OUTSEL
158                  | COMP_CSR_COMP1HYST
159                  | COMP_CSR_COMP1POL
160                  | COMP_CSR_COMP1EN
161                 ) << __COMP_BITOFFSET_INSTANCE(COMPx)
162                );
163     }
164     else
165     {
166       CLEAR_BIT(COMP->CSR,
167                 (  COMP_CSR_COMP1MODE
168                  | COMP_CSR_COMP1INSEL
169                  | COMP_CSR_COMP1OUTSEL
170                  | COMP_CSR_COMP1HYST
171                  | COMP_CSR_COMP1POL
172                  | COMP_CSR_COMP1EN
173                 ) << __COMP_BITOFFSET_INSTANCE(COMPx)
174                );
175     }
176 
177   }
178   else
179   {
180     /* Comparator instance is locked: de-initialization by software is         */
181     /* not possible.                                                           */
182     /* The only way to unlock the comparator is a device hardware reset.       */
183     status = ERROR;
184   }
185 
186   return status;
187 }
188 
189 /**
190   * @brief  Initialize some features of COMP instance.
191   * @note   This function configures features of the selected COMP instance.
192   *         Some features are also available at scope COMP common instance
193   *         (common to several COMP instances).
194   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
195   * @param  COMPx COMP instance
196   * @param  COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
197   * @retval An ErrorStatus enumeration value:
198   *          - SUCCESS: COMP registers are initialized
199   *          - ERROR: COMP registers are not initialized
200   */
LL_COMP_Init(COMP_TypeDef * COMPx,LL_COMP_InitTypeDef * COMP_InitStruct)201 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
202 {
203   ErrorStatus status = SUCCESS;
204 
205   /* Check the parameters */
206   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
207   assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
208   assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
209   assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
210   assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
211   assert_param(IS_LL_COMP_OUTPUT_SELECTION(COMP_InitStruct->OutputSelection));
212   assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
213 
214   /* Note: Hardware constraint (refer to description of this function)        */
215   /*       COMP instance must not be locked.                                  */
216   if(LL_COMP_IsLocked(COMPx) == 0U)
217   {
218     /* Configuration of comparator instance :                                 */
219     /*  - PowerMode                                                           */
220     /*  - InputPlus                                                           */
221     /*  - InputMinus                                                          */
222     /*  - InputHysteresis                                                     */
223     /*  - OutputSelection                                                     */
224     /*  - OutputPolarity                                                      */
225     /* Note: Connection switch is applicable only to COMP instance COMP1,     */
226     /*       therefore is COMP2 is selected the equivalent bit is             */
227     /*       kept unmodified.                                                 */
228     if(COMPx == COMP1)
229     {
230       MODIFY_REG(COMP->CSR,
231                  (  COMP_CSR_COMP1MODE
232                   | COMP_CSR_COMP1INSEL
233                   | COMP_CSR_COMP1SW1
234                   | COMP_CSR_COMP1OUTSEL
235                   | COMP_CSR_COMP1HYST
236                   | COMP_CSR_COMP1POL
237                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
238                 ,
239                  (  COMP_InitStruct->PowerMode
240                   | COMP_InitStruct->InputPlus
241                   | COMP_InitStruct->InputMinus
242                   | COMP_InitStruct->InputHysteresis
243                   | COMP_InitStruct->OutputSelection
244                   | COMP_InitStruct->OutputPolarity
245                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
246                 );
247     }
248     else
249     {
250       MODIFY_REG(COMP->CSR,
251                  (  COMP_CSR_COMP1MODE
252                   | COMP_CSR_COMP1INSEL
253                   | COMP_CSR_COMP1OUTSEL
254                   | COMP_CSR_COMP1HYST
255                   | COMP_CSR_COMP1POL
256                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
257                 ,
258                  (  COMP_InitStruct->PowerMode
259                   | COMP_InitStruct->InputPlus
260                   | COMP_InitStruct->InputMinus
261                   | COMP_InitStruct->InputHysteresis
262                   | COMP_InitStruct->OutputSelection
263                   | COMP_InitStruct->OutputPolarity
264                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
265                 );
266     }
267 
268   }
269   else
270   {
271     /* Initialization error: COMP instance is locked.                         */
272     status = ERROR;
273   }
274 
275   return status;
276 }
277 
278 /**
279   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
280   * @param COMP_InitStruct pointer to a @ref LL_COMP_InitTypeDef structure
281   *                         whose fields will be set to default values.
282   * @retval None
283   */
LL_COMP_StructInit(LL_COMP_InitTypeDef * COMP_InitStruct)284 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
285 {
286   /* Set COMP_InitStruct fields to default values */
287   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_ULTRALOWPOWER;
288   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
289   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
290   COMP_InitStruct->InputHysteresis      = LL_COMP_HYSTERESIS_NONE;
291   COMP_InitStruct->OutputSelection      = LL_COMP_OUTPUT_NONE;
292   COMP_InitStruct->OutputPolarity       = LL_COMP_OUTPUTPOL_NONINVERTED;
293 }
294 
295 /**
296   * @}
297   */
298 
299 /**
300   * @}
301   */
302 
303 /**
304   * @}
305   */
306 
307 #endif /* COMP1 || COMP2 */
308 
309 /**
310   * @}
311   */
312 
313 #endif /* USE_FULL_LL_DRIVER */
314 
315