1 /**
2 ******************************************************************************
3 * @file stm32f3xx_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 and 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 STM32F3xx device family integrates up to 7 analog comparators COMP1, COMP2...COMP7:
30 (#) The non inverting input and inverting input can be set to GPIO pins.
31 For STM32F3xx devices please refer to the COMP peripheral section in corresponding
32 Reference Manual.
33
34 (#) The COMP output is available using HAL_COMP_GetOutputLevel()
35 and can be set on GPIO pins.
36 For STM32F3xx devices please refer to the COMP peripheral section in corresponding
37 Reference Manual.
38
39 (#) The COMP output can be redirected to embedded timers (TIM1, TIM2, TIM3...).
40 For STM32F3xx devices please refer to the COMP peripheral section in corresponding
41 Reference Manual.
42
43 (#) Each couple of comparators COMP1 and COMP2, COMP3 and COMP4, COMP5 and COMP6 can be combined in window
44 mode and respectively COMP1, COMP3 and COMP5 non inverting input is used as common non-inverting input.
45
46 (#) The seven comparators have interrupt capability with wake-up
47 from Sleep and Stop modes (through the EXTI controller):
48 (++) COMP1 is internally connected to EXTI Line 21
49 (++) COMP2 is internally connected to EXTI Line 22
50 (++) COMP3 is internally connected to EXTI Line 29
51 (++) COMP4 is internally connected to EXTI Line 30
52 (++) COMP5 is internally connected to EXTI Line 31
53 (++) COMP6 is internally connected to EXTI Line 32
54 (++) COMP7 is internally connected to EXTI Line 33.
55
56 From the corresponding IRQ handler, the right interrupt source can be retrieved with the
57 adequate macro __HAL_COMP_COMPx_EXTI_GET_FLAG().
58
59
60 ##### How to use this driver #####
61 ================================================================================
62 [..]
63 This driver provides functions to configure and program the Comparators of all STM32F3xx devices.
64
65 To use the comparator, perform the following steps:
66
67 (#) Fill in the HAL_COMP_MspInit() to
68 (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
69 (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
70 output to the GPIO pin
71 (++) If required enable the COMP interrupt (EXTI line Interrupt): by configuring and enabling EXTI line in Interrupt mode and
72 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
73 interrupt vector using HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() functions.
74
75 (#) Configure the comparator using HAL_COMP_Init() function:
76 (++) Select the inverting input (input minus)
77 (++) Select the non-inverting input (input plus)
78 (++) Select the output polarity
79 (++) Select the output redirection
80 (++) Select the hysteresis level
81 (++) Select the power mode
82 (++) Select the event/interrupt mode
83
84 -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order
85 to enable the comparator(s).
86
87 (#) On-the-fly reconfiguration of comparator(s) may be done by calling again HAL_COMP_Init(
88 function with new input parameter values; HAL_COMP_MspInit() function shall be adapted
89 to support multi configurations.
90
91 (#) Enable the comparator using HAL_COMP_Start() or HAL_COMP_Start_IT() functions.
92
93 (#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions
94 to manage comparator outputs (events and output level).
95
96 (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
97 function.
98
99 (#) De-initialize the comparator using HAL_COMP_DeInit() function.
100
101 (#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function.
102 Only a MCU reset can reset that protection.
103
104 *** Callback registration ***
105 =============================================
106 [..]
107
108 The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
109 allows the user to configure dynamically the driver callbacks.
110 Use Functions HAL_COMP_RegisterCallback()
111 to register an interrupt callback.
112 [..]
113
114 Function HAL_COMP_RegisterCallback() allows to register 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 This function takes as parameters the HAL peripheral handle, the Callback ID
120 and a pointer to the user callback function.
121 [..]
122
123 Use function HAL_COMP_UnRegisterCallback to reset a callback to the default
124 weak function.
125 [..]
126
127 HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
128 and the Callback ID.
129 This function allows to reset following callbacks:
130 (+) OperationCpltCallback : callback for End of operation.
131 (+) ErrorCallback : callback for error detection.
132 (+) MspInitCallback : callback for Msp Init.
133 (+) MspDeInitCallback : callback for Msp DeInit.
134 [..]
135
136 By default, after the HAL_COMP_Init() and when the state is HAL_COMP_STATE_RESET
137 all callbacks are set to the corresponding weak functions:
138 examples HAL_COMP_OperationCpltCallback(), HAL_COMP_ErrorCallback().
139 Exception done for MspInit and MspDeInit functions that are
140 reset to the legacy weak functions in the HAL_COMP_Init()/ HAL_COMP_DeInit() only when
141 these callbacks are null (not registered beforehand).
142 [..]
143
144 If MspInit or MspDeInit are not null, the HAL_COMP_Init()/ HAL_COMP_DeInit()
145 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
146 [..]
147
148 Callbacks can be registered/unregistered in HAL_COMP_STATE_READY state only.
149 Exception done MspInit/MspDeInit functions that can be registered/unregistered
150 in HAL_COMP_STATE_READY or HAL_COMP_STATE_RESET state,
151 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
152 [..]
153
154 Then, the user first registers the MspInit/MspDeInit user callbacks
155 using HAL_COMP_RegisterCallback() before calling HAL_COMP_DeInit()
156 or HAL_COMP_Init() function.
157 [..]
158
159 When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
160 not defined, the callback registration feature is not available and all callbacks
161 are set to the corresponding weak functions.
162
163 ******************************************************************************
164 */
165
166 /*
167 Additional Tables:
168
169 Table 1. COMP Inputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
170 +------------------------------------------------------------------------------------------+
171 | | | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
172 |-----------------|----------------|---------------|---------------------------------------|
173 | | 1U/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
174 | | 1U/2 VREFINT | OK | OK | OK | OK | OK | OK | OK |
175 | | 3U/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
176 | Inverting Input | VREFINT | OK | OK | OK | OK | OK | OK | OK |
177 | | DAC1 OUT (PA4) | OK | OK | OK | OK | OK | OK | OK |
178 | | DAC2 OUT (PA5) | OK | OK | OK | OK | OK | OK | OK |
179 | | IO1 | PA0 | PA2 | PD15U | PE8 | PD13U | PD10U | PC0 |
180 | | IO2 | --- | --- | PB12U | PB2 | PB10U | PB15U | --- |
181 |-----------------|----------------|-------|-------|-------|-------|-------|-------|-------|
182 | Non Inverting | IO1 | PA1 | PA7 | PB14U | PB0 | PD12U | PD11U | PA0 |
183 | Input | IO2 | --- | PA3 | PD14U | PE7 | PB13U | PB11U | PC1 |
184 +------------------------------------------------------------------------------------------+
185
186 Table 2. COMP Outputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
187 +-------------------------------------------------------+
188 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
189 |-------|-------|-------|-------|-------|-------|-------|
190 | PA0 | PA2 | PB1 | PC8 | PC7 | PA10U | PC2 |
191 | PF4 | PA7 | --- | PA8 | PA9 | PC6 | --- |
192 | PA6 | PA12U | --- | --- | --- | --- | --- |
193 | PA11U | PB9 | --- | --- | --- | --- | --- |
194 | PB8 | --- | --- | --- | --- | --- | --- |
195 +-------------------------------------------------------+
196
197 Table 3. COMP Outputs redirection to embedded timers for the STM32F303xB/STM32F303xC devices
198 +----------------------------------------------------------------------------------------------------------------------+
199 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
200 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
201 | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN |
202 | | | | | | | |
203 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
204 | | | | | | | |
205 | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN |
206 | | | | | | | |
207 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
208 | | | | | | | |
209 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
210 | + | + | + | + | + | + | + |
211 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
212 | | | | | | | |
213 | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
214 | | | | | | | |
215 | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
216 | | | | | | | |
217 | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
218 | | | | | | | |
219 | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
220 | | | | | | | |
221 | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
222 | | | | | | | |
223 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
224 +----------------------------------------------------------------------------------------------------------------------+
225
226 Table 4. COMP Outputs redirection to embedded timers for the STM32F303xE devices
227 +----------------------------------------------------------------------------------------------------------------------+
228 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
229 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
230 | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1U) | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1U) |
231 | | | | | | | |
232 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
233 | | | | | | | |
234 | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1U) | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1U) |
235 | | | | | | | |
236 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
237 | | | | | | | |
238 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
239 | + | + | + | + | + | + | + |
240 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
241 | | | | | | | |
242 | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
243 | | | | | | | |
244 | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
245 | | | | | | | |
246 | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
247 | | | | | | | |
248 | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
249 | | | | | | | |
250 | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
251 | | | | | | | |
252 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
253 | | | | | | | |
254 | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1U)| TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1U)|
255 | | | | | | | |
256 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
257 | | | | | | | |
258 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
259 | + | + | + | + | + | + | + |
260 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
261 | + | + | + | + | + | + | + |
262 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
263 | | | | | | | |
264 +----------------------------------------------------------------------------------------------------------------------+
265 (1U): This connection consists of connecting both GPIO and COMP output to TIM1/8U/20 BRK input through an OR gate, instead
266 of connecting the GPIO to the TIM1/8U/20 BRK input and the COMP output to the TIM1/8U/20 BRK_ACTH input. The aim is to
267 add a digital filter (3 bits) on the COMP output.
268
269 Table 5. COMP Outputs blanking sources for the STM32F303xB/STM32F303xC/STM32F303xE devices
270 +----------------------------------------------------------------------------------------------------------------------+
271 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
272 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
273 | TIM1 OC5 | TIM1 OC5 | TIM1 OC5 | TIM3 OC4 | -------- | TIM8 OC5 | TIM1 OC5 |
274 | | | | | | | |
275 | TIM2 OC3 | TIM2 OC3 | -------- | TIM8 OC5 | TIM3 OC3 | TIM2 OC4 | TIM8 OC5 |
276 | | | | | | | |
277 | TIM3 OC3 | TIM3 OC3 | TIM2 OC4 | TIM15 OC1 | TIM8 OC5 | TIM15 OC2 | TIM15 OC2 |
278 | | | | | | | |
279 +----------------------------------------------------------------------------------------------------------------------+
280
281 */
282
283 /* Includes ------------------------------------------------------------------*/
284 #include "stm32f3xx_hal.h"
285
286 /** @addtogroup STM32F3xx_HAL_Driver
287 * @{
288 */
289
290 /** @defgroup COMP COMP
291 * @brief COMP HAL module driver
292 * @{
293 */
294
295 #ifdef HAL_COMP_MODULE_ENABLED
296
297 /* Private typedef -----------------------------------------------------------*/
298 /* Private define ------------------------------------------------------------*/
299 /** @defgroup COMP_Private_Constants COMP Private Constants
300 * @{
301 */
302 #define COMP_LOCK_DISABLE (0x00000000U)
303 #define COMP_LOCK_ENABLE COMP_CSR_COMPxLOCK
304 /**
305 * @}
306 */
307
308 /* Private macro -------------------------------------------------------------*/
309 /* Private variables ---------------------------------------------------------*/
310 /* Private function prototypes -----------------------------------------------*/
311 /* Exported functions --------------------------------------------------------*/
312
313 /** @defgroup COMP_Exported_Functions COMP Exported Functions
314 * @{
315 */
316
317 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
318 * @brief Initialization and de-initialization functions.
319 *
320 @verbatim
321 ===============================================================================
322 ##### Initialization and de-initialization functions #####
323 ===============================================================================
324 [..] This section provides functions to initialize and de-initialize comparators.
325
326 @endverbatim
327 * @{
328 */
329
330 /**
331 * @brief Initialize the COMP peripheral according to the specified
332 * parameters in the COMP_InitTypeDef and initialize the associated handle.
333 * @note If the selected comparator is locked, initialization cannot be performed.
334 * To unlock the configuration, perform a system reset.
335 * @param hcomp COMP handle
336 * @retval HAL status
337 */
HAL_COMP_Init(COMP_HandleTypeDef * hcomp)338 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
339 {
340 HAL_StatusTypeDef status = HAL_OK;
341
342 /* Check the COMP handle allocation and lock status */
343 if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
344 {
345 status = HAL_ERROR;
346 }
347 else
348 {
349 /* Check the parameters */
350 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
351 assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
352 assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
353 assert_param(IS_COMP_NONINVERTINGINPUT_INSTANCE(hcomp->Instance, hcomp->Init.NonInvertingInput));
354 assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
355 assert_param(IS_COMP_OUTPUT_INSTANCE(hcomp->Instance, hcomp->Init.Output));
356 assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
357 assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
358 assert_param(IS_COMP_MODE(hcomp->Init.Mode));
359 assert_param(IS_COMP_BLANKINGSRCE(hcomp->Init.BlankingSrce));
360 assert_param(IS_COMP_BLANKINGSRCE_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
361 assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
362
363 if (hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
364 {
365 assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
366 assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
367 }
368
369 /* Init SYSCFG and the low level hardware to access comparators */
370 __HAL_RCC_SYSCFG_CLK_ENABLE();
371
372 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
373 /* Init the COMP Callback settings */
374 hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
375
376 if (hcomp->MspInitCallback == NULL)
377 {
378 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
379 }
380
381 /* Init the low level hardware */
382 hcomp->MspInitCallback(hcomp);
383 #else
384 /* Init the low level hardware : SYSCFG to access comparators */
385 HAL_COMP_MspInit(hcomp);
386 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
387
388 if (hcomp->State == HAL_COMP_STATE_RESET)
389 {
390 /* Allocate lock resource and initialize it */
391 hcomp->Lock = HAL_UNLOCKED;
392 }
393
394 /* Manage inverting input comparator inverting input connected to a GPIO */
395 /* for STM32F302x, STM32F32xx, STM32F33x. */
396 hcomp->Init.InvertingInput = COMP_INVERTINGINPUT_SELECTION(hcomp->Instance, hcomp->Init.InvertingInput);
397
398 /* Set COMP parameters */
399 /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
400 /* Set COMPxNONINSEL bits according to hcomp->Init.NonInvertingInput value */
401 /* Set COMPxBLANKING bits according to hcomp->Init.BlankingSrce value */
402 /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */
403 /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
404 /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */
405 /* Set COMPxMODE bits according to hcomp->Init.Mode value */
406 COMP_INIT(hcomp);
407
408 /* Initialize the COMP state*/
409 hcomp->State = HAL_COMP_STATE_READY;
410 }
411
412 return status;
413 }
414
415 /**
416 * @brief DeInitialize the COMP peripheral.
417 * @note If the selected comparator is locked, deinitialization cannot be performed.
418 * To unlock the configuration, perform a system reset.
419 * @param hcomp COMP handle
420 * @retval HAL status
421 */
HAL_COMP_DeInit(COMP_HandleTypeDef * hcomp)422 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
423 {
424 HAL_StatusTypeDef status = HAL_OK;
425
426 /* Check the COMP handle allocation and lock status */
427 if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
428 {
429 status = HAL_ERROR;
430 }
431 else
432 {
433 /* Check the parameter */
434 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
435
436 /* Set COMP_CSR register to reset value */
437 COMP_DEINIT(hcomp);
438
439 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
440 if (hcomp->MspDeInitCallback == NULL)
441 {
442 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
443 }
444
445 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
446 hcomp->MspDeInitCallback(hcomp);
447 #else
448 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
449 HAL_COMP_MspDeInit(hcomp);
450 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
451
452 hcomp->State = HAL_COMP_STATE_RESET;
453
454 /* Release Lock */
455 __HAL_UNLOCK(hcomp);
456 }
457
458 return status;
459 }
460
461 /**
462 * @brief Initialize the COMP MSP.
463 * @param hcomp COMP handle
464 * @retval None
465 */
HAL_COMP_MspInit(COMP_HandleTypeDef * hcomp)466 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
467 {
468 /* Prevent unused argument(s) compilation warning */
469 UNUSED(hcomp);
470
471 /* NOTE : This function should not be modified, when the callback is needed,
472 the HAL_COMP_MspInit could be implemented in the user file
473 */
474 }
475
476 /**
477 * @brief DeInitialize the COMP MSP.
478 * @param hcomp COMP handle
479 * @retval None
480 */
HAL_COMP_MspDeInit(COMP_HandleTypeDef * hcomp)481 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
482 {
483 /* Prevent unused argument(s) compilation warning */
484 UNUSED(hcomp);
485
486 /* NOTE : This function should not be modified, when the callback is needed,
487 the HAL_COMP_MspDeInit could be implemented in the user file
488 */
489 }
490
491 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
492 /**
493 * @brief Register a User COMP Callback
494 * To be used instead of the weak predefined callback
495 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
496 * the configuration information for the specified COMP.
497 * @param CallbackID ID of the callback to be registered
498 * This parameter can be one of the following values:
499 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
500 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
501 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
502 * @param pCallback pointer to the Callback function
503 * @retval HAL status
504 */
HAL_COMP_RegisterCallback(COMP_HandleTypeDef * hcomp,HAL_COMP_CallbackIDTypeDef CallbackID,pCOMP_CallbackTypeDef pCallback)505 HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
506 {
507 HAL_StatusTypeDef status = HAL_OK;
508
509 if (pCallback == NULL)
510 {
511 /* Update the error code */
512 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
513
514 return HAL_ERROR;
515 }
516
517 if (HAL_COMP_STATE_READY == hcomp->State)
518 {
519 switch (CallbackID)
520 {
521 case HAL_COMP_TRIGGER_CB_ID :
522 hcomp->TriggerCallback = pCallback;
523 break;
524
525 case HAL_COMP_MSPINIT_CB_ID :
526 hcomp->MspInitCallback = pCallback;
527 break;
528
529 case HAL_COMP_MSPDEINIT_CB_ID :
530 hcomp->MspDeInitCallback = pCallback;
531 break;
532
533 default :
534 /* Update the error code */
535 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
536
537 /* Return error status */
538 status = HAL_ERROR;
539 break;
540 }
541 }
542 else if (HAL_COMP_STATE_RESET == hcomp->State)
543 {
544 switch (CallbackID)
545 {
546 case HAL_COMP_MSPINIT_CB_ID :
547 hcomp->MspInitCallback = pCallback;
548 break;
549
550 case HAL_COMP_MSPDEINIT_CB_ID :
551 hcomp->MspDeInitCallback = pCallback;
552 break;
553
554 default :
555 /* Update the error code */
556 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
557
558 /* Return error status */
559 status = HAL_ERROR;
560 break;
561 }
562 }
563 else
564 {
565 /* Update the error code */
566 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
567
568 /* Return error status */
569 status = HAL_ERROR;
570 }
571
572 return status;
573 }
574
575 /**
576 * @brief Unregister a COMP Callback
577 * COMP callback is redirected to the weak predefined callback
578 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
579 * the configuration information for the specified COMP.
580 * @param CallbackID ID of the callback to be unregistered
581 * This parameter can be one of the following values:
582 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
583 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
584 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
585 * @retval HAL status
586 */
HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef * hcomp,HAL_COMP_CallbackIDTypeDef CallbackID)587 HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
588 {
589 HAL_StatusTypeDef status = HAL_OK;
590
591 if (HAL_COMP_STATE_READY == hcomp->State)
592 {
593 switch (CallbackID)
594 {
595 case HAL_COMP_TRIGGER_CB_ID :
596 hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
597 break;
598
599 case HAL_COMP_MSPINIT_CB_ID :
600 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
601 break;
602
603 case HAL_COMP_MSPDEINIT_CB_ID :
604 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
605 break;
606
607 default :
608 /* Update the error code */
609 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
610
611 /* Return error status */
612 status = HAL_ERROR;
613 break;
614 }
615 }
616 else if (HAL_COMP_STATE_RESET == hcomp->State)
617 {
618 switch (CallbackID)
619 {
620 case HAL_COMP_MSPINIT_CB_ID :
621 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
622 break;
623
624 case HAL_COMP_MSPDEINIT_CB_ID :
625 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
626 break;
627
628 default :
629 /* Update the error code */
630 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
631
632 /* Return error status */
633 status = HAL_ERROR;
634 break;
635 }
636 }
637 else
638 {
639 /* Update the error code */
640 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
641
642 /* Return error status */
643 status = HAL_ERROR;
644 }
645
646 return status;
647 }
648
649 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
650
651 /**
652 * @}
653 */
654
655 /** @defgroup COMP_Exported_Functions_Group2 Start Stop operation functions
656 * @brief Start-Stop operation functions.
657 *
658 @verbatim
659 ===============================================================================
660 ##### Start Stop operation functions #####
661 ===============================================================================
662 [..] This section provides functions allowing to:
663 (+) Start a comparator without interrupt generation.
664 (+) Stop a comparator without interrupt generation.
665 (+) Start a comparator with interrupt generation.
666 (+) Stop a comparator with interrupt generation.
667 (+) Handle interrupts from a comparator with associated callback function.
668
669 @endverbatim
670 * @{
671 */
672
673 /**
674 * @brief Start the comparator.
675 * @param hcomp COMP handle
676 * @retval HAL status
677 */
HAL_COMP_Start(COMP_HandleTypeDef * hcomp)678 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
679 {
680 HAL_StatusTypeDef status = HAL_OK;
681 uint32_t extiline = 0U;
682
683 /* Check the COMP handle allocation and lock status */
684 if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
685 {
686 status = HAL_ERROR;
687 }
688 else
689 {
690 /* Check the parameter */
691 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
692
693 if (hcomp->State == HAL_COMP_STATE_READY)
694 {
695 /* Get the EXTI Line output configuration */
696 extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
697
698 /* Configure the event generation */
699 if ((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_EVENT_RISING | COMP_TRIGGERMODE_EVENT_FALLING)) != RESET)
700 {
701 /* Configure the event trigger rising edge */
702 if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_RISING) != RESET)
703 {
704 COMP_EXTI_RISING_ENABLE(extiline);
705 }
706 else
707 {
708 COMP_EXTI_RISING_DISABLE(extiline);
709 }
710
711 /* Configure the trigger falling edge */
712 if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_FALLING) != RESET)
713 {
714 COMP_EXTI_FALLING_ENABLE(extiline);
715 }
716 else
717 {
718 COMP_EXTI_FALLING_DISABLE(extiline);
719 }
720
721 /* Enable EXTI event generation */
722 COMP_EXTI_ENABLE_EVENT(extiline);
723
724 /* Clear COMP EXTI pending bit */
725 COMP_EXTI_CLEAR_FLAG(extiline);
726 }
727
728 /* Enable the selected comparator */
729 __HAL_COMP_ENABLE(hcomp);
730
731 hcomp->State = HAL_COMP_STATE_BUSY;
732 }
733 else
734 {
735 status = HAL_ERROR;
736 }
737 }
738
739 return status;
740 }
741
742 /**
743 * @brief Stop the comparator.
744 * @param hcomp COMP handle
745 * @retval HAL status
746 */
HAL_COMP_Stop(COMP_HandleTypeDef * hcomp)747 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
748 {
749 HAL_StatusTypeDef status = HAL_OK;
750
751 /* Check the COMP handle allocation and lock status */
752 if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
753 {
754 status = HAL_ERROR;
755 }
756 else
757 {
758 /* Check the parameter */
759 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
760
761 if (hcomp->State == HAL_COMP_STATE_BUSY)
762 {
763 /* Disable the EXTI Line event mode if any */
764 COMP_EXTI_DISABLE_EVENT(COMP_GET_EXTI_LINE(hcomp->Instance));
765
766 /* Disable the selected comparator */
767 __HAL_COMP_DISABLE(hcomp);
768
769 hcomp->State = HAL_COMP_STATE_READY;
770 }
771 else
772 {
773 status = HAL_ERROR;
774 }
775 }
776
777 return status;
778 }
779
780 /**
781 * @brief Start the comparator in Interrupt mode.
782 * @param hcomp COMP handle
783 * @retval HAL status.
784 */
HAL_COMP_Start_IT(COMP_HandleTypeDef * hcomp)785 HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
786 {
787 HAL_StatusTypeDef status = HAL_OK;
788 uint32_t extiline = 0U;
789
790 /* Check the COMP handle allocation and lock status */
791 if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
792 {
793 status = HAL_ERROR;
794 }
795 else
796 {
797 /* Check the parameter */
798 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
799
800 if (hcomp->State == HAL_COMP_STATE_READY)
801 {
802 /* Configure the EXTI event generation */
803 if ((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_IT_RISING | COMP_TRIGGERMODE_IT_FALLING)) != RESET)
804 {
805 /* Get the EXTI Line output configuration */
806 extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
807
808 /* Configure the trigger rising edge */
809 if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
810 {
811 COMP_EXTI_RISING_ENABLE(extiline);
812 }
813 else
814 {
815 COMP_EXTI_RISING_DISABLE(extiline);
816 }
817 /* Configure the trigger falling edge */
818 if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
819 {
820 COMP_EXTI_FALLING_ENABLE(extiline);
821 }
822 else
823 {
824 COMP_EXTI_FALLING_DISABLE(extiline);
825 }
826
827 /* Clear COMP EXTI pending bit if any */
828 COMP_EXTI_CLEAR_FLAG(extiline);
829
830 /* Enable EXTI interrupt mode */
831 COMP_EXTI_ENABLE_IT(extiline);
832
833 /* Enable the selected comparator */
834 __HAL_COMP_ENABLE(hcomp);
835
836 hcomp->State = HAL_COMP_STATE_BUSY;
837 }
838 else
839 {
840 status = HAL_ERROR;
841 }
842 }
843 else
844 {
845 status = HAL_ERROR;
846 }
847 }
848
849 return status;
850 }
851
852 /**
853 * @brief Stop the comparator in Interrupt mode.
854 * @param hcomp COMP handle
855 * @retval HAL status
856 */
HAL_COMP_Stop_IT(COMP_HandleTypeDef * hcomp)857 HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
858 {
859 HAL_StatusTypeDef status = HAL_OK;
860
861 /* Disable the EXTI Line interrupt mode */
862 COMP_EXTI_DISABLE_IT(COMP_GET_EXTI_LINE(hcomp->Instance));
863
864 status = HAL_COMP_Stop(hcomp);
865
866 return status;
867 }
868
869 /**
870 * @brief Comparator IRQ Handler.
871 * @param hcomp COMP handle
872 * @retval HAL status
873 */
HAL_COMP_IRQHandler(COMP_HandleTypeDef * hcomp)874 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
875 {
876 uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
877
878 /* Check COMP EXTI flag */
879 if (COMP_EXTI_GET_FLAG(extiline) != RESET)
880 {
881 /* Clear COMP EXTI pending bit */
882 COMP_EXTI_CLEAR_FLAG(extiline);
883
884 /* COMP trigger callback */
885 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
886 hcomp->TriggerCallback(hcomp);
887 #else
888 HAL_COMP_TriggerCallback(hcomp);
889 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
890 }
891 }
892
893 /**
894 * @brief Comparator trigger callback.
895 * @param hcomp COMP handle
896 * @retval None
897 */
HAL_COMP_TriggerCallback(COMP_HandleTypeDef * hcomp)898 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
899 {
900 /* Prevent unused argument(s) compilation warning */
901 UNUSED(hcomp);
902
903 /* NOTE : This function should not be modified, when the callback is needed,
904 the HAL_COMP_TriggerCallback should be implemented in the user file
905 */
906 }
907
908 /**
909 * @}
910 */
911
912 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
913 * @brief Management functions.
914 *
915 @verbatim
916 ===============================================================================
917 ##### Peripheral Control functions #####
918 ===============================================================================
919 [..]
920 This subsection provides a set of functions allowing to control the comparators.
921
922 @endverbatim
923 * @{
924 */
925
926 /**
927 * @brief Lock the selected comparator configuration.
928 * @note A system reset is required to unlock the comparator configuration.
929 * @param hcomp COMP handle
930 * @retval HAL status
931 */
HAL_COMP_Lock(COMP_HandleTypeDef * hcomp)932 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
933 {
934 HAL_StatusTypeDef status = HAL_OK;
935
936 /* Check the COMP handle allocation and lock status */
937 if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
938 {
939 status = HAL_ERROR;
940 }
941 else
942 {
943 /* Check the parameter */
944 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
945
946 /* Set lock flag on state */
947 switch (hcomp->State)
948 {
949 case HAL_COMP_STATE_BUSY:
950 hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
951 break;
952 case HAL_COMP_STATE_READY:
953 hcomp->State = HAL_COMP_STATE_READY_LOCKED;
954 break;
955 default:
956 /* unexpected state */
957 status = HAL_ERROR;
958 break;
959 }
960 }
961
962 if (status == HAL_OK)
963 {
964 /* Set the lock bit corresponding to selected comparator */
965 __HAL_COMP_LOCK(hcomp);
966 }
967
968 return status;
969 }
970
971 /**
972 * @brief Return the output level (high or low) of the selected comparator.
973 * The output level depends on the selected polarity.
974 * If the polarity is not inverted:
975 * - Comparator output is low when the non-inverting input is at a lower
976 * voltage than the inverting input
977 * - Comparator output is high when the non-inverting input is at a higher
978 * voltage than the inverting input
979 * If the polarity is inverted:
980 * - Comparator output is high when the non-inverting input is at a lower
981 * voltage than the inverting input
982 * - Comparator output is low when the non-inverting input is at a higher
983 * voltage than the inverting input
984 * @param hcomp COMP handle
985 * @retval Returns the selected comparator output level:
986 * @arg @ref COMP_OUTPUTLEVEL_LOW
987 * @arg @ref COMP_OUTPUTLEVEL_HIGH
988 *
989 */
HAL_COMP_GetOutputLevel(COMP_HandleTypeDef * hcomp)990 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
991 {
992 uint32_t level = 0U;
993
994 /* Check the parameter */
995 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
996
997 level = READ_BIT(hcomp->Instance->CSR, COMP_CSR_COMPxOUT);
998
999 if (level != 0U)
1000 {
1001 return (COMP_OUTPUTLEVEL_HIGH);
1002 }
1003 return (COMP_OUTPUTLEVEL_LOW);
1004 }
1005
1006 /**
1007 * @}
1008 */
1009
1010 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
1011 * @brief Peripheral State functions.
1012 *
1013 @verbatim
1014 ===============================================================================
1015 ##### Peripheral State functions #####
1016 ===============================================================================
1017 [..]
1018 This subsection permits to get in run-time the status of the peripheral.
1019
1020 @endverbatim
1021 * @{
1022 */
1023
1024 /**
1025 * @brief Return the COMP handle state.
1026 * @param hcomp COMP handle
1027 * @retval HAL state
1028 */
HAL_COMP_GetState(COMP_HandleTypeDef * hcomp)1029 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
1030 {
1031 /* Check the COMP handle allocation */
1032 if (hcomp == NULL)
1033 {
1034 return HAL_COMP_STATE_RESET;
1035 }
1036
1037 /* Check the parameter */
1038 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
1039
1040 /* Return COMP handle state */
1041 return hcomp->State;
1042 }
1043
1044 /**
1045 * @brief Return the COMP error code.
1046 * @param hcomp COMP handle
1047 * @retval COMP error code
1048 */
HAL_COMP_GetError(COMP_HandleTypeDef * hcomp)1049 uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
1050 {
1051 /* Check the parameters */
1052 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
1053
1054 return hcomp->ErrorCode;
1055 }
1056
1057 /**
1058 * @}
1059 */
1060
1061 /**
1062 * @}
1063 */
1064
1065 #endif /* HAL_COMP_MODULE_ENABLED */
1066 /**
1067 * @}
1068 */
1069
1070 /**
1071 * @}
1072 */
1073
1074