1 /**
2   ******************************************************************************
3   * @file    stm32g0xx_ll_comp.c
4   * @author  MCD Application Team
5   * @brief   COMP LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2018 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 "stm32g0xx_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 /* USE_FULL_ASSERT */
28 
29 /** @addtogroup STM32G0xx_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_HIGHSPEED)                        \
53    || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED)                      \
54   )
55 
56 /* Note: On this STM32 series, comparator input plus parameters are           */
57 /*       the same on all COMP instances.                                      */
58 /*       However, comparator instance kept as macro parameter for             */
59 /*       compatibility with other STM32 families.                             */
60 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
61   (   ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                             \
62    || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2)                             \
63    || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3)                             \
64   )
65 
66 /* Note: On this STM32 series, comparator input minus parameters are          */
67 /*       the same on all COMP instances.                                      */
68 /*       However, comparator instance kept as macro parameter for             */
69 /*       compatibility with other STM32 families.                             */
70 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
71   (   ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                    \
72    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
73    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
74    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
75    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                      \
76    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                      \
77    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
78    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2)                           \
79    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3)                           \
80   )
81 
82 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__)                      \
83   (   ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE)                      \
84    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW)                       \
85    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM)                    \
86    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH)                      \
87   )
88 
89 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__)                               \
90   (   ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED)                        \
91    || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED)                           \
92   )
93 
94 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__OUTPUT_BLANKING_SOURCE__)          \
95   (   ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE)               \
96    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC4)           \
97    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5)           \
98    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3)           \
99    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3)           \
100    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC2)          \
101   )
102 
103 /**
104   * @}
105   */
106 
107 
108 /* Private function prototypes -----------------------------------------------*/
109 
110 /* Exported functions --------------------------------------------------------*/
111 /** @addtogroup COMP_LL_Exported_Functions
112   * @{
113   */
114 
115 /** @addtogroup COMP_LL_EF_Init
116   * @{
117   */
118 
119 /**
120   * @brief  De-initialize registers of the selected COMP instance
121   *         to their default reset values.
122   * @note   If comparator is locked, de-initialization by software is
123   *         not possible.
124   *         The only way to unlock the comparator is a device hardware reset.
125   * @param  COMPx COMP instance
126   * @retval An ErrorStatus enumeration value:
127   *          - SUCCESS: COMP registers are de-initialized
128   *          - ERROR: COMP registers are not de-initialized
129   */
LL_COMP_DeInit(COMP_TypeDef * COMPx)130 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
131 {
132   ErrorStatus status = SUCCESS;
133 
134   /* Check the parameters */
135   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
136 
137   /* Note: Hardware constraint (refer to description of this function):       */
138   /*       COMP instance must not be locked.                                  */
139   if (LL_COMP_IsLocked(COMPx) == 0UL)
140   {
141     LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL);
142 
143   }
144   else
145   {
146     /* Comparator instance is locked: de-initialization by software is         */
147     /* not possible.                                                           */
148     /* The only way to unlock the comparator is a device hardware reset.       */
149     status = ERROR;
150   }
151 
152   return status;
153 }
154 
155 /**
156   * @brief  Initialize some features of COMP instance.
157   * @note   This function configures features of the selected COMP instance.
158   *         Some features are also available at scope COMP common instance
159   *         (common to several COMP instances).
160   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
161   * @param  COMPx COMP instance
162   * @param  COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
163   * @retval An ErrorStatus enumeration value:
164   *          - SUCCESS: COMP registers are initialized
165   *          - ERROR: COMP registers are not initialized
166   */
LL_COMP_Init(COMP_TypeDef * COMPx,LL_COMP_InitTypeDef * COMP_InitStruct)167 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
168 {
169   ErrorStatus status = SUCCESS;
170 
171   /* Check the parameters */
172   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
173   assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
174   assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
175   assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
176   assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
177   assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
178   assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMP_InitStruct->OutputBlankingSource));
179 
180   /* Note: Hardware constraint (refer to description of this function)        */
181   /*       COMP instance must not be locked.                                  */
182   if (LL_COMP_IsLocked(COMPx) == 0UL)
183   {
184     /* Configuration of comparator instance :                                 */
185     /*  - PowerMode                                                           */
186     /*  - InputPlus                                                           */
187     /*  - InputMinus                                                          */
188     /*  - InputHysteresis                                                     */
189     /*  - OutputPolarity                                                      */
190     /*  - OutputBlankingSource                                                */
191     MODIFY_REG(COMPx->CSR,
192                COMP_CSR_PWRMODE
193                | COMP_CSR_INPSEL
194                | COMP_CSR_INMSEL
195                | COMP_CSR_HYST
196                | COMP_CSR_POLARITY
197                | COMP_CSR_BLANKING
198                ,
199                COMP_InitStruct->PowerMode
200                | COMP_InitStruct->InputPlus
201                | COMP_InitStruct->InputMinus
202                | COMP_InitStruct->InputHysteresis
203                | COMP_InitStruct->OutputPolarity
204                | COMP_InitStruct->OutputBlankingSource
205               );
206 
207   }
208   else
209   {
210     /* Initialization error: COMP instance is locked.                         */
211     status = ERROR;
212   }
213 
214   return status;
215 }
216 
217 /**
218   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
219   * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
220   *                        whose fields will be set to default values.
221   * @retval None
222   */
LL_COMP_StructInit(LL_COMP_InitTypeDef * COMP_InitStruct)223 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
224 {
225   /* Set COMP_InitStruct fields to default values */
226   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_MEDIUMSPEED;
227   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
228   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
229   COMP_InitStruct->InputHysteresis      = LL_COMP_HYSTERESIS_NONE;
230   COMP_InitStruct->OutputPolarity       = LL_COMP_OUTPUTPOL_NONINVERTED;
231   COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
232 }
233 
234 /**
235   * @}
236   */
237 
238 /**
239   * @}
240   */
241 
242 /**
243   * @}
244   */
245 
246 #endif /* COMP1 || COMP2 */
247 
248 /**
249   * @}
250   */
251 
252 #endif /* USE_FULL_LL_DRIVER */
253