]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_comp.c
Merge commit '4d116a04e94cf0d19317d5b44e4fa9f34a3e5594'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_comp.c
1 /**
2 ******************************************************************************
3 * @file stm32f3xx_hal_comp.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 12-Sept-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/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 STM32F3xx device family integrates up to 7 analog comparators COMP1, COMP2...COMP7:
23 (#) The non inverting input and inverting input can be set to GPIO pins
24 as shown in table1. COMP Inputs below for STM32F303xB/STM32F303xC as example.
25 For other STM32F3xx devices please refer to the COMP peripheral section in corresponding
26 Reference Manual.
27
28 (#) The COMP output is available using HAL_COMP_GetOutputLevel()
29 and can be set on GPIO pins. Refer to table 2. COMP Outputs below for STM32F303xB/STM32F303xC as example.
30 For other STM32F3xx devices please refer to the COMP peripheral section in corresponding
31 Reference Manual.
32
33 (#) The COMP output can be redirected to embedded timers (TIM1, TIM2, TIM3...)
34 Refer to table 3. COMP Outputs redirection to embedded timers below for STM32F303xB/STM32F303xC as example.
35 For other STM32F3xx devices please refer to the COMP peripheral section in corresponding
36 Reference Manual.
37
38 (#) The comparators COMP1 and COMP2, COMP3 and COMP4, COMP5 and COMP6 can be combined in window
39 mode and only COMP1, COMP3 and COMP5 non inverting input can be used as non-inverting input.
40
41 (#) The seven comparators have interrupt capability with wake-up
42 from Sleep and Stop modes (through the EXTI controller):
43 (++) COMP1 is internally connected to EXTI Line 21
44 (++) COMP2 is internally connected to EXTI Line 22
45 (++) COMP3 is internally connected to EXTI Line 29
46 (++) COMP4 is internally connected to EXTI Line 30
47 (++) COMP5 is internally connected to EXTI Line 31
48 (++) COMP6 is internally connected to EXTI Line 32
49 (++) COMP7 is internally connected to EXTI Line 33
50 From the corresponding IRQ handler, the right interrupt source can be retrieved with the
51 macro __HAL_COMP_EXTI_GET_FLAG(). Possible values are:
52 (++) COMP_EXTI_LINE_COMP1_EVENT
53 (++) COMP_EXTI_LINE_COMP2_EVENT
54 (++) COMP_EXTI_LINE_COMP3_EVENT
55 (++) COMP_EXTI_LINE_COMP4_EVENT
56 (++) COMP_EXTI_LINE_COMP5_EVENT
57 (++) COMP_EXTI_LINE_COMP6_EVENT
58 (++) COMP_EXTI_LINE_COMP7_EVENT
59
60 [..] Table 1. COMP Inputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
61 +------------------------------------------------------------------------------------------+
62 | | | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
63 |-----------------|----------------|---------------|---------------------------------------|
64 | | 1/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
65 | | 1/2 VREFINT | OK | OK | OK | OK | OK | OK | OK |
66 | | 3/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
67 | Inverting Input | VREFINT | OK | OK | OK | OK | OK | OK | OK |
68 | | DAC1 OUT (PA4) | OK | OK | OK | OK | OK | OK | OK |
69 | | DAC2 OUT (PA5) | OK | OK | OK | OK | OK | OK | OK |
70 | | IO1 | PA0 | PA2 | PD15 | PE8 | PD13 | PD10 | PC0 |
71 | | IO2 | --- | --- | PB12 | PB2 | PB10 | PB15 | --- |
72 |-----------------|----------------|-------|-------|-------|-------|-------|-------|-------|
73 | Non Inverting | IO1 | PA1 | PA7 | PB14 | PB0 | PD12 | PD11 | PA0 |
74 | Input | IO2 | --- | PA3 | PD14 | PE7 | PB13 | PB11 | PC1 |
75 +------------------------------------------------------------------------------------------+
76
77 [..] Table 2. COMP Outputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
78 +-------------------------------------------------------+
79 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
80 |-------|-------|-------|-------|-------|-------|-------|
81 | PA0 | PA2 | PB1 | PC8 | PC7 | PA10 | PC2 |
82 | PF4 | PA7 | --- | PA8 | PA9 | PC6 | --- |
83 | PA6 | PA12 | --- | --- | --- | --- | --- |
84 | PA11 | PB9 | --- | --- | --- | --- | --- |
85 | PB8 | --- | --- | --- | --- | --- | --- |
86 +-------------------------------------------------------+
87
88 [..] Table 3. COMP Outputs redirection to embedded timers for the STM32F303xB/STM32F303xC devices
89 +----------------------------------------------------------------------------------------------------------------------+
90 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
91 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
92 | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN |
93 | | | | | | | |
94 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
95 | | | | | | | |
96 | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN |
97 | | | | | | | |
98 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
99 | | | | | | | |
100 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
101 | + | + | + | + | + | + | + |
102 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
103 | | | | | | | |
104 | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
105 | | | | | | | |
106 | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
107 | | | | | | | |
108 | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
109 | | | | | | | |
110 | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
111 | | | | | | | |
112 | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
113 | | | | | | | |
114 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
115 +----------------------------------------------------------------------------------------------------------------------+
116
117 [..] Table 4. COMP Outputs redirection to embedded timers for the STM32F303xE devices
118 +----------------------------------------------------------------------------------------------------------------------+
119 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
120 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
121 | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1) | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1) |
122 | | | | | | | |
123 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
124 | | | | | | | |
125 | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1) | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1) |
126 | | | | | | | |
127 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
128 | | | | | | | |
129 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
130 | + | + | + | + | + | + | + |
131 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
132 | | | | | | | |
133 | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
134 | | | | | | | |
135 | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
136 | | | | | | | |
137 | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
138 | | | | | | | |
139 | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
140 | | | | | | | |
141 | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
142 | | | | | | | |
143 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
144 | | | | | | | |
145 | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1)| TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1)|
146 | | | | | | | |
147 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
148 | | | | | | | |
149 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
150 | + | + | + | + | + | + | + |
151 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
152 | + | + | + | + | + | + | + |
153 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
154 | | | | | | | |
155 +----------------------------------------------------------------------------------------------------------------------+
156 (1):This connection consists of connecting both GPIO and COMP output to TIM1/8/20 BRK input through an OR gate, instead
157 of connecting the GPIO to the TIM1/8/20 BRK input and the COMP output to the TIM1/8/20 BRK_ACTH input. The aim is to
158 add a digital filter (3 bits) on the COMP output.
159
160 [..] Table 5. COMP Outputs blanking sources for the STM32F303xB/STM32F303xC/STM32F303xE devices
161 +----------------------------------------------------------------------------------------------------------------------+
162 | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
163 |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
164 | TIM1 OC5 | TIM1 OC5 | TIM1 OC5 | TIM3 OC4 | -------- | TIM8 OC5 | TIM1 OC5 |
165 | | | | | | | |
166 | TIM2 OC3 | TIM2 OC3 | -------- | TIM8 OC5 | TIM3 OC3 | TIM2 OC4 | TIM8 OC5 |
167 | | | | | | | |
168 | TIM3 OC3 | TIM3 OC3 | TIM2 OC4 | TIM15 OC1 | TIM8 OC5 | TIM15 OC2 | TIM15 OC2 |
169 | | | | | | | |
170 +----------------------------------------------------------------------------------------------------------------------+
171
172 ##### How to use this driver #####
173 ================================================================================
174 [..]
175 This driver provides functions to configure and program the Comparators of all STM32F3xx devices.
176
177 To use the comparator, perform the following steps:
178
179 (#) Fill in the HAL_COMP_MspInit() to
180 (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
181 (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
182 output to the GPIO pin
183 (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
184 selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
185 interrupt vector using HAL_NVIC_EnableIRQ() function.
186
187 (#) Configure the comparator using HAL_COMP_Init() function:
188 (++) Select the inverting input
189 (++) Select the non-inverting input
190 (++) Select the output polarity
191 (++) Select the output redirection
192 (++) Select the hysteresis level
193 (++) Select the power mode
194 (++) Select the event/interrupt mode
195
196 (#) Enable the comparator using HAL_COMP_Start() function or HAL_COMP_Start_IT() function for interrupt mode
197
198 (#) Read the comparator output level with HAL_COMP_GetOutputLevel()
199
200 @endverbatim
201 ******************************************************************************
202 * @attention
203 *
204 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
205 *
206 * Redistribution and use in source and binary forms, with or without modification,
207 * are permitted provided that the following conditions are met:
208 * 1. Redistributions of source code must retain the above copyright notice,
209 * this list of conditions and the following disclaimer.
210 * 2. Redistributions in binary form must reproduce the above copyright notice,
211 * this list of conditions and the following disclaimer in the documentation
212 * and/or other materials provided with the distribution.
213 * 3. Neither the name of STMicroelectronics nor the names of its contributors
214 * may be used to endorse or promote products derived from this software
215 * without specific prior written permission.
216 *
217 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
218 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
220 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
221 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
223 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
224 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
225 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
226 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
227 *
228 ******************************************************************************
229 */
230
231 /* Includes ------------------------------------------------------------------*/
232 #include "stm32f3xx_hal.h"
233
234 /** @addtogroup STM32F3xx_HAL_Driver
235 * @{
236 */
237
238 /** @defgroup COMP COMP HAL module driver
239 * @brief COMP HAL module driver
240 * @{
241 */
242
243 #ifdef HAL_COMP_MODULE_ENABLED
244
245 /* Private typedef -----------------------------------------------------------*/
246 /* Private define ------------------------------------------------------------*/
247 /* Private macro -------------------------------------------------------------*/
248 /* Private variables ---------------------------------------------------------*/
249 /* Private function prototypes -----------------------------------------------*/
250 /* Exported functions --------------------------------------------------------*/
251
252 /** @defgroup COMP_Exported_Functions COMP Exported Functions
253 * @{
254 */
255
256 /** @defgroup COMP_Exported_Functions_Group1 Initialization and de-initialization functions
257 * @brief Initialization and Configuration functions
258 *
259 @verbatim
260 ===============================================================================
261 ##### Initialization/de-initialization functions #####
262 ===============================================================================
263 [..] This section provides functions to initialize and de-initialize comparators
264
265 @endverbatim
266 * @{
267 */
268
269 /**
270 * @brief Initializes the COMP according to the specified
271 * parameters in the COMP_InitTypeDef and create the associated handle.
272 * @note If the selected comparator is locked, initialization can't be performed.
273 * To unlock the configuration, perform a system reset.
274 * @param hcomp: COMP handle
275 * @retval HAL status
276 */
277 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
278 {
279 HAL_StatusTypeDef status = HAL_OK;
280
281 /* Check the COMP handle allocation and lock status */
282 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
283 {
284 status = HAL_ERROR;
285 }
286 else
287 {
288 /* Check the parameter */
289 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
290 assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
291 assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
292 assert_param(IS_COMP_NONINVERTINGINPUT_INSTANCE(hcomp->Instance, hcomp->Init.NonInvertingInput));
293 assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
294 assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
295 assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
296 assert_param(IS_COMP_MODE(hcomp->Init.Mode));
297 assert_param(IS_COMP_BLANKINGSRCE(hcomp->Init.BlankingSrce));
298 assert_param(IS_COMP_BLANKINGSRCE_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
299
300 if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLED)
301 {
302 assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
303 }
304
305 if(hcomp->State == HAL_COMP_STATE_RESET)
306 {
307 /* Init SYSCFG and the low level hardware to access comparators */
308 __SYSCFG_CLK_ENABLE();
309
310 HAL_COMP_MspInit(hcomp);
311 }
312
313 /* Set COMP parameters */
314 /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
315 /* Set COMPxNONINSEL bits according to hcomp->Init.NonInvertingInput value */
316 /* Set COMPxBLANKING bits according to hcomp->Init.BlankingSrce value */
317 /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */
318 /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
319 /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */
320 /* Set COMPxMODE bits according to hcomp->Init.Mode value */
321 COMP_INIT(hcomp);
322
323 /* Initialize the COMP state*/
324 if(hcomp->State == HAL_COMP_STATE_RESET)
325 {
326 hcomp->State = HAL_COMP_STATE_READY;
327 }
328 }
329
330 return status;
331 }
332
333 /**
334 * @brief DeInitializes the COMP peripheral
335 * @note Deinitialization can't be performed if the COMP configuration is locked.
336 * To unlock the configuration, perform a system reset.
337 * @param hcomp: COMP handle
338 * @retval HAL status
339 */
340 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
341 {
342 HAL_StatusTypeDef status = HAL_OK;
343
344 /* Check the COMP handle allocation and lock status */
345 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
346 {
347 status = HAL_ERROR;
348 }
349 else
350 {
351 /* Check the parameter */
352 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
353
354 /* Set COMP_CSR register to reset value */
355 COMP_DEINIT(hcomp);
356
357 /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
358 HAL_COMP_MspDeInit(hcomp);
359
360 hcomp->State = HAL_COMP_STATE_RESET;
361 }
362
363 return status;
364 }
365
366 /**
367 * @brief Initializes the COMP MSP.
368 * @param hcomp: COMP handle
369 * @retval None
370 */
371 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
372 {
373 /* NOTE : This function Should not be modified, when the callback is needed,
374 the HAL_COMP_MspInit could be implemented in the user file
375 */
376 }
377
378 /**
379 * @brief DeInitializes COMP MSP.
380 * @param hcomp: COMP handle
381 * @retval None
382 */
383 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
384 {
385 /* NOTE : This function Should not be modified, when the callback is needed,
386 the HAL_COMP_MspDeInit could be implenetd in the user file
387 */
388 }
389
390 /**
391 * @}
392 */
393
394 /** @defgroup COMP_Exported_Functions_Group2 Input and Output operation functions
395 * @brief Data transfers functions
396 *
397 @verbatim
398 ===============================================================================
399 ##### IO operation functions #####
400 ===============================================================================
401 [..]
402 This subsection provides a set of functions allowing to manage the COMP data
403 transfers.
404
405 @endverbatim
406 * @{
407 */
408
409 /**
410 * @brief Start the comparator
411 * @param hcomp: COMP handle
412 * @retval HAL status
413 */
414 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
415 {
416 HAL_StatusTypeDef status = HAL_OK;
417
418 /* Check the COMP handle allocation and lock status */
419 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
420 {
421 status = HAL_ERROR;
422 }
423 else
424 {
425 /* Check the parameter */
426 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
427
428 if(hcomp->State == HAL_COMP_STATE_READY)
429 {
430 /* Enable the selected comparator */
431 COMP_START(hcomp);
432
433 hcomp->State = HAL_COMP_STATE_BUSY;
434 }
435 else
436 {
437 status = HAL_ERROR;
438 }
439 }
440
441 return status;
442 }
443
444 /**
445 * @brief Stop the comparator
446 * @param hcomp: COMP handle
447 * @retval HAL status
448 */
449 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
450 {
451 HAL_StatusTypeDef status = HAL_OK;
452
453 /* Check the COMP handle allocation and lock status */
454 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
455 {
456 status = HAL_ERROR;
457 }
458 else
459 {
460 /* Check the parameter */
461 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
462
463 if(hcomp->State == HAL_COMP_STATE_BUSY)
464 {
465 /* Disable the selected comparator */
466 COMP_STOP(hcomp);
467
468 hcomp->State = HAL_COMP_STATE_READY;
469 }
470 else
471 {
472 status = HAL_ERROR;
473 }
474 }
475
476 return status;
477 }
478
479 /**
480 * @brief Enables the interrupt and starts the comparator
481 * @param hcomp: COMP handle
482 * @retval HAL status.
483 */
484 HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
485 {
486 HAL_StatusTypeDef status = HAL_OK;
487 uint32_t extiline = 0;
488
489 /* Check the parameter */
490 assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
491
492 status = HAL_COMP_Start(hcomp);
493 if(status == HAL_OK)
494 {
495 /* Check the Exti Line output configuration */
496 extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
497 /* Configure the rising edge */
498 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
499 {
500 __HAL_COMP_EXTI_RISING_IT_ENABLE(extiline);
501 }
502 else
503 {
504 __HAL_COMP_EXTI_RISING_IT_DISABLE(extiline);
505 }
506 /* Configure the falling edge */
507 if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
508 {
509 __HAL_COMP_EXTI_FALLING_IT_ENABLE(extiline);
510 }
511 else
512 {
513 __HAL_COMP_EXTI_FALLING_IT_DISABLE(extiline);
514 }
515 /* Enable Exti interrupt mode */
516 __HAL_COMP_EXTI_ENABLE_IT(extiline);
517 /* Clear COMP Exti pending bit */
518 __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
519 }
520
521 return status;
522 }
523
524 /**
525 * @brief Disable the interrupt and Stop the comparator
526 * @param hcomp: COMP handle
527 * @retval HAL status
528 */
529 HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
530 {
531 HAL_StatusTypeDef status = HAL_OK;
532
533 /* Disable the Exti Line interrupt mode */
534 __HAL_COMP_EXTI_DISABLE_IT(__HAL_COMP_GET_EXTI_LINE(hcomp->Instance));
535
536 status = HAL_COMP_Stop(hcomp);
537
538 return status;
539 }
540
541 /**
542 * @brief Comparator IRQ Handler
543 * @param hcomp: COMP handle
544 * @retval HAL status
545 */
546 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
547 {
548 uint32_t extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
549
550 /* Check COMP Exti flag */
551 if(__HAL_COMP_EXTI_GET_FLAG(extiline) != RESET)
552 {
553 /* Clear COMP Exti pending bit */
554 __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
555
556 /* COMP trigger user callback */
557 HAL_COMP_TriggerCallback(hcomp);
558 }
559 }
560
561 /**
562 * @}
563 */
564
565 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
566 * @brief management functions
567 *
568 @verbatim
569 ===============================================================================
570 ##### Peripheral Control functions #####
571 ===============================================================================
572 [..]
573 This subsection provides a set of functions allowing to control the COMP data
574 transfers.
575
576 @endverbatim
577 * @{
578 */
579
580 /**
581 * @brief Lock the selected comparator configuration.
582 * @param hcomp: COMP handle
583 * @retval HAL status
584 */
585 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
586 {
587 HAL_StatusTypeDef status = HAL_OK;
588
589 /* Check the COMP handle allocation and lock status */
590 if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
591 {
592 status = HAL_ERROR;
593 }
594 else
595 {
596 /* Check the parameter */
597 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
598
599 /* Set lock flag */
600 hcomp->State |= COMP_STATE_BIT_LOCK;
601
602 /* Set the lock bit corresponding to selected comparator */
603 COMP_LOCK(hcomp);
604 }
605
606 return status;
607 }
608
609 /**
610 * @brief Return the output level (high or low) of the selected comparator.
611 * The output level depends on the selected polarity.
612 * If the polarity is not inverted:
613 * - Comparator output is low when the non-inverting input is at a lower
614 * voltage than the inverting input
615 * - Comparator output is high when the non-inverting input is at a higher
616 * voltage than the inverting input
617 * If the polarity is inverted:
618 * - Comparator output is high when the non-inverting input is at a lower
619 * voltage than the inverting input
620 * - Comparator output is low when the non-inverting input is at a higher
621 * voltage than the inverting input
622 * @param hcomp: COMP handle
623 * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
624 *
625 */
626 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
627 {
628 uint32_t level=0;
629
630 /* Check the parameter */
631 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
632
633 level = READ_BIT(hcomp->Instance->CSR, COMP_CSR_COMPxOUT);
634
635 if(level != 0)
636 {
637 return(COMP_OUTPUTLEVEL_HIGH);
638 }
639 return(COMP_OUTPUTLEVEL_LOW);
640 }
641
642 /**
643 * @brief Comparator callback.
644 * @param hcomp: COMP handle
645 * @retval None
646 */
647 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
648 {
649 /* NOTE : This function should not be modified, when the callback is needed,
650 the HAL_COMP_TriggerCallback should be implemented in the user file
651 */
652 }
653
654
655 /**
656 * @}
657 */
658
659 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
660 * @brief Peripheral State functions
661 *
662 @verbatim
663 ===============================================================================
664 ##### Peripheral State functions #####
665 ===============================================================================
666 [..]
667 This subsection permit to get in run-time the status of the peripheral
668 and the data flow.
669
670 @endverbatim
671 * @{
672 */
673
674 /**
675 * @brief Return the COMP state
676 * @param hcomp : COMP handle
677 * @retval HAL state
678 */
679 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
680 {
681 /* Check the COMP handle allocation */
682 if(hcomp == HAL_NULL)
683 {
684 return HAL_COMP_STATE_RESET;
685 }
686
687 /* Check the parameter */
688 assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
689
690 return hcomp->State;
691 }
692 /**
693 * @}
694 */
695
696 /**
697 * @}
698 */
699
700 #endif /* HAL_COMP_MODULE_ENABLED */
701 /**
702 * @}
703 */
704
705 /**
706 * @}
707 */
708
709 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Imprint / Impressum