2 ******************************************************************************
3 * @file stm32f0xx_hal_comp.c
4 * @author MCD Application Team
6 * @date 11-December-2014
7 * @brief COMP HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the COMP peripheral:
10 * + Initialization/de-initialization functions
11 * + I/O operation functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
16 ================================================================================
17 ##### COMP Peripheral features #####
18 ================================================================================
21 The STM32F0xx device family integrates up to 2 analog comparators COMP1 and COMP2:
22 (#) The non inverting input and inverting input can be set to GPIO pins
23 as shown in table1. COMP Inputs below.
25 (#) The COMP output is available using HAL_COMP_GetOutputLevel()
26 and can be set on GPIO pins. Refer to table 2. COMP Outputs below.
28 (#) The COMP output can be redirected to embedded timers (TIM1, TIM2 and TIM3)
29 Refer to table 3. COMP Outputs redirection to embedded timers below.
31 (#) The comparators COMP1 and COMP2 can be combined in window mode.
33 (#) The comparators have interrupt capability with wake-up
34 from Sleep and Stop modes (through the EXTI controller):
35 (++) COMP1 is internally connected to EXTI Line 21
36 (++) COMP2 is internally connected to EXTI Line 22
37 From the corresponding IRQ handler, the right interrupt source can be retrieved with the
38 macro __HAL_COMP_EXTI_GET_FLAG(). Possible values are:
39 (++) COMP_EXTI_LINE_COMP1_EVENT
40 (++) COMP_EXTI_LINE_COMP2_EVENT
43 [..] Table 1. COMP Inputs for the STM32F05x, STM32F07x and STM32F09x devices
44 +--------------------------------------------------+
46 |-----------------|----------------|---------------|
47 | | 1/4 VREFINT | OK | OK |
48 | | 1/2 VREFINT | OK | OK |
49 | | 3/4 VREFINT | OK | OK |
50 | Inverting Input | VREFINT | OK | OK |
51 | | DAC1 OUT (PA4) | OK | OK |
52 | | DAC2 OUT (PA5) | OK | OK |
54 |-----------------|----------------|-------|-------|
55 | Non Inverting | | PA1 | PA3 |
57 +--------------------------------------------------+
59 [..] Table 2. COMP Outputs for the STM32F05x, STM32F07x and STM32F09x devices
68 [..] Table 3. COMP Outputs redirection to embedded timers for the STM32F05x, STM32F07x and STM32F09x devices
69 +---------------------------------+
71 |----------------|----------------|
72 | TIM1 BKIN | TIM1 BKIN |
74 | TIM1 OCREFCLR | TIM1 OCREFCLR |
76 | TIM1 IC1 | TIM1 IC1 |
78 | TIM2 IC4 | TIM2 IC4 |
80 | TIM2 OCREFCLR | TIM2 OCREFCLR |
82 | TIM3 IC1 | TIM3 IC1 |
84 | TIM3 OCREFCLR | TIM3 OCREFCLR |
85 +---------------------------------+
87 ##### How to use this driver #####
88 ================================================================================
90 This driver provides functions to configure and program the Comparators of STM32F05x, STM32F07x and STM32F09x devices.
92 To use the comparator, perform the following steps:
94 (#) Fill in the HAL_COMP_MspInit() to
95 (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
96 (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
97 output to the GPIO pin
98 (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
99 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
100 interrupt vector using HAL_NVIC_EnableIRQ() function.
102 (#) Configure the comparator using HAL_COMP_Init() function:
103 (++) Select the inverting input
104 (++) Select the non inverting input
105 (++) Select the output polarity
106 (++) Select the output redirection
107 (++) Select the hysteresis level
108 (++) Select the power mode
109 (++) Select the event/interrupt mode
111 (#) Enable the comparator using HAL_COMP_Start() function or HAL_COMP_Start_IT() function for interrupt mode
113 (#) Read the comparator output level with HAL_COMP_GetOutputLevel()
116 ******************************************************************************
119 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
121 * Redistribution and use in source and binary forms, with or without modification,
122 * are permitted provided that the following conditions are met:
123 * 1. Redistributions of source code must retain the above copyright notice,
124 * this list of conditions and the following disclaimer.
125 * 2. Redistributions in binary form must reproduce the above copyright notice,
126 * this list of conditions and the following disclaimer in the documentation
127 * and/or other materials provided with the distribution.
128 * 3. Neither the name of STMicroelectronics nor the names of its contributors
129 * may be used to endorse or promote products derived from this software
130 * without specific prior written permission.
132 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
133 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
134 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
135 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
136 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
137 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
138 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
139 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
140 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
141 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
143 ******************************************************************************
146 /* Includes ------------------------------------------------------------------*/
147 #include "stm32f0xx_hal.h"
149 #ifdef HAL_COMP_MODULE_ENABLED
151 #if defined(STM32F051x8) || defined(STM32F058xx) || \
152 defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || \
153 defined(STM32F091xC) || defined (STM32F098xx)
155 /** @addtogroup STM32F0xx_HAL_Driver
159 /** @defgroup COMP COMP HAL Module Driver
160 * @brief COMP HAL module driver
164 /* Private typedef -----------------------------------------------------------*/
165 /* Private define ------------------------------------------------------------*/
166 /** @defgroup COMP_Private_Constants COMP Private Constants
169 /* CSR register reset value */
170 #define COMP_CSR_RESET_VALUE ((uint32_t)0x00000000)
171 /* CSR register masks */
172 #define COMP_CSR_RESET_PARAMETERS_MASK ((uint32_t)0x00003FFF)
173 #define COMP_CSR_UPDATE_PARAMETERS_MASK ((uint32_t)0x00003FFE)
174 /* CSR COMPx non inverting input mask */
175 #define COMP_CSR_COMPxNONINSEL_MASK ((uint16_t)COMP_CSR_COMP1SW1)
176 /* CSR COMP2 shift */
177 #define COMP_CSR_COMP1_SHIFT 0U
178 #define COMP_CSR_COMP2_SHIFT 16U
182 /* Private macro -------------------------------------------------------------*/
183 /* Private variables ---------------------------------------------------------*/
184 /* Private function prototypes -----------------------------------------------*/
185 /* Private functions ---------------------------------------------------------*/
187 /** @defgroup COMP_Exported_Functions COMP Exported Functions
191 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
192 * @brief Initialization and Configuration functions
195 ===============================================================================
196 ##### Initialization and Configuration functions #####
197 ===============================================================================
198 [..] This section provides functions to initialize and de-initialize comparators
205 * @brief Initializes the COMP according to the specified
206 * parameters in the COMP_InitTypeDef and create the associated handle.
207 * @note If the selected comparator is locked, initialization can't be performed.
208 * To unlock the configuration, perform a system reset.
209 * @param hcomp: COMP handle
212 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
214 HAL_StatusTypeDef status = HAL_OK;
215 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
217 /* Check the COMP handle allocation and lock status */
218 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
224 /* Check the parameter */
225 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
226 assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
227 assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
228 assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
229 assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
230 assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
231 assert_param(IS_COMP_MODE(hcomp->Init.Mode));
233 if(hcomp->Init.NonInvertingInput == COMP_NONINVERTINGINPUT_DAC1SWITCHCLOSED)
235 assert_param(IS_COMP_DAC1SWITCH_INSTANCE(hcomp->Instance));
238 if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLED)
240 assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
243 if(hcomp->State == HAL_COMP_STATE_RESET)
245 /* Init SYSCFG and the low level hardware to access comparators */
246 __SYSCFG_CLK_ENABLE();
248 HAL_COMP_MspInit(hcomp);
251 /* Set COMP parameters */
252 /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
253 /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */
254 /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
255 /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */
256 /* Set COMPxMODE bits according to hcomp->Init.Mode value */
257 if(hcomp->Instance == COMP2)
259 regshift = COMP_CSR_COMP2_SHIFT;
261 MODIFY_REG(COMP->CSR,
262 (uint32_t)(COMP_CSR_COMPxINSEL | COMP_CSR_COMPxNONINSEL_MASK | \
263 COMP_CSR_COMPxOUTSEL | COMP_CSR_COMPxPOL | \
264 COMP_CSR_COMPxHYST | COMP_CSR_COMPxMODE) << regshift,
265 (hcomp->Init.InvertingInput | \
266 hcomp->Init.NonInvertingInput | \
267 hcomp->Init.Output | \
268 hcomp->Init.OutputPol | \
269 hcomp->Init.Hysteresis | \
270 hcomp->Init.Mode) << regshift);
272 if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLED)
274 COMP->CSR |= COMP_CSR_WNDWEN;
277 /* Initialize the COMP state*/
278 if(hcomp->State == HAL_COMP_STATE_RESET)
280 hcomp->State = HAL_COMP_STATE_READY;
288 * @brief DeInitializes the COMP peripheral
289 * @note Deinitialization can't be performed if the COMP configuration is locked.
290 * To unlock the configuration, perform a system reset.
291 * @param hcomp: COMP handle
294 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
296 HAL_StatusTypeDef status = HAL_OK;
297 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
299 /* Check the COMP handle allocation and lock status */
300 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
306 /* Check the parameter */
307 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
309 /* Set COMP_CSR register to reset value for the corresponding COMP instance */
310 if(hcomp->Instance == COMP2)
312 regshift = COMP_CSR_COMP2_SHIFT;
314 MODIFY_REG(COMP->CSR,
315 COMP_CSR_RESET_PARAMETERS_MASK << regshift,
316 COMP_CSR_RESET_VALUE << regshift);
318 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
319 HAL_COMP_MspDeInit(hcomp);
321 hcomp->State = HAL_COMP_STATE_RESET;
328 * @brief Initializes the COMP MSP.
329 * @param hcomp: COMP handle
332 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
334 /* NOTE : This function Should not be modified, when the callback is needed,
335 the HAL_COMP_MspInit could be implenetd in the user file
340 * @brief DeInitializes COMP MSP.
341 * @param hcomp: COMP handle
344 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
346 /* NOTE : This function Should not be modified, when the callback is needed,
347 the HAL_COMP_MspDeInit could be implenetd in the user file
355 /** @defgroup COMP_Exported_Functions_Group2 I/O operation functions
356 * @brief Data transfers functions
359 ===============================================================================
360 ##### IO operation functions #####
361 ===============================================================================
363 This subsection provides a set of functions allowing to manage the COMP data
371 * @brief Start the comparator
372 * @param hcomp: COMP handle
375 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
377 HAL_StatusTypeDef status = HAL_OK;
378 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
380 /* Check the COMP handle allocation and lock status */
381 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
387 /* Check the parameter */
388 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
390 if(hcomp->State == HAL_COMP_STATE_READY)
392 /* Enable the selected comparator */
393 if(hcomp->Instance == COMP2)
395 regshift = COMP_CSR_COMP2_SHIFT;
397 SET_BIT(COMP->CSR, (uint32_t)COMP_CSR_COMPxEN << regshift);
399 hcomp->State = HAL_COMP_STATE_BUSY;
411 * @brief Stop the comparator
412 * @param hcomp: COMP handle
415 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
417 HAL_StatusTypeDef status = HAL_OK;
418 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
420 /* Check the COMP handle allocation and lock status */
421 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
427 /* Check the parameter */
428 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
430 if(hcomp->State == HAL_COMP_STATE_BUSY)
432 /* Disable the selected comparator */
433 if(hcomp->Instance == COMP2)
435 regshift = COMP_CSR_COMP2_SHIFT;
437 CLEAR_BIT(COMP->CSR, (uint32_t)COMP_CSR_COMPxEN << regshift);
439 hcomp->State = HAL_COMP_STATE_READY;
451 * @brief Enables the interrupt and starts the comparator
452 * @param hcomp: COMP handle
453 * @retval HAL status.
455 HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
457 HAL_StatusTypeDef status = HAL_OK;
458 uint32_t extiline = 0;
460 /* Check the parameter */
461 assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
463 status = HAL_COMP_Start(hcomp);
466 /* Check the Exti Line output configuration */
467 extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
468 /* Configure the rising edge */
469 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
471 __HAL_COMP_EXTI_RISING_IT_ENABLE(extiline);
475 __HAL_COMP_EXTI_RISING_IT_DISABLE(extiline);
477 /* Configure the falling edge */
478 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
480 __HAL_COMP_EXTI_FALLING_IT_ENABLE(extiline);
484 __HAL_COMP_EXTI_FALLING_IT_DISABLE(extiline);
486 /* Enable Exti interrupt mode */
487 __HAL_COMP_EXTI_ENABLE_IT(extiline);
488 /* Clear COMP Exti pending bit */
489 __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
496 * @brief Disable the interrupt and Stop the comparator
497 * @param hcomp: COMP handle
500 HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
502 HAL_StatusTypeDef status = HAL_OK;
504 /* Disable the Exti Line interrupt mode */
505 __HAL_COMP_EXTI_DISABLE_IT(__HAL_COMP_GET_EXTI_LINE(hcomp->Instance));
507 status = HAL_COMP_Stop(hcomp);
513 * @brief Comparator IRQ Handler
514 * @param hcomp: COMP handle
517 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
519 uint32_t extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
521 /* Check COMP Exti flag */
522 if(__HAL_COMP_EXTI_GET_FLAG(extiline) != RESET)
524 /* Clear COMP Exti pending bit */
525 __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
527 /* COMP trigger user callback */
528 HAL_COMP_TriggerCallback(hcomp);
536 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
537 * @brief management functions
540 ===============================================================================
541 ##### Peripheral Control functions #####
542 ===============================================================================
544 This subsection provides a set of functions allowing to control the COMP data
552 * @brief Lock the selected comparator configuration.
553 * @param hcomp: COMP handle
556 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
558 HAL_StatusTypeDef status = HAL_OK;
559 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
561 /* Check the COMP handle allocation and lock status */
562 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
568 /* Check the parameter */
569 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
572 hcomp->State |= COMP_STATE_BIT_LOCK;
574 /* Set the lock bit corresponding to selected comparator */
575 if(hcomp->Instance == COMP2)
577 regshift = COMP_CSR_COMP2_SHIFT;
579 SET_BIT(COMP->CSR, (uint32_t)COMP_CSR_COMPxLOCK << regshift);
586 * @brief Return the output level (high or low) of the selected comparator.
587 * The output level depends on the selected polarity.
588 * If the polarity is not inverted:
589 * - Comparator output is low when the non-inverting input is at a lower
590 * voltage than the inverting input
591 * - Comparator output is high when the non-inverting input is at a higher
592 * voltage than the inverting input
593 * If the polarity is inverted:
594 * - Comparator output is high when the non-inverting input is at a lower
595 * voltage than the inverting input
596 * - Comparator output is low when the non-inverting input is at a higher
597 * voltage than the inverting input
598 * @param hcomp: COMP handle
599 * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
602 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
605 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
607 /* Check the parameter */
608 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
610 if(hcomp->Instance == COMP2)
612 regshift = COMP_CSR_COMP2_SHIFT;
614 level = READ_BIT(COMP->CSR, (uint32_t)COMP_CSR_COMPxOUT << regshift);
618 return(COMP_OUTPUTLEVEL_HIGH);
620 return(COMP_OUTPUTLEVEL_LOW);
624 * @brief Comparator callback.
625 * @param hcomp: COMP handle
628 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
630 /* NOTE : This function should not be modified, when the callback is needed,
631 the HAL_COMP_TriggerCallback should be implemented in the user file
640 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
641 * @brief Peripheral State functions
644 ===============================================================================
645 ##### Peripheral State functions #####
646 ===============================================================================
648 This subsection permit to get in run-time the status of the peripheral
656 * @brief Return the COMP state
657 * @param hcomp : COMP handle
660 uint32_t HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
662 /* Check the COMP handle allocation */
665 return HAL_COMP_STATE_RESET;
668 /* Check the parameter */
669 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
689 #endif /* STM32F051x8 || STM32F058xx || */
690 /* STM32F071xB || STM32F072xB || STM32F078xx || */
691 /* STM32F091xC || defined (STM32F098xx) */
693 #endif /* HAL_COMP_MODULE_ENABLED */
695 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/