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