1 /*
2 * SPDX-FileCopyrightText: 2016 STMicroelectronics
3 * SPDX-FileCopyrightText: 2019-2025 SiFli Technologies(Nanjing) Co., Ltd
4 *
5 * SPDX-License-Identifier: BSD-3-Clause AND Apache-2.0
6 */
7
8 #include <string.h>
9 #include "bf0_hal.h"
10
11 /** @addtogroup BF0_HAL_Driver
12 * @{
13 */
14
15 #ifdef HAL_COMP_MODULE_ENABLED
16
17
18 /** @defgroup COMP COMP
19 * @brief COMP HAL module driver
20 * @{
21 */
22
23 /* Private typedef -----------------------------------------------------------*/
24 /* Private define ------------------------------------------------------------*/
25 /** @addtogroup COMP_Private_Constants
26 * @{
27 */
28
29 /* Delay for COMP startup time. */
30 /* Note: Delay required to reach propagation delay specification. */
31 /* Literal set to maximum value (refer to device datasheet, */
32 /* parameter "tSTART"). */
33 /* Unit: us */
34 #define COMP_DELAY_STARTUP_US (200UL) /*!< Delay for COMP startup time */
35
36 /* Delay for COMP voltage scaler stabilization time. */
37 /* Literal set to maximum value (refer to device datasheet, */
38 /* parameter "tSTART_SCALER"). */
39 /* Unit: us */
40 #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200UL) /*!< Delay for COMP voltage scaler stabilization time */
41
42 #define COMP_OUTPUT_LEVEL_BITOFFSET_POS (LPCOMP_CR1_VAL_Pos)
43
44 /**
45 * @}
46 */
47
48 /* Private macro -------------------------------------------------------------*/
49 /* Private variables ---------------------------------------------------------*/
50 /* Private function prototypes -----------------------------------------------*/
51 /* Exported functions --------------------------------------------------------*/
52
53 /** @defgroup COMP_Exported_Functions COMP Exported Functions
54 * @{
55 */
56
57 /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
58 * @brief Initialization and de-initialization functions.
59 *
60 @verbatim
61 ===============================================================================
62 ##### Initialization and de-initialization functions #####
63 ===============================================================================
64 [..] This section provides functions to initialize and de-initialize comparators
65
66 @endverbatim
67 * @{
68 */
69
70 /**
71 * @brief Initialize the COMP according to the specified
72 * parameters in the COMP_InitTypeDef and initialize the associated handle.
73 * @note If the selected comparator is locked, initialization can't be performed.
74 * To unlock the configuration, perform a system reset.
75 * @param hcomp COMP handle
76 * @retval HAL status
77 */
HAL_COMP_Init(COMP_HandleTypeDef * hcomp)78 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
79 {
80 uint32_t tmp_csr;
81 HAL_StatusTypeDef status = HAL_OK;
82
83 /* Check the COMP handle allocation and lock status */
84 if (hcomp == NULL)
85 {
86 status = HAL_ERROR;
87 }
88 else
89 {
90 if (hcomp->State == HAL_COMP_STATE_RESET)
91 {
92 /* Allocate lock resource and initialize it */
93 hcomp->Lock = HAL_UNLOCKED;
94
95 /* Set COMP error code to none */
96 COMP_CLEAR_ERRORCODE(hcomp);
97
98 /* Init SYSCFG and the low level hardware to access comparators */
99 /* Note: HAL_COMP_Init() calls __HAL_RCC_SYSCFG_CLK_ENABLE() */
100 /* to enable internal control clock of the comparators. */
101 /* However, this is a legacy strategy. In future STM32 families, */
102 /* COMP clock enable must be implemented by user */
103 /* in "HAL_COMP_MspInit()". */
104 /* Therefore, for compatibility anticipation, it is recommended */
105 /* to implement __HAL_RCC_SYSCFG_CLK_ENABLE() */
106 /* in "HAL_COMP_MspInit()". */
107 //__HAL_RCC_SYSCFG_CLK_ENABLE();
108 hwp_tsen->BGR |= TSEN_BGR_EN; //enable anau bandgap
109
110 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
111 /* Init the COMP Callback settings */
112 hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
113
114 if (hcomp->MspInitCallback == NULL)
115 {
116 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
117 }
118
119 /* Init the low level hardware */
120 hcomp->MspInitCallback(hcomp);
121 #else
122 /* Init the low level hardware */
123 HAL_COMP_MspInit(hcomp);
124 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
125 }
126
127 /* Set COMP parameters */
128 tmp_csr = (hcomp->Init.NonInvertingInput
129 | hcomp->Init.InvertingInput
130 | hcomp->Init.InternalVRef
131 | hcomp->Init.Hysteresis
132 | hcomp->Init.Mode
133 | hcomp->Init.TriggerMode
134 );
135
136 /* Set parameters in COMP register */
137 /* Note: Update all bits except read-only, lock and enable bits */
138
139 MODIFY_REG(hcomp->Instance->CR1,
140 LPCOMP_CR1_POL | LPCOMP_CR1_MODE | LPCOMP_CR1_VREFINT |
141 LPCOMP_CR1_INMSEL | LPCOMP_CR1_INPSEL | LPCOMP_CR1_HYST |
142 LPCOMP_CR1_SENS | LPCOMP_CR1_IE,
143 tmp_csr
144 );
145
146 /* Set HAL COMP handle state */
147 /* Note: Transition from state reset to state ready, */
148 /* otherwise (coming from state ready or busy) no state update. */
149 if (hcomp->State == HAL_COMP_STATE_RESET)
150 {
151 hcomp->State = HAL_COMP_STATE_READY;
152 }
153 }
154
155 return status;
156 }
157
158 /**
159 * @brief DeInitialize the COMP peripheral.
160 * @note Deinitialization cannot be performed if the COMP configuration is locked.
161 * To unlock the configuration, perform a system reset.
162 * @param hcomp COMP handle
163 * @retval HAL status
164 */
HAL_COMP_DeInit(COMP_HandleTypeDef * hcomp)165 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
166 {
167 HAL_StatusTypeDef status = HAL_OK;
168
169 /* Check the COMP handle allocation and lock status */
170 if (hcomp == NULL)
171 {
172 status = HAL_ERROR;
173 }
174 else
175 {
176 /* Check the parameter */
177
178 /* Set COMP_CSR register to reset value */
179 //WRITE_REG(hcomp->Instance->CR1, 0x00000000UL);
180
181 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
182 if (hcomp->MspDeInitCallback == NULL)
183 {
184 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
185 }
186
187 /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
188 hcomp->MspDeInitCallback(hcomp);
189 #else
190 /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
191 HAL_COMP_MspDeInit(hcomp);
192 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
193
194 hwp_tsen->BGR &= ~TSEN_BGR_EN;
195
196 /* Set HAL COMP handle state */
197 hcomp->State = HAL_COMP_STATE_RESET;
198
199 /* Release Lock */
200 __HAL_UNLOCK(hcomp);
201 }
202
203 return status;
204 }
205
206 /**
207 * @brief Initialize the COMP MSP.
208 * @param hcomp COMP handle
209 * @retval None
210 */
HAL_COMP_MspInit(COMP_HandleTypeDef * hcomp)211 __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
212 {
213 /* Prevent unused argument(s) compilation warning */
214 UNUSED(hcomp);
215
216 /* NOTE : This function should not be modified, when the callback is needed,
217 the HAL_COMP_MspInit could be implemented in the user file
218 */
219 // set lpcomp out pin
220 //HAL_PIN_Set(PAD_PB24, LPCOMP1_OUT, PIN_NOPULL, 0);
221 //HAL_PIN_Set(PAD_PB25, LPCOMP2_OUT, PIN_NOPULL, 0);
222
223 // set lpcomp input pin, they are analog pin, no function in list, set to GPIO and set mode to input
224 HAL_PIN_Set(PAD_PB01, GPIO_B1, PIN_NOPULL, 0); //#LPCOMP1_P
225 HAL_PIN_Set(PAD_PB03, GPIO_B3, PIN_NOPULL, 0); //#LPCOMP1_N
226 HAL_PIN_Select(PAD_PB01, 10, 0);
227 HAL_PIN_Select(PAD_PB03, 10, 0);
228 //HAL_PIN_Set(PAD_PB04, GPIO_B4, PIN_NOPULL, 0); //#LPCOMP2_P
229 //HAL_PIN_Set(PAD_PB05, GPIO_B5, PIN_NOPULL, 0); //#LPCOMP2_N
230 hwp_tsen->BGR |= TSEN_BGR_EN;
231 }
232
233 /**
234 * @brief DeInitialize the COMP MSP.
235 * @param hcomp COMP handle
236 * @retval None
237 */
HAL_COMP_MspDeInit(COMP_HandleTypeDef * hcomp)238 __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
239 {
240 /* Prevent unused argument(s) compilation warning */
241 UNUSED(hcomp);
242
243 /* NOTE : This function should not be modified, when the callback is needed,
244 the HAL_COMP_MspDeInit could be implemented in the user file
245 */
246
247 //HAL_PIN_Set(PAD_PB01, GPIO_B1, PIN_NOPULL, 0); //#LPCOMP1_P
248 //HAL_PIN_Set(PAD_PB03, GPIO_B3, PIN_NOPULL, 0); //#LPCOMP1_N
249 //HAL_PIN_Select(PAD_PB01, 10, 0);
250 //HAL_PIN_Select(PAD_PB03, 10, 0);
251 // recover to default
252 HAL_PIN_Set(PAD_PB01, GPIO_B1, PIN_PULLUP, 0); // I2C4 INT
253 HAL_PIN_Set(PAD_PB03, GPIO_B3, PIN_NOPULL, 0);
254 //HAL_PIN_Set(PAD_PB04, GPIO_B4, PIN_NOPULL, 0); //#LPCOMP2_P
255 //HAL_PIN_Set(PAD_PB05, GPIO_B5, PIN_NOPULL, 0); //#LPCOMP2_N
256 hwp_tsen->BGR &= ~TSEN_BGR_EN;
257 }
258
259 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
260 /**
261 * @brief Register a User COMP Callback
262 * To be used instead of the weak predefined callback
263 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
264 * the configuration information for the specified COMP.
265 * @param CallbackID ID of the callback to be registered
266 * This parameter can be one of the following values:
267 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
268 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
269 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
270 * @param pCallback pointer to the Callback function
271 * @retval HAL status
272 */
HAL_COMP_RegisterCallback(COMP_HandleTypeDef * hcomp,HAL_COMP_CallbackIDTypeDef CallbackID,pCOMP_CallbackTypeDef pCallback)273 HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
274 {
275 HAL_StatusTypeDef status = HAL_OK;
276
277 if (pCallback == NULL)
278 {
279 /* Update the error code */
280 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
281
282 return HAL_ERROR;
283 }
284
285 if (HAL_COMP_STATE_READY == hcomp->State)
286 {
287 switch (CallbackID)
288 {
289 case HAL_COMP_TRIGGER_CB_ID :
290 hcomp->TriggerCallback = pCallback;
291 break;
292
293 case HAL_COMP_MSPINIT_CB_ID :
294 hcomp->MspInitCallback = pCallback;
295 break;
296
297 case HAL_COMP_MSPDEINIT_CB_ID :
298 hcomp->MspDeInitCallback = pCallback;
299 break;
300
301 default :
302 /* Update the error code */
303 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
304
305 /* Return error status */
306 status = HAL_ERROR;
307 break;
308 }
309 }
310 else if (HAL_COMP_STATE_RESET == hcomp->State)
311 {
312 switch (CallbackID)
313 {
314 case HAL_COMP_MSPINIT_CB_ID :
315 hcomp->MspInitCallback = pCallback;
316 break;
317
318 case HAL_COMP_MSPDEINIT_CB_ID :
319 hcomp->MspDeInitCallback = pCallback;
320 break;
321
322 default :
323 /* Update the error code */
324 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
325
326 /* Return error status */
327 status = HAL_ERROR;
328 break;
329 }
330 }
331 else
332 {
333 /* Update the error code */
334 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
335
336 /* Return error status */
337 status = HAL_ERROR;
338 }
339
340 return status;
341 }
342
343 /**
344 * @brief Unregister a COMP Callback
345 * COMP callback is redirected to the weak predefined callback
346 * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
347 * the configuration information for the specified COMP.
348 * @param CallbackID ID of the callback to be unregistered
349 * This parameter can be one of the following values:
350 * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
351 * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
352 * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
353 * @retval HAL status
354 */
HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef * hcomp,HAL_COMP_CallbackIDTypeDef CallbackID)355 HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
356 {
357 HAL_StatusTypeDef status = HAL_OK;
358
359 if (HAL_COMP_STATE_READY == hcomp->State)
360 {
361 switch (CallbackID)
362 {
363 case HAL_COMP_TRIGGER_CB_ID :
364 hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
365 break;
366
367 case HAL_COMP_MSPINIT_CB_ID :
368 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
369 break;
370
371 case HAL_COMP_MSPDEINIT_CB_ID :
372 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
373 break;
374
375 default :
376 /* Update the error code */
377 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
378
379 /* Return error status */
380 status = HAL_ERROR;
381 break;
382 }
383 }
384 else if (HAL_COMP_STATE_RESET == hcomp->State)
385 {
386 switch (CallbackID)
387 {
388 case HAL_COMP_MSPINIT_CB_ID :
389 hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
390 break;
391
392 case HAL_COMP_MSPDEINIT_CB_ID :
393 hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
394 break;
395
396 default :
397 /* Update the error code */
398 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
399
400 /* Return error status */
401 status = HAL_ERROR;
402 break;
403 }
404 }
405 else
406 {
407 /* Update the error code */
408 hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
409
410 /* Return error status */
411 status = HAL_ERROR;
412 }
413
414 return status;
415 }
416
417 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
418
419 /**
420 * @}
421 */
422
423 /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions
424 * @brief Start-Stop operation functions.
425 *
426 @verbatim
427 ===============================================================================
428 ##### IO operation functions #####
429 ===============================================================================
430 [..] This section provides functions allowing to:
431 (+) Start a comparator instance.
432 (+) Stop a comparator instance.
433
434 @endverbatim
435 * @{
436 */
437
438 /**
439 * @brief Start the comparator.
440 * @param hcomp COMP handle
441 * @retval HAL status
442 */
HAL_COMP_Start(COMP_HandleTypeDef * hcomp)443 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
444 {
445 __IO uint32_t wait_loop_index = 0UL;
446 HAL_StatusTypeDef status = HAL_OK;
447
448 /* Check the COMP handle allocation and lock status */
449 if (hcomp == NULL)
450 {
451 status = HAL_ERROR;
452 }
453 else
454 {
455 if (hcomp->State == HAL_COMP_STATE_READY)
456 {
457 /* Enable the selected comparator */
458 if (hcomp->Init.WorkingPin == 0)
459 SET_BIT(hcomp->Instance->CR1, LPCOMP_CR1_EN);
460 else if (hcomp->Init.WorkingPin == 1)
461 SET_BIT(hcomp->Instance->CR2, LPCOMP_CR2_EN);
462 else
463 SET_BIT(hcomp->Instance->CR1, LPCOMP_CR1_EN);
464
465 /* Set HAL COMP handle state */
466 hcomp->State = HAL_COMP_STATE_BUSY;
467
468 /* Delay for COMP startup time */
469 /* Wait loop initialization and execution */
470 /* Note: Variable divided by 2 to compensate partially */
471 /* CPU processing cycles, scaling in us split to not */
472 /* exceed 32 bits register capacity and handle low frequency. */
473 wait_loop_index = COMP_DELAY_STARTUP_US / 10UL;
474 while (wait_loop_index != 0UL)
475 {
476 wait_loop_index--;
477 HAL_Delay_us(10);
478 }
479 }
480 else
481 {
482 status = HAL_ERROR;
483 }
484 }
485
486 return status;
487 }
488
489 /**
490 * @brief Stop the comparator.
491 * @param hcomp COMP handle
492 * @retval HAL status
493 */
HAL_COMP_Stop(COMP_HandleTypeDef * hcomp)494 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
495 {
496 HAL_StatusTypeDef status = HAL_OK;
497
498 /* Check the COMP handle allocation and lock status */
499 if (hcomp == NULL)
500 {
501 status = HAL_ERROR;
502 }
503 else
504 {
505
506 /* Check compliant states: HAL_COMP_STATE_READY or HAL_COMP_STATE_BUSY */
507 /* (all states except HAL_COMP_STATE_RESET and except locked status. */
508 if (hcomp->State != HAL_COMP_STATE_RESET)
509 {
510 /* Disable the selected comparator */
511 if (hcomp->Init.WorkingPin == 0)
512 CLEAR_BIT(hcomp->Instance->CR1, LPCOMP_CR1_EN);
513 else if (hcomp->Init.WorkingPin == 1)
514 CLEAR_BIT(hcomp->Instance->CR2, LPCOMP_CR2_EN);
515 else
516 CLEAR_BIT(hcomp->Instance->CR1, LPCOMP_CR1_EN);
517
518 /* Set HAL COMP handle state */
519 hcomp->State = HAL_COMP_STATE_READY;
520 }
521 else
522 {
523 status = HAL_ERROR;
524 }
525 }
526
527 return status;
528 }
529
530 /**
531 * @brief select which comparator to work.
532 * @param hcomp COMP handle
533 * @param cfg configure parameters
534 * @retval HAL status
535 */
HAL_COMP_Config(COMP_HandleTypeDef * hcomp,COMP_ConfigTypeDef * cfg)536 HAL_StatusTypeDef HAL_COMP_Config(COMP_HandleTypeDef *hcomp, COMP_ConfigTypeDef *cfg)
537 {
538 HAL_StatusTypeDef status = HAL_OK;
539 uint32_t tmp_csr;
540
541 /* Check the COMP handle allocation and lock status */
542 if (hcomp == NULL || cfg == NULL)
543 {
544 return HAL_ERROR;
545 }
546 // check use which comp
547 if (cfg->WorkingPin == 0) // comp1
548 {
549 /* Set COMP1 parameters */
550 tmp_csr = (cfg->InvertingInput
551 | cfg->InternalVRef
552 | cfg->Mode
553 | cfg->TriggerMode
554 );
555
556 /* Set parameters in COMP register */
557 /* Note: Update all bits except read-only, lock and enable bits */
558
559 MODIFY_REG(hcomp->Instance->CR1,
560 LPCOMP_CR1_MODE | LPCOMP_CR1_VREFINT | LPCOMP_CR1_INMSEL | LPCOMP_CR1_SENS,
561 tmp_csr
562 );
563 }
564 else if (cfg->WorkingPin == 1) // comp2
565 {
566 /* Set COMP2 parameters */
567 tmp_csr = (cfg->InvertingInput
568 | cfg->InternalVRef
569 | cfg->Mode
570 | cfg->TriggerMode
571 );
572
573 /* Set parameters in COMP register */
574 /* Note: Update all bits except read-only, lock and enable bits */
575
576 MODIFY_REG(hcomp->Instance->CR2,
577 LPCOMP_CR2_MODE | LPCOMP_CR2_VREFINT | LPCOMP_CR2_INMSEL | LPCOMP_CR2_SENS,
578 tmp_csr
579 );
580 }
581 else if (cfg->WorkingPin == 2) // comb
582 {
583 /* Set COMP1 parameters */
584 tmp_csr = (cfg->InvertingInput
585 | cfg->InternalVRef
586 | cfg->Mode
587 | cfg->TriggerMode
588 );
589
590 /* Set parameters in COMP register */
591 /* Note: Update all bits except read-only, lock and enable bits */
592
593 MODIFY_REG(hcomp->Instance->CR1,
594 LPCOMP_CR1_MODE | LPCOMP_CR1_VREFINT | LPCOMP_CR1_INMSEL | LPCOMP_CR1_SENS,
595 tmp_csr
596 );
597 MODIFY_REG(hcomp->Instance->CR2,
598 LPCOMP_CR2_MODE | LPCOMP_CR2_VREFINT | LPCOMP_CR2_INMSEL | LPCOMP_CR2_SENS,
599 tmp_csr
600 );
601 CLEAR_BIT(hcomp->Instance->CR1, LPCOMP_CR1_INPSEL); // SET input to COMP1_P
602 SET_BIT(hcomp->Instance->CR2, LPCOMP_CR2_INPSEL); // SET input to COMP2_P
603 }
604 else
605 return HAL_ERROR;
606 return status;
607 }
608
609 /**
610 * @brief polling compare result.
611 * @param hcomp COMP handle
612 * @param channel channel select, 0, 1 valid
613 * @param timeout timeout in ms
614 * @retval compare result
615 */
HAL_COMP_PollForComp(COMP_HandleTypeDef * hcomp,uint8_t channel,uint32_t timeout)616 int HAL_COMP_PollForComp(COMP_HandleTypeDef *hcomp, uint8_t channel, uint32_t timeout)
617 {
618 uint32_t value;
619 volatile uint32_t *creg;
620 __IO uint32_t wait_loop_index = 0UL;
621
622 /* Check the COMP handle allocation and lock status */
623 if (hcomp == NULL || channel > 2)
624 {
625 return -1;
626 }
627
628 //status = HAL_COMP_Start(hcomp);
629 //if (status != HAL_OK)
630 // return -1;
631
632 /* Get tick count */
633 if (channel == 1)
634 creg = &(hcomp->Instance->CR2);
635 else
636 creg = &(hcomp->Instance->CR1);
637
638 SET_BIT(*creg, LPCOMP_CR1_EN);
639 #if 0
640 uint32_t tickstart = HAL_GetTick();
641 while (HAL_IS_BIT_CLR(*creg, LPCOMP_CR1_STAT))
642 {
643 /* Check if timeout is disabled (set to infinite wait) */
644 if (timeout != HAL_MAX_DELAY)
645 {
646 if ((timeout == 0) || ((HAL_GetTick() - tickstart) > timeout))
647 {
648 /* Update COMP state machine to timeout */
649 //SET_BIT(hcomp->State, HAL_ADC_STATE_TIMEOUT);
650
651 /* Process unlocked */
652 __HAL_UNLOCK(hcomp);
653
654 return -2;
655 }
656 }
657 HAL_Delay_us(10);
658 }
659 // Clear state
660 *creg |= LPCOMP_CR1_STAT;
661 #else
662 wait_loop_index = COMP_DELAY_STARTUP_US / 10UL;
663 while (wait_loop_index != 0UL)
664 {
665 wait_loop_index--;
666 HAL_Delay_us(10);
667 }
668 #endif
669 value = (*creg & LPCOMP_CR1_VAL) >> LPCOMP_CR1_VAL_Pos;
670
671 CLEAR_BIT(*creg, LPCOMP_CR1_EN);
672
673 //hcomp->State = HAL_COMP_STATE_READY;
674
675 return value;
676 }
677
678
679 /**
680 * @brief Comparator IRQ handler.
681 * @param hcomp COMP handle
682 * @retval None
683 */
HAL_COMP_IRQHandler(COMP_HandleTypeDef * hcomp)684 void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
685 {
686 /* COMP trigger user callback */
687 #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
688 hcomp->TriggerCallback(hcomp);
689 #else
690 HAL_COMP_TriggerCallback(hcomp);
691 #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
692
693 // clear enable to avoid int always come for level int
694 if (hcomp->Init.WorkingPin == 0)
695 {
696 //CLEAR_BIT(hcomp->Instance->CR1, LPCOMP_CR1_EN);
697 hcomp->Instance->CR1 |= LPCOMP_CR1_STAT;
698 }
699 else
700 {
701 //CLEAR_BIT(hcomp->Instance->CR2, LPCOMP_CR2_EN);
702 hcomp->Instance->CR2 |= LPCOMP_CR2_STAT;
703 }
704
705 }
706
707 /**
708 * @}
709 */
710
711 /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
712 * @brief Management functions.
713 *
714 @verbatim
715 ===============================================================================
716 ##### Peripheral Control functions #####
717 ===============================================================================
718 [..]
719 This subsection provides a set of functions allowing to control the comparators.
720
721 @endverbatim
722 * @{
723 */
724
725
726 /**
727 * @brief Return the output level (high or low) of the selected comparator.
728 * The output level depends on the selected polarity.
729 * If the polarity is not inverted:
730 * - Comparator output is low when the input plus is at a lower
731 * voltage than the input minus
732 * - Comparator output is high when the input plus is at a higher
733 * voltage than the input minus
734 * If the polarity is inverted:
735 * - Comparator output is high when the input plus is at a lower
736 * voltage than the input minus
737 * - Comparator output is low when the input plus is at a higher
738 * voltage than the input minus
739 * @param hcomp COMP handle
740 * @retval Returns the selected comparator output level:
741 * @arg COMP_OUTPUT_LEVEL_LOW
742 * @arg COMP_OUTPUT_LEVEL_HIGH
743 *
744 */
HAL_COMP_GetOutputLevel(COMP_HandleTypeDef * hcomp)745 uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
746 {
747 /* Check the parameter */
748 if (hcomp->Init.WorkingPin == 1)
749 return (uint32_t)(READ_BIT(hcomp->Instance->CR2, LPCOMP_CR2_VAL)
750 >> LPCOMP_CR2_VAL_Pos);
751 else
752 return (uint32_t)(READ_BIT(hcomp->Instance->CR1, LPCOMP_CR1_VAL)
753 >> LPCOMP_CR1_VAL_Pos);
754 }
755
756 /**
757 * @brief Comparator trigger callback.
758 * @param hcomp COMP handle
759 * @retval None
760 */
HAL_COMP_TriggerCallback(COMP_HandleTypeDef * hcomp)761 __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
762 {
763 /* Prevent unused argument(s) compilation warning */
764 UNUSED(hcomp);
765
766 /* NOTE : This function should not be modified, when the callback is needed,
767 the HAL_COMP_TriggerCallback should be implemented in the user file
768 */
769 }
770
771
772 /**
773 * @}
774 */
775
776 /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
777 * @brief Peripheral State functions.
778 *
779 @verbatim
780 ===============================================================================
781 ##### Peripheral State functions #####
782 ===============================================================================
783 [..]
784 This subsection permit to get in run-time the status of the peripheral.
785
786 @endverbatim
787 * @{
788 */
789
790 /**
791 * @brief Return the COMP handle state.
792 * @param hcomp COMP handle
793 * @retval HAL state
794 */
HAL_COMP_GetState(COMP_HandleTypeDef * hcomp)795 HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
796 {
797 /* Check the COMP handle allocation */
798 if (hcomp == NULL)
799 {
800 return HAL_COMP_STATE_RESET;
801 }
802
803 /* Return HAL COMP handle state */
804 return hcomp->State;
805 }
806
807 /**
808 * @brief Return the COMP error code.
809 * @param hcomp COMP handle
810 * @retval COMP error code
811 */
HAL_COMP_GetError(COMP_HandleTypeDef * hcomp)812 uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
813 {
814 /* Check the parameters */
815
816 return hcomp->ErrorCode;
817 }
818
819 /**
820 * @}
821 */
822
823 /**
824 * @}
825 */
826
827 /**
828 * @}
829 */
830
831
832 #endif /* HAL_COMP_MODULE_ENABLED */
833
834 /**
835 * @}
836 */
837