QuakeGod
2024-07-27 842bb64195f958b050867c50db66fc0aa413dafb
提交 | 用户 | age
483170 1 /**
Q 2   ******************************************************************************
3   * @file    stm32f0xx_ll_comp.c
4   * @author  MCD Application Team
5   * @brief   COMP LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
10   *
11   * Redistribution and use in source and binary forms, with or without modification,
12   * are permitted provided that the following conditions are met:
13   *   1. Redistributions of source code must retain the above copyright notice,
14   *      this list of conditions and the following disclaimer.
15   *   2. Redistributions in binary form must reproduce the above copyright notice,
16   *      this list of conditions and the following disclaimer in the documentation
17   *      and/or other materials provided with the distribution.
18   *   3. Neither the name of STMicroelectronics nor the names of its contributors
19   *      may be used to endorse or promote products derived from this software
20   *      without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   *
33   ******************************************************************************
34   */
35 #if defined(USE_FULL_LL_DRIVER)
36
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32f0xx_ll_comp.h"
39
40 #ifdef  USE_FULL_ASSERT
41   #include "stm32_assert.h"
42 #else
43   #define assert_param(expr) ((void)0U)
44 #endif
45
46 /** @addtogroup STM32F0xx_LL_Driver
47   * @{
48   */
49
50 #if defined (COMP1) || defined (COMP2)
51
52 /** @addtogroup COMP_LL COMP
53   * @{
54   */
55
56 /* Private types -------------------------------------------------------------*/
57 /* Private variables ---------------------------------------------------------*/
58 /* Private constants ---------------------------------------------------------*/
59 /* Private macros ------------------------------------------------------------*/
60
61 /** @addtogroup COMP_LL_Private_Macros
62   * @{
63   */
64
65 /* Check of parameters for configuration of COMP hierarchical scope:          */
66 /* COMP instance.                                                             */
67
68 #define IS_LL_COMP_POWER_MODE(__POWER_MODE__)                                  \
69   (   ((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED)                        \
70    || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED)                      \
71    || ((__POWER_MODE__) == LL_COMP_POWERMODE_LOWPOWER)                         \
72    || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER)                    \
73   )
74
75 /* Note: On this STM32 serie, comparator input plus parameters are            */
76 /*       the different depending on COMP instances.                           */
77 #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
78   (((__COMP_INSTANCE__) == COMP1)                                              \
79     ? (                                                                        \
80           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                         \
81        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_DAC1_CH1)                    \
82       )                                                                        \
83       :                                                                        \
84       (                                                                        \
85           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                         \
86       )                                                                        \
87   )
88
89 /* Note: On this STM32 serie, comparator input minus parameters are           */
90 /*       the same on all COMP instances.                                      */
91 /*       However, comparator instance kept as macro parameter for             */
92 /*       compatibility with other STM32 families.                             */
93 #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
94   (   ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                    \
95    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
96    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
97    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
98    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                      \
99    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                      \
100    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
101   )
102
103 #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__)                      \
104   (   ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE)                      \
105    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW)                       \
106    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM)                    \
107    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH)                      \
108   )
109
110 #define IS_LL_COMP_OUTPUT_SELECTION(__OUTPUT_SELECTION__)                      \
111   (   ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_NONE)                          \
112    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_BKIN)                     \
113    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_IC1)                      \
114    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_OCCLR)                    \
115    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_IC4)                      \
116    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_OCCLR)                    \
117    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_IC1)                      \
118    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_OCCLR)                    \
119   )
120
121 #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__)                               \
122   (   ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED)                        \
123    || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED)                           \
124   )
125
126 /**
127   * @}
128   */
129
130
131 /* Private function prototypes -----------------------------------------------*/
132
133 /* Exported functions --------------------------------------------------------*/
134 /** @addtogroup COMP_LL_Exported_Functions
135   * @{
136   */
137
138 /** @addtogroup COMP_LL_EF_Init
139   * @{
140   */
141
142 /**
143   * @brief  De-initialize registers of the selected COMP instance
144   *         to their default reset values.
145   * @note   If comparator is locked, de-initialization by software is
146   *         not possible.
147   *         The only way to unlock the comparator is a device hardware reset.
148   * @param  COMPx COMP instance
149   * @retval An ErrorStatus enumeration value:
150   *          - SUCCESS: COMP registers are de-initialized
151   *          - ERROR: COMP registers are not de-initialized
152   */
153 ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
154 {
155   ErrorStatus status = SUCCESS;
156   
157   /* Check the parameters */
158   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
159   
160   /* Note: Hardware constraint (refer to description of this function):       */
161   /*       COMP instance must not be locked.                                  */
162   if(LL_COMP_IsLocked(COMPx) == 0U)
163   {
164     /* Note: Connection switch is applicable only to COMP instance COMP1,     */
165     /*       therefore is COMP2 is selected the equivalent bit is             */
166     /*       kept unmodified.                                                 */
167     if(COMPx == COMP1)
168     {
169       CLEAR_BIT(COMP->CSR,
170                 (  COMP_CSR_COMP1MODE
171                  | COMP_CSR_COMP1INSEL
172                  | COMP_CSR_COMP1SW1
173                  | COMP_CSR_COMP1OUTSEL
174                  | COMP_CSR_COMP1HYST
175                  | COMP_CSR_COMP1POL
176                  | COMP_CSR_COMP1EN
177                 ) << __COMP_BITOFFSET_INSTANCE(COMPx)
178                );
179     }
180     else
181     {
182       CLEAR_BIT(COMP->CSR,
183                 (  COMP_CSR_COMP1MODE
184                  | COMP_CSR_COMP1INSEL
185                  | COMP_CSR_COMP1OUTSEL
186                  | COMP_CSR_COMP1HYST
187                  | COMP_CSR_COMP1POL
188                  | COMP_CSR_COMP1EN
189                 ) << __COMP_BITOFFSET_INSTANCE(COMPx)
190                );
191     }
192     
193   }
194   else
195   {
196     /* Comparator instance is locked: de-initialization by software is         */
197     /* not possible.                                                           */
198     /* The only way to unlock the comparator is a device hardware reset.       */
199     status = ERROR;
200   }
201   
202   return status;
203 }
204
205 /**
206   * @brief  Initialize some features of COMP instance.
207   * @note   This function configures features of the selected COMP instance.
208   *         Some features are also available at scope COMP common instance
209   *         (common to several COMP instances).
210   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
211   * @param  COMPx COMP instance
212   * @param  COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
213   * @retval An ErrorStatus enumeration value:
214   *          - SUCCESS: COMP registers are initialized
215   *          - ERROR: COMP registers are not initialized
216   */
217 ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
218 {
219   ErrorStatus status = SUCCESS;
220   
221   /* Check the parameters */
222   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
223   assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
224   assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
225   assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
226   assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
227   assert_param(IS_LL_COMP_OUTPUT_SELECTION(COMP_InitStruct->OutputSelection));
228   assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
229   
230   /* Note: Hardware constraint (refer to description of this function)        */
231   /*       COMP instance must not be locked.                                  */
232   if(LL_COMP_IsLocked(COMPx) == 0U)
233   {
234     /* Configuration of comparator instance :                                 */
235     /*  - PowerMode                                                           */
236     /*  - InputPlus                                                           */
237     /*  - InputMinus                                                          */
238     /*  - InputHysteresis                                                     */
239     /*  - OutputSelection                                                     */
240     /*  - OutputPolarity                                                      */
241     /* Note: Connection switch is applicable only to COMP instance COMP1,     */
242     /*       therefore is COMP2 is selected the equivalent bit is             */
243     /*       kept unmodified.                                                 */
244     if(COMPx == COMP1)
245     {
246       MODIFY_REG(COMP->CSR,
247                  (  COMP_CSR_COMP1MODE
248                   | COMP_CSR_COMP1INSEL
249                   | COMP_CSR_COMP1SW1
250                   | COMP_CSR_COMP1OUTSEL
251                   | COMP_CSR_COMP1HYST
252                   | COMP_CSR_COMP1POL
253                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
254                 ,
255                  (  COMP_InitStruct->PowerMode
256                   | COMP_InitStruct->InputPlus
257                   | COMP_InitStruct->InputMinus
258                   | COMP_InitStruct->InputHysteresis
259                   | COMP_InitStruct->OutputSelection
260                   | COMP_InitStruct->OutputPolarity
261                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
262                 );
263     }
264     else
265     {
266       MODIFY_REG(COMP->CSR,
267                  (  COMP_CSR_COMP1MODE
268                   | COMP_CSR_COMP1INSEL
269                   | COMP_CSR_COMP1OUTSEL
270                   | COMP_CSR_COMP1HYST
271                   | COMP_CSR_COMP1POL
272                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
273                 ,
274                  (  COMP_InitStruct->PowerMode
275                   | COMP_InitStruct->InputPlus
276                   | COMP_InitStruct->InputMinus
277                   | COMP_InitStruct->InputHysteresis
278                   | COMP_InitStruct->OutputSelection
279                   | COMP_InitStruct->OutputPolarity
280                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
281                 );
282     }
283     
284   }
285   else
286   {
287     /* Initialization error: COMP instance is locked.                         */
288     status = ERROR;
289   }
290   
291   return status;
292 }
293
294 /**
295   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
296   * @param COMP_InitStruct pointer to a @ref LL_COMP_InitTypeDef structure
297   *                         whose fields will be set to default values.
298   * @retval None
299   */
300 void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
301 {
302   /* Set COMP_InitStruct fields to default values */
303   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_ULTRALOWPOWER;
304   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
305   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
306   COMP_InitStruct->InputHysteresis      = LL_COMP_HYSTERESIS_NONE;
307   COMP_InitStruct->OutputSelection      = LL_COMP_OUTPUT_NONE;
308   COMP_InitStruct->OutputPolarity       = LL_COMP_OUTPUTPOL_NONINVERTED;
309 }
310
311 /**
312   * @}
313   */
314
315 /**
316   * @}
317   */
318
319 /**
320   * @}
321   */
322
323 #endif /* COMP1 || COMP2 */
324
325 /**
326   * @}
327   */
328
329 #endif /* USE_FULL_LL_DRIVER */
330
331 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/