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