2 ******************************************************************************
\r
3 * @file stm32l1xx_comp.c
\r
4 * @author MCD Application Team
\r
6 * @date 05-March-2012
\r
7 * @brief This file provides firmware functions to manage the following
\r
8 * functionalities of the comparators (COMP1 and COMP2) peripheral:
\r
9 * + Comparators configuration
\r
10 * + Window mode control
\r
11 * + Internal Reference Voltage (VREFINT) output
\r
14 ===============================================================================
\r
15 ##### How to use this driver #####
\r
16 ===============================================================================
\r
17 [..] The device integrates two analog comparators COMP1 and COMP2:
\r
18 (+) COMP1 is a fixed threshold (VREFINT) that shares the non inverting
\r
19 input with the ADC channels.
\r
20 (+) COMP2 is a rail-to-rail comparator whose the inverting input can be
\r
21 selected among: DAC_OUT1, DAC_OUT2, 1/4 VREFINT, 1/2 VERFINT, 3/4
\r
22 VREFINT, VREFINT, PB3 and whose the output can be redirected to
\r
23 embedded timers: TIM2, TIM3, TIM4, TIM10.
\r
25 (+) The two comparators COMP1 and COMP2 can be combined in window mode.
\r
28 (#@) Comparator APB clock must be enabled to get write access
\r
29 to comparator register using
\r
30 RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, ENABLE).
\r
32 (#@) COMP1 comparator and ADC can't be used at the same time since
\r
33 they share the same ADC switch matrix (analog switches).
\r
35 (#@) When an I/O is used as comparator input, the corresponding GPIO
\r
36 registers should be configured in analog mode.
\r
38 (#@) Comparators outputs (CMP1OUT and CMP2OUT) are not mapped on
\r
39 GPIO pin. They are only internal.
\r
40 To get the comparator output level, use COMP_GetOutputLevel().
\r
42 (#@) COMP1 and COMP2 outputs are internally connected to EXTI Line 21
\r
43 and EXTI Line 22 respectively.
\r
44 Interrupts can be used by configuring the EXTI Line using the
\r
45 EXTI peripheral driver.
\r
47 (#@) After enabling the comparator (COMP1 or COMP2), user should wait
\r
48 for start-up time (tSTART) to get right output levels.
\r
49 Please refer to product datasheet for more information on tSTART.
\r
51 (#@) Comparators cannot be used to exit the device from Sleep or Stop
\r
52 mode when the internal reference voltage is switched off using
\r
53 the PWR_UltraLowPowerCmd() function (ULP bit in the PWR_CR register).
\r
56 ******************************************************************************
\r
59 * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
\r
61 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
\r
62 * You may not use this file except in compliance with the License.
\r
63 * You may obtain a copy of the License at:
\r
65 * http://www.st.com/software_license_agreement_liberty_v2
\r
67 * Unless required by applicable law or agreed to in writing, software
\r
68 * distributed under the License is distributed on an "AS IS" BASIS,
\r
69 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
70 * See the License for the specific language governing permissions and
\r
71 * limitations under the License.
\r
73 ******************************************************************************
\r
76 /* Includes ------------------------------------------------------------------*/
\r
77 #include "stm32l1xx_comp.h"
\r
79 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
84 * @brief COMP driver modules.
\r
88 /* Private typedef -----------------------------------------------------------*/
\r
89 /* Private define ------------------------------------------------------------*/
\r
90 /* Private macro -------------------------------------------------------------*/
\r
91 /* Private variables ---------------------------------------------------------*/
\r
92 /* Private function prototypes -----------------------------------------------*/
\r
93 /* Private functions ---------------------------------------------------------*/
\r
95 /** @defgroup COMP_Private_Functions
\r
99 /** @defgroup COMP_Group1 Initialization and Configuration functions
\r
100 * @brief Initialization and Configuration functions.
\r
103 ===============================================================================
\r
104 ##### Initialization and Configuration functions #####
\r
105 ===============================================================================
\r
112 * @brief Deinitializes COMP peripheral registers to their default reset values.
\r
116 void COMP_DeInit(void)
\r
118 COMP->CSR = ((uint32_t)0x00000000); /*!< Set COMP->CSR to reset value */
\r
122 * @brief Initializes the COMP2 peripheral according to the specified parameters
\r
123 * in the COMP_InitStruct.
\r
124 * @note This function configures only COMP2.
\r
125 * @note COMP2 comparator is enabled as soon as the INSEL[2:0] bits are
\r
126 * different from "000".
\r
127 * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure that contains
\r
128 * the configuration information for the specified COMP peripheral.
\r
131 void COMP_Init(COMP_InitTypeDef* COMP_InitStruct)
\r
133 uint32_t tmpreg = 0;
\r
135 /* Check the parameters */
\r
136 assert_param(IS_COMP_INVERTING_INPUT(COMP_InitStruct->COMP_InvertingInput));
\r
137 assert_param(IS_COMP_OUTPUT(COMP_InitStruct->COMP_OutputSelect));
\r
138 assert_param(IS_COMP_SPEED(COMP_InitStruct->COMP_Speed));
\r
140 /*!< Get the COMP CSR value */
\r
141 tmpreg = COMP->CSR;
\r
143 /*!< Clear the INSEL[2:0], OUTSEL[1:0] and SPEED bits */
\r
144 tmpreg &= (uint32_t) (~(uint32_t) (COMP_CSR_OUTSEL | COMP_CSR_INSEL | COMP_CSR_SPEED));
\r
146 /*!< Configure COMP: speed, inversion input selection and output redirection */
\r
147 /*!< Set SPEED bit according to COMP_InitStruct->COMP_Speed value */
\r
148 /*!< Set INSEL bits according to COMP_InitStruct->COMP_InvertingInput value */
\r
149 /*!< Set OUTSEL bits according to COMP_InitStruct->COMP_OutputSelect value */
\r
150 tmpreg |= (uint32_t)((COMP_InitStruct->COMP_Speed | COMP_InitStruct->COMP_InvertingInput
\r
151 | COMP_InitStruct->COMP_OutputSelect));
\r
153 /*!< The COMP2 comparator is enabled as soon as the INSEL[2:0] bits value are
\r
154 different from "000" */
\r
155 /*!< Write to COMP_CSR register */
\r
156 COMP->CSR = tmpreg;
\r
160 * @brief Enable or disable the COMP1 peripheral.
\r
161 * @note After enabling COMP1, the following functions should be called to
\r
162 * connect the selected GPIO input to COMP1 non inverting input:
\r
163 * @note Enable switch control mode using SYSCFG_RISwitchControlModeCmd()
\r
164 * @note Close VCOMP switch using SYSCFG_RIIOSwitchConfig()
\r
165 * @note Close the I/O switch number n corresponding to the I/O
\r
166 * using SYSCFG_RIIOSwitchConfig()
\r
167 * @param NewState: new state of the COMP1 peripheral.
\r
168 * This parameter can be: ENABLE or DISABLE.
\r
169 * @note This function enables/disables only the COMP1.
\r
172 void COMP_Cmd(FunctionalState NewState)
\r
174 /* Check the parameter */
\r
175 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
177 if (NewState != DISABLE)
\r
179 /* Enable the COMP1 */
\r
180 COMP->CSR |= (uint32_t) COMP_CSR_CMP1EN;
\r
184 /* Disable the COMP1 */
\r
185 COMP->CSR &= (uint32_t)(~COMP_CSR_CMP1EN);
\r
190 * @brief Return the output level (high or low) of the selected comparator.
\r
191 * @note Comparator output is low when the noninverting input is at a lower
\r
192 * voltage than the inverting input.
\r
193 * @note Comparator output is high when the noninverting input is at a higher
\r
194 * voltage than the inverting input.
\r
195 * @note Comparators outputs aren't available on GPIO (outputs levels are
\r
196 * only internal). The COMP1 and COMP2 outputs are connected internally
\r
197 * to the EXTI Line 21 and Line 22 respectively.
\r
198 * @param COMP_Selection: the selected comparator.
\r
199 * This parameter can be one of the following values:
\r
200 * @arg COMP_Selection_COMP1: COMP1 selected
\r
201 * @arg COMP_Selection_COMP2: COMP2 selected
\r
202 * @retval Returns the selected comparator output level.
\r
204 uint8_t COMP_GetOutputLevel(uint32_t COMP_Selection)
\r
206 uint8_t compout = 0x0;
\r
208 /* Check the parameters */
\r
209 assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
\r
211 /* Check if Comparator 1 is selected */
\r
212 if(COMP_Selection == COMP_Selection_COMP1)
\r
214 /* Check if comparator 1 output level is high */
\r
215 if((COMP->CSR & COMP_CSR_CMP1OUT) != (uint8_t) RESET)
\r
217 /* Get Comparator 1 output level */
\r
218 compout = (uint8_t) COMP_OutputLevel_High;
\r
220 /* comparator 1 output level is low */
\r
223 /* Get Comparator 1 output level */
\r
224 compout = (uint8_t) COMP_OutputLevel_Low;
\r
227 /* Comparator 2 is selected */
\r
230 /* Check if comparator 2 output level is high */
\r
231 if((COMP->CSR & COMP_CSR_CMP2OUT) != (uint8_t) RESET)
\r
233 /* Get Comparator output level */
\r
234 compout = (uint8_t) COMP_OutputLevel_High;
\r
236 /* comparator 2 output level is low */
\r
239 /* Get Comparator 2 output level */
\r
240 compout = (uint8_t) COMP_OutputLevel_Low;
\r
243 /* Return the comparator output level */
\r
244 return (uint8_t)(compout);
\r
248 * @brief Close or Open the SW1 switch.
\r
249 * @param NewState: new state of the SW1 switch.
\r
250 * This parameter can be: ENABLE or DISABLE.
\r
251 * @note ENABLE to close the SW1 switch
\r
252 * @note DISABLE to open the SW1 switch
\r
255 void COMP_SW1SwitchConfig(FunctionalState NewState)
\r
257 /* Check the parameters */
\r
258 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
260 if (NewState != DISABLE)
\r
262 /* Close SW1 switch */
\r
263 COMP->CSR |= (uint32_t) COMP_CSR_SW1;
\r
267 /* Open SW1 switch */
\r
268 COMP->CSR &= (uint32_t)(~COMP_CSR_SW1);
\r
276 /** @defgroup COMP_Group2 Window mode control function
\r
277 * @brief Window mode control function.
\r
280 ===============================================================================
\r
281 ##### Window mode control function #####
\r
282 ===============================================================================
\r
289 * @brief Enables or disables the window mode.
\r
291 * @note COMP1 inverting input is fixed to VREFINT defining the first
\r
293 * @note COMP2 inverting input is configurable (DAC_OUT1, DAC_OUT2, VREFINT
\r
294 * sub-multiples, PB3) defining the second threshold.
\r
295 * @note COMP1 and COMP2 non inverting inputs are connected together.
\r
296 * @note In window mode, only the Group 6 (PB4 or PB5) can be used as
\r
297 * noninverting inputs.
\r
298 * @param NewState: new state of the window mode.
\r
299 * This parameter can be ENABLE or DISABLE.
\r
302 void COMP_WindowCmd(FunctionalState NewState)
\r
304 /* Check the parameters */
\r
305 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
307 if (NewState != DISABLE)
\r
309 /* Enable the window mode */
\r
310 COMP->CSR |= (uint32_t) COMP_CSR_WNDWE;
\r
314 /* Disable the window mode */
\r
315 COMP->CSR &= (uint32_t)(~COMP_CSR_WNDWE);
\r
323 /** @defgroup COMP_Group3 Internal Reference Voltage output function
\r
324 * @brief Internal Reference Voltage (VREFINT) output function.
\r
327 ===============================================================================
\r
328 ##### Internal Reference Voltage (VREFINT) output function #####
\r
329 ===============================================================================
\r
336 * @brief Enables or disables the output of internal reference voltage (VREFINT).
\r
337 * The VREFINT output can be routed to any I/O in group 3: CH8 (PB0) or
\r
339 * To correctly use this function, the SYSCFG_RIIOSwitchConfig() function
\r
340 * should be called after.
\r
341 * @param NewState: new state of the Vrefint output.
\r
342 * This parameter can be: ENABLE or DISABLE.
\r
345 void COMP_VrefintOutputCmd(FunctionalState NewState)
\r
347 /* Check the parameters */
\r
348 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
350 if (NewState != DISABLE)
\r
352 /* Enable the output of internal reference voltage */
\r
353 COMP->CSR |= (uint32_t) COMP_CSR_VREFOUTEN;
\r
357 /* Disable the output of internal reference voltage */
\r
358 COMP->CSR &= (uint32_t) (~COMP_CSR_VREFOUTEN);
\r
378 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r