]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/stm32l1xx_hal_comp.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L1 / stm32l1xx_hal_comp.c
1 /**
2   ******************************************************************************
3   * @file    stm32l1xx_hal_comp.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    5-September-2014
7   * @brief   COMP HAL module driver.
8   *    
9   *          This file provides firmware functions to manage the following 
10   *          functionalities of the COMP peripheral:
11   *           + Initialization and de-initialization functions
12   *           + I/O operation functions
13   *           + Peripheral Control functions 
14   *           + Peripheral State functions
15   *         
16   @verbatim
17 ================================================================================
18           ##### COMP Peripheral features #####
19 ================================================================================
20            
21   [..]       
22       The STM32L1xx device family integrates 2 analog comparators COMP1 and 
23       COMP2:
24       (#) The non inverting input and inverting input can be set to GPIO pins.
25           Refer to "table1. COMP Inputs" below.
26           HAL COMP driver configures the Routing Interface (RI) to connect the
27           selected I/O pins to comparator input.
28           Caution: Comparator COMP1 and ADC cannot be used at the same time as 
29           ADC since they share the ADC switch matrix: COMP1 non-inverting 
30           input is routed through ADC switch matrix. Except if ADC is intented 
31           to measure voltage on COMP1 non-inverting input: it can be performed 
32           on ADC channel VCOMP.
33
34       (#) The COMP output is available using HAL_COMP_GetOutputLevel().
35
36       (#) The COMP output can be redirected to embedded timers (TIM2, TIM3, 
37           TIM4, TIM10).
38           COMP output cannot be redirected to any I/O pin.
39           Refer to "table 2. COMP Outputs redirection to embedded timers" below.
40
41       (#) The comparators COMP1 and COMP2 can be combined in window mode. 
42           In this mode, COMP2 non inverting input is used as common 
43           non-inverting input.
44   
45       (#) The 2 comparators have interrupt capability with wake-up
46           from Sleep and Stop modes (through the EXTI controller):
47           (++) COMP1 is internally connected to EXTI Line 21
48           (++) COMP2 is internally connected to EXTI Line 22
49
50           From the corresponding IRQ handler, the right interrupt source can be
51           retrieved with macro __HAL_COMP_EXTI_GET_FLAG(). Possible values are: 
52           (++) COMP_EXTI_LINE_COMP1_EVENT
53           (++) COMP_EXTI_LINE_COMP2_EVENT
54   
55       (#) The comparators also offer the possibility to ouput the voltage 
56           reference (VrefInt), used on inverting inputs, on I/O pin through 
57           a buffer. To use it, refer to macro "__HAL_VREFINT_OUT_ENABLE()".
58
59             
60 [..] Table 1. COMP Inputs for the STM32L1xx devices
61  +----------------------------------------------------------------------+
62  |                 |                                |  COMP1  |  COMP2  |
63  |-----------------|--------------------------------|---------|---------|
64  |                 | 1/4 VREFINT                    |   --    |   OK    |
65  |                 | 1/2 VREFINT                    |   --    |   OK    |
66  |                 | 3/4 VREFINT                    |   --    |   OK    |
67  | Inverting       | VREFINT                        |   OK    |   OK    |
68  | input           | DAC Ch1 OUT (PA4)              |   --    |   OK    |
69  |                 | DAC Ch2 OUT (PA5)              |   --    |   OK    |
70  |                 | IO: PB3                        |   --    |   OK    |
71  |-----------------|--------------------------------|---------|---------|
72  |                 | IO:                            |         |         |
73  |                 |   PB4, 5, 6*, 7*               |   ---   |   OK    |
74  | Non-inverting   |   PA0*, 1*, 2*, 3*, 4, 5, 6, 7 |   OK    |   ---   |
75  | input           |   PB0, 1, 12, 13, 14, 15       |   OK    |   ---   |
76  |                 |   PC0, 1, 2, 3, 4, 5           |   OK    |   ---   |
77  |                 |   PE7, 8, 9, 10                |   OK    |   ---   |
78  |                 |   PF6, 7, 8, 9, 10             |   OK    |   ---   |
79  |                 | OPAMP1 output                  |   OK    |   ---   |
80  |                 | OPAMP2 output                  |   OK    |   ---   |
81  |                 | OPAMP3 output**                |   OK    |   ---   |
82  +----------------------------------------------------------------------+
83  *: Available on devices category Cat.3, Cat.4, Cat.5 only. 
84  **: Available on devices category Cat.4 only. 
85
86  [..] Table 2. COMP Outputs redirection to embedded timers
87  +-----------------------------------+     
88  |      COMP1      |      COMP2      |
89  |-----------------|-----------------|
90  |                 |  TIM2 IC4       |
91  |                 |  TIM2 OCREF CLR |
92  | (no redirection |  TIM3 IC4       |
93  |   to timers)    |  TIM3 OCREF CLR |
94  |                 |  TIM4 IC4       |
95  |                 |  TIM4 OCREF CLR |
96  |                 |  TIM10 IC1      |
97  +-----------------------------------+
98
99
100             ##### How to use this driver #####
101 ================================================================================
102   [..]
103       This driver provides functions to configure and program the Comparators of all STM32L1xx devices.
104
105       To use the comparator, perform the following steps:
106   
107       (#)  Initialize the COMP low level resources by implementing the HAL_COMP_MspInit().
108       (++) Configure the comparator input I/O pin using HAL_GPIO_Init():
109            - For all inputs: I/O pin in analog mode (Schmitt trigger disabled)
110            - Possible alternate configuration, for non-inverting inputs of comparator 2: I/O pin in floating mode (Schmitt trigger enabled).
111            It is recommended to use analog configuration to avoid any overconsumption around VDD/2.
112       (++) Enable COMP Peripheral clock using macro __COMP_CLK_ENABLE()
113       (++) If required enable the COMP interrupt (EXTI line Interrupt): enable
114            the comparator interrupt vector using HAL_NVIC_EnableIRQ(COMP_IRQn)
115            and HAL_NVIC_SetPriority(COMP_IRQn, xxx, xxx) functions.
116   
117       (#) Configure the comparator using HAL_COMP_Init() function:
118       (++) Select the inverting input (COMP2 only)
119       (++) Select the non-inverting input
120       (++) Select the output redirection to timers (COMP2 only)
121       (++) Select the speed mode (COMP2 only)
122       (++) Select the window mode (related to COMP1 and COMP2, but selected 
123            by COMP2 only)
124       (++) Select the pull-up/down resistors on non-inverting input (COMP1 only)
125
126       (#) Enable the comparator using HAL_COMP_Start() or HAL_COMP_Start_IT() 
127           function
128     
129       (#) If needed, use HAL_COMP_GetOutputLevel() or HAL_COMP_TriggerCallback()
130           functions to manage comparator actions (output level or events)
131
132       (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT() 
133           function
134
135       (#) De-initialize the comparator using HAL_COMP_DeInit() function
136
137
138   @endverbatim
139   ******************************************************************************
140   * @attention
141   *
142   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
143   *
144   * Redistribution and use in source and binary forms, with or without modification,
145   * are permitted provided that the following conditions are met:
146   *   1. Redistributions of source code must retain the above copyright notice,
147   *      this list of conditions and the following disclaimer.
148   *   2. Redistributions in binary form must reproduce the above copyright notice,
149   *      this list of conditions and the following disclaimer in the documentation
150   *      and/or other materials provided with the distribution.
151   *   3. Neither the name of STMicroelectronics nor the names of its contributors
152   *      may be used to endorse or promote products derived from this software
153   *      without specific prior written permission.
154   *
155   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
156   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
157   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
158   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
159   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
160   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
161   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
162   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
163   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
164   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
165   *
166   ******************************************************************************  
167   */
168
169 /* Includes ------------------------------------------------------------------*/
170 #include "stm32l1xx_hal.h"
171
172 /** @addtogroup STM32L1xx_HAL_Driver
173   * @{
174   */
175
176 /** @defgroup COMP COMP
177   * @brief COMP HAL module driver
178   * @{
179   */
180
181 #ifdef HAL_COMP_MODULE_ENABLED
182
183 /* Private typedef -----------------------------------------------------------*/
184 /* Private define ------------------------------------------------------------*/
185
186 /** @defgroup COMP_Private_Constants COMP Private Constants
187   * @{
188   */
189   /* Delay for COMP startup time.                                             */
190   /* Maximum delay is 10us for comparator 1 and 25us for comparator 2 in slow */
191   /* mode (refer to device datasheet, parameter tSTART).                      */
192   /* Delay in CPU cycles, fixed to worst case: maximum CPU frequency 32MHz to */
193   /* have the minimum number of CPU cycles to fulfill this delay.             */
194   /*  - Comparator 1: delay minimum of 320 CPU cyles. Wait loop takes 3 CPU   */
195   /*                 cycles per iteration, therefore total wait iterations    */
196   /*                 number must be initialized at 106 iterations.            */
197   /*  - Comparator 2: delay minimum of 800 CPU cyles. Wait loop takes 3 CPU   */
198   /*                 cycles per iteration, therefore total wait iterations    */
199   /*                 number must be initialized at 266 iterations.            */
200 #define COMP1_START_DELAY_CPU_CYCLES       ((uint32_t)106)
201 #define COMP2_START_DELAY_CPU_CYCLES       ((uint32_t)266)
202
203   /* Comparator status "locked": to update COMP handle state (software lock   */
204   /* only on COMP of STM32L1xx devices) by bitfield:                          */
205   /* states HAL_COMP_STATE_READY_LOCKED, HAL_COMP_STATE_BUSY_LOCKED.          */
206 #define COMP_STATE_BIT_LOCK     ((uint32_t) 0x00000010)  
207
208 /**
209   * @}
210   */
211
212
213 /* Private macro -------------------------------------------------------------*/
214 /* Private variables ---------------------------------------------------------*/
215 /* Private function prototypes -----------------------------------------------*/
216 /* Private functions ---------------------------------------------------------*/
217
218 /** @defgroup COMP_Exported_Functions COMP Exported Functions
219   * @{
220   */
221
222 /** @defgroup COMP_Exported_Functions_Group1 Initialization and de-initialization functions 
223  *  @brief    Initialization and Configuration functions 
224  *
225 @verbatim    
226  ===============================================================================
227               ##### Initialization and de-initialization functions #####
228  ===============================================================================
229     [..]  This section provides functions to initialize and de-initialize comparators 
230
231 @endverbatim
232   * @{
233   */
234
235 /**
236   * @brief  Initializes the COMP according to the specified
237   *         parameters in the COMP_InitTypeDef and create the associated handle.
238   * @note   If the selected comparator is locked, initialization can't be performed.
239   *         To unlock the configuration, perform a system reset.
240   * @param  hcomp: COMP handle
241   * @retval HAL status
242   */
243 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
244
245   HAL_StatusTypeDef status = HAL_OK;
246   
247   /* Check the COMP handle allocation and lock status */
248   if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
249   {
250     status = HAL_ERROR;
251   }
252   else
253   {
254     /* Check the parameter */
255     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
256
257     if (hcomp->Instance == COMP1)
258     {
259       assert_param(IS_COMP_NONINVERTINGINPUTPULL(hcomp->Init.NonInvertingInputPull));
260     }
261     else /* if (hcomp->Instance == COMP2) */
262     {
263       assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
264       assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
265       assert_param(IS_COMP_MODE(hcomp->Init.Mode));
266       assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
267     }
268   
269     /* In window mode, non-inverting inputs of the 2 comparators are          */
270     /* connected together and are using inputs of COMP2 only. If COMP1 is     */
271     /* selected, this parameter is discarded.                                 */
272     if ((hcomp->Init.WindowMode == COMP_WINDOWMODE_DISABLED) ||
273         (hcomp->Instance == COMP2)                             )
274     {
275       assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
276     }
277     
278       
279     /* Enable SYSCFG clock and the low level hardware to access comparators */
280     if(hcomp->State == HAL_COMP_STATE_RESET)
281     {
282       /* Enable SYSCFG clock to control the routing Interface (RI) */
283       __SYSCFG_CLK_ENABLE();
284       
285       /* Init the low level hardware */
286       HAL_COMP_MspInit(hcomp);
287     }
288   
289     /* Configuration of comparator:                                           */
290     /*  - Output selection                                                    */
291     /*  - Inverting input selection                                           */
292     /*  - Window mode                                                         */
293     /*  - Mode fast/slow speed                                                */
294     /*  - Inverting input pull-up/down resistors                              */
295     
296     /* Configuration depending on comparator instance */
297     if (hcomp->Instance == COMP1)
298     {
299       MODIFY_REG(COMP->CSR, COMP_CSR_400KPD | COMP_CSR_10KPD | COMP_CSR_400KPU | COMP_CSR_10KPU,
300                             hcomp->Init.NonInvertingInputPull                                   );
301     }
302     else /* if (hcomp->Instance == COMP2) */
303     {
304       /* Note: If comparator 2 is not enabled, inverting input (parameter     */
305       /*       "hcomp->Init.InvertingInput") is configured into function      */
306       /*       "HAL_COMP_Start()" since inverting  input selection also       */
307       /*       enables the comparator 2.                                      */
308       /*       If comparator 2 is already enabled, inverting input is         */
309       /*       reconfigured on the fly.                                       */
310       if (__COMP_IS_ENABLED(hcomp) == RESET)
311       {
312         MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL  |
313                               COMP_CSR_WNDWE   |
314                               COMP_CSR_SPEED          ,
315                               hcomp->Init.Output     |
316                               hcomp->Init.WindowMode |
317                               hcomp->Init.Mode        );
318       }
319       else
320       {
321         MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL  |
322                               COMP_CSR_INSEL   |
323                               COMP_CSR_WNDWE   |
324                               COMP_CSR_SPEED              ,
325                               hcomp->Init.Output         |
326                               hcomp->Init.InvertingInput |
327                               hcomp->Init.WindowMode     |
328                               hcomp->Init.Mode            );
329       }
330     }
331     
332     /* Configure Routing Interface (RI) switches for comparator non-inverting */
333     /* input.                                                                 */
334     /* Except in 2 cases:                                                     */
335     /* - if non-inverting input has no selection: it can be the case for      */
336     /*   COMP1 in window mode.                                                */
337     /* - particular case for PC3: if switch COMP1_SW1 is closed               */
338     /*   (by macro "__HAL_OPAMP_OPAMP3OUT_CONNECT_ADC_COMP1()" or             */
339     /*   "__HAL_RI_SWITCH_COMP1_SW1_CLOSE()"), connection between pin PC3     */
340     /*    (or OPAMP3, if available) and COMP1 is done directly, without going */
341     /*    through ADC switch matrix.                                          */
342     if (__COMP_ROUTING_INTERFACE_TOBECONFIGURED(hcomp))
343     {
344       if (hcomp->Instance == COMP1)
345       {
346         /* Enable the switch control mode */
347         __HAL_RI_SWITCHCONTROLMODE_ENABLE();
348
349         /* Close the analog switch of ADC switch matrix to COMP1 (ADC         */
350         /* channel 26: Vcomp)                                                 */
351         __HAL_RI_IOSWITCH_CLOSE(RI_IOSWITCH_VCOMP);
352       }
353
354       /* Close the I/O analog switch corresponding to comparator              */
355       /* non-inverting input selected.                                        */
356       __HAL_RI_IOSWITCH_CLOSE(hcomp->Init.NonInvertingInput);
357     }
358
359     
360     /* Initialize the COMP state*/
361     if(hcomp->State == HAL_COMP_STATE_RESET)
362     {
363       hcomp->State = HAL_COMP_STATE_READY;
364     }
365   }
366   
367   return status;
368 }
369
370
371 /**
372   * @brief  DeInitializes the COMP peripheral 
373   * @note   Deinitialization can't be performed if the COMP configuration is locked.
374   *         To unlock the configuration, perform a system reset.
375   * @param  hcomp: COMP handle
376   * @retval HAL status
377   */
378 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
379 {
380   HAL_StatusTypeDef status = HAL_OK;
381
382   /* Check the COMP handle allocation and lock status */
383   if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
384   {
385     status = HAL_ERROR;
386   }
387   else
388   {
389     /* Check the parameter */
390     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
391     
392     /* Reset configuration depending on comparator instance */
393     if (hcomp->Instance == COMP1)
394     {
395       CLEAR_BIT(COMP->CSR , COMP_CSR_400KPD | COMP_CSR_10KPD | COMP_CSR_400KPU | COMP_CSR_10KPU);
396     }
397     else /* if (hcomp->Instance == COMP2) */
398     {
399       CLEAR_BIT(COMP->CSR , COMP_CSR_OUTSEL |
400                             COMP_CSR_WNDWE  |
401                             COMP_CSR_INSEL  |
402                             COMP_CSR_SPEED   );
403     }
404     
405     
406     /* Restore default state of Routing Interface (RI) switches for           */
407     /* comparator non-inverting input.                                        */
408     if (hcomp->Init.NonInvertingInput != COMP_NONINVERTINGINPUT_NONE)
409     {
410       /* Open the I/O analog switch corresponding to comparator               */
411       /* non-inverting input selected.                                        */
412       __HAL_RI_IOSWITCH_OPEN(hcomp->Init.NonInvertingInput);
413     }
414     if (hcomp->Instance == COMP1)
415     {
416       /* Open the analog switch of ADC switch matrix to COMP1 (ADC            */
417       /* channel 26: Vcomp)                                                   */
418       __HAL_RI_IOSWITCH_OPEN(RI_IOSWITCH_VCOMP);
419       
420       /* Disable the switch control mode */
421       __HAL_RI_SWITCHCONTROLMODE_DISABLE();
422     }
423   
424     
425     /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
426     HAL_COMP_MspDeInit(hcomp);
427
428     hcomp->State = HAL_COMP_STATE_RESET;
429     
430     /* Process unlocked */
431     __HAL_UNLOCK(hcomp);
432   }
433   
434   return status;
435 }
436
437 /**
438   * @brief  Initializes the COMP MSP.
439   * @param  hcomp: COMP handle
440   * @retval None
441   */
442 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
443 {
444   /* NOTE : This function Should not be modified, when the callback is needed,
445             the HAL_COMP_MspInit could be implenetd in the user file
446    */
447 }
448
449 /**
450   * @brief  DeInitializes COMP MSP.
451   * @param  hcomp: COMP handle
452   * @retval None
453   */
454 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
455 {
456   /* NOTE : This function Should not be modified, when the callback is needed,
457             the HAL_COMP_MspDeInit could be implenetd in the user file
458    */
459 }
460
461 /**
462   * @}
463   */
464
465 /** @defgroup COMP_Exported_Functions_Group2 I/O operation functions 
466  *  @brief   I/O operation functions
467  *
468 @verbatim   
469  ===============================================================================
470                       ##### IO operation functions #####
471  ===============================================================================  
472     [..]
473     This subsection provides a set of functions allowing to manage the COMP 
474     start and stop actions with or without interruption on ExtI line.
475
476 @endverbatim
477   * @{
478   */
479
480 /**
481   * @brief  Start the comparator 
482   * @param  hcomp: COMP handle
483   * @retval HAL status
484   */
485 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
486
487   HAL_StatusTypeDef status = HAL_OK;
488   uint32_t wait_loop_cycles = 0;
489   __IO uint32_t wait_loop_index = 0;
490   
491   /* Check the COMP handle allocation and lock status */
492   if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
493   {
494     status = HAL_ERROR;
495   }
496   else
497   {
498     /* Check the parameter */
499     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
500
501     if(hcomp->State == HAL_COMP_STATE_READY)
502     {
503       
504       /* Note: For comparator 2, inverting input (parameter                   */
505       /*       "hcomp->Init.InvertingInput") is configured into this          */
506       /*       function instead of function "HAL_COMP_Init()" since           */
507       /*       inverting input selection also enables the comparator 2.       */
508       __HAL_COMP_ENABLE(hcomp);
509
510       /* Set delay for COMP startup time */
511       if (hcomp->Instance == COMP1)
512       {
513         wait_loop_cycles = COMP1_START_DELAY_CPU_CYCLES;
514       }
515       else /* if (hcomp->Instance == COMP2) */
516       {
517         wait_loop_cycles = COMP2_START_DELAY_CPU_CYCLES;
518       }
519
520       /* Delay for COMP startup time.                                         */
521       /* Delay fixed to worst case: maximum CPU frequency                     */
522       while(wait_loop_index < wait_loop_cycles)
523       {
524         wait_loop_index++;
525       }
526
527       /* Update COMP state */
528       hcomp->State = HAL_COMP_STATE_BUSY;
529       
530     }
531     else
532     {
533       status = HAL_ERROR;
534     }
535   }
536
537   return status;
538 }
539
540 /**
541   * @brief  Stop the comparator 
542   * @param  hcomp: COMP handle
543   * @retval HAL status
544   */
545 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
546
547   HAL_StatusTypeDef status = HAL_OK;
548   
549   /* Check the COMP handle allocation and lock status */
550   if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
551   {
552     status = HAL_ERROR;
553   }
554   else
555   {
556     /* Check the parameter */
557     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
558
559     if(hcomp->State == HAL_COMP_STATE_BUSY)
560     {
561       /* Disable the selected comparator */
562       __HAL_COMP_DISABLE(hcomp);
563       
564       /* Update COMP state */
565       hcomp->State = HAL_COMP_STATE_READY;
566     }
567     else
568     {
569       status = HAL_ERROR;
570     }
571   }
572   
573   return status;
574 }
575
576 /**
577   * @brief  Enables the interrupt and starts the comparator
578   * @param  hcomp: COMP handle
579   * @retval HAL status.
580   */
581 HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
582
583   HAL_StatusTypeDef status = HAL_OK; 
584   uint32_t extiline = 0;
585   
586   status = HAL_COMP_Start(hcomp);
587   if(status == HAL_OK)
588   {
589     /* Check the parameter */
590     assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
591     
592     /* Get the Exti Line output configuration */
593     extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
594     
595     /* Configure the rising edge */
596     /* COMP TriggerMode set to COMP_TRIGGERMODE_IT_RISING or                  */
597     /* COMP_TRIGGERMODE_IT_RISING_FALLING.                                    */
598     if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
599     {
600       __HAL_COMP_EXTI_RISING_IT_ENABLE(extiline);
601     }
602     else
603     {
604       __HAL_COMP_EXTI_RISING_IT_DISABLE(extiline);
605     }
606     
607     /* Configure the falling edge */
608     /* COMP TriggerMode set to COMP_TRIGGERMODE_IT_FALLING or                 */
609     /* COMP_TRIGGERMODE_IT_RISING_FALLING.                                    */
610     if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
611     {
612       __HAL_COMP_EXTI_FALLING_IT_ENABLE(extiline);
613     }
614     else
615     {
616       __HAL_COMP_EXTI_FALLING_IT_DISABLE(extiline);
617     }
618     
619     /* Enable Exti interrupt mode */
620     __HAL_COMP_EXTI_ENABLE_IT(extiline);
621     /* Clear COMP Exti pending bit */
622     __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
623   }
624
625   return status;
626 }
627
628 /**
629   * @brief  Disable the interrupt and Stop the comparator 
630   * @param  hcomp: COMP handle
631   * @retval HAL status
632   */
633 HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
634
635   HAL_StatusTypeDef status = HAL_OK;
636   
637   /* Disable the Exti Line interrupt mode */
638   __HAL_COMP_EXTI_DISABLE_IT(__HAL_COMP_GET_EXTI_LINE(hcomp->Instance));
639   
640   status = HAL_COMP_Stop(hcomp);
641   
642   return status;
643 }
644
645 /**
646   * @brief  Comparator IRQ Handler 
647   * @param  hcomp: COMP handle
648   * @retval HAL status
649   */
650 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
651 {
652   uint32_t extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
653   
654   /* Check COMP Exti flag */
655   if(__HAL_COMP_EXTI_GET_FLAG(extiline) != RESET)
656   {
657     /* Clear COMP Exti pending bit */
658     __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
659
660     /* COMP trigger user callback */
661     HAL_COMP_TriggerCallback(hcomp);    
662   }
663 }
664
665 /**
666   * @}
667   */
668
669 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
670  *  @brief   Peripheral Control functions
671  *
672 @verbatim   
673  ===============================================================================
674                       ##### Peripheral Control functions #####
675  ===============================================================================  
676     [..]
677     This subsection provides a set of functions allowing to control the COMP 
678     management functions: Lock status, comparator output level check, IRQ
679     callback (in case of usage of comparator with interruption on ExtI line).
680
681 @endverbatim
682   * @{
683   */
684
685 /**
686   * @brief  Lock the selected comparator configuration.
687   *         Caution: On STM32L1, HAL COMP lock is software lock only (not  
688   *         hardware lock as on some other STM32 devices)
689   * @param  hcomp: COMP handle
690   * @retval HAL status
691   */
692 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
693 {
694   HAL_StatusTypeDef status = HAL_OK;
695
696   /* Check the COMP handle allocation and lock status */
697   if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
698   {
699     status = HAL_ERROR;
700   }
701   else
702   {
703     /* Check the parameter */
704     assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
705
706     /* Set lock flag */
707     hcomp->State |= COMP_STATE_BIT_LOCK;
708   }
709   
710   return status; 
711 }
712
713 /**
714   * @brief  Return the output level (high or low) of the selected comparator. 
715   *         The output level depends on the selected polarity.
716   *           - Comparator output is low when the non-inverting input is at a lower
717   *             voltage than the inverting input
718   *           - Comparator output is high when the non-inverting input is at a higher
719   *             voltage than the inverting input
720   * @param  hcomp: COMP handle
721   * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
722   *       
723   */
724 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
725 {
726   uint32_t level = 0;
727   
728   /* Check the parameter */
729   assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
730
731   /* Read output level of the selected comparator */
732   if(READ_BIT(COMP->CSR, __COMP_CSR_CMPXOUT(hcomp)) == RESET)
733   {
734     level = COMP_OUTPUTLEVEL_LOW;
735   }
736   else
737   {
738     level = COMP_OUTPUTLEVEL_HIGH;
739   }
740   
741   return(level);
742 }
743
744 /**
745   * @brief  Comparator callback.
746   * @param  hcomp: COMP handle
747   * @retval None
748   */
749 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
750 {
751   /* NOTE : This function should not be modified, when the callback is needed,
752             the HAL_COMP_TriggerCallback should be implemented in the user file
753    */
754 }
755
756
757 /**
758   * @}
759   */
760
761 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions 
762  *  @brief   Peripheral State functions
763  *
764 @verbatim   
765  ===============================================================================
766                       ##### Peripheral State functions #####
767  ===============================================================================  
768     [..]
769     This subsection permit to get in run-time the status of the peripheral.
770
771 @endverbatim
772   * @{
773   */
774
775 /**
776   * @brief  Return the COMP state
777   * @param  hcomp : COMP handle
778   * @retval HAL state
779   */
780 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
781 {
782   /* Check the COMP handle allocation */
783   if(hcomp == HAL_NULL)
784   {
785     return HAL_COMP_STATE_RESET;
786   }
787
788   /* Check the parameter */
789   assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
790
791   return hcomp->State;
792 }
793 /**
794   * @}
795   */
796
797 /**
798   * @}
799   */
800
801 #endif /* HAL_COMP_MODULE_ENABLED */
802 /**
803   * @}
804   */
805
806 /**
807   * @}
808   */
809
810 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/