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