2 ******************************************************************************
3 * @file stm32l0xx_hal_comp.c
4 * @author MCD Application Team
6 * @date 06-February-2015
7 * @brief COMP HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the COMP peripheral:
11 * + Initialization/de-initialization functions
12 * + I/O operation functions
13 * + Peripheral Control functions
14 * + Peripheral State functions
17 ================================================================================
18 ##### COMP Peripheral features #####
19 ================================================================================
22 The STM32L0xx device family integrates 2 analog comparators COMP1 and COMP2:
23 (#) The non inverting input and inverting input can be set to GPIO pins
24 as shown in table1. COMP Inputs below.
26 (#) The COMP output is available using HAL_COMP_GetOutputLevel()
27 and can be set on GPIO pins. Refer to table 2. COMP Outputs below.
29 (#) The COMP output can be redirected to embedded timers (TIM2, TIM21, TIM22...) and LPTIM
30 Refer to TIM and LPTIM drivers.
32 (#) The comparators COMP1 and COMP2 can be combined in window mode and only COMP2 non inverting input can be used as non-inverting input.
34 (#) The 2 comparators have interrupt capability with wake-up
35 from Sleep and Stop modes (through the EXTI controller):
36 (++) COMP1 is internally connected to EXTI Line 21
37 (++) COMP2 is internally connected to EXTI Line 22
39 From the corresponding IRQ handler, the right interrupt source can be retrieved with the
40 macro __HAL_COMP_EXTI_GET_FLAG(). Possible values are:
41 (++) COMP_EXTI_LINE_COMP1
42 (++) COMP_EXTI_LINE_COMP2
45 [..] Table 1. COMP Inputs for the STM32L0xx devices
46 +--------------------------------------------------+
48 |-----------------|----------------|---------------|
49 | | 1/4 VREFINT | -- | OK |
50 | | 1/2 VREFINT | -- | OK |
51 | | 3/4 VREFINT | -- | OK |
52 | Inverting Input | VREFINT | OK | OK |
53 | | DAC OUT (PA4) | OK | OK |
57 |-----------------|----------------|-------|-------|
58 | Non Inverting | IO1 | PA1 | PA3 |
59 | Input | IO2 | --- | PB4 |
63 +--------------------------------------------------+
65 [..] Table 2. COMP Outputs for the STM32L0xx devices
75 ##### How to use this driver #####
76 ================================================================================
78 This driver provides functions to configure and program the Comparators of all STM32L0xx devices.
80 To use the comparator, perform the following steps:
82 (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit().
83 (++) Configure the comparator input in analog mode using HAL_GPIO_Init().
84 (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
85 output to the GPIO pin.
86 (++) If required enable the VREFINT reference using HAL_VREFINT_Cmd() and HAL_COMP_EnableBuffer_Cmd().
87 (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
88 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
89 interrupt vector using HAL_NVIC_EnableIRQ() function.
91 (#) Configure the comparator using HAL_COMP_Init() function:
92 (++) Select the inverting input
93 (++) Select the non-inverting input
94 (++) Select the output polarity
95 (++) Select the power mode
96 (++) Select the window mode
98 (#) Enable the comparator using HAL_COMP_Start() function
101 ******************************************************************************
104 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
106 * Redistribution and use in source and binary forms, with or without modification,
107 * are permitted provided that the following conditions are met:
108 * 1. Redistributions of source code must retain the above copyright notice,
109 * this list of conditions and the following disclaimer.
110 * 2. Redistributions in binary form must reproduce the above copyright notice,
111 * this list of conditions and the following disclaimer in the documentation
112 * and/or other materials provided with the distribution.
113 * 3. Neither the name of STMicroelectronics nor the names of its contributors
114 * may be used to endorse or promote products derived from this software
115 * without specific prior written permission.
117 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
118 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
119 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
120 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
121 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
122 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
123 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
124 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
125 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
126 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
128 ******************************************************************************
131 /* Includes ------------------------------------------------------------------*/
132 #include "stm32l0xx_hal.h"
134 /** @addtogroup STM32L0xx_HAL_Driver
139 * @brief COMP HAL module driver
143 #ifdef HAL_COMP_MODULE_ENABLED
145 /* Private typedef -----------------------------------------------------------*/
146 /* Private define ------------------------------------------------------------*/
147 /* CSR register reset value */
148 #define COMP_CSR_RESET_VALUE ((uint32_t)0x00000000)
151 /** @addtogroup COMP_Exported_Functions
155 /** @addtogroup COMP_Exported_Functions_Group1
156 * @brief Initialization and Configuration functions
159 ===============================================================================
160 ##### Initialization/de-initialization functions #####
161 ===============================================================================
162 [..] This section provides functions to initialize and de-initialize comparators
169 * @brief Initializes the COMP according to the specified
170 * parameters in the COMP_InitTypeDef and create the associated handle.
171 * @note If the selected comparator is locked, initialization can't be performed.
172 * To unlock the configuration, perform a system reset.
173 * @note When the LPTIM connection is enabled, the following pins LPTIM_IN1(PB5, PC0)
174 and LPTIM_IN2(PB7, PC2) should not be configured in AF.
175 * @param hcomp: COMP handle
178 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
180 HAL_StatusTypeDef status = HAL_OK;
182 /* Check the COMP handle allocation and lock status */
183 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != 0x00))
189 /* Check the parameter */
190 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
191 assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
192 assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
193 assert_param(IS_COMP_LPTIMCONNECTION(hcomp->Init.LPTIMConnection));
194 assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
195 assert_param(IS_COMP_MODE(hcomp->Init.Mode));
197 if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
199 assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
200 assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
203 if(hcomp->State == HAL_COMP_STATE_RESET)
205 /* Init SYSCFG and the low level hardware to access comparators */
206 __HAL_RCC_SYSCFG_CLK_ENABLE();
207 /* Init the low level hardware : SYSCFG to access comparators */
208 HAL_COMP_MspInit(hcomp);
211 /* Change COMP peripheral state */
212 hcomp->State = HAL_COMP_STATE_BUSY;
214 /* Set COMP parameters */
215 /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
216 /* Set COMPxNONINSEL bits according to hcomp->Init.NonInvertingInput value */
217 /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
218 /* Set COMPxMODE bits according to hcomp->Init.Mode value */
219 /* Set COMP1WM bit according to hcomp->Init.WindowMode value */
220 MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_UPDATE_PARAMETERS_MASK, \
221 hcomp->Init.InvertingInput | \
222 hcomp->Init.NonInvertingInput | \
223 hcomp->Init.LPTIMConnection | \
224 hcomp->Init.OutputPol | \
226 hcomp->Init.WindowMode);
229 /* Initialize the COMP state*/
230 hcomp->State = HAL_COMP_STATE_READY;
238 * @brief DeInitializes the COMP peripheral
239 * @note Deinitialization can't be performed if the COMP configuration is locked.
240 * To unlock the configuration, perform a system reset.
241 * @param hcomp: COMP handle
244 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
246 HAL_StatusTypeDef status = HAL_OK;
248 /* Check the COMP handle allocation and lock status */
249 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != 0x00))
255 /* Check the parameter */
256 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
258 /* Set COMP_CSR register to reset value */
259 WRITE_REG(hcomp->Instance->CSR, COMP_CSR_RESET_VALUE);
261 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
262 HAL_COMP_MspDeInit(hcomp);
264 hcomp->State = HAL_COMP_STATE_RESET;
274 * @brief Initializes the COMP MSP.
275 * @param hcomp: COMP handle
278 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
280 /* NOTE : This function Should not be modified, when the callback is needed,
281 the HAL_COMP_MspInit could be implenetd in the user file
286 * @brief DeInitializes COMP MSP.
287 * @param hcomp: COMP handle
290 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
292 /* NOTE : This function Should not be modified, when the callback is needed,
293 the HAL_COMP_MspDeInit could be implenetd in the user file
301 /** @addtogroup COMP_Exported_Functions_Group2
302 * @brief Data transfers functions
305 ===============================================================================
306 ##### IO operation functions #####
307 ===============================================================================
309 This subsection provides a set of functions allowing to manage the COMP data
317 * @brief Start the comparator
318 * @param hcomp: COMP handle
321 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
323 HAL_StatusTypeDef status = HAL_OK;
325 /* Check the COMP handle allocation and lock status */
326 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != 0x00))
332 /* Check the parameter */
333 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
335 if(hcomp->State == HAL_COMP_STATE_READY)
337 /* Enable the selected comparator */
338 __HAL_COMP_ENABLE(hcomp);
340 hcomp->State = HAL_COMP_STATE_BUSY;
352 * @brief Stop the comparator
353 * @param hcomp: COMP handle
356 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
358 HAL_StatusTypeDef status = HAL_OK;
360 /* Check the COMP handle allocation and lock status */
361 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != 0x00))
367 /* Check the parameter */
368 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
370 if(hcomp->State == HAL_COMP_STATE_BUSY)
372 /* Disable the selected comparator */
373 __HAL_COMP_DISABLE(hcomp);
375 hcomp->State = HAL_COMP_STATE_READY;
387 * @brief Enables the interrupt and starts the comparator
388 * @param hcomp: COMP handle
389 * @retval HAL status.
391 HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
393 HAL_StatusTypeDef status = HAL_OK;
394 uint32_t extiline = 0;
396 status = HAL_COMP_Start(hcomp);
399 /* Check the Exti Line output configuration */
400 extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
402 /* Configure the rising edge */
403 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != 0x00)
405 if (extiline == COMP_EXTI_LINE_COMP1)
407 __HAL_COMP_COMP1_EXTI_ENABLE_RISING_EDGE();
411 __HAL_COMP_COMP2_EXTI_ENABLE_RISING_EDGE();
416 if (extiline == COMP_EXTI_LINE_COMP1)
418 __HAL_COMP_COMP1_EXTI_DISABLE_RISING_EDGE();
422 __HAL_COMP_COMP2_EXTI_DISABLE_RISING_EDGE();
426 /* Configure the falling edge */
427 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != 0x00)
429 if (extiline == COMP_EXTI_LINE_COMP1)
431 __HAL_COMP_COMP1_EXTI_ENABLE_FALLING_EDGE();
435 __HAL_COMP_COMP2_EXTI_ENABLE_FALLING_EDGE();
440 if (extiline == COMP_EXTI_LINE_COMP1)
442 __HAL_COMP_COMP1_EXTI_DISABLE_FALLING_EDGE();
446 __HAL_COMP_COMP2_EXTI_DISABLE_FALLING_EDGE();
450 if (extiline == COMP_EXTI_LINE_COMP1)
452 /* Clear COMP Exti pending bit */
453 __HAL_COMP_COMP1_EXTI_CLEAR_FLAG();
454 /* Enable Exti interrupt mode */
455 __HAL_COMP_COMP1_EXTI_ENABLE_IT();
458 /* Clear COMP Exti pending bit */
459 __HAL_COMP_COMP2_EXTI_CLEAR_FLAG();
460 /* Enable Exti interrupt mode */
461 __HAL_COMP_COMP2_EXTI_ENABLE_IT();
469 * @brief Disable the interrupt and Stop the comparator
470 * @param hcomp: COMP handle
473 HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
475 HAL_StatusTypeDef status = HAL_OK;
477 if (COMP_GET_EXTI_LINE(hcomp->Instance) == COMP_EXTI_LINE_COMP1)
479 __HAL_COMP_COMP1_EXTI_DISABLE_IT();
481 if (COMP_GET_EXTI_LINE(hcomp->Instance) == COMP_EXTI_LINE_COMP2)
483 __HAL_COMP_COMP2_EXTI_DISABLE_IT();
486 status = HAL_COMP_Stop(hcomp);
492 * @brief Comparator IRQ Handler
493 * @param hcomp: COMP handle
496 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
499 /* Check COMP Exti flag */
501 if(__HAL_COMP_COMP1_EXTI_GET_FLAG() != RESET)
503 /* Clear COMP Exti pending bit */
504 __HAL_COMP_COMP1_EXTI_CLEAR_FLAG();
506 if(__HAL_COMP_COMP2_EXTI_GET_FLAG() != RESET)
508 /* Clear COMP Exti pending bit */
509 __HAL_COMP_COMP2_EXTI_CLEAR_FLAG();
512 /* COMP trigger user callback */
513 HAL_COMP_TriggerCallback(hcomp);
520 /** @addtogroup COMP_Exported_Functions_Group3
521 * @brief management functions
524 ===============================================================================
525 ##### Peripheral Control functions #####
526 ===============================================================================
528 This subsection provides a set of functions allowing to control the COMP data
536 * @brief Lock the selected comparator configuration.
537 * @param hcomp: COMP handle
540 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
542 HAL_StatusTypeDef status = HAL_OK;
544 /* Check the COMP handle allocation and lock status */
545 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != 0x00))
551 /* Check the parameter */
552 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
554 /* Set lock flag on state */
557 case HAL_COMP_STATE_BUSY:
558 hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
560 case HAL_COMP_STATE_READY:
561 hcomp->State = HAL_COMP_STATE_READY_LOCKED;
564 /* unexpected state */
572 /* Set the lock bit corresponding to selected comparator */
573 __HAL_COMP_LOCK(hcomp);
580 * @brief Return the output level (high or low) of the selected comparator.
581 * The output level depends on the selected polarity.
582 * If the polarity is not inverted:
583 * - Comparator output is low when the non-inverting input is at a lower
584 * voltage than the inverting input
585 * - Comparator output is high when the non-inverting input is at a higher
586 * voltage than the inverting input
587 * If the polarity is inverted:
588 * - Comparator output is high when the non-inverting input is at a lower
589 * voltage than the inverting input
590 * - Comparator output is low when the non-inverting input is at a higher
591 * voltage than the inverting input
592 * @param hcomp: COMP handle
593 * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
596 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
598 /* Check the parameter */
599 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
601 return((uint32_t)(hcomp->Instance->CSR & COMP_OUTPUTLEVEL_HIGH));
605 * @brief Comparator callback.
606 * @param hcomp: COMP handle
609 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
611 /* NOTE : This function should not be modified, when the callback is needed,
612 the HAL_COMP_TriggerCallback should be implemented in the user file
621 /** @addtogroup COMP_Exported_Functions_Group4
622 * @brief Peripheral State functions
625 ===============================================================================
626 ##### Peripheral State functions #####
627 ===============================================================================
629 This subsection permit to get in run-time the status of the peripheral
637 * @brief Return the COMP state
638 * @param hcomp : COMP handle
641 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
643 /* Check the COMP handle allocation */
646 return HAL_COMP_STATE_RESET;
649 /* Check the parameter */
650 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
664 #endif /* HAL_COMP_MODULE_ENABLED */
673 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/