1 /**
2   ******************************************************************************
3   * @file    stm32wbxx_ll_comp.c
4   * @author  MCD Application Team
5   * @brief   COMP LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2019 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 "stm32wbxx_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 STM32WBxx_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 families.                             */
62 #if defined(LL_COMP_INPUT_PLUS_IO1)
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   )
68 #else
69 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
70   (((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2)                                \
71    || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3)                             \
72   )
73 #endif
74 
75 /* Note: On this STM32 series, comparator input minus parameters are          */
76 /*       the same on all COMP instances.                                      */
77 /*       However, comparator instance kept as macro parameter for             */
78 /*       compatibility with other STM32 families.                             */
79 #if defined(LL_COMP_INPUT_MINUS_IO2)
80 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
81   (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                    \
82    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
83    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
84    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
85    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
86    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2)                           \
87    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3)                           \
88    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO4)                           \
89    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO5)                           \
90   )
91 #else
92 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
93   (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                    \
94    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
95    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
96    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
97    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
98    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3)                           \
99    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO4)                           \
100    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO5)                           \
101   )
102 #endif
103 
104 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__)                      \
105   (((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE)                         \
106    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW)                       \
107    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM)                    \
108    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH)                      \
109   )
110 
111 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__)                               \
112   (((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED)                           \
113    || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED)                           \
114   )
115 
116 #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__OUTPUT_BLANKING_SOURCE__)          \
117   (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE)               \
118    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5)           \
119    || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3)           \
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, CSR, 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,const LL_COMP_InitTypeDef * COMP_InitStruct)186 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, const 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     MODIFY_REG(COMPx->CSR,
211                COMP_CSR_PWRMODE
212                | COMP_CSR_INPSEL
213                | COMP_CSR_SCALEN
214                | COMP_CSR_BRGEN
215                | COMP_CSR_INMESEL
216                | COMP_CSR_INMSEL
217                | COMP_CSR_HYST
218                | COMP_CSR_POLARITY
219                | COMP_CSR_BLANKING
220                ,
221                COMP_InitStruct->PowerMode
222                | COMP_InitStruct->InputPlus
223                | COMP_InitStruct->InputMinus
224                | COMP_InitStruct->InputHysteresis
225                | COMP_InitStruct->OutputPolarity
226                | COMP_InitStruct->OutputBlankingSource
227               );
228 
229   }
230   else
231   {
232     /* Initialization error: COMP instance is locked.                         */
233     status = ERROR;
234   }
235 
236   return status;
237 }
238 
239 /**
240   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
241   * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
242   *                        whose fields will be set to default values.
243   * @retval None
244   */
LL_COMP_StructInit(LL_COMP_InitTypeDef * COMP_InitStruct)245 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
246 {
247   /* Set COMP_InitStruct fields to default values */
248   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_ULTRALOWPOWER;
249 #if defined(LL_COMP_INPUT_PLUS_IO1)
250   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
251 #else
252   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO2;
253 #endif
254   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
255   COMP_InitStruct->InputHysteresis      = LL_COMP_HYSTERESIS_NONE;
256   COMP_InitStruct->OutputPolarity       = LL_COMP_OUTPUTPOL_NONINVERTED;
257   COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
258 }
259 
260 /**
261   * @}
262   */
263 
264 /**
265   * @}
266   */
267 
268 /**
269   * @}
270   */
271 
272 #endif /* COMP1 || COMP2 */
273 
274 /**
275   * @}
276   */
277 
278 #endif /* USE_FULL_LL_DRIVER */
279