1 /**
2 ******************************************************************************
3 * @file stm32f0xx_hal_comp.c
4 * @author MCD Application Team
5 * @brief COMP HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the COMP peripheral:
8 * + Initialization/de-initialization functions
9 * + Peripheral Control functions
10 * + Peripheral State functions
11 *
12 ******************************************************************************
13 * @attention
14 *
15 * Copyright (c) 2016 STMicroelectronics.
16 * All rights reserved.
17 *
18 * This software is licensed under terms that can be found in the LICENSE file
19 * in the root directory of this software component.
20 * If no LICENSE file comes with this software, it is provided AS-IS.
21 *
22 ******************************************************************************
23 @verbatim
24 ================================================================================
25 ##### COMP Peripheral features #####
26 ================================================================================
27
28 [..]
29 The STM32F0xx device family integrates up to 2 analog comparators COMP1 and COMP2:
30 (+) The non inverting input and inverting input can be set to GPIO pins.
31
32 (+) The COMP output is available using HAL_COMP_GetOutputLevel()
33 and can be set on GPIO pins.
34
35 (+) The COMP output can be redirected to embedded timers (TIM1, TIM2 and TIM3).
36
37 (+) The comparators COMP1 and COMP2 can be combined in window mode.
38
39 (+) The comparators have interrupt capability with wake-up
40 from Sleep and Stop modes (through the EXTI controller):
41 (++) COMP1 is internally connected to EXTI Line 21
42 (++) COMP2 is internally connected to EXTI Line 22
43
44 (+) From the corresponding IRQ handler, the right interrupt source can be retrieved with the
45 macros __HAL_COMP_COMP1_EXTI_GET_FLAG() and __HAL_COMP_COMP2_EXTI_GET_FLAG().
46
47
48 ##### How to use this driver #####
49 ================================================================================
50 [..]
51 This driver provides functions to configure and program the Comparators of STM32F05x, STM32F07x and STM32F09x devices.
52
53 To use the comparator, perform the following steps:
54
55 (#) Fill in the HAL_COMP_MspInit() to
56 (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
57 (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
58 output to the GPIO pin
59 (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
60 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
61 interrupt vector using HAL_NVIC_EnableIRQ() function.
62
63 (#) Configure the comparator using HAL_COMP_Init() function:
64 (++) Select the inverting input (input minus)
65 (++) Select the non inverting input (input plus)
66 (++) Select the output polarity
67 (++) Select the output redirection
68 (++) Select the hysteresis level
69 (++) Select the power mode
70 (++) Select the event/interrupt mode
71 (++) Select the window mode
72
73 -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order
74 to access the comparator(s) registers.
75
76 (#) Enable the comparator using HAL_COMP_Start() function or HAL_COMP_Start_IT() function for interrupt mode.
77
78 (#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions
79 to manage comparator outputs (event/interrupt triggered and output level).
80
81 (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
82 function.
83
84 (#) De-initialize the comparator using HAL_COMP_DeInit() function.
85
86 (#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function.
87 Only a MCU reset can reset that protection.
88
89 *** Callback registration ***
90 =============================================
91 [..]
92
93 The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
94 allows the user to configure dynamically the driver callbacks.
95 Use Functions HAL_COMP_RegisterCallback()
96 to register an interrupt callback.
97 [..]
98
99 Function HAL_COMP_RegisterCallback() allows to register following callbacks:
100 (+) OperationCpltCallback : callback for End of operation.
101 (+) ErrorCallback : callback for error detection.
102 (+) MspInitCallback : callback for Msp Init.
103 (+) MspDeInitCallback : callback for Msp DeInit.
104 This function takes as parameters the HAL peripheral handle, the Callback ID
105 and a pointer to the user callback function.
106 [..]
107
108 Use function HAL_COMP_UnRegisterCallback to reset a callback to the default
109 weak function.
110 [..]
111
112 HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
113 and the Callback ID.
114 This function allows to reset following callbacks:
115 (+) OperationCpltCallback : callback for End of operation.
116 (+) ErrorCallback : callback for error detection.
117 (+) MspInitCallback : callback for Msp Init.
118 (+) MspDeInitCallback : callback for Msp DeInit.
119 [..]
120
121 By default, after the HAL_COMP_Init() and when the state is HAL_COMP_STATE_RESET
122 all callbacks are set to the corresponding weak functions:
123 examples HAL_COMP_OperationCpltCallback(), HAL_COMP_ErrorCallback().
124 Exception done for MspInit and MspDeInit functions that are
125 reset to the legacy weak functions in the HAL_COMP_Init()/ HAL_COMP_DeInit() only when
126 these callbacks are null (not registered beforehand).
127 [..]
128
129 If MspInit or MspDeInit are not null, the HAL_COMP_Init()/ HAL_COMP_DeInit()
130 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
131 [..]
132
133 Callbacks can be registered/unregistered in HAL_COMP_STATE_READY state only.
134 Exception done MspInit/MspDeInit functions that can be registered/unregistered
135 in HAL_COMP_STATE_READY or HAL_COMP_STATE_RESET state,
136 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
137 [..]
138
139 Then, the user first registers the MspInit/MspDeInit user callbacks
140 using HAL_COMP_RegisterCallback() before calling HAL_COMP_DeInit()
141 or HAL_COMP_Init() function.
142 [..]
143
144 When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
145 not defined, the callback registration feature is not available and all callbacks
146 are set to the corresponding weak functions.
147
148 @endverbatim
149 ******************************************************************************
150 */
151
152 /*
153 Additional Tables:
154
155 Table 1. COMP Inputs for the STM32F05x, STM32F07x and STM32F09x devices
156 +--------------------------------------------------+
157 | | | COMP1 | COMP2 |
158 |-----------------|----------------|---------------|
159 | | 1/4 VREFINT | OK | OK |
160 | | 1/2 VREFINT | OK | OK |
161 | | 3/4 VREFINT | OK | OK |
162 | Inverting Input | VREFINT | OK | OK |
163 | | DAC1 OUT (PA4) | OK | OK |
164 | | DAC2 OUT (PA5) | OK | OK |
165 | | IO1 | PA0 | PA2 |
166 |-----------------|----------------|-------|-------|
167 | Non Inverting | | PA1 | PA3 |
168 | Input | | | |
169 +--------------------------------------------------+
170
171 Table 2. COMP Outputs for the STM32F05x, STM32F07x and STM32F09x devices
172 +---------------+
173 | COMP1 | COMP2 |
174 |-------|-------|
175 | PA0 | PA2 |
176 | PA6 | PA7 |
177 | PA11 | PA12 |
178 +---------------+
179
180 Table 3. COMP Outputs redirection to embedded timers for the STM32F05x, STM32F07x and STM32F09x devices
181 +---------------------------------+
182 | COMP1 | COMP2 |
183 |----------------|----------------|
184 | TIM1 BKIN | TIM1 BKIN |
185 | | |
186 | TIM1 OCREFCLR | TIM1 OCREFCLR |
187 | | |
188 | TIM1 IC1 | TIM1 IC1 |
189 | | |
190 | TIM2 IC4 | TIM2 IC4 |
191 | | |
192 | TIM2 OCREFCLR | TIM2 OCREFCLR |
193 | | |
194 | TIM3 IC1 | TIM3 IC1 |
195 | | |
196 | TIM3 OCREFCLR | TIM3 OCREFCLR |
197 +---------------------------------+
198
199 */
200
201 /* Includes ------------------------------------------------------------------*/
202 #include "stm32f0xx_hal.h"
203
204 #ifdef HAL_COMP_MODULE_ENABLED
205
206 #if defined (COMP1) || defined (COMP2)
207
208 /** @addtogroup STM32F0xx_HAL_Driver
209 * @{
210 */
211
212 /** @defgroup COMP COMP
213 * @brief COMP HAL module driver
214 * @{
215 */
216
217 /* Private typedef -----------------------------------------------------------*/
218 /* Private define ------------------------------------------------------------*/
219
220 /** @defgroup COMP_Private_Constants COMP Private Constants
221 * @{
222 */
223
224 /* Delay for COMP startup time. */
225 /* Note: Delay required to reach propagation delay specification. */
226 /* Literal set to maximum value (refer to device datasheet, */
227 /* parameter "tSTART"). */
228 /* Unit: us */
229 #define COMP_DELAY_STARTUP_US (60U) /*!< Delay for COMP startup time */
230
231 /* CSR register reset value */
232 #define COMP_CSR_RESET_VALUE (0x00000000U)
233 /* CSR register masks */
234 #define COMP_CSR_RESET_PARAMETERS_MASK (0x00003FFFU)
235 #define COMP_CSR_UPDATE_PARAMETERS_MASK (0x00003FFEU)
236 /* CSR COMPx non inverting input mask */
237 #define COMP_CSR_COMPxNONINSEL_MASK ((uint16_t)COMP_CSR_COMP1SW1)
238 /* CSR COMP2 shift */
239 #define COMP_CSR_COMP1_SHIFT 0U
240 #define COMP_CSR_COMP2_SHIFT 16U
241 /**
242 * @}
243 */
244 /* Private macro -------------------------------------------------------------*/
245 /* Private variables ---------------------------------------------------------*/
246 /* Private function prototypes -----------------------------------------------*/
247 /* Private functions ---------------------------------------------------------*/
248
249 /** @defgroup COMP_Exported_Functions COMP Exported Functions
250 * @{
251 */
252
253 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
254 * @brief Initialization and Configuration functions
255 *
256 @verbatim
257 ===============================================================================
258 ##### Initialization and Configuration functions #####
259 ===============================================================================
260 [..] This section provides functions to initialize and de-initialize comparators
261
262 @endverbatim
263 * @{
264 */
265
266 /**
267 * @brief Initializes the COMP according to the specified
268 * parameters in the COMP_InitTypeDef and create the associated handle.
269 * @note If the selected comparator is locked, initialization can't be performed.
270 * To unlock the configuration, perform a system reset.
271 * @param hcomp COMP handle
272 * @retval HAL status
273 */
HAL_COMP_Init(COMP_HandleTypeDef * hcomp)274 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
275 {
276 HAL_StatusTypeDef status = HAL_OK;
277 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
278
279 /* Check the COMP handle allocation and lock status */
280 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
281 {
282 status = HAL_ERROR;
283 }
284 else
285 {
286 /* Check the parameter */
287 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
288 assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
289 assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
290 assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
291 assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
292 assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
293 assert_param(IS_COMP_MODE(hcomp->Init.Mode));
294
295 if(hcomp->Init.NonInvertingInput == COMP_NONINVERTINGINPUT_DAC1SWITCHCLOSED)
296 {
297 assert_param(IS_COMP_DAC1SWITCH_INSTANCE(hcomp->Instance));
298 }
299
300 if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
301 {
302 assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
303 }
304
305 /* Init SYSCFG and the low level hardware to access comparators */
306 __HAL_RCC_SYSCFG_CLK_ENABLE();
307
308 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
309 /* Init the COMP Callback settings */
310 hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
311
312 if (hcomp->MspInitCallback == NULL)
313 {
314 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
315 }
316
317 /* Init the low level hardware */
318 hcomp->MspInitCallback(hcomp);
319 #else
320 /* Init the low level hardware : SYSCFG to access comparators */
321 HAL_COMP_MspInit(hcomp);
322 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
323
324 if(hcomp->State == HAL_COMP_STATE_RESET)
325 {
326 /* Allocate lock resource and initialize it */
327 hcomp->Lock = HAL_UNLOCKED;
328 }
329
330 /* Change COMP peripheral state */
331 hcomp->State = HAL_COMP_STATE_BUSY;
332
333 /* Set COMP parameters */
334 /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
335 /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */
336 /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
337 /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */
338 /* Set COMPxMODE bits according to hcomp->Init.Mode value */
339 if(hcomp->Instance == COMP2)
340 {
341 regshift = COMP_CSR_COMP2_SHIFT;
342 }
343 MODIFY_REG(COMP->CSR,
344 (COMP_CSR_COMPxINSEL | COMP_CSR_COMPxNONINSEL_MASK | \
345 COMP_CSR_COMPxOUTSEL | COMP_CSR_COMPxPOL | \
346 COMP_CSR_COMPxHYST | COMP_CSR_COMPxMODE) << regshift,
347 (hcomp->Init.InvertingInput | \
348 hcomp->Init.NonInvertingInput | \
349 hcomp->Init.Output | \
350 hcomp->Init.OutputPol | \
351 hcomp->Init.Hysteresis | \
352 hcomp->Init.Mode) << regshift);
353
354 if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
355 {
356 COMP->CSR |= COMP_CSR_WNDWEN;
357 }
358
359 /* Initialize the COMP state*/
360 hcomp->State = HAL_COMP_STATE_READY;
361 }
362
363 return status;
364 }
365
366 /**
367 * @brief DeInitializes the COMP peripheral
368 * @note Deinitialization can't be performed if the COMP configuration is locked.
369 * To unlock the configuration, perform a system reset.
370 * @param hcomp COMP handle
371 * @retval HAL status
372 */
HAL_COMP_DeInit(COMP_HandleTypeDef * hcomp)373 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
374 {
375 HAL_StatusTypeDef status = HAL_OK;
376 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
377
378 /* Check the COMP handle allocation and lock status */
379 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
380 {
381 status = HAL_ERROR;
382 }
383 else
384 {
385 /* Check the parameter */
386 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
387
388 /* Set COMP_CSR register to reset value for the corresponding COMP instance */
389 if(hcomp->Instance == COMP2)
390 {
391 regshift = COMP_CSR_COMP2_SHIFT;
392 }
393 MODIFY_REG(COMP->CSR,
394 COMP_CSR_RESET_PARAMETERS_MASK << regshift,
395 COMP_CSR_RESET_VALUE << regshift);
396
397 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
398 if (hcomp->MspDeInitCallback == NULL)
399 {
400 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
401 }
402
403 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
404 hcomp->MspDeInitCallback(hcomp);
405 #else
406 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
407 HAL_COMP_MspDeInit(hcomp);
408 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
409
410 hcomp->State = HAL_COMP_STATE_RESET;
411
412 /* Release Lock */
413 __HAL_UNLOCK(hcomp);
414 }
415
416 return status;
417 }
418
419 /**
420 * @brief Initializes the COMP MSP.
421 * @param hcomp COMP handle
422 * @retval None
423 */
HAL_COMP_MspInit(COMP_HandleTypeDef * hcomp)424 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
425 {
426 /* Prevent unused argument(s) compilation warning */
427 UNUSED(hcomp);
428
429 /* NOTE : This function Should not be modified, when the callback is needed,
430 the HAL_COMP_MspInit could be implenetd in the user file
431 */
432 }
433
434 /**
435 * @brief DeInitializes COMP MSP.
436 * @param hcomp COMP handle
437 * @retval None
438 */
HAL_COMP_MspDeInit(COMP_HandleTypeDef * hcomp)439 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
440 {
441 /* Prevent unused argument(s) compilation warning */
442 UNUSED(hcomp);
443
444 /* NOTE : This function Should not be modified, when the callback is needed,
445 the HAL_COMP_MspDeInit could be implenetd in the user file
446 */
447 }
448
449 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
450 /**
451 * @brief Register a User COMP Callback
452 * To be used instead of the weak predefined callback
453 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
454 * the configuration information for the specified COMP.
455 * @param CallbackID ID of the callback to be registered
456 * This parameter can be one of the following values:
457 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
458 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
459 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
460 * @param pCallback pointer to the Callback function
461 * @retval HAL status
462 */
HAL_COMP_RegisterCallback(COMP_HandleTypeDef * hcomp,HAL_COMP_CallbackIDTypeDef CallbackID,pCOMP_CallbackTypeDef pCallback)463 HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
464 {
465 HAL_StatusTypeDef status = HAL_OK;
466
467 if (pCallback == NULL)
468 {
469 /* Update the error code */
470 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
471
472 return HAL_ERROR;
473 }
474
475 if (HAL_COMP_STATE_READY == hcomp->State)
476 {
477 switch (CallbackID)
478 {
479 case HAL_COMP_TRIGGER_CB_ID :
480 hcomp->TriggerCallback = pCallback;
481 break;
482
483 case HAL_COMP_MSPINIT_CB_ID :
484 hcomp->MspInitCallback = pCallback;
485 break;
486
487 case HAL_COMP_MSPDEINIT_CB_ID :
488 hcomp->MspDeInitCallback = pCallback;
489 break;
490
491 default :
492 /* Update the error code */
493 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
494
495 /* Return error status */
496 status = HAL_ERROR;
497 break;
498 }
499 }
500 else if (HAL_COMP_STATE_RESET == hcomp->State)
501 {
502 switch (CallbackID)
503 {
504 case HAL_COMP_MSPINIT_CB_ID :
505 hcomp->MspInitCallback = pCallback;
506 break;
507
508 case HAL_COMP_MSPDEINIT_CB_ID :
509 hcomp->MspDeInitCallback = pCallback;
510 break;
511
512 default :
513 /* Update the error code */
514 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
515
516 /* Return error status */
517 status = HAL_ERROR;
518 break;
519 }
520 }
521 else
522 {
523 /* Update the error code */
524 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
525
526 /* Return error status */
527 status = HAL_ERROR;
528 }
529
530 return status;
531 }
532
533 /**
534 * @brief Unregister a COMP Callback
535 * COMP callback is redirected to the weak predefined callback
536 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
537 * the configuration information for the specified COMP.
538 * @param CallbackID ID of the callback to be unregistered
539 * This parameter can be one of the following values:
540 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
541 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
542 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
543 * @retval HAL status
544 */
HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef * hcomp,HAL_COMP_CallbackIDTypeDef CallbackID)545 HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
546 {
547 HAL_StatusTypeDef status = HAL_OK;
548
549 if (HAL_COMP_STATE_READY == hcomp->State)
550 {
551 switch (CallbackID)
552 {
553 case HAL_COMP_TRIGGER_CB_ID :
554 hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
555 break;
556
557 case HAL_COMP_MSPINIT_CB_ID :
558 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
559 break;
560
561 case HAL_COMP_MSPDEINIT_CB_ID :
562 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
563 break;
564
565 default :
566 /* Update the error code */
567 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
568
569 /* Return error status */
570 status = HAL_ERROR;
571 break;
572 }
573 }
574 else if (HAL_COMP_STATE_RESET == hcomp->State)
575 {
576 switch (CallbackID)
577 {
578 case HAL_COMP_MSPINIT_CB_ID :
579 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
580 break;
581
582 case HAL_COMP_MSPDEINIT_CB_ID :
583 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
584 break;
585
586 default :
587 /* Update the error code */
588 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
589
590 /* Return error status */
591 status = HAL_ERROR;
592 break;
593 }
594 }
595 else
596 {
597 /* Update the error code */
598 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
599
600 /* Return error status */
601 status = HAL_ERROR;
602 }
603
604 return status;
605 }
606
607 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
608
609 /**
610 * @}
611 */
612
613 /** @defgroup COMP_Exported_Functions_Group2 I/O operation functions
614 * @brief Data transfers functions
615 *
616 @verbatim
617 ===============================================================================
618 ##### IO operation functions #####
619 ===============================================================================
620 [..]
621 This subsection provides a set of functions allowing to manage the COMP data
622 transfers.
623
624 @endverbatim
625 * @{
626 */
627
628 /**
629 * @brief Start the comparator
630 * @param hcomp COMP handle
631 * @retval HAL status
632 */
HAL_COMP_Start(COMP_HandleTypeDef * hcomp)633 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
634 {
635 uint32_t wait_loop_index = 0U;
636 HAL_StatusTypeDef status = HAL_OK;
637 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
638
639 /* Check the COMP handle allocation and lock status */
640 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
641 {
642 status = HAL_ERROR;
643 }
644 else
645 {
646 /* Check the parameter */
647 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
648
649 if(hcomp->State == HAL_COMP_STATE_READY)
650 {
651 /* Enable the selected comparator */
652 if(hcomp->Instance == COMP2)
653 {
654 regshift = COMP_CSR_COMP2_SHIFT;
655 }
656 SET_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift);
657
658 /* Set HAL COMP handle state */
659 hcomp->State = HAL_COMP_STATE_BUSY;
660
661 /* Delay for COMP startup time */
662 wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / 1000000U));
663 while(wait_loop_index != 0U)
664 {
665 wait_loop_index--;
666 }
667 }
668 else
669 {
670 status = HAL_ERROR;
671 }
672 }
673
674 return status;
675 }
676
677 /**
678 * @brief Stop the comparator
679 * @param hcomp COMP handle
680 * @retval HAL status
681 */
HAL_COMP_Stop(COMP_HandleTypeDef * hcomp)682 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
683 {
684 HAL_StatusTypeDef status = HAL_OK;
685 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
686
687 /* Check the COMP handle allocation and lock status */
688 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
689 {
690 status = HAL_ERROR;
691 }
692 else
693 {
694 /* Check the parameter */
695 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
696
697 if(hcomp->State == HAL_COMP_STATE_BUSY)
698 {
699 /* Disable the selected comparator */
700 if(hcomp->Instance == COMP2)
701 {
702 regshift = COMP_CSR_COMP2_SHIFT;
703 }
704 CLEAR_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift);
705
706 hcomp->State = HAL_COMP_STATE_READY;
707 }
708 else
709 {
710 status = HAL_ERROR;
711 }
712 }
713
714 return status;
715 }
716
717 /**
718 * @brief Enables the interrupt and starts the comparator
719 * @param hcomp COMP handle
720 * @retval HAL status.
721 */
HAL_COMP_Start_IT(COMP_HandleTypeDef * hcomp)722 HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
723 {
724 HAL_StatusTypeDef status = HAL_OK;
725 uint32_t extiline = 0U;
726
727 /* Check the parameter */
728 assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
729
730 status = HAL_COMP_Start(hcomp);
731 if(status == HAL_OK)
732 {
733 /* Check the Exti Line output configuration */
734 extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
735 /* Configure the rising edge */
736 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
737 {
738 SET_BIT(EXTI->RTSR, extiline);
739 }
740 else
741 {
742 CLEAR_BIT(EXTI->RTSR, extiline);
743 }
744 /* Configure the falling edge */
745 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
746 {
747 SET_BIT(EXTI->FTSR, extiline);
748 }
749 else
750 {
751 CLEAR_BIT(EXTI->FTSR, extiline);
752 }
753
754 /* Clear COMP EXTI pending bit */
755 WRITE_REG(EXTI->PR, extiline);
756
757 /* Enable Exti interrupt mode */
758 SET_BIT(EXTI->IMR, extiline);
759 }
760
761 return status;
762 }
763
764 /**
765 * @brief Disable the interrupt and Stop the comparator
766 * @param hcomp COMP handle
767 * @retval HAL status
768 */
HAL_COMP_Stop_IT(COMP_HandleTypeDef * hcomp)769 HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
770 {
771 HAL_StatusTypeDef status = HAL_OK;
772
773 /* Disable the Exti Line interrupt mode */
774 CLEAR_BIT(EXTI->IMR, COMP_GET_EXTI_LINE(hcomp->Instance));
775
776 status = HAL_COMP_Stop(hcomp);
777
778 return status;
779 }
780
781 /**
782 * @brief Comparator IRQ Handler
783 * @param hcomp COMP handle
784 * @retval HAL status
785 */
HAL_COMP_IRQHandler(COMP_HandleTypeDef * hcomp)786 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
787 {
788 uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
789
790 /* Check COMP Exti flag */
791 if(READ_BIT(EXTI->PR, extiline) != RESET)
792 {
793 /* Clear COMP Exti pending bit */
794 WRITE_REG(EXTI->PR, extiline);
795
796 /* COMP trigger callback */
797 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
798 hcomp->TriggerCallback(hcomp);
799 #else
800 HAL_COMP_TriggerCallback(hcomp);
801 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
802 }
803 }
804
805 /**
806 * @}
807 */
808
809 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
810 * @brief management functions
811 *
812 @verbatim
813 ===============================================================================
814 ##### Peripheral Control functions #####
815 ===============================================================================
816 [..]
817 This subsection provides a set of functions allowing to control the COMP data
818 transfers.
819
820 @endverbatim
821 * @{
822 */
823
824 /**
825 * @brief Lock the selected comparator configuration.
826 * @param hcomp COMP handle
827 * @retval HAL status
828 */
HAL_COMP_Lock(COMP_HandleTypeDef * hcomp)829 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
830 {
831 HAL_StatusTypeDef status = HAL_OK;
832 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
833
834 /* Check the COMP handle allocation and lock status */
835 if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
836 {
837 status = HAL_ERROR;
838 }
839 else
840 {
841 /* Check the parameter */
842 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
843
844 /* Set lock flag */
845 hcomp->State |= COMP_STATE_BIT_LOCK;
846
847 /* Set the lock bit corresponding to selected comparator */
848 if(hcomp->Instance == COMP2)
849 {
850 regshift = COMP_CSR_COMP2_SHIFT;
851 }
852 SET_BIT(COMP->CSR, COMP_CSR_COMPxLOCK << regshift);
853 }
854
855 return status;
856 }
857
858 /**
859 * @brief Return the output level (high or low) of the selected comparator.
860 * The output level depends on the selected polarity.
861 * If the polarity is not inverted:
862 * - Comparator output is low when the non-inverting input is at a lower
863 * voltage than the inverting input
864 * - Comparator output is high when the non-inverting input is at a higher
865 * voltage than the inverting input
866 * If the polarity is inverted:
867 * - Comparator output is high when the non-inverting input is at a lower
868 * voltage than the inverting input
869 * - Comparator output is low when the non-inverting input is at a higher
870 * voltage than the inverting input
871 * @param hcomp COMP handle
872 * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
873 *
874 */
HAL_COMP_GetOutputLevel(COMP_HandleTypeDef * hcomp)875 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
876 {
877 uint32_t level=0;
878 uint32_t regshift = COMP_CSR_COMP1_SHIFT;
879
880 /* Check the parameter */
881 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
882
883 if(hcomp->Instance == COMP2)
884 {
885 regshift = COMP_CSR_COMP2_SHIFT;
886 }
887 level = READ_BIT(COMP->CSR, COMP_CSR_COMPxOUT << regshift);
888
889 if(level != 0U)
890 {
891 return(COMP_OUTPUTLEVEL_HIGH);
892 }
893 return(COMP_OUTPUTLEVEL_LOW);
894 }
895
896 /**
897 * @brief Comparator trigger callback.
898 * @param hcomp COMP handle
899 * @retval None
900 */
HAL_COMP_TriggerCallback(COMP_HandleTypeDef * hcomp)901 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
902 {
903 /* Prevent unused argument(s) compilation warning */
904 UNUSED(hcomp);
905
906 /* NOTE : This function should not be modified, when the callback is needed,
907 the HAL_COMP_TriggerCallback should be implemented in the user file
908 */
909 }
910
911
912 /**
913 * @}
914 */
915
916 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
917 * @brief Peripheral State functions
918 *
919 @verbatim
920 ===============================================================================
921 ##### Peripheral State functions #####
922 ===============================================================================
923 [..]
924 This subsection permit to get in run-time the status of the peripheral
925 and the data flow.
926
927 @endverbatim
928 * @{
929 */
930
931 /**
932 * @brief Return the COMP state
933 * @param hcomp COMP handle
934 * @retval HAL state
935 */
HAL_COMP_GetState(COMP_HandleTypeDef * hcomp)936 uint32_t HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
937 {
938 /* Check the COMP handle allocation */
939 if(hcomp == NULL)
940 {
941 return HAL_COMP_STATE_RESET;
942 }
943
944 /* Check the parameter */
945 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
946
947 return hcomp->State;
948 }
949
950 /**
951 * @brief Return the COMP error code.
952 * @param hcomp COMP handle
953 * @retval COMP error code
954 */
HAL_COMP_GetError(COMP_HandleTypeDef * hcomp)955 uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
956 {
957 /* Check the parameters */
958 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
959
960 return hcomp->ErrorCode;
961 }
962
963 /**
964 * @}
965 */
966
967 /**
968 * @}
969 */
970
971 /**
972 * @}
973 */
974
975 /**
976 * @}
977 */
978
979 #endif /* COMP1 || COMP2 */
980
981 #endif /* HAL_COMP_MODULE_ENABLED */
982
983