You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1011 lines
32 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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. * + Start/Stop operation functions in polling mode
  10. * + Start/Stop operation functions in interrupt mode (through EXTI interrupt)
  11. * + Peripheral control functions
  12. * + Peripheral state functions
  13. *
  14. @verbatim
  15. ================================================================================
  16. ##### COMP Peripheral features #####
  17. ================================================================================
  18. [..]
  19. The STM32WBxx device family integrates two analog comparators instances:
  20. COMP1, COMP2 except for the STM32WB15xx, STM32WB10xx products featuring only
  21. one instance: COMP1.
  22. In the rest of the file, all comments related to a pair of comparators are not
  23. applicable to STM32WB15xx, STM32WB10xx.
  24. (#) Comparators input minus (inverting input) and input plus (non inverting input)
  25. can be set to internal references or to GPIO pins
  26. (refer to GPIO list in reference manual).
  27. (#) Comparators output level is available using HAL_COMP_GetOutputLevel()
  28. and can be redirected to other peripherals: GPIO pins (in mode
  29. alternate functions for comparator), timers.
  30. (refer to GPIO list in reference manual).
  31. (#) The comparators have interrupt capability through the EXTI controller
  32. with wake-up from sleep and stop modes.
  33. (#) Pairs of comparators instances can be combined in window mode
  34. (2 consecutive instances odd and even COMP<x> and COMP<x+1>).
  35. From the corresponding IRQ handler, the right interrupt source can be retrieved
  36. using macro __HAL_COMP_COMPx_EXTI_GET_FLAG().
  37. ##### How to use this driver #####
  38. ================================================================================
  39. [..]
  40. This driver provides functions to configure and program the comparator instances
  41. of STM32WBxx devices.
  42. To use the comparator, perform the following steps:
  43. (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit():
  44. (++) Configure the GPIO connected to comparator inputs plus and minus in analog mode
  45. using HAL_GPIO_Init().
  46. (++) If needed, configure the GPIO connected to comparator output in alternate function mode
  47. using HAL_GPIO_Init().
  48. (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
  49. selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
  50. interrupt vector using HAL_NVIC_EnableIRQ() function.
  51. (#) Configure the comparator using HAL_COMP_Init() function:
  52. (++) Select the input minus (inverting input)
  53. (++) Select the input plus (non-inverting input)
  54. (++) Select the hysteresis
  55. (++) Select the blanking source
  56. (++) Select the output polarity
  57. (++) Select the power mode
  58. (++) Select the window mode
  59. -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE()
  60. to enable internal control clock of the comparators.
  61. However, this is a legacy strategy. In future STM32 families,
  62. COMP clock enable must be implemented by user in "HAL_COMP_MspInit()".
  63. Therefore, for compatibility anticipation, it is recommended to
  64. implement __HAL_RCC_SYSCFG_CLK_ENABLE() in "HAL_COMP_MspInit()".
  65. (#) Reconfiguration on-the-fly of comparator can be done by calling again
  66. function HAL_COMP_Init() with new input structure parameters values.
  67. (#) Enable the comparator using HAL_COMP_Start() function.
  68. (#) Use HAL_COMP_TriggerCallback() or HAL_COMP_GetOutputLevel() functions
  69. to manage comparator outputs (events and output level).
  70. (#) Disable the comparator using HAL_COMP_Stop() function.
  71. (#) De-initialize the comparator using HAL_COMP_DeInit() function.
  72. (#) For safety purpose, comparator configuration can be locked using HAL_COMP_Lock() function.
  73. The only way to unlock the comparator is a device hardware reset.
  74. *** Callback registration ***
  75. =============================================
  76. [..]
  77. The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
  78. allows the user to configure dynamically the driver callbacks.
  79. Use Functions HAL_COMP_RegisterCallback()
  80. to register an interrupt callback.
  81. [..]
  82. Function HAL_COMP_RegisterCallback() allows to register following callbacks:
  83. (+) TriggerCallback : callback for COMP trigger.
  84. (+) MspInitCallback : callback for Msp Init.
  85. (+) MspDeInitCallback : callback for Msp DeInit.
  86. This function takes as parameters the HAL peripheral handle, the Callback ID
  87. and a pointer to the user callback function.
  88. [..]
  89. Use function HAL_COMP_UnRegisterCallback to reset a callback to the default
  90. weak function.
  91. [..]
  92. HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
  93. and the Callback ID.
  94. This function allows to reset following callbacks:
  95. (+) TriggerCallback : callback for COMP trigger.
  96. (+) MspInitCallback : callback for Msp Init.
  97. (+) MspDeInitCallback : callback for Msp DeInit.
  98. [..]
  99. By default, after the HAL_COMP_Init() and when the state is HAL_COMP_STATE_RESET
  100. all callbacks are set to the corresponding weak functions:
  101. example HAL_COMP_TriggerCallback().
  102. Exception done for MspInit and MspDeInit functions that are
  103. reset to the legacy weak functions in the HAL_COMP_Init()/ HAL_COMP_DeInit() only when
  104. these callbacks are null (not registered beforehand).
  105. [..]
  106. If MspInit or MspDeInit are not null, the HAL_COMP_Init()/ HAL_COMP_DeInit()
  107. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  108. [..]
  109. Callbacks can be registered/unregistered in HAL_COMP_STATE_READY state only.
  110. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  111. in HAL_COMP_STATE_READY or HAL_COMP_STATE_RESET state,
  112. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  113. [..]
  114. Then, the user first registers the MspInit/MspDeInit user callbacks
  115. using HAL_COMP_RegisterCallback() before calling HAL_COMP_DeInit()
  116. or HAL_COMP_Init() function.
  117. [..]
  118. When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
  119. not defined, the callback registration feature is not available and all callbacks
  120. are set to the corresponding weak functions.
  121. @endverbatim
  122. ******************************************************************************
  123. ******************************************************************************
  124. * @attention
  125. *
  126. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  127. * All rights reserved.</center></h2>
  128. *
  129. * This software component is licensed by ST under BSD 3-Clause license,
  130. * the "License"; You may not use this file except in compliance with the
  131. * License. You may obtain a copy of the License at:
  132. * opensource.org/licenses/BSD-3-Clause
  133. *
  134. ******************************************************************************
  135. */
  136. /* Includes ------------------------------------------------------------------*/
  137. #include "stm32wbxx_hal.h"
  138. /** @addtogroup STM32WBxx_HAL_Driver
  139. * @{
  140. */
  141. #ifdef HAL_COMP_MODULE_ENABLED
  142. #if defined (COMP1) || defined (COMP2)
  143. /** @defgroup COMP COMP
  144. * @brief COMP HAL module driver
  145. * @{
  146. */
  147. /* Private typedef -----------------------------------------------------------*/
  148. /* Private define ------------------------------------------------------------*/
  149. /** @addtogroup COMP_Private_Constants
  150. * @{
  151. */
  152. /* Delay for COMP startup time. */
  153. /* Note: Delay required to reach propagation delay specification. */
  154. /* Literal set to maximum value (refer to device datasheet, */
  155. /* parameter "tSTART"). */
  156. /* Unit: us */
  157. #define COMP_DELAY_STARTUP_US (80UL) /*!< Delay for COMP startup time */
  158. /* Delay for COMP voltage scaler stabilization time. */
  159. /* Literal set to maximum value (refer to device datasheet, */
  160. /* parameter "tSTART_SCALER"). */
  161. /* Unit: us */
  162. #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200UL) /*!< Delay for COMP voltage scaler stabilization time */
  163. #define COMP_OUTPUT_LEVEL_BITOFFSET_POS (30UL)
  164. /**
  165. * @}
  166. */
  167. /* Private macro -------------------------------------------------------------*/
  168. /* Private variables ---------------------------------------------------------*/
  169. /* Private function prototypes -----------------------------------------------*/
  170. /* Exported functions --------------------------------------------------------*/
  171. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  172. * @{
  173. */
  174. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  175. * @brief Initialization and de-initialization functions.
  176. *
  177. @verbatim
  178. ===============================================================================
  179. ##### Initialization and de-initialization functions #####
  180. ===============================================================================
  181. [..] This section provides functions to initialize and de-initialize comparators
  182. @endverbatim
  183. * @{
  184. */
  185. /**
  186. * @brief Initialize the COMP according to the specified
  187. * parameters in the COMP_InitTypeDef and initialize the associated handle.
  188. * @note If the selected comparator is locked, initialization can't be performed.
  189. * To unlock the configuration, perform a system reset.
  190. * @param hcomp COMP handle
  191. * @retval HAL status
  192. */
  193. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  194. {
  195. uint32_t tmp_csr;
  196. uint32_t exti_line;
  197. uint32_t comp_voltage_scaler_initialized; /* Value "0" if comparator voltage scaler is not initialized */
  198. __IO uint32_t wait_loop_index = 0UL;
  199. HAL_StatusTypeDef status = HAL_OK;
  200. /* Check the COMP handle allocation and lock status */
  201. if(hcomp == NULL)
  202. {
  203. status = HAL_ERROR;
  204. }
  205. else if(__HAL_COMP_IS_LOCKED(hcomp))
  206. {
  207. status = HAL_ERROR;
  208. }
  209. else
  210. {
  211. /* Check the parameters */
  212. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  213. assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.InputPlus));
  214. assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InputMinus));
  215. assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  216. assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode));
  217. assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  218. assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
  219. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  220. #if defined(COMP2)
  221. assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
  222. #endif
  223. if(hcomp->State == HAL_COMP_STATE_RESET)
  224. {
  225. /* Allocate lock resource and initialize it */
  226. hcomp->Lock = HAL_UNLOCKED;
  227. /* Set COMP error code to none */
  228. COMP_CLEAR_ERRORCODE(hcomp);
  229. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  230. /* Init the COMP Callback settings */
  231. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  232. if (hcomp->MspInitCallback == NULL)
  233. {
  234. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  235. }
  236. /* Init the low level hardware */
  237. /* Note: Internal control clock of the comparators must */
  238. /* be enabled in "HAL_COMP_MspInit()" */
  239. /* using "__HAL_RCC_SYSCFG_CLK_ENABLE()". */
  240. hcomp->MspInitCallback(hcomp);
  241. #else
  242. /* Init the low level hardware */
  243. /* Note: Internal control clock of the comparators must */
  244. /* be enabled in "HAL_COMP_MspInit()" */
  245. /* using "__HAL_RCC_SYSCFG_CLK_ENABLE()". */
  246. HAL_COMP_MspInit(hcomp);
  247. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  248. }
  249. /* Memorize voltage scaler state before initialization */
  250. comp_voltage_scaler_initialized = READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN);
  251. /* Set COMP parameters */
  252. tmp_csr = ( hcomp->Init.InputMinus
  253. | hcomp->Init.InputPlus
  254. | hcomp->Init.BlankingSrce
  255. | hcomp->Init.Hysteresis
  256. | hcomp->Init.OutputPol
  257. | hcomp->Init.Mode
  258. );
  259. /* Set parameters in COMP register */
  260. /* Note: Update all bits except read-only, lock and enable bits */
  261. #if defined (COMP_CSR_WINMODE)
  262. MODIFY_REG(hcomp->Instance->CSR,
  263. COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
  264. COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST |
  265. COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL,
  266. tmp_csr
  267. );
  268. #else
  269. MODIFY_REG(hcomp->Instance->CSR,
  270. COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
  271. COMP_CSR_POLARITY | COMP_CSR_HYST |
  272. COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL,
  273. tmp_csr
  274. );
  275. #endif
  276. #if defined(COMP2)
  277. /* Set window mode */
  278. /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */
  279. /* instances. Therefore, this function can update another COMP */
  280. /* instance that the one currently selected. */
  281. if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON)
  282. {
  283. SET_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
  284. }
  285. else
  286. {
  287. CLEAR_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
  288. }
  289. #endif /* COMP2 */
  290. /* Delay for COMP scaler bridge voltage stabilization */
  291. /* Apply the delay if voltage scaler bridge is required and not already enabled */
  292. if ((READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) != 0UL) &&
  293. (comp_voltage_scaler_initialized == 0UL) )
  294. {
  295. /* Wait loop initialization and execution */
  296. /* Note: Variable divided by 2 to compensate partially */
  297. /* CPU processing cycles, scaling in us split to not */
  298. /* exceed 32 bits register capacity and handle low frequency. */
  299. wait_loop_index = ((COMP_DELAY_VOLTAGE_SCALER_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
  300. while(wait_loop_index != 0UL)
  301. {
  302. wait_loop_index--;
  303. }
  304. }
  305. /* Get the EXTI line corresponding to the selected COMP instance */
  306. exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  307. /* Manage EXTI settings */
  308. if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != 0UL)
  309. {
  310. /* Configure EXTI rising edge */
  311. if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != 0UL)
  312. {
  313. LL_EXTI_EnableRisingTrig_0_31(exti_line);
  314. }
  315. else
  316. {
  317. LL_EXTI_DisableRisingTrig_0_31(exti_line);
  318. }
  319. /* Configure EXTI falling edge */
  320. if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != 0UL)
  321. {
  322. LL_EXTI_EnableFallingTrig_0_31(exti_line);
  323. }
  324. else
  325. {
  326. LL_EXTI_DisableFallingTrig_0_31(exti_line);
  327. }
  328. /* Clear COMP EXTI pending bit (if any) */
  329. LL_EXTI_ClearFlag_0_31(exti_line);
  330. /* Configure EXTI event mode */
  331. if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != 0UL)
  332. {
  333. LL_EXTI_EnableEvent_0_31(exti_line);
  334. }
  335. else
  336. {
  337. LL_EXTI_DisableEvent_0_31(exti_line);
  338. }
  339. /* Configure EXTI interrupt mode */
  340. if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != 0UL)
  341. {
  342. LL_EXTI_EnableIT_0_31(exti_line);
  343. }
  344. else
  345. {
  346. LL_EXTI_DisableIT_0_31(exti_line);
  347. }
  348. }
  349. else
  350. {
  351. /* Disable EXTI event mode */
  352. LL_EXTI_DisableEvent_0_31(exti_line);
  353. /* Disable EXTI interrupt mode */
  354. LL_EXTI_DisableIT_0_31(exti_line);
  355. }
  356. /* Set HAL COMP handle state */
  357. /* Note: Transition from state reset to state ready, */
  358. /* otherwise (coming from state ready or busy) no state update. */
  359. if (hcomp->State == HAL_COMP_STATE_RESET)
  360. {
  361. hcomp->State = HAL_COMP_STATE_READY;
  362. }
  363. }
  364. return status;
  365. }
  366. /**
  367. * @brief DeInitialize the COMP peripheral.
  368. * @note Deinitialization cannot be performed if the COMP configuration is locked.
  369. * To unlock the configuration, perform a system reset.
  370. * @param hcomp COMP handle
  371. * @retval HAL status
  372. */
  373. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  374. {
  375. HAL_StatusTypeDef status = HAL_OK;
  376. /* Check the COMP handle allocation and lock status */
  377. if(hcomp == NULL)
  378. {
  379. status = HAL_ERROR;
  380. }
  381. else if(__HAL_COMP_IS_LOCKED(hcomp))
  382. {
  383. status = HAL_ERROR;
  384. }
  385. else
  386. {
  387. /* Check the parameter */
  388. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  389. /* Set COMP_CSR register to reset value */
  390. WRITE_REG(hcomp->Instance->CSR, 0x00000000UL);
  391. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  392. if (hcomp->MspDeInitCallback == NULL)
  393. {
  394. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  395. }
  396. /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
  397. hcomp->MspDeInitCallback(hcomp);
  398. #else
  399. /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
  400. HAL_COMP_MspDeInit(hcomp);
  401. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  402. /* Set HAL COMP handle state */
  403. hcomp->State = HAL_COMP_STATE_RESET;
  404. /* Release Lock */
  405. __HAL_UNLOCK(hcomp);
  406. }
  407. return status;
  408. }
  409. /**
  410. * @brief Initialize the COMP MSP.
  411. * @param hcomp COMP handle
  412. * @retval None
  413. */
  414. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  415. {
  416. /* Prevent unused argument(s) compilation warning */
  417. UNUSED(hcomp);
  418. /* NOTE : This function should not be modified, when the callback is needed,
  419. the HAL_COMP_MspInit could be implemented in the user file
  420. */
  421. }
  422. /**
  423. * @brief DeInitialize the COMP MSP.
  424. * @param hcomp COMP handle
  425. * @retval None
  426. */
  427. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  428. {
  429. /* Prevent unused argument(s) compilation warning */
  430. UNUSED(hcomp);
  431. /* NOTE : This function should not be modified, when the callback is needed,
  432. the HAL_COMP_MspDeInit could be implemented in the user file
  433. */
  434. }
  435. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  436. /**
  437. * @brief Register a User COMP Callback
  438. * To be used instead of the weak predefined callback
  439. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  440. * the configuration information for the specified COMP.
  441. * @param CallbackID ID of the callback to be registered
  442. * This parameter can be one of the following values:
  443. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  444. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  445. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  446. * @param pCallback pointer to the Callback function
  447. * @retval HAL status
  448. */
  449. HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
  450. {
  451. HAL_StatusTypeDef status = HAL_OK;
  452. if (pCallback == NULL)
  453. {
  454. /* Update the error code */
  455. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  456. return HAL_ERROR;
  457. }
  458. if (HAL_COMP_STATE_READY == hcomp->State)
  459. {
  460. switch (CallbackID)
  461. {
  462. case HAL_COMP_TRIGGER_CB_ID :
  463. hcomp->TriggerCallback = pCallback;
  464. break;
  465. case HAL_COMP_MSPINIT_CB_ID :
  466. hcomp->MspInitCallback = pCallback;
  467. break;
  468. case HAL_COMP_MSPDEINIT_CB_ID :
  469. hcomp->MspDeInitCallback = pCallback;
  470. break;
  471. default :
  472. /* Update the error code */
  473. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  474. /* Return error status */
  475. status = HAL_ERROR;
  476. break;
  477. }
  478. }
  479. else if (HAL_COMP_STATE_RESET == hcomp->State)
  480. {
  481. switch (CallbackID)
  482. {
  483. case HAL_COMP_MSPINIT_CB_ID :
  484. hcomp->MspInitCallback = pCallback;
  485. break;
  486. case HAL_COMP_MSPDEINIT_CB_ID :
  487. hcomp->MspDeInitCallback = pCallback;
  488. break;
  489. default :
  490. /* Update the error code */
  491. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  492. /* Return error status */
  493. status = HAL_ERROR;
  494. break;
  495. }
  496. }
  497. else
  498. {
  499. /* Update the error code */
  500. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  501. /* Return error status */
  502. status = HAL_ERROR;
  503. }
  504. return status;
  505. }
  506. /**
  507. * @brief Unregister a COMP Callback
  508. * COMP callback is redirected to the weak predefined callback
  509. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  510. * the configuration information for the specified COMP.
  511. * @param CallbackID ID of the callback to be unregistered
  512. * This parameter can be one of the following values:
  513. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  514. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  515. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  516. * @retval HAL status
  517. */
  518. HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
  519. {
  520. HAL_StatusTypeDef status = HAL_OK;
  521. if (HAL_COMP_STATE_READY == hcomp->State)
  522. {
  523. switch (CallbackID)
  524. {
  525. case HAL_COMP_TRIGGER_CB_ID :
  526. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  527. break;
  528. case HAL_COMP_MSPINIT_CB_ID :
  529. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  530. break;
  531. case HAL_COMP_MSPDEINIT_CB_ID :
  532. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  533. break;
  534. default :
  535. /* Update the error code */
  536. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  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 = HAL_COMP_MspInit; /* Legacy weak MspInit */
  548. break;
  549. case HAL_COMP_MSPDEINIT_CB_ID :
  550. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  551. break;
  552. default :
  553. /* Update the error code */
  554. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  555. /* Return error status */
  556. status = HAL_ERROR;
  557. break;
  558. }
  559. }
  560. else
  561. {
  562. /* Update the error code */
  563. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  564. /* Return error status */
  565. status = HAL_ERROR;
  566. }
  567. return status;
  568. }
  569. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  570. /**
  571. * @}
  572. */
  573. /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions
  574. * @brief Start-Stop operation functions.
  575. *
  576. @verbatim
  577. ===============================================================================
  578. ##### IO operation functions #####
  579. ===============================================================================
  580. [..] This section provides functions allowing to:
  581. (+) Start a comparator instance.
  582. (+) Stop a comparator instance.
  583. @endverbatim
  584. * @{
  585. */
  586. /**
  587. * @brief Start the comparator.
  588. * @param hcomp COMP handle
  589. * @retval HAL status
  590. */
  591. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  592. {
  593. __IO uint32_t wait_loop_index = 0UL;
  594. HAL_StatusTypeDef status = HAL_OK;
  595. /* Check the COMP handle allocation and lock status */
  596. if(hcomp == NULL)
  597. {
  598. status = HAL_ERROR;
  599. }
  600. else if(__HAL_COMP_IS_LOCKED(hcomp))
  601. {
  602. status = HAL_ERROR;
  603. }
  604. else
  605. {
  606. /* Check the parameter */
  607. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  608. if(hcomp->State == HAL_COMP_STATE_READY)
  609. {
  610. /* Enable the selected comparator */
  611. SET_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
  612. /* Set HAL COMP handle state */
  613. hcomp->State = HAL_COMP_STATE_BUSY;
  614. /* Delay for COMP startup time */
  615. /* Wait loop initialization and execution */
  616. /* Note: Variable divided by 2 to compensate partially */
  617. /* CPU processing cycles, scaling in us split to not */
  618. /* exceed 32 bits register capacity and handle low frequency. */
  619. wait_loop_index = ((COMP_DELAY_STARTUP_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
  620. while(wait_loop_index != 0UL)
  621. {
  622. wait_loop_index--;
  623. }
  624. }
  625. else
  626. {
  627. status = HAL_ERROR;
  628. }
  629. }
  630. return status;
  631. }
  632. /**
  633. * @brief Stop the comparator.
  634. * @param hcomp COMP handle
  635. * @retval HAL status
  636. */
  637. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  638. {
  639. HAL_StatusTypeDef status = HAL_OK;
  640. /* Check the COMP handle allocation and lock status */
  641. if(hcomp == NULL)
  642. {
  643. status = HAL_ERROR;
  644. }
  645. else if(__HAL_COMP_IS_LOCKED(hcomp))
  646. {
  647. status = HAL_ERROR;
  648. }
  649. else
  650. {
  651. /* Check the parameter */
  652. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  653. /* Check compliant states: HAL_COMP_STATE_READY or HAL_COMP_STATE_BUSY */
  654. /* (all states except HAL_COMP_STATE_RESET and except locked status. */
  655. if(hcomp->State != HAL_COMP_STATE_RESET)
  656. {
  657. /* Disable the selected comparator */
  658. CLEAR_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
  659. /* Set HAL COMP handle state */
  660. hcomp->State = HAL_COMP_STATE_READY;
  661. }
  662. else
  663. {
  664. status = HAL_ERROR;
  665. }
  666. }
  667. return status;
  668. }
  669. /**
  670. * @brief Comparator IRQ handler.
  671. * @param hcomp COMP handle
  672. * @retval None
  673. */
  674. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  675. {
  676. /* Get the EXTI line corresponding to the selected COMP instance */
  677. uint32_t exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  678. /* Check COMP EXTI flag */
  679. if(LL_EXTI_IsActiveFlag_0_31(exti_line) != 0UL)
  680. {
  681. #if defined(COMP2)
  682. /* Check whether comparator is in independent or window mode */
  683. if(READ_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE) != 0UL)
  684. {
  685. /* Clear COMP EXTI line pending bit of the pair of comparators */
  686. /* in window mode. */
  687. /* Note: Pair of comparators in window mode can both trig IRQ when */
  688. /* input voltage is changing from "out of window" area */
  689. /* (low or high ) to the other "out of window" area (high or low).*/
  690. /* Both flags must be cleared to call comparator trigger */
  691. /* callback is called once. */
  692. LL_EXTI_ClearFlag_0_31((COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
  693. }
  694. else
  695. #endif /* COMP2 */
  696. {
  697. /* Clear COMP EXTI line pending bit */
  698. LL_EXTI_ClearFlag_0_31(exti_line);
  699. }
  700. /* COMP trigger user callback */
  701. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  702. hcomp->TriggerCallback(hcomp);
  703. #else
  704. HAL_COMP_TriggerCallback(hcomp);
  705. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  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. @endverbatim
  721. * @{
  722. */
  723. /**
  724. * @brief Lock the selected comparator configuration.
  725. * @note A system reset is required to unlock the comparator configuration.
  726. * @note Locking the comparator from reset state is possible
  727. * if __HAL_RCC_SYSCFG_CLK_ENABLE() is being called before.
  728. * @param hcomp COMP handle
  729. * @retval HAL status
  730. */
  731. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  732. {
  733. HAL_StatusTypeDef status = HAL_OK;
  734. /* Check the COMP handle allocation and lock status */
  735. if(hcomp == NULL)
  736. {
  737. status = HAL_ERROR;
  738. }
  739. else if(__HAL_COMP_IS_LOCKED(hcomp))
  740. {
  741. status = HAL_ERROR;
  742. }
  743. else
  744. {
  745. /* Check the parameter */
  746. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  747. /* Set HAL COMP handle state */
  748. switch(hcomp->State)
  749. {
  750. case HAL_COMP_STATE_RESET:
  751. hcomp->State = HAL_COMP_STATE_RESET_LOCKED;
  752. break;
  753. case HAL_COMP_STATE_READY:
  754. hcomp->State = HAL_COMP_STATE_READY_LOCKED;
  755. break;
  756. default: /* HAL_COMP_STATE_BUSY */
  757. hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
  758. break;
  759. }
  760. }
  761. if(status == HAL_OK)
  762. {
  763. /* Set the lock bit corresponding to selected comparator */
  764. __HAL_COMP_LOCK(hcomp);
  765. }
  766. return status;
  767. }
  768. /**
  769. * @brief Return the output level (high or low) of the selected comparator.
  770. * The output level depends on the selected polarity.
  771. * If the polarity is not inverted:
  772. * - Comparator output is low when the input plus is at a lower
  773. * voltage than the input minus
  774. * - Comparator output is high when the input plus is at a higher
  775. * voltage than the input minus
  776. * If the polarity is inverted:
  777. * - Comparator output is high when the input plus is at a lower
  778. * voltage than the input minus
  779. * - Comparator output is low when the input plus is at a higher
  780. * voltage than the input minus
  781. * @param hcomp COMP handle
  782. * @retval Returns the selected comparator output level:
  783. * @arg COMP_OUTPUT_LEVEL_LOW
  784. * @arg COMP_OUTPUT_LEVEL_HIGH
  785. *
  786. */
  787. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  788. {
  789. /* Check the parameter */
  790. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  791. return (uint32_t)(READ_BIT(hcomp->Instance->CSR, COMP_CSR_VALUE)
  792. >> COMP_OUTPUT_LEVEL_BITOFFSET_POS);
  793. }
  794. /**
  795. * @brief Comparator trigger callback.
  796. * @param hcomp COMP handle
  797. * @retval None
  798. */
  799. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  800. {
  801. /* Prevent unused argument(s) compilation warning */
  802. UNUSED(hcomp);
  803. /* NOTE : This function should not be modified, when the callback is needed,
  804. the HAL_COMP_TriggerCallback should be implemented in the user file
  805. */
  806. }
  807. /**
  808. * @}
  809. */
  810. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  811. * @brief Peripheral State functions.
  812. *
  813. @verbatim
  814. ===============================================================================
  815. ##### Peripheral State functions #####
  816. ===============================================================================
  817. [..]
  818. This subsection permit to get in run-time the status of the peripheral.
  819. @endverbatim
  820. * @{
  821. */
  822. /**
  823. * @brief Return the COMP handle state.
  824. * @param hcomp COMP handle
  825. * @retval HAL state
  826. */
  827. HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  828. {
  829. /* Check the COMP handle allocation */
  830. if(hcomp == NULL)
  831. {
  832. return HAL_COMP_STATE_RESET;
  833. }
  834. /* Check the parameter */
  835. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  836. /* Return HAL COMP handle state */
  837. return hcomp->State;
  838. }
  839. /**
  840. * @brief Return the COMP error code.
  841. * @param hcomp COMP handle
  842. * @retval COMP error code
  843. */
  844. uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
  845. {
  846. /* Check the parameters */
  847. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  848. return hcomp->ErrorCode;
  849. }
  850. /**
  851. * @}
  852. */
  853. /**
  854. * @}
  855. */
  856. /**
  857. * @}
  858. */
  859. #endif /* COMP1 || COMP2 */
  860. #endif /* HAL_COMP_MODULE_ENABLED */
  861. /**
  862. * @}
  863. */
  864. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/