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.
 
 
 

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