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