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