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.
 
 
 

1251 lines
40 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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
  11. * + Peripheral control functions
  12. * + Peripheral state functions
  13. @verbatim
  14. ================================================================================
  15. ##### COMP Peripheral features #####
  16. ================================================================================
  17. [..]
  18. The STM32H7xx device family integrates two analog comparators instances
  19. COMP1 and COMP2:
  20. (#) The COMP input minus (inverting input) and input plus (non inverting input)
  21. can be set to internal references or to GPIO pins
  22. (refer to GPIO list in reference manual).
  23. (#) The COMP output level is available using HAL_COMP_GetOutputLevel()
  24. and can be redirected to other peripherals: GPIO pins (in mode
  25. alternate functions for comparator), timers.
  26. (refer to GPIO list in reference manual).
  27. (#) Pairs of comparators instances can be combined in window mode
  28. (2 consecutive instances odd and even COMP<x> and COMP<x+1>).
  29. (#) The comparators have interrupt capability through the EXTI controller
  30. with wake-up from sleep and stop modes:
  31. (++) COMP1 is internally connected to EXTI Line 20
  32. (++) COMP2 is internally connected to EXTI Line 21
  33. [..]
  34. From the corresponding IRQ handler, the right interrupt source can be retrieved
  35. using macro __HAL_COMP_COMP1_EXTI_GET_FLAG() and __HAL_COMP_COMP2_EXTI_GET_FLAG().
  36. ##### How to use this driver #####
  37. ================================================================================
  38. [..]
  39. This driver provides functions to configure and program the comparator instances of
  40. STM32H7xx devices.
  41. To use the comparator, perform the following steps:
  42. (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit():
  43. (++) Configure the GPIO connected to comparator inputs plus and minus in analog mode
  44. using HAL_GPIO_Init().
  45. (++) If needed, configure the GPIO connected to comparator output in alternate function mode
  46. using HAL_GPIO_Init().
  47. (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
  48. selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
  49. interrupt vector using HAL_NVIC_EnableIRQ() function.
  50. (#) Configure the comparator using HAL_COMP_Init() function:
  51. (++) Select the input minus (inverting input)
  52. (++) Select the input plus (non-inverting input)
  53. (++) Select the hysteresis
  54. (++) Select the blanking source
  55. (++) Select the output polarity
  56. (++) Select the power mode
  57. (++) Select the window mode
  58. -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE()
  59. to enable internal control clock of the comparators.
  60. However, this is a legacy strategy.
  61. Therefore, for compatibility anticipation, it is recommended to
  62. implement __HAL_RCC_SYSCFG_CLK_ENABLE() in "HAL_COMP_MspInit()".
  63. In STM32H7,COMP clock enable __HAL_RCC_COMP12_CLK_ENABLE() must
  64. be implemented by user 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() or HAL_COMP_Start_IT()to be enabled
  68. with the interrupt through NVIC of the CPU.
  69. Note: HAL_COMP_Start_IT() must be called after each interrupt otherwise the interrupt
  70. mode will stay disabled.
  71. (#) Use HAL_COMP_GetOutputLevel() or HAL_COMP_TriggerCallback()
  72. functions to manage comparator outputs(output level or events)
  73. (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
  74. to disable the interrupt too.
  75. (#) De-initialize the comparator using HAL_COMP_DeInit() function.
  76. (#) For safety purpose, comparator configuration can be locked using HAL_COMP_Lock() function.
  77. The only way to unlock the comparator is a device hardware reset.
  78. *** Callback registration ***
  79. =============================================
  80. [..]
  81. The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
  82. allows the user to configure dynamically the driver callbacks.
  83. Use Functions @ref HAL_COMP_RegisterCallback()
  84. to register an interrupt callback.
  85. [..]
  86. Function @ref HAL_COMP_RegisterCallback() allows to register following callbacks:
  87. (+) TriggerCallback : callback for COMP trigger.
  88. (+) MspInitCallback : callback for Msp Init.
  89. (+) MspDeInitCallback : callback for Msp DeInit.
  90. This function takes as parameters the HAL peripheral handle, the Callback ID
  91. and a pointer to the user callback function.
  92. [..]
  93. Use function @ref HAL_COMP_UnRegisterCallback to reset a callback to the default
  94. weak function.
  95. [..]
  96. @ref HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
  97. and the Callback ID.
  98. This function allows to reset following callbacks:
  99. (+) TriggerCallback : callback for COMP trigger.
  100. (+) MspInitCallback : callback for Msp Init.
  101. (+) MspDeInitCallback : callback for Msp DeInit.
  102. [..]
  103. By default, after the @ref HAL_COMP_Init() and when the state is @ref HAL_COMP_STATE_RESET
  104. all callbacks are set to the corresponding weak functions:
  105. example @ref HAL_COMP_TriggerCallback().
  106. Exception done for MspInit and MspDeInit functions that are
  107. reset to the legacy weak functions in the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit() only when
  108. these callbacks are null (not registered beforehand).
  109. [..]
  110. If MspInit or MspDeInit are not null, the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit()
  111. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  112. [..]
  113. Callbacks can be registered/unregistered in @ref HAL_COMP_STATE_READY state only.
  114. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  115. in @ref HAL_COMP_STATE_READY or @ref HAL_COMP_STATE_RESET state,
  116. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  117. [..]
  118. Then, the user first registers the MspInit/MspDeInit user callbacks
  119. using @ref HAL_COMP_RegisterCallback() before calling @ref HAL_COMP_DeInit()
  120. or @ref HAL_COMP_Init() function.
  121. [..]
  122. When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
  123. not defined, the callback registration feature is not available and all callbacks
  124. are set to the corresponding weak functions.
  125. @endverbatim
  126. ******************************************************************************
  127. Table 1. COMP inputs and output for STM32H7xx devices
  128. +---------------------------------------------------------+
  129. | | | COMP1 | COMP2 |
  130. |----------------|----------------|-----------|-----------|
  131. | | IO1 | PB0 | PE9 |
  132. | Input plus | IO2 | PB2 | PE11 |
  133. | | | | |
  134. |----------------|----------------|-----------------------|
  135. | | 1/4 VrefInt | Available | Available |
  136. | | 1/2 VrefInt | Available | Available |
  137. | | 3/4 VrefInt | Available | Available |
  138. | Input minus | VrefInt | Available | Available |
  139. | | DAC1 channel 1 | Available | Available |
  140. | | DAC1 channel 2 | Available | Available |
  141. | | IO1 | PB1 | PE10 |
  142. | | IO2 | PC4 | PE7 |
  143. | | | | |
  144. | | | | |
  145. | | | | |
  146. +---------------------------------------------------------+
  147. | Output | | PC5 (1) | PE8 (1) |
  148. | | | PE12 (1) | PE13 (1) |
  149. | | | TIM (2) | TIM (2) |
  150. +---------------------------------------------------------+
  151. (1) GPIO must be set to alternate function for comparator
  152. (2) Comparators output to timers is set in timers instances.
  153. ******************************************************************************
  154. * @attention
  155. *
  156. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  157. * All rights reserved.</center></h2>
  158. *
  159. * This software component is licensed by ST under BSD 3-Clause license,
  160. * the "License"; You may not use this file except in compliance with the
  161. * License. You may obtain a copy of the License at:
  162. * opensource.org/licenses/BSD-3-Clause
  163. *
  164. ******************************************************************************
  165. */
  166. /* Includes ------------------------------------------------------------------*/
  167. #include "stm32h7xx_hal.h"
  168. /** @addtogroup STM32H7xx_HAL_Driver
  169. * @{
  170. */
  171. /** @defgroup COMP COMP
  172. * @brief COMP HAL module driver
  173. * @{
  174. */
  175. #ifdef HAL_COMP_MODULE_ENABLED
  176. /* Private typedef -----------------------------------------------------------*/
  177. /* Private define ------------------------------------------------------------*/
  178. /** @addtogroup COMP_Private_Constants
  179. * @{
  180. */
  181. /* Delay for COMP startup time. */
  182. /* Note: Delay required to reach propagation delay specification. */
  183. /* Literal set to maximum value (refer to device datasheet, */
  184. /* parameter "tSTART"). */
  185. /* Unit: us */
  186. #define COMP_DELAY_STARTUP_US (80UL) /*!< Delay for COMP startup time */
  187. /* Delay for COMP voltage scaler stabilization time. */
  188. /* Literal set to maximum value (refer to device datasheet, */
  189. /* parameter "tSTART_SCALER"). */
  190. /* Unit: us */
  191. #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200UL) /*!< Delay for COMP voltage scaler stabilization time */
  192. /**
  193. * @}
  194. */
  195. /* Private macro -------------------------------------------------------------*/
  196. /* Private variables ---------------------------------------------------------*/
  197. /* Private function prototypes -----------------------------------------------*/
  198. /* Exported functions --------------------------------------------------------*/
  199. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  200. * @{
  201. */
  202. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  203. * @brief Initialization and de-initialization functions.
  204. *
  205. @verbatim
  206. ===============================================================================
  207. ##### Initialization and de-initialization functions #####
  208. ===============================================================================
  209. [..] This section provides functions to initialize and de-initialize comparators
  210. @endverbatim
  211. * @{
  212. */
  213. /**
  214. * @brief Initialize the COMP according to the specified
  215. * parameters in the COMP_InitTypeDef and initialize the associated handle.
  216. * @note If the selected comparator is locked, initialization can't be performed.
  217. * To unlock the configuration, perform a system reset.
  218. * @param hcomp COMP handle
  219. * @retval HAL status
  220. */
  221. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  222. {
  223. uint32_t tmp_csr ;
  224. uint32_t exti_line ;
  225. uint32_t comp_voltage_scaler_initialized; /* Value "0" is comparator voltage scaler is not initialized */
  226. __IO uint32_t wait_loop_index = 0UL;
  227. HAL_StatusTypeDef status = HAL_OK;
  228. /* Check the COMP handle allocation and lock status */
  229. if(hcomp == NULL)
  230. {
  231. status = HAL_ERROR;
  232. }
  233. else if(__HAL_COMP_IS_LOCKED(hcomp))
  234. {
  235. status = HAL_ERROR;
  236. }
  237. else
  238. {
  239. /* Check the parameters */
  240. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  241. assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput));
  242. assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput));
  243. assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  244. assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode));
  245. assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  246. assert_param(IS_COMP_BLANKINGSRCE(hcomp->Init.BlankingSrce));
  247. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  248. assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
  249. if(hcomp->State == HAL_COMP_STATE_RESET)
  250. {
  251. /* Allocate lock resource and initialize it */
  252. hcomp->Lock = HAL_UNLOCKED;
  253. /* Set COMP error code to none */
  254. COMP_CLEAR_ERRORCODE(hcomp);
  255. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  256. /* Init the COMP Callback settings */
  257. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  258. if (hcomp->MspInitCallback == NULL)
  259. {
  260. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  261. }
  262. /* Init the low level hardware */
  263. hcomp->MspInitCallback(hcomp);
  264. #else
  265. /* Init the low level hardware */
  266. HAL_COMP_MspInit(hcomp);
  267. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  268. }
  269. /* Memorize voltage scaler state before initialization */
  270. comp_voltage_scaler_initialized = READ_BIT(hcomp->Instance->CFGR, COMP_CFGRx_SCALEN);
  271. /* Set COMP parameters */
  272. /* Set INMSEL bits according to hcomp->Init.InvertingInput value */
  273. /* Set INPSEL bits according to hcomp->Init.NonInvertingInput value */
  274. /* Set BLANKING bits according to hcomp->Init.BlankingSrce value */
  275. /* Set HYST bits according to hcomp->Init.Hysteresis value */
  276. /* Set POLARITY bit according to hcomp->Init.OutputPol value */
  277. /* Set POWERMODE bits according to hcomp->Init.Mode value */
  278. tmp_csr = (hcomp->Init.InvertingInput | \
  279. hcomp->Init.NonInvertingInput | \
  280. hcomp->Init.BlankingSrce | \
  281. hcomp->Init.Hysteresis | \
  282. hcomp->Init.OutputPol | \
  283. hcomp->Init.Mode );
  284. /* Set parameters in COMP register */
  285. /* Note: Update all bits except read-only, lock and enable bits */
  286. #if defined (COMP_CFGRx_INP2SEL)
  287. MODIFY_REG(hcomp->Instance->CFGR,
  288. COMP_CFGRx_PWRMODE | COMP_CFGRx_INMSEL | COMP_CFGRx_INPSEL |
  289. COMP_CFGRx_INP2SEL | COMP_CFGRx_WINMODE | COMP_CFGRx_POLARITY | COMP_CFGRx_HYST |
  290. COMP_CFGRx_BLANKING | COMP_CFGRx_BRGEN | COMP_CFGRx_SCALEN,
  291. tmp_csr
  292. );
  293. #else
  294. MODIFY_REG(hcomp->Instance->CFGR,
  295. COMP_CFGRx_PWRMODE | COMP_CFGRx_INMSEL | COMP_CFGRx_INPSEL |
  296. COMP_CFGRx_WINMODE | COMP_CFGRx_POLARITY | COMP_CFGRx_HYST |
  297. COMP_CFGRx_BLANKING | COMP_CFGRx_BRGEN | COMP_CFGRx_SCALEN,
  298. tmp_csr
  299. );
  300. #endif
  301. /* Set window mode */
  302. /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */
  303. /* instances. Therefore, this function can update another COMP */
  304. /* instance that the one currently selected. */
  305. if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON)
  306. {
  307. SET_BIT(hcomp->Instance->CFGR, COMP_CFGRx_WINMODE);
  308. }
  309. else
  310. {
  311. CLEAR_BIT(hcomp->Instance->CFGR, COMP_CFGRx_WINMODE);
  312. }
  313. /* Delay for COMP scaler bridge voltage stabilization */
  314. /* Apply the delay if voltage scaler bridge is enabled for the first time */
  315. if ((READ_BIT(hcomp->Instance->CFGR, COMP_CFGRx_SCALEN) != 0UL) &&
  316. (comp_voltage_scaler_initialized != 0UL) )
  317. {
  318. /* Wait loop initialization and execution */
  319. /* Note: Variable divided by 2 to compensate partially */
  320. /* CPU processing cycles.*/
  321. wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000UL * 2UL)));
  322. while(wait_loop_index != 0UL)
  323. {
  324. wait_loop_index --;
  325. }
  326. }
  327. /* Get the EXTI line corresponding to the selected COMP instance */
  328. exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  329. /* Manage EXTI settings */
  330. if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != 0UL)
  331. {
  332. /* Configure EXTI rising edge */
  333. if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != 0UL)
  334. {
  335. SET_BIT(EXTI->RTSR1, exti_line);
  336. }
  337. else
  338. {
  339. CLEAR_BIT(EXTI->RTSR1, exti_line);
  340. }
  341. /* Configure EXTI falling edge */
  342. if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != 0UL)
  343. {
  344. SET_BIT(EXTI->FTSR1, exti_line);
  345. }
  346. else
  347. {
  348. CLEAR_BIT(EXTI->FTSR1, exti_line);
  349. }
  350. #if !defined (CORE_CM4)
  351. /* Clear COMP EXTI pending bit (if any) */
  352. WRITE_REG(EXTI->PR1, exti_line);
  353. /* Configure EXTI event mode */
  354. if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != 0UL)
  355. {
  356. SET_BIT(EXTI->EMR1, exti_line);
  357. }
  358. else
  359. {
  360. CLEAR_BIT(EXTI->EMR1, exti_line);
  361. }
  362. /* Configure EXTI interrupt mode */
  363. if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != 0UL)
  364. {
  365. SET_BIT(EXTI->IMR1, exti_line);
  366. }
  367. else
  368. {
  369. CLEAR_BIT(EXTI->IMR1, exti_line);
  370. }
  371. }
  372. else
  373. {
  374. /* Disable EXTI event mode */
  375. CLEAR_BIT(EXTI->EMR1, exti_line);
  376. /* Disable EXTI interrupt mode */
  377. CLEAR_BIT(EXTI->IMR1, exti_line);
  378. }
  379. #else
  380. /* Clear COMP EXTI pending bit (if any) */
  381. WRITE_REG(EXTI->C2PR1, exti_line);
  382. /* Configure EXTI event mode */
  383. if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != 0UL)
  384. {
  385. SET_BIT(EXTI->C2EMR1, exti_line);
  386. }
  387. else
  388. {
  389. CLEAR_BIT(EXTI->C2EMR1, exti_line);
  390. }
  391. /* Configure EXTI interrupt mode */
  392. if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != 0UL)
  393. {
  394. SET_BIT(EXTI->C2IMR1, exti_line);
  395. }
  396. else
  397. {
  398. CLEAR_BIT(EXTI->C2IMR1, exti_line);
  399. }
  400. }
  401. else
  402. {
  403. /* Disable EXTI event mode */
  404. CLEAR_BIT(EXTI->C2EMR1, exti_line);
  405. /* Disable EXTI interrupt mode */
  406. CLEAR_BIT(EXTI->C2IMR1, exti_line);
  407. }
  408. #endif
  409. /* Set HAL COMP handle state */
  410. /* Note: Transition from state reset to state ready, */
  411. /* otherwise (coming from state ready or busy) no state update. */
  412. if (hcomp->State == HAL_COMP_STATE_RESET)
  413. {
  414. hcomp->State = HAL_COMP_STATE_READY;
  415. }
  416. }
  417. return status;
  418. }
  419. /**
  420. * @brief DeInitialize the COMP peripheral.
  421. * @note Deinitialization cannot be performed if the COMP configuration is locked.
  422. * To unlock the configuration, perform a system reset.
  423. * @param hcomp COMP handle
  424. * @retval HAL status
  425. */
  426. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  427. {
  428. HAL_StatusTypeDef status = HAL_OK;
  429. /* Check the COMP handle allocation and lock status */
  430. if(hcomp == NULL)
  431. {
  432. status = HAL_ERROR;
  433. }
  434. else if(__HAL_COMP_IS_LOCKED(hcomp))
  435. {
  436. status = HAL_ERROR;
  437. }
  438. else
  439. {
  440. /* Check the parameter */
  441. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  442. /* Set COMP_CFGR register to reset value */
  443. WRITE_REG(hcomp->Instance->CFGR, 0x00000000UL);
  444. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  445. if (hcomp->MspDeInitCallback == NULL)
  446. {
  447. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  448. }
  449. /* DeInit the low level hardware */
  450. hcomp->MspDeInitCallback(hcomp);
  451. #else
  452. /* DeInit the low level hardware */
  453. HAL_COMP_MspDeInit(hcomp);
  454. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  455. /* Set HAL COMP handle state */
  456. hcomp->State = HAL_COMP_STATE_RESET;
  457. /* Release Lock */
  458. __HAL_UNLOCK(hcomp);
  459. }
  460. return status;
  461. }
  462. /**
  463. * @brief Initialize the COMP MSP.
  464. * @param hcomp COMP handle
  465. * @retval None
  466. */
  467. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  468. {
  469. /* Prevent unused argument(s) compilation warning */
  470. UNUSED(hcomp);
  471. /* NOTE : This function should not be modified, when the callback is needed,
  472. the HAL_COMP_MspInit could be implemented in the user file
  473. */
  474. }
  475. /**
  476. * @brief DeInitialize the COMP MSP.
  477. * @param hcomp COMP handle
  478. * @retval None
  479. */
  480. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  481. {
  482. /* Prevent unused argument(s) compilation warning */
  483. UNUSED(hcomp);
  484. /* NOTE : This function should not be modified, when the callback is needed,
  485. the HAL_COMP_MspDeInit could be implemented in the user file
  486. */
  487. }
  488. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  489. /**
  490. * @brief Register a User COMP Callback
  491. * To be used instead of the weak predefined callback
  492. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  493. * the configuration information for the specified COMP.
  494. * @param CallbackID ID of the callback to be registered
  495. * This parameter can be one of the following values:
  496. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  497. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  498. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  499. * @param pCallback pointer to the Callback function
  500. * @retval HAL status
  501. */
  502. HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
  503. {
  504. HAL_StatusTypeDef status = HAL_OK;
  505. if (pCallback == NULL)
  506. {
  507. /* Update the error code */
  508. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  509. return HAL_ERROR;
  510. }
  511. if (HAL_COMP_STATE_READY == hcomp->State)
  512. {
  513. switch (CallbackID)
  514. {
  515. case HAL_COMP_TRIGGER_CB_ID :
  516. hcomp->TriggerCallback = pCallback;
  517. break;
  518. case HAL_COMP_MSPINIT_CB_ID :
  519. hcomp->MspInitCallback = pCallback;
  520. break;
  521. case HAL_COMP_MSPDEINIT_CB_ID :
  522. hcomp->MspDeInitCallback = pCallback;
  523. break;
  524. default :
  525. /* Update the error code */
  526. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  527. /* Return error status */
  528. status = HAL_ERROR;
  529. break;
  530. }
  531. }
  532. else if (HAL_COMP_STATE_RESET == hcomp->State)
  533. {
  534. switch (CallbackID)
  535. {
  536. case HAL_COMP_MSPINIT_CB_ID :
  537. hcomp->MspInitCallback = pCallback;
  538. break;
  539. case HAL_COMP_MSPDEINIT_CB_ID :
  540. hcomp->MspDeInitCallback = pCallback;
  541. break;
  542. default :
  543. /* Update the error code */
  544. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  545. /* Return error status */
  546. status = HAL_ERROR;
  547. break;
  548. }
  549. }
  550. else
  551. {
  552. /* Update the error code */
  553. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  554. /* Return error status */
  555. status = HAL_ERROR;
  556. }
  557. return status;
  558. }
  559. /**
  560. * @brief Unregister a COMP Callback
  561. * COMP callback is redirected to the weak predefined callback
  562. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  563. * the configuration information for the specified COMP.
  564. * @param CallbackID ID of the callback to be unregistered
  565. * This parameter can be one of the following values:
  566. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  567. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  568. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  569. * @retval HAL status
  570. */
  571. HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
  572. {
  573. HAL_StatusTypeDef status = HAL_OK;
  574. if (HAL_COMP_STATE_READY == hcomp->State)
  575. {
  576. switch (CallbackID)
  577. {
  578. case HAL_COMP_TRIGGER_CB_ID :
  579. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  580. break;
  581. case HAL_COMP_MSPINIT_CB_ID :
  582. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  583. break;
  584. case HAL_COMP_MSPDEINIT_CB_ID :
  585. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  586. break;
  587. default :
  588. /* Update the error code */
  589. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  590. /* Return error status */
  591. status = HAL_ERROR;
  592. break;
  593. }
  594. }
  595. else if (HAL_COMP_STATE_RESET == hcomp->State)
  596. {
  597. switch (CallbackID)
  598. {
  599. case HAL_COMP_MSPINIT_CB_ID :
  600. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  601. break;
  602. case HAL_COMP_MSPDEINIT_CB_ID :
  603. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  604. break;
  605. default :
  606. /* Update the error code */
  607. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  608. /* Return error status */
  609. status = HAL_ERROR;
  610. break;
  611. }
  612. }
  613. else
  614. {
  615. /* Update the error code */
  616. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  617. /* Return error status */
  618. status = HAL_ERROR;
  619. }
  620. return status;
  621. }
  622. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  623. /**
  624. * @}
  625. */
  626. /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions
  627. * @brief Start-Stop operation functions.
  628. *
  629. @verbatim
  630. ===============================================================================
  631. ##### IO operation functions #####
  632. ===============================================================================
  633. [..] This section provides functions allowing to:
  634. (+) Start a Comparator instance without interrupt.
  635. (+) Stop a Comparator instance without interrupt.
  636. (+) Start a Comparator instance with interrupt generation.
  637. (+) Stop a Comparator instance with interrupt generation.
  638. @endverbatim
  639. * @{
  640. */
  641. /**
  642. * @brief Start the comparator.
  643. * @param hcomp COMP handle
  644. * @retval HAL status
  645. */
  646. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  647. {
  648. __IO uint32_t wait_loop_index = 0UL;
  649. HAL_StatusTypeDef status = HAL_OK;
  650. /* Check the COMP handle allocation and lock status */
  651. if(hcomp == NULL)
  652. {
  653. status = HAL_ERROR;
  654. }
  655. else if(__HAL_COMP_IS_LOCKED(hcomp))
  656. {
  657. status = HAL_ERROR;
  658. }
  659. else
  660. {
  661. /* Check the parameter */
  662. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  663. if(hcomp->State == HAL_COMP_STATE_READY)
  664. {
  665. /* Enable the selected comparator */
  666. SET_BIT(hcomp->Instance->CFGR, COMP_CFGRx_EN);
  667. /* Set HAL COMP handle state */
  668. hcomp->State = HAL_COMP_STATE_BUSY;
  669. /* Delay for COMP startup time */
  670. /* Wait loop initialization and execution */
  671. /* Note: Variable divided by 2 to compensate partially */
  672. /* CPU processing cycles. */
  673. wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / (1000000UL * 2UL)));
  674. while(wait_loop_index != 0UL)
  675. {
  676. wait_loop_index--;
  677. }
  678. }
  679. else
  680. {
  681. status = HAL_ERROR;
  682. }
  683. }
  684. return status;
  685. }
  686. /**
  687. * @brief Stop the comparator.
  688. * @param hcomp COMP handle
  689. * @retval HAL status
  690. */
  691. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  692. {
  693. HAL_StatusTypeDef status = HAL_OK;
  694. /* Check the COMP handle allocation and lock status */
  695. if(hcomp == NULL)
  696. {
  697. status = HAL_ERROR;
  698. }
  699. else if(__HAL_COMP_IS_LOCKED(hcomp))
  700. {
  701. status = HAL_ERROR;
  702. }
  703. else
  704. {
  705. /* Check the parameter */
  706. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  707. /* Check compliant states: HAL_COMP_STATE_READY or HAL_COMP_STATE_BUSY */
  708. /* (all states except HAL_COMP_STATE_RESET and except locked status. */
  709. if(hcomp->State != HAL_COMP_STATE_RESET)
  710. {
  711. /* Disable the selected comparator */
  712. CLEAR_BIT(hcomp->Instance->CFGR, COMP_CFGRx_EN);
  713. /* Set HAL COMP handle state */
  714. hcomp->State = HAL_COMP_STATE_READY;
  715. }
  716. else
  717. {
  718. status = HAL_ERROR;
  719. }
  720. }
  721. return status;
  722. }
  723. /**
  724. * @brief Enable the interrupt and start the comparator.
  725. * @param hcomp COMP handle
  726. * @retval HAL status
  727. */
  728. HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
  729. {
  730. __IO uint32_t wait_loop_index = 0UL;
  731. HAL_StatusTypeDef status = HAL_OK;
  732. /* Check the COMP handle allocation and lock status */
  733. if(hcomp == NULL)
  734. {
  735. status = HAL_ERROR;
  736. }
  737. else if(__HAL_COMP_IS_LOCKED(hcomp))
  738. {
  739. status = HAL_ERROR;
  740. }
  741. else
  742. {
  743. /* Check the parameter */
  744. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  745. /* Set HAL COMP handle state */
  746. if(hcomp->State == HAL_COMP_STATE_READY)
  747. {
  748. /* Enable the selected comparator */
  749. SET_BIT(hcomp->Instance->CFGR, COMP_CFGRx_EN);
  750. /* Enable the Interrupt comparator */
  751. SET_BIT(hcomp->Instance->CFGR, COMP_CFGRx_ITEN);
  752. hcomp->State = HAL_COMP_STATE_BUSY;
  753. /* Delay for COMP startup time */
  754. /* Wait loop initialization and execution */
  755. /* Note: Variable divided by 2 to compensate partially */
  756. /* CPU processing cycles. */
  757. wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / (1000000UL * 2UL)));
  758. while(wait_loop_index != 0UL)
  759. {
  760. wait_loop_index--;
  761. }
  762. }
  763. else
  764. {
  765. status = HAL_ERROR;
  766. }
  767. }
  768. return status;
  769. }
  770. /**
  771. * @brief Disable the interrupt and Stop the comparator.
  772. * @param hcomp COMP handle
  773. * @retval HAL status
  774. */
  775. HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
  776. {
  777. HAL_StatusTypeDef status;
  778. /* Disable the EXTI Line interrupt mode */
  779. #if !defined (CORE_CM4)
  780. CLEAR_BIT(EXTI->IMR1, COMP_GET_EXTI_LINE(hcomp->Instance));
  781. #else
  782. CLEAR_BIT(EXTI->C2IMR1, COMP_GET_EXTI_LINE(hcomp->Instance));
  783. #endif
  784. /* Disable the Interrupt comparator */
  785. CLEAR_BIT(hcomp->Instance->CFGR, COMP_CFGRx_ITEN);
  786. status = HAL_COMP_Stop(hcomp);
  787. return status;
  788. }
  789. /**
  790. * @brief Comparator IRQ Handler.
  791. * @param hcomp COMP handle
  792. * @retval HAL status
  793. */
  794. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  795. {
  796. /* Get the EXTI line corresponding to the selected COMP instance */
  797. uint32_t exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  798. #if defined(DUAL_CORE)
  799. /* EXTI line interrupt detected */
  800. if (HAL_GetCurrentCPUID() == CM7_CPUID)
  801. {
  802. /* Check COMP EXTI flag */
  803. if(READ_BIT(EXTI->PR1, exti_line) != 0UL)
  804. {
  805. /* Check whether comparator is in independent or window mode */
  806. if(READ_BIT(COMP12_COMMON->CFGR, COMP_CFGRx_WINMODE) != 0UL)
  807. {
  808. /* Clear COMP EXTI line pending bit of the pair of comparators */
  809. /* in window mode. */
  810. /* Note: Pair of comparators in window mode can both trig IRQ when */
  811. /* input voltage is changing from "out of window" area */
  812. /* (low or high ) to the other "out of window" area (high or low).*/
  813. /* Both flags must be cleared to call comparator trigger */
  814. /* callback is called once. */
  815. WRITE_REG(EXTI->PR1, (COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
  816. }
  817. else
  818. {
  819. /* Clear COMP EXTI line pending bit */
  820. WRITE_REG(EXTI->PR1, exti_line);
  821. }
  822. /* COMP trigger user callback */
  823. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  824. hcomp->TriggerCallback(hcomp);
  825. #else
  826. HAL_COMP_TriggerCallback(hcomp);
  827. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  828. }
  829. }
  830. else
  831. {
  832. /* Check COMP EXTI flag */
  833. if(READ_BIT(EXTI->C2PR1, exti_line) != 0UL)
  834. {
  835. /* Check whether comparator is in independent or window mode */
  836. if(READ_BIT(COMP12_COMMON->CFGR, COMP_CFGRx_WINMODE) != 0UL)
  837. {
  838. /* Clear COMP EXTI line pending bit of the pair of comparators */
  839. /* in window mode. */
  840. /* Note: Pair of comparators in window mode can both trig IRQ when */
  841. /* input voltage is changing from "out of window" area */
  842. /* (low or high ) to the other "out of window" area (high or low).*/
  843. /* Both flags must be cleared to call comparator trigger */
  844. /* callback is called once. */
  845. WRITE_REG(EXTI->C2PR1, (COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
  846. }
  847. else
  848. {
  849. /* Clear COMP EXTI line pending bit */
  850. WRITE_REG(EXTI->C2PR1, exti_line);
  851. }
  852. /* COMP trigger user callback */
  853. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  854. hcomp->TriggerCallback(hcomp);
  855. #else
  856. HAL_COMP_TriggerCallback(hcomp);
  857. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  858. }
  859. }
  860. #else
  861. /* Check COMP EXTI flag */
  862. if(READ_BIT(EXTI->PR1, exti_line) != 0UL)
  863. {
  864. /* Check whether comparator is in independent or window mode */
  865. if(READ_BIT(COMP12_COMMON->CFGR, COMP_CFGRx_WINMODE) != 0UL)
  866. {
  867. /* Clear COMP EXTI line pending bit of the pair of comparators */
  868. /* in window mode. */
  869. /* Note: Pair of comparators in window mode can both trig IRQ when */
  870. /* input voltage is changing from "out of window" area */
  871. /* (low or high ) to the other "out of window" area (high or low).*/
  872. /* Both flags must be cleared to call comparator trigger */
  873. /* callback is called once. */
  874. WRITE_REG(EXTI->PR1, (COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
  875. }
  876. else
  877. {
  878. /* Clear COMP EXTI line pending bit */
  879. WRITE_REG(EXTI->PR1, exti_line);
  880. }
  881. /* COMP trigger user callback */
  882. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  883. hcomp->TriggerCallback(hcomp);
  884. #else
  885. HAL_COMP_TriggerCallback(hcomp);
  886. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  887. }
  888. #endif /*DUAL_CORE*/
  889. /* Get COMP interrupt source */
  890. if (__HAL_COMP_GET_IT_SOURCE(hcomp, COMP_IT_EN) != RESET)
  891. {
  892. if((__HAL_COMP_GET_FLAG( COMP_FLAG_C1I)) != 0UL)
  893. {
  894. /* Clear the COMP channel 1 interrupt flag */
  895. __HAL_COMP_CLEAR_C1IFLAG();
  896. /* Disable COMP interrupt */
  897. __HAL_COMP_DISABLE_IT(hcomp,COMP_IT_EN);
  898. }
  899. if((__HAL_COMP_GET_FLAG( COMP_FLAG_C2I)) != 0UL)
  900. {
  901. /* Clear the COMP channel 2 interrupt flag */
  902. __HAL_COMP_CLEAR_C2IFLAG();
  903. /* Disable COMP interrupt */
  904. __HAL_COMP_DISABLE_IT(hcomp,COMP_IT_EN);
  905. }
  906. /* Change COMP state */
  907. hcomp->State = HAL_COMP_STATE_READY;
  908. /* COMP trigger user callback */
  909. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  910. hcomp->TriggerCallback(hcomp);
  911. #else
  912. HAL_COMP_TriggerCallback(hcomp);
  913. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  914. }
  915. }
  916. /**
  917. * @}
  918. */
  919. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  920. * @brief Management functions.
  921. *
  922. @verbatim
  923. ===============================================================================
  924. ##### Peripheral Control functions #####
  925. ===============================================================================
  926. [..]
  927. This subsection provides a set of functions allowing to control the comparators.
  928. @endverbatim
  929. * @{
  930. */
  931. /**
  932. * @brief Lock the selected comparator configuration.
  933. * @note A system reset is required to unlock the comparator configuration.
  934. * @param hcomp COMP handle
  935. * @retval HAL status
  936. */
  937. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  938. {
  939. HAL_StatusTypeDef status = HAL_OK;
  940. /* Check the COMP handle allocation and lock status */
  941. if(hcomp == NULL)
  942. {
  943. status = HAL_ERROR;
  944. }
  945. else if(__HAL_COMP_IS_LOCKED(hcomp))
  946. {
  947. status = HAL_ERROR;
  948. }
  949. else
  950. {
  951. /* Check the parameter */
  952. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  953. /* Set HAL COMP handle state */
  954. switch(hcomp->State)
  955. {
  956. case HAL_COMP_STATE_RESET:
  957. hcomp->State = HAL_COMP_STATE_RESET_LOCKED;
  958. break;
  959. case HAL_COMP_STATE_READY:
  960. hcomp->State = HAL_COMP_STATE_READY_LOCKED;
  961. break;
  962. default: /* HAL_COMP_STATE_BUSY */
  963. hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
  964. break;
  965. }
  966. }
  967. if(status == HAL_OK)
  968. {
  969. /* Set the lock bit corresponding to selected comparator */
  970. __HAL_COMP_LOCK(hcomp);
  971. }
  972. return status;
  973. }
  974. /**
  975. * @brief Return the output level (high or low) of the selected comparator.
  976. * @note The output level depends on the selected polarity.
  977. * If the polarity is not inverted:
  978. * - Comparator output is low when the input plus is at a lower
  979. * voltage than the input minus
  980. * - Comparator output is high when the input plus is at a higher
  981. * voltage than the input minus
  982. * If the polarity is inverted:
  983. * - Comparator output is high when the input plus is at a lower
  984. * voltage than the input minus
  985. * - Comparator output is low when the input plus is at a higher
  986. * voltage than the input minus
  987. * @param hcomp COMP handle
  988. * @retval Returns the selected comparator output level:
  989. * @arg @ref COMP_OUTPUT_LEVEL_LOW
  990. * @arg @ref COMP_OUTPUT_LEVEL_HIGH
  991. *
  992. */
  993. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  994. {
  995. /* Check the parameter */
  996. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  997. if (hcomp->Instance == COMP1)
  998. {
  999. return (uint32_t)(READ_BIT(COMP12->SR, COMP_SR_C1VAL));
  1000. }
  1001. else
  1002. {
  1003. return (uint32_t)((READ_BIT(COMP12->SR, COMP_SR_C2VAL))>> 1UL);
  1004. }
  1005. }
  1006. /**
  1007. * @brief Comparator trigger callback.
  1008. * @param hcomp COMP handle
  1009. * @retval None
  1010. */
  1011. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  1012. {
  1013. /* Prevent unused argument(s) compilation warning */
  1014. UNUSED(hcomp);
  1015. /* NOTE : This function should not be modified, when the callback is needed,
  1016. the HAL_COMP_TriggerCallback should be implemented in the user file
  1017. */
  1018. }
  1019. /**
  1020. * @}
  1021. */
  1022. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  1023. * @brief Peripheral State functions.
  1024. *
  1025. @verbatim
  1026. ===============================================================================
  1027. ##### Peripheral State functions #####
  1028. ===============================================================================
  1029. [..]
  1030. This subsection permit to get in run-time the status of the peripheral.
  1031. @endverbatim
  1032. * @{
  1033. */
  1034. /**
  1035. * @brief Return the COMP handle state.
  1036. * @param hcomp COMP handle
  1037. * @retval HAL state
  1038. */
  1039. HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  1040. {
  1041. /* Check the COMP handle allocation */
  1042. if(hcomp == NULL)
  1043. {
  1044. return HAL_COMP_STATE_RESET;
  1045. }
  1046. /* Check the parameter */
  1047. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  1048. /* Return HAL COMP handle state */
  1049. return hcomp->State;
  1050. }
  1051. /**
  1052. * @brief Return the COMP error code.
  1053. * @param hcomp COMP handle
  1054. * @retval COMP error code
  1055. */
  1056. uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
  1057. {
  1058. /* Check the parameters */
  1059. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  1060. return hcomp->ErrorCode;
  1061. }
  1062. /**
  1063. * @}
  1064. */
  1065. /**
  1066. * @}
  1067. */
  1068. #endif /* HAL_COMP_MODULE_ENABLED */
  1069. /**
  1070. * @}
  1071. */
  1072. /**
  1073. * @}
  1074. */
  1075. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/