提交 | 用户 | age
|
483170
|
1 |
/** |
Q |
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 |
* + I/O operation functions |
|
10 |
* + Peripheral Control functions |
|
11 |
* + Peripheral State functions |
|
12 |
* |
|
13 |
@verbatim |
|
14 |
================================================================================ |
|
15 |
##### COMP Peripheral features ##### |
|
16 |
================================================================================ |
|
17 |
|
|
18 |
[..] |
|
19 |
The STM32F0xx device family integrates up to 2 analog comparators COMP1 and COMP2: |
|
20 |
(+) The non inverting input and inverting input can be set to GPIO pins. |
|
21 |
|
|
22 |
(+) The COMP output is available using HAL_COMP_GetOutputLevel() |
|
23 |
and can be set on GPIO pins. |
|
24 |
|
|
25 |
(+) The COMP output can be redirected to embedded timers (TIM1, TIM2 and TIM3). |
|
26 |
|
|
27 |
(+) The comparators COMP1 and COMP2 can be combined in window mode. |
|
28 |
|
|
29 |
(+) The comparators have interrupt capability with wake-up |
|
30 |
from Sleep and Stop modes (through the EXTI controller): |
|
31 |
(++) COMP1 is internally connected to EXTI Line 21 |
|
32 |
(++) COMP2 is internally connected to EXTI Line 22 |
|
33 |
|
|
34 |
(+) From the corresponding IRQ handler, the right interrupt source can be retrieved with the |
|
35 |
macros __HAL_COMP_COMP1_EXTI_GET_FLAG() and __HAL_COMP_COMP2_EXTI_GET_FLAG(). |
|
36 |
|
|
37 |
|
|
38 |
##### How to use this driver ##### |
|
39 |
================================================================================ |
|
40 |
[..] |
|
41 |
This driver provides functions to configure and program the Comparators of STM32F05x, STM32F07x and STM32F09x devices. |
|
42 |
|
|
43 |
To use the comparator, perform the following steps: |
|
44 |
|
|
45 |
(#) Fill in the HAL_COMP_MspInit() to |
|
46 |
(++) Configure the comparator input in analog mode using HAL_GPIO_Init() |
|
47 |
(++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator |
|
48 |
output to the GPIO pin |
|
49 |
(++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and |
|
50 |
selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator |
|
51 |
interrupt vector using HAL_NVIC_EnableIRQ() function. |
|
52 |
|
|
53 |
(#) Configure the comparator using HAL_COMP_Init() function: |
|
54 |
(++) Select the inverting input (input minus) |
|
55 |
(++) Select the non inverting input (input plus) |
|
56 |
(++) Select the output polarity |
|
57 |
(++) Select the output redirection |
|
58 |
(++) Select the hysteresis level |
|
59 |
(++) Select the power mode |
|
60 |
(++) Select the event/interrupt mode |
|
61 |
(++) Select the window mode |
|
62 |
|
|
63 |
-@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order |
|
64 |
to access the comparator(s) registers. |
|
65 |
|
|
66 |
(#) Enable the comparator using HAL_COMP_Start() function or HAL_COMP_Start_IT() function for interrupt mode. |
|
67 |
|
|
68 |
(#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions |
|
69 |
to manage comparator outputs (event/interrupt triggered and output level). |
|
70 |
|
|
71 |
(#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT() |
|
72 |
function. |
|
73 |
|
|
74 |
(#) De-initialize the comparator using HAL_COMP_DeInit() function. |
|
75 |
|
|
76 |
(#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function. |
|
77 |
Only a MCU reset can reset that protection. |
|
78 |
|
|
79 |
@endverbatim |
|
80 |
****************************************************************************** |
|
81 |
* @attention |
|
82 |
* |
|
83 |
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
|
84 |
* |
|
85 |
* Redistribution and use in source and binary forms, with or without modification, |
|
86 |
* are permitted provided that the following conditions are met: |
|
87 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
88 |
* this list of conditions and the following disclaimer. |
|
89 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
90 |
* this list of conditions and the following disclaimer in the documentation |
|
91 |
* and/or other materials provided with the distribution. |
|
92 |
* 3. Neither the name of STMicroelectronics nor the names of its contributors |
|
93 |
* may be used to endorse or promote products derived from this software |
|
94 |
* without specific prior written permission. |
|
95 |
* |
|
96 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
97 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
98 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
99 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
100 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
101 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
102 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
103 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
104 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
105 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
106 |
* |
|
107 |
****************************************************************************** |
|
108 |
*/ |
|
109 |
|
|
110 |
/* |
|
111 |
Additional Tables: |
|
112 |
|
|
113 |
Table 1. COMP Inputs for the STM32F05x, STM32F07x and STM32F09x devices |
|
114 |
+--------------------------------------------------+ |
|
115 |
| | | COMP1 | COMP2 | |
|
116 |
|-----------------|----------------|---------------| |
|
117 |
| | 1/4 VREFINT | OK | OK | |
|
118 |
| | 1/2 VREFINT | OK | OK | |
|
119 |
| | 3/4 VREFINT | OK | OK | |
|
120 |
| Inverting Input | VREFINT | OK | OK | |
|
121 |
| | DAC1 OUT (PA4) | OK | OK | |
|
122 |
| | DAC2 OUT (PA5) | OK | OK | |
|
123 |
| | IO1 | PA0 | PA2 | |
|
124 |
|-----------------|----------------|-------|-------| |
|
125 |
| Non Inverting | | PA1 | PA3 | |
|
126 |
| Input | | | | |
|
127 |
+--------------------------------------------------+ |
|
128 |
|
|
129 |
Table 2. COMP Outputs for the STM32F05x, STM32F07x and STM32F09x devices |
|
130 |
+---------------+ |
|
131 |
| COMP1 | COMP2 | |
|
132 |
|-------|-------| |
|
133 |
| PA0 | PA2 | |
|
134 |
| PA6 | PA7 | |
|
135 |
| PA11 | PA12 | |
|
136 |
+---------------+ |
|
137 |
|
|
138 |
Table 3. COMP Outputs redirection to embedded timers for the STM32F05x, STM32F07x and STM32F09x devices |
|
139 |
+---------------------------------+ |
|
140 |
| COMP1 | COMP2 | |
|
141 |
|----------------|----------------| |
|
142 |
| TIM1 BKIN | TIM1 BKIN | |
|
143 |
| | | |
|
144 |
| TIM1 OCREFCLR | TIM1 OCREFCLR | |
|
145 |
| | | |
|
146 |
| TIM1 IC1 | TIM1 IC1 | |
|
147 |
| | | |
|
148 |
| TIM2 IC4 | TIM2 IC4 | |
|
149 |
| | | |
|
150 |
| TIM2 OCREFCLR | TIM2 OCREFCLR | |
|
151 |
| | | |
|
152 |
| TIM3 IC1 | TIM3 IC1 | |
|
153 |
| | | |
|
154 |
| TIM3 OCREFCLR | TIM3 OCREFCLR | |
|
155 |
+---------------------------------+ |
|
156 |
|
|
157 |
*/ |
|
158 |
|
|
159 |
/* Includes ------------------------------------------------------------------*/ |
|
160 |
#include "stm32f0xx_hal.h" |
|
161 |
|
|
162 |
#ifdef HAL_COMP_MODULE_ENABLED |
|
163 |
|
|
164 |
#if defined(STM32F051x8) || defined(STM32F058xx) || \ |
|
165 |
defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || \ |
|
166 |
defined(STM32F091xC) || defined (STM32F098xx) |
|
167 |
|
|
168 |
/** @addtogroup STM32F0xx_HAL_Driver |
|
169 |
* @{ |
|
170 |
*/ |
|
171 |
|
|
172 |
/** @defgroup COMP COMP |
|
173 |
* @brief COMP HAL module driver |
|
174 |
* @{ |
|
175 |
*/ |
|
176 |
|
|
177 |
/* Private typedef -----------------------------------------------------------*/ |
|
178 |
/* Private define ------------------------------------------------------------*/ |
|
179 |
|
|
180 |
/** @defgroup COMP_Private_Constants COMP Private Constants |
|
181 |
* @{ |
|
182 |
*/ |
|
183 |
|
|
184 |
/* Delay for COMP startup time. */ |
|
185 |
/* Note: Delay required to reach propagation delay specification. */ |
|
186 |
/* Literal set to maximum value (refer to device datasheet, */ |
|
187 |
/* parameter "tSTART"). */ |
|
188 |
/* Unit: us */ |
|
189 |
#define COMP_DELAY_STARTUP_US (60U) /*!< Delay for COMP startup time */ |
|
190 |
|
|
191 |
/* CSR register reset value */ |
|
192 |
#define COMP_CSR_RESET_VALUE (0x00000000U) |
|
193 |
/* CSR register masks */ |
|
194 |
#define COMP_CSR_RESET_PARAMETERS_MASK (0x00003FFFU) |
|
195 |
#define COMP_CSR_UPDATE_PARAMETERS_MASK (0x00003FFEU) |
|
196 |
/* CSR COMPx non inverting input mask */ |
|
197 |
#define COMP_CSR_COMPxNONINSEL_MASK ((uint16_t)COMP_CSR_COMP1SW1) |
|
198 |
/* CSR COMP2 shift */ |
|
199 |
#define COMP_CSR_COMP1_SHIFT 0U |
|
200 |
#define COMP_CSR_COMP2_SHIFT 16U |
|
201 |
/** |
|
202 |
* @} |
|
203 |
*/ |
|
204 |
/* Private macro -------------------------------------------------------------*/ |
|
205 |
/* Private variables ---------------------------------------------------------*/ |
|
206 |
/* Private function prototypes -----------------------------------------------*/ |
|
207 |
/* Private functions ---------------------------------------------------------*/ |
|
208 |
|
|
209 |
/** @defgroup COMP_Exported_Functions COMP Exported Functions |
|
210 |
* @{ |
|
211 |
*/ |
|
212 |
|
|
213 |
/** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions |
|
214 |
* @brief Initialization and Configuration functions |
|
215 |
* |
|
216 |
@verbatim |
|
217 |
=============================================================================== |
|
218 |
##### Initialization and Configuration functions ##### |
|
219 |
=============================================================================== |
|
220 |
[..] This section provides functions to initialize and de-initialize comparators |
|
221 |
|
|
222 |
@endverbatim |
|
223 |
* @{ |
|
224 |
*/ |
|
225 |
|
|
226 |
/** |
|
227 |
* @brief Initializes the COMP according to the specified |
|
228 |
* parameters in the COMP_InitTypeDef and create the associated handle. |
|
229 |
* @note If the selected comparator is locked, initialization can't be performed. |
|
230 |
* To unlock the configuration, perform a system reset. |
|
231 |
* @param hcomp COMP handle |
|
232 |
* @retval HAL status |
|
233 |
*/ |
|
234 |
HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp) |
|
235 |
{ |
|
236 |
HAL_StatusTypeDef status = HAL_OK; |
|
237 |
uint32_t regshift = COMP_CSR_COMP1_SHIFT; |
|
238 |
|
|
239 |
/* Check the COMP handle allocation and lock status */ |
|
240 |
if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET)) |
|
241 |
{ |
|
242 |
status = HAL_ERROR; |
|
243 |
} |
|
244 |
else |
|
245 |
{ |
|
246 |
/* Check the parameter */ |
|
247 |
assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); |
|
248 |
assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput)); |
|
249 |
assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput)); |
|
250 |
assert_param(IS_COMP_OUTPUT(hcomp->Init.Output)); |
|
251 |
assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol)); |
|
252 |
assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis)); |
|
253 |
assert_param(IS_COMP_MODE(hcomp->Init.Mode)); |
|
254 |
|
|
255 |
if(hcomp->Init.NonInvertingInput == COMP_NONINVERTINGINPUT_DAC1SWITCHCLOSED) |
|
256 |
{ |
|
257 |
assert_param(IS_COMP_DAC1SWITCH_INSTANCE(hcomp->Instance)); |
|
258 |
} |
|
259 |
|
|
260 |
if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE) |
|
261 |
{ |
|
262 |
assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance)); |
|
263 |
} |
|
264 |
|
|
265 |
/* Init SYSCFG and the low level hardware to access comparators */ |
|
266 |
__HAL_RCC_SYSCFG_CLK_ENABLE(); |
|
267 |
|
|
268 |
/* Init the low level hardware : SYSCFG to access comparators */ |
|
269 |
HAL_COMP_MspInit(hcomp); |
|
270 |
|
|
271 |
if(hcomp->State == HAL_COMP_STATE_RESET) |
|
272 |
{ |
|
273 |
/* Allocate lock resource and initialize it */ |
|
274 |
hcomp->Lock = HAL_UNLOCKED; |
|
275 |
} |
|
276 |
|
|
277 |
/* Change COMP peripheral state */ |
|
278 |
hcomp->State = HAL_COMP_STATE_BUSY; |
|
279 |
|
|
280 |
/* Set COMP parameters */ |
|
281 |
/* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */ |
|
282 |
/* Set COMPxOUTSEL bits according to hcomp->Init.Output value */ |
|
283 |
/* Set COMPxPOL bit according to hcomp->Init.OutputPol value */ |
|
284 |
/* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */ |
|
285 |
/* Set COMPxMODE bits according to hcomp->Init.Mode value */ |
|
286 |
if(hcomp->Instance == COMP2) |
|
287 |
{ |
|
288 |
regshift = COMP_CSR_COMP2_SHIFT; |
|
289 |
} |
|
290 |
MODIFY_REG(COMP->CSR, |
|
291 |
(COMP_CSR_COMPxINSEL | COMP_CSR_COMPxNONINSEL_MASK | \ |
|
292 |
COMP_CSR_COMPxOUTSEL | COMP_CSR_COMPxPOL | \ |
|
293 |
COMP_CSR_COMPxHYST | COMP_CSR_COMPxMODE) << regshift, |
|
294 |
(hcomp->Init.InvertingInput | \ |
|
295 |
hcomp->Init.NonInvertingInput | \ |
|
296 |
hcomp->Init.Output | \ |
|
297 |
hcomp->Init.OutputPol | \ |
|
298 |
hcomp->Init.Hysteresis | \ |
|
299 |
hcomp->Init.Mode) << regshift); |
|
300 |
|
|
301 |
if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE) |
|
302 |
{ |
|
303 |
COMP->CSR |= COMP_CSR_WNDWEN; |
|
304 |
} |
|
305 |
|
|
306 |
/* Initialize the COMP state*/ |
|
307 |
hcomp->State = HAL_COMP_STATE_READY; |
|
308 |
} |
|
309 |
|
|
310 |
return status; |
|
311 |
} |
|
312 |
|
|
313 |
/** |
|
314 |
* @brief DeInitializes the COMP peripheral |
|
315 |
* @note Deinitialization can't be performed if the COMP configuration is locked. |
|
316 |
* To unlock the configuration, perform a system reset. |
|
317 |
* @param hcomp COMP handle |
|
318 |
* @retval HAL status |
|
319 |
*/ |
|
320 |
HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp) |
|
321 |
{ |
|
322 |
HAL_StatusTypeDef status = HAL_OK; |
|
323 |
uint32_t regshift = COMP_CSR_COMP1_SHIFT; |
|
324 |
|
|
325 |
/* Check the COMP handle allocation and lock status */ |
|
326 |
if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET)) |
|
327 |
{ |
|
328 |
status = HAL_ERROR; |
|
329 |
} |
|
330 |
else |
|
331 |
{ |
|
332 |
/* Check the parameter */ |
|
333 |
assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); |
|
334 |
|
|
335 |
/* Set COMP_CSR register to reset value for the corresponding COMP instance */ |
|
336 |
if(hcomp->Instance == COMP2) |
|
337 |
{ |
|
338 |
regshift = COMP_CSR_COMP2_SHIFT; |
|
339 |
} |
|
340 |
MODIFY_REG(COMP->CSR, |
|
341 |
COMP_CSR_RESET_PARAMETERS_MASK << regshift, |
|
342 |
COMP_CSR_RESET_VALUE << regshift); |
|
343 |
|
|
344 |
/* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */ |
|
345 |
HAL_COMP_MspDeInit(hcomp); |
|
346 |
|
|
347 |
hcomp->State = HAL_COMP_STATE_RESET; |
|
348 |
|
|
349 |
/* Release Lock */ |
|
350 |
__HAL_UNLOCK(hcomp); |
|
351 |
} |
|
352 |
|
|
353 |
return status; |
|
354 |
} |
|
355 |
|
|
356 |
/** |
|
357 |
* @brief Initializes the COMP MSP. |
|
358 |
* @param hcomp COMP handle |
|
359 |
* @retval None |
|
360 |
*/ |
|
361 |
__weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp) |
|
362 |
{ |
|
363 |
/* Prevent unused argument(s) compilation warning */ |
|
364 |
UNUSED(hcomp); |
|
365 |
|
|
366 |
/* NOTE : This function Should not be modified, when the callback is needed, |
|
367 |
the HAL_COMP_MspInit could be implenetd in the user file |
|
368 |
*/ |
|
369 |
} |
|
370 |
|
|
371 |
/** |
|
372 |
* @brief DeInitializes COMP MSP. |
|
373 |
* @param hcomp COMP handle |
|
374 |
* @retval None |
|
375 |
*/ |
|
376 |
__weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp) |
|
377 |
{ |
|
378 |
/* Prevent unused argument(s) compilation warning */ |
|
379 |
UNUSED(hcomp); |
|
380 |
|
|
381 |
/* NOTE : This function Should not be modified, when the callback is needed, |
|
382 |
the HAL_COMP_MspDeInit could be implenetd in the user file |
|
383 |
*/ |
|
384 |
} |
|
385 |
|
|
386 |
/** |
|
387 |
* @} |
|
388 |
*/ |
|
389 |
|
|
390 |
/** @defgroup COMP_Exported_Functions_Group2 I/O operation functions |
|
391 |
* @brief Data transfers functions |
|
392 |
* |
|
393 |
@verbatim |
|
394 |
=============================================================================== |
|
395 |
##### IO operation functions ##### |
|
396 |
=============================================================================== |
|
397 |
[..] |
|
398 |
This subsection provides a set of functions allowing to manage the COMP data |
|
399 |
transfers. |
|
400 |
|
|
401 |
@endverbatim |
|
402 |
* @{ |
|
403 |
*/ |
|
404 |
|
|
405 |
/** |
|
406 |
* @brief Start the comparator |
|
407 |
* @param hcomp COMP handle |
|
408 |
* @retval HAL status |
|
409 |
*/ |
|
410 |
HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp) |
|
411 |
{ |
|
412 |
uint32_t wait_loop_index = 0U; |
|
413 |
HAL_StatusTypeDef status = HAL_OK; |
|
414 |
uint32_t regshift = COMP_CSR_COMP1_SHIFT; |
|
415 |
|
|
416 |
/* Check the COMP handle allocation and lock status */ |
|
417 |
if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET)) |
|
418 |
{ |
|
419 |
status = HAL_ERROR; |
|
420 |
} |
|
421 |
else |
|
422 |
{ |
|
423 |
/* Check the parameter */ |
|
424 |
assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); |
|
425 |
|
|
426 |
if(hcomp->State == HAL_COMP_STATE_READY) |
|
427 |
{ |
|
428 |
/* Enable the selected comparator */ |
|
429 |
if(hcomp->Instance == COMP2) |
|
430 |
{ |
|
431 |
regshift = COMP_CSR_COMP2_SHIFT; |
|
432 |
} |
|
433 |
SET_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift); |
|
434 |
|
|
435 |
/* Set HAL COMP handle state */ |
|
436 |
hcomp->State = HAL_COMP_STATE_BUSY; |
|
437 |
|
|
438 |
/* Delay for COMP startup time */ |
|
439 |
wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / 1000000U)); |
|
440 |
while(wait_loop_index != 0U) |
|
441 |
{ |
|
442 |
wait_loop_index--; |
|
443 |
} |
|
444 |
} |
|
445 |
else |
|
446 |
{ |
|
447 |
status = HAL_ERROR; |
|
448 |
} |
|
449 |
} |
|
450 |
|
|
451 |
return status; |
|
452 |
} |
|
453 |
|
|
454 |
/** |
|
455 |
* @brief Stop the comparator |
|
456 |
* @param hcomp COMP handle |
|
457 |
* @retval HAL status |
|
458 |
*/ |
|
459 |
HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp) |
|
460 |
{ |
|
461 |
HAL_StatusTypeDef status = HAL_OK; |
|
462 |
uint32_t regshift = COMP_CSR_COMP1_SHIFT; |
|
463 |
|
|
464 |
/* Check the COMP handle allocation and lock status */ |
|
465 |
if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET)) |
|
466 |
{ |
|
467 |
status = HAL_ERROR; |
|
468 |
} |
|
469 |
else |
|
470 |
{ |
|
471 |
/* Check the parameter */ |
|
472 |
assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); |
|
473 |
|
|
474 |
if(hcomp->State == HAL_COMP_STATE_BUSY) |
|
475 |
{ |
|
476 |
/* Disable the selected comparator */ |
|
477 |
if(hcomp->Instance == COMP2) |
|
478 |
{ |
|
479 |
regshift = COMP_CSR_COMP2_SHIFT; |
|
480 |
} |
|
481 |
CLEAR_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift); |
|
482 |
|
|
483 |
hcomp->State = HAL_COMP_STATE_READY; |
|
484 |
} |
|
485 |
else |
|
486 |
{ |
|
487 |
status = HAL_ERROR; |
|
488 |
} |
|
489 |
} |
|
490 |
|
|
491 |
return status; |
|
492 |
} |
|
493 |
|
|
494 |
/** |
|
495 |
* @brief Enables the interrupt and starts the comparator |
|
496 |
* @param hcomp COMP handle |
|
497 |
* @retval HAL status. |
|
498 |
*/ |
|
499 |
HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp) |
|
500 |
{ |
|
501 |
HAL_StatusTypeDef status = HAL_OK; |
|
502 |
uint32_t extiline = 0U; |
|
503 |
|
|
504 |
/* Check the parameter */ |
|
505 |
assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode)); |
|
506 |
|
|
507 |
status = HAL_COMP_Start(hcomp); |
|
508 |
if(status == HAL_OK) |
|
509 |
{ |
|
510 |
/* Check the Exti Line output configuration */ |
|
511 |
extiline = COMP_GET_EXTI_LINE(hcomp->Instance); |
|
512 |
/* Configure the rising edge */ |
|
513 |
if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET) |
|
514 |
{ |
|
515 |
SET_BIT(EXTI->RTSR, extiline); |
|
516 |
} |
|
517 |
else |
|
518 |
{ |
|
519 |
CLEAR_BIT(EXTI->RTSR, extiline); |
|
520 |
} |
|
521 |
/* Configure the falling edge */ |
|
522 |
if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET) |
|
523 |
{ |
|
524 |
SET_BIT(EXTI->FTSR, extiline); |
|
525 |
} |
|
526 |
else |
|
527 |
{ |
|
528 |
CLEAR_BIT(EXTI->FTSR, extiline); |
|
529 |
} |
|
530 |
|
|
531 |
/* Clear COMP EXTI pending bit */ |
|
532 |
WRITE_REG(EXTI->PR, extiline); |
|
533 |
|
|
534 |
/* Enable Exti interrupt mode */ |
|
535 |
SET_BIT(EXTI->IMR, extiline); |
|
536 |
} |
|
537 |
|
|
538 |
return status; |
|
539 |
} |
|
540 |
|
|
541 |
/** |
|
542 |
* @brief Disable the interrupt and Stop the comparator |
|
543 |
* @param hcomp COMP handle |
|
544 |
* @retval HAL status |
|
545 |
*/ |
|
546 |
HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp) |
|
547 |
{ |
|
548 |
HAL_StatusTypeDef status = HAL_OK; |
|
549 |
|
|
550 |
/* Disable the Exti Line interrupt mode */ |
|
551 |
CLEAR_BIT(EXTI->IMR, COMP_GET_EXTI_LINE(hcomp->Instance)); |
|
552 |
|
|
553 |
status = HAL_COMP_Stop(hcomp); |
|
554 |
|
|
555 |
return status; |
|
556 |
} |
|
557 |
|
|
558 |
/** |
|
559 |
* @brief Comparator IRQ Handler |
|
560 |
* @param hcomp COMP handle |
|
561 |
* @retval HAL status |
|
562 |
*/ |
|
563 |
void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp) |
|
564 |
{ |
|
565 |
uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance); |
|
566 |
|
|
567 |
/* Check COMP Exti flag */ |
|
568 |
if(READ_BIT(EXTI->PR, extiline) != RESET) |
|
569 |
{ |
|
570 |
/* Clear COMP Exti pending bit */ |
|
571 |
WRITE_REG(EXTI->PR, extiline); |
|
572 |
|
|
573 |
/* COMP trigger user callback */ |
|
574 |
HAL_COMP_TriggerCallback(hcomp); |
|
575 |
} |
|
576 |
} |
|
577 |
|
|
578 |
/** |
|
579 |
* @} |
|
580 |
*/ |
|
581 |
|
|
582 |
/** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions |
|
583 |
* @brief management functions |
|
584 |
* |
|
585 |
@verbatim |
|
586 |
=============================================================================== |
|
587 |
##### Peripheral Control functions ##### |
|
588 |
=============================================================================== |
|
589 |
[..] |
|
590 |
This subsection provides a set of functions allowing to control the COMP data |
|
591 |
transfers. |
|
592 |
|
|
593 |
@endverbatim |
|
594 |
* @{ |
|
595 |
*/ |
|
596 |
|
|
597 |
/** |
|
598 |
* @brief Lock the selected comparator configuration. |
|
599 |
* @param hcomp COMP handle |
|
600 |
* @retval HAL status |
|
601 |
*/ |
|
602 |
HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp) |
|
603 |
{ |
|
604 |
HAL_StatusTypeDef status = HAL_OK; |
|
605 |
uint32_t regshift = COMP_CSR_COMP1_SHIFT; |
|
606 |
|
|
607 |
/* Check the COMP handle allocation and lock status */ |
|
608 |
if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET)) |
|
609 |
{ |
|
610 |
status = HAL_ERROR; |
|
611 |
} |
|
612 |
else |
|
613 |
{ |
|
614 |
/* Check the parameter */ |
|
615 |
assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); |
|
616 |
|
|
617 |
/* Set lock flag */ |
|
618 |
hcomp->State |= COMP_STATE_BIT_LOCK; |
|
619 |
|
|
620 |
/* Set the lock bit corresponding to selected comparator */ |
|
621 |
if(hcomp->Instance == COMP2) |
|
622 |
{ |
|
623 |
regshift = COMP_CSR_COMP2_SHIFT; |
|
624 |
} |
|
625 |
SET_BIT(COMP->CSR, COMP_CSR_COMPxLOCK << regshift); |
|
626 |
} |
|
627 |
|
|
628 |
return status; |
|
629 |
} |
|
630 |
|
|
631 |
/** |
|
632 |
* @brief Return the output level (high or low) of the selected comparator. |
|
633 |
* The output level depends on the selected polarity. |
|
634 |
* If the polarity is not inverted: |
|
635 |
* - Comparator output is low when the non-inverting input is at a lower |
|
636 |
* voltage than the inverting input |
|
637 |
* - Comparator output is high when the non-inverting input is at a higher |
|
638 |
* voltage than the inverting input |
|
639 |
* If the polarity is inverted: |
|
640 |
* - Comparator output is high when the non-inverting input is at a lower |
|
641 |
* voltage than the inverting input |
|
642 |
* - Comparator output is low when the non-inverting input is at a higher |
|
643 |
* voltage than the inverting input |
|
644 |
* @param hcomp COMP handle |
|
645 |
* @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH. |
|
646 |
* |
|
647 |
*/ |
|
648 |
uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp) |
|
649 |
{ |
|
650 |
uint32_t level=0; |
|
651 |
uint32_t regshift = COMP_CSR_COMP1_SHIFT; |
|
652 |
|
|
653 |
/* Check the parameter */ |
|
654 |
assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); |
|
655 |
|
|
656 |
if(hcomp->Instance == COMP2) |
|
657 |
{ |
|
658 |
regshift = COMP_CSR_COMP2_SHIFT; |
|
659 |
} |
|
660 |
level = READ_BIT(COMP->CSR, COMP_CSR_COMPxOUT << regshift); |
|
661 |
|
|
662 |
if(level != 0U) |
|
663 |
{ |
|
664 |
return(COMP_OUTPUTLEVEL_HIGH); |
|
665 |
} |
|
666 |
return(COMP_OUTPUTLEVEL_LOW); |
|
667 |
} |
|
668 |
|
|
669 |
/** |
|
670 |
* @brief Comparator callback. |
|
671 |
* @param hcomp COMP handle |
|
672 |
* @retval None |
|
673 |
*/ |
|
674 |
__weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) |
|
675 |
{ |
|
676 |
/* Prevent unused argument(s) compilation warning */ |
|
677 |
UNUSED(hcomp); |
|
678 |
|
|
679 |
/* NOTE : This function should not be modified, when the callback is needed, |
|
680 |
the HAL_COMP_TriggerCallback should be implemented in the user file |
|
681 |
*/ |
|
682 |
} |
|
683 |
|
|
684 |
|
|
685 |
/** |
|
686 |
* @} |
|
687 |
*/ |
|
688 |
|
|
689 |
/** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions |
|
690 |
* @brief Peripheral State functions |
|
691 |
* |
|
692 |
@verbatim |
|
693 |
=============================================================================== |
|
694 |
##### Peripheral State functions ##### |
|
695 |
=============================================================================== |
|
696 |
[..] |
|
697 |
This subsection permit to get in run-time the status of the peripheral |
|
698 |
and the data flow. |
|
699 |
|
|
700 |
@endverbatim |
|
701 |
* @{ |
|
702 |
*/ |
|
703 |
|
|
704 |
/** |
|
705 |
* @brief Return the COMP state |
|
706 |
* @param hcomp COMP handle |
|
707 |
* @retval HAL state |
|
708 |
*/ |
|
709 |
uint32_t HAL_COMP_GetState(COMP_HandleTypeDef *hcomp) |
|
710 |
{ |
|
711 |
/* Check the COMP handle allocation */ |
|
712 |
if(hcomp == NULL) |
|
713 |
{ |
|
714 |
return HAL_COMP_STATE_RESET; |
|
715 |
} |
|
716 |
|
|
717 |
/* Check the parameter */ |
|
718 |
assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); |
|
719 |
|
|
720 |
return hcomp->State; |
|
721 |
} |
|
722 |
/** |
|
723 |
* @} |
|
724 |
*/ |
|
725 |
|
|
726 |
/** |
|
727 |
* @} |
|
728 |
*/ |
|
729 |
|
|
730 |
/** |
|
731 |
* @} |
|
732 |
*/ |
|
733 |
|
|
734 |
/** |
|
735 |
* @} |
|
736 |
*/ |
|
737 |
|
|
738 |
#endif /* STM32F051x8 || STM32F058xx || */ |
|
739 |
/* STM32F071xB || STM32F072xB || STM32F078xx || */ |
|
740 |
/* STM32F091xC || defined (STM32F098xx) */ |
|
741 |
|
|
742 |
#endif /* HAL_COMP_MODULE_ENABLED */ |
|
743 |
|
|
744 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |