stm32h7xx_hal_comp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_comp.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 21-April-2017
  7. * @brief COMP HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the COMP peripheral:
  10. * + Initialization and de-initialization functions
  11. * + Start/Stop operation functions in polling mode
  12. * + Start/Stop operation functions in interrupt mode
  13. * + Peripheral control functions
  14. * + Peripheral state functions
  15. @verbatim
  16. ================================================================================
  17. ##### COMP Peripheral features #####
  18. ================================================================================
  19. [..]
  20. The STM32H7xx device family integrates two analog comparators instances
  21. COMP1 and COMP2:
  22. (#) The COMP input minus (inverting input) and input plus (non inverting input)
  23. can be set to internal references or to GPIO pins
  24. (refer to GPIO list in reference manual).
  25. (#) The COMP output level is available using HAL_COMP_GetOutputLevel()
  26. and can be redirected to other peripherals: GPIO pins (in mode
  27. alternate functions for comparator), timers.
  28. (refer to GPIO list in reference manual).
  29. (#) Pairs of comparators instances can be combined in window mode
  30. (2 consecutive instances odd and even COMP<x> and COMP<x+1>).
  31. (#) The comparators have interrupt capability through the EXTI controller
  32. with wake-up from sleep and stop modes:
  33. (++) COMP1 is internally connected to EXTI Line 20
  34. (++) COMP2 is internally connected to EXTI Line 21
  35. [..]
  36. From the corresponding IRQ handler, the right interrupt source can be retrieved
  37. using macro __HAL_COMP_COMP1_EXTI_GET_FLAG() and __HAL_COMP_COMP2_EXTI_GET_FLAG().
  38. ##### How to use this driver #####
  39. ================================================================================
  40. [..]
  41. This driver provides functions to configure and program the comparator instances of
  42. STM32H7xx devices.
  43. To use the comparator, perform the following steps:
  44. (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit():
  45. (++) Configure the GPIO connected to comparator inputs plus and minus in analog mode
  46. using HAL_GPIO_Init().
  47. (++) If needed, configure the GPIO connected to comparator output in alternate function mode
  48. using HAL_GPIO_Init().
  49. (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
  50. selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
  51. interrupt vector using HAL_NVIC_EnableIRQ() function.
  52. (#) Configure the comparator using HAL_COMP_Init() function:
  53. (++) Select the input minus (inverting input)
  54. (++) Select the input plus (non-inverting input)
  55. (++) Select the hysteresis
  56. (++) Select the blanking source
  57. (++) Select the output polarity
  58. (++) Select the power mode
  59. (++) Select the window mode
  60. -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE()
  61. to enable internal control clock of the comparators.
  62. However, this is a legacy strategy.
  63. Therefore, for compatibility anticipation, it is recommended to
  64. implement __HAL_RCC_SYSCFG_CLK_ENABLE() in "HAL_COMP_MspInit()".
  65. In STM32H7,COMP clock enable __HAL_RCC_COMP12_CLK_ENABLE() must
  66. be implemented by user in "HAL_COMP_MspInit()".
  67. (#) Reconfiguration on-the-fly of comparator can be done by calling again
  68. function HAL_COMP_Init() with new input structure parameters values.
  69. (#) Enable the comparator using HAL_COMP_Start() or HAL_COMP_Start_IT()to be enabled
  70. with the interrupt through NVIC of the CPU.
  71. Note: HAL_COMP_Start_IT() must be called after each interrupt otherwise the interrupt
  72. mode will stay disabled.
  73. (#) Use HAL_COMP_GetOutputLevel() or HAL_COMP_TriggerCallback()
  74. functions to manage comparator outputs(output level or events)
  75. (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
  76. to disable the interrupt too.
  77. (#) De-initialize the comparator using HAL_COMP_DeInit() function.
  78. (#) For safety purpose, comparator configuration can be locked using HAL_COMP_Lock() function.
  79. The only way to unlock the comparator is a device hardware reset.
  80. @endverbatim
  81. ******************************************************************************
  82. Table 1. COMP inputs and output for STM32H7xx devices
  83. +---------------------------------------------------------+
  84. | | | COMP1 | COMP2 |
  85. |----------------|----------------|-----------|-----------|
  86. | | IO1 | PB0 | PE9 |
  87. | Input plus | IO2 | PB2 | PE11 |
  88. | | | | |
  89. |----------------|----------------|-----------------------|
  90. | | 1/4 VrefInt | Available | Available |
  91. | | 1/2 VrefInt | Available | Available |
  92. | | 3/4 VrefInt | Available | Available |
  93. | Input minus | VrefInt | Available | Available |
  94. | | DAC1 channel 1 | Available | Available |
  95. | | DAC1 channel 2 | Available | Available |
  96. | | IO1 | PB1 | PE10 |
  97. | | IO2 | PC4 | PE7 |
  98. | | | | |
  99. | | | | |
  100. | | | | |
  101. +---------------------------------------------------------+
  102. | Output | | PC5 (1) | PE8 (1) |
  103. | | | PE12 (1) | PE13 (1) |
  104. | | | TIM (2) | TIM (2) |
  105. +---------------------------------------------------------+
  106. (1) GPIO must be set to alternate function for comparator
  107. (2) Comparators output to timers is set in timers instances.
  108. ******************************************************************************
  109. * @attention
  110. *
  111. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  112. *
  113. * Redistribution and use in source and binary forms, with or without modification,
  114. * are permitted provided that the following conditions are met:
  115. * 1. Redistributions of source code must retain the above copyright notice,
  116. * this list of conditions and the following disclaimer.
  117. * 2. Redistributions in binary form must reproduce the above copyright notice,
  118. * this list of conditions and the following disclaimer in the documentation
  119. * and/or other materials provided with the distribution.
  120. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  121. * may be used to endorse or promote products derived from this software
  122. * without specific prior written permission.
  123. *
  124. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  125. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  126. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  127. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  128. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  129. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  130. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  131. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  132. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  133. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  134. *
  135. ******************************************************************************
  136. */
  137. /* Includes ------------------------------------------------------------------*/
  138. #include "stm32h7xx_hal.h"
  139. /** @addtogroup STM32H7xx_HAL_Driver
  140. * @{
  141. */
  142. /** @defgroup COMP COMP
  143. * @brief COMP HAL module driver
  144. * @{
  145. */
  146. #ifdef HAL_COMP_MODULE_ENABLED
  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 ((uint32_t) 80U) /*!< 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 ((uint32_t) 200U) /*!< Delay for COMP voltage scaler stabilization time */
  163. /**
  164. * @}
  165. */
  166. /* Private macro -------------------------------------------------------------*/
  167. /* Private variables ---------------------------------------------------------*/
  168. /* Private function prototypes -----------------------------------------------*/
  169. /* Exported functions --------------------------------------------------------*/
  170. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  171. * @{
  172. */
  173. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  174. * @brief Initialization and de-initialization functions.
  175. *
  176. @verbatim
  177. ===============================================================================
  178. ##### Initialization and de-initialization functions #####
  179. ===============================================================================
  180. [..] This section provides functions to initialize and de-initialize comparators
  181. @endverbatim
  182. * @{
  183. */
  184. /**
  185. * @brief Initialize the COMP according to the specified
  186. * parameters in the COMP_InitTypeDef and initialize the associated handle.
  187. * @note If the selected comparator is locked, initialization can't be performed.
  188. * To unlock the configuration, perform a system reset.
  189. * @param hcomp: COMP handle
  190. * @retval HAL status
  191. */
  192. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  193. {
  194. uint32_t tmp_csr = 0;
  195. uint32_t exti_line = 0;
  196. uint32_t comp_voltage_scaler_not_initialized = 0;
  197. __IO uint32_t wait_loop_index = 0;
  198. HAL_StatusTypeDef status = HAL_OK;
  199. /* Check the COMP handle allocation and lock status */
  200. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  201. {
  202. status = HAL_ERROR;
  203. }
  204. else
  205. {
  206. /* Check the parameters */
  207. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  208. assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput));
  209. assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput));
  210. assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  211. assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode));
  212. assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  213. assert_param(IS_COMP_BLANKINGSRCE(hcomp->Init.BlankingSrce));
  214. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  215. assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
  216. if(hcomp->State == HAL_COMP_STATE_RESET)
  217. {
  218. /* Allocate lock resource and initialize it */
  219. hcomp->Lock = HAL_UNLOCKED;
  220. /* Init the low level hardware */
  221. HAL_COMP_MspInit(hcomp);
  222. }
  223. /* Memorize voltage scaler state before initialization */
  224. comp_voltage_scaler_not_initialized = (READ_BIT(hcomp->Instance->CFGR, COMP_CFGRx_SCALEN) == 0);
  225. /* Set COMP parameters */
  226. /* Set INMSEL bits according to hcomp->Init.InvertingInput value */
  227. /* Set INPSEL bits according to hcomp->Init.NonInvertingInput value */
  228. /* Set BLANKING bits according to hcomp->Init.BlankingSrce value */
  229. /* Set HYST bits according to hcomp->Init.Hysteresis value */
  230. /* Set POLARITY bit according to hcomp->Init.OutputPol value */
  231. /* Set POWERMODE bits according to hcomp->Init.Mode value */
  232. tmp_csr = (hcomp->Init.InvertingInput | \
  233. hcomp->Init.NonInvertingInput | \
  234. hcomp->Init.BlankingSrce | \
  235. hcomp->Init.Hysteresis | \
  236. hcomp->Init.OutputPol | \
  237. hcomp->Init.Mode );
  238. /* Set parameters in COMP register */
  239. /* Note: Update all bits except read-only, lock and enable bits */
  240. MODIFY_REG(hcomp->Instance->CFGR,
  241. COMP_CFGRx_PWRMODE | COMP_CFGRx_INMSEL | COMP_CFGRx_INPSEL |
  242. COMP_CFGRx_WINMODE | COMP_CFGRx_POLARITY | COMP_CFGRx_HYST |
  243. COMP_CFGRx_BLANKING | COMP_CFGRx_BRGEN | COMP_CFGRx_SCALEN,
  244. tmp_csr
  245. );
  246. /* Set window mode */
  247. /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */
  248. /* instances. Therefore, this function can update another COMP */
  249. /* instance that the one currently selected. */
  250. if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON)
  251. {
  252. SET_BIT(hcomp->Instance->CFGR, COMP_CFGRx_WINMODE);
  253. }
  254. else
  255. {
  256. CLEAR_BIT(hcomp->Instance->CFGR, COMP_CFGRx_WINMODE);
  257. }
  258. /* Delay for COMP scaler bridge voltage stabilization */
  259. /* Apply the delay if voltage scaler bridge is enabled for the first time */
  260. if ((READ_BIT(hcomp->Instance->CFGR, COMP_CFGRx_SCALEN) != 0) &&
  261. (comp_voltage_scaler_not_initialized != 0) )
  262. {
  263. /* Wait loop initialization and execution */
  264. /* Note: Variable divided by 2 to compensate partially */
  265. /* CPU processing cycles.*/
  266. wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000 * 2)));
  267. while(wait_loop_index != 0)
  268. {
  269. wait_loop_index --;
  270. }
  271. }
  272. /* Get the EXTI line corresponding to the selected COMP instance */
  273. exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  274. /* Manage EXTI settings */
  275. if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != RESET)
  276. {
  277. /* Configure EXTI rising edge */
  278. if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != RESET)
  279. {
  280. SET_BIT(EXTI->RTSR1, exti_line);
  281. }
  282. else
  283. {
  284. CLEAR_BIT(EXTI->RTSR1, exti_line);
  285. }
  286. /* Configure EXTI falling edge */
  287. if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != RESET)
  288. {
  289. SET_BIT(EXTI->FTSR1, exti_line);
  290. }
  291. else
  292. {
  293. CLEAR_BIT(EXTI->FTSR1, exti_line);
  294. }
  295. /* Clear COMP EXTI pending bit (if any) */
  296. WRITE_REG(EXTI_D1->PR1, exti_line);
  297. /* Configure EXTI event mode */
  298. if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != RESET)
  299. {
  300. SET_BIT(EXTI_D1->EMR1, exti_line);
  301. }
  302. else
  303. {
  304. CLEAR_BIT(EXTI_D1->EMR1, exti_line);
  305. }
  306. /* Configure EXTI interrupt mode */
  307. if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != RESET)
  308. {
  309. SET_BIT(EXTI_D1->IMR1, exti_line);
  310. }
  311. else
  312. {
  313. CLEAR_BIT(EXTI_D1->IMR1, exti_line);
  314. }
  315. }
  316. else
  317. {
  318. /* Disable EXTI event mode */
  319. CLEAR_BIT(EXTI_D1->EMR1, exti_line);
  320. /* Disable EXTI interrupt mode */
  321. CLEAR_BIT(EXTI_D1->IMR1, exti_line);
  322. }
  323. /* Set HAL COMP handle state */
  324. /* Note: Transition from state reset to state ready, */
  325. /* otherwise (coming from state ready or busy) no state update. */
  326. if (hcomp->State == HAL_COMP_STATE_RESET)
  327. {
  328. hcomp->State = HAL_COMP_STATE_READY;
  329. }
  330. }
  331. return status;
  332. }
  333. /**
  334. * @brief DeInitialize the COMP peripheral.
  335. * @note Deinitialization cannot be performed if the COMP configuration is locked.
  336. * To unlock the configuration, perform a system reset.
  337. * @param hcomp COMP handle
  338. * @retval HAL status
  339. */
  340. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  341. {
  342. HAL_StatusTypeDef status = HAL_OK;
  343. /* Check the COMP handle allocation and lock status */
  344. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  345. {
  346. status = HAL_ERROR;
  347. }
  348. else
  349. {
  350. /* Check the parameter */
  351. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  352. /* Set COMP_CFGR register to reset value */
  353. WRITE_REG(hcomp->Instance->CFGR, 0x00000000);
  354. /* DeInit the low level hardware */
  355. HAL_COMP_MspDeInit(hcomp);
  356. /* Set HAL COMP handle state */
  357. hcomp->State = HAL_COMP_STATE_RESET;
  358. /* Release Lock */
  359. __HAL_UNLOCK(hcomp);
  360. }
  361. return status;
  362. }
  363. /**
  364. * @brief Initialize the COMP MSP.
  365. * @param hcomp COMP handle
  366. * @retval None
  367. */
  368. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  369. {
  370. /* Prevent unused argument(s) compilation warning */
  371. UNUSED(hcomp);
  372. /* NOTE : This function should not be modified, when the callback is needed,
  373. the HAL_COMP_MspInit could be implemented in the user file
  374. */
  375. }
  376. /**
  377. * @brief DeInitialize the COMP MSP.
  378. * @param hcomp COMP handle
  379. * @retval None
  380. */
  381. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  382. {
  383. /* Prevent unused argument(s) compilation warning */
  384. UNUSED(hcomp);
  385. /* NOTE : This function should not be modified, when the callback is needed,
  386. the HAL_COMP_MspDeInit could be implemented in the user file
  387. */
  388. }
  389. /**
  390. * @}
  391. */
  392. /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions
  393. * @brief Start-Stop operation functions.
  394. *
  395. @verbatim
  396. ===============================================================================
  397. ##### IO operation functions #####
  398. ===============================================================================
  399. [..] This section provides functions allowing to:
  400. (+) Start a Comparator instance without interrupt.
  401. (+) Stop a Comparator instance without interrupt.
  402. (+) Start a Comparator instance with interrupt generation.
  403. (+) Stop a Comparator instance with interrupt generation.
  404. @endverbatim
  405. * @{
  406. */
  407. /**
  408. * @brief Start the comparator.
  409. * @param hcomp COMP handle
  410. * @retval HAL status
  411. */
  412. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  413. {
  414. __IO uint32_t wait_loop_index = 0;
  415. HAL_StatusTypeDef status = HAL_OK;
  416. /* Check the COMP handle allocation and lock status */
  417. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  418. {
  419. status = HAL_ERROR;
  420. }
  421. else
  422. {
  423. /* Check the parameter */
  424. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  425. if(hcomp->State == HAL_COMP_STATE_READY)
  426. {
  427. /* Enable the selected comparator */
  428. SET_BIT(hcomp->Instance->CFGR, COMP_CFGRx_EN);
  429. /* Set HAL COMP handle state */
  430. hcomp->State = HAL_COMP_STATE_BUSY;
  431. /* Delay for COMP startup time */
  432. /* Wait loop initialization and execution */
  433. /* Note: Variable divided by 2 to compensate partially */
  434. /* CPU processing cycles. */
  435. wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / (1000000 * 2)));
  436. while(wait_loop_index != 0)
  437. {
  438. wait_loop_index--;
  439. }
  440. }
  441. else
  442. {
  443. status = HAL_ERROR;
  444. }
  445. }
  446. return status;
  447. }
  448. /**
  449. * @brief Stop the comparator.
  450. * @param hcomp COMP handle
  451. * @retval HAL status
  452. */
  453. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  454. {
  455. HAL_StatusTypeDef status = HAL_OK;
  456. /* Check the COMP handle allocation and lock status */
  457. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  458. {
  459. status = HAL_ERROR;
  460. }
  461. else
  462. {
  463. /* Check the parameter */
  464. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  465. if((hcomp->State == HAL_COMP_STATE_BUSY) ||
  466. (hcomp->State == HAL_COMP_STATE_READY) )
  467. {
  468. /* Disable the selected comparator */
  469. CLEAR_BIT(hcomp->Instance->CFGR, COMP_CFGRx_EN);
  470. /* Set HAL COMP handle state */
  471. hcomp->State = HAL_COMP_STATE_READY;
  472. }
  473. else
  474. {
  475. status = HAL_ERROR;
  476. }
  477. }
  478. return status;
  479. }
  480. /**
  481. * @brief Enable the interrupt and start the comparator.
  482. * @param hcomp COMP handle
  483. * @retval HAL status
  484. */
  485. HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
  486. {
  487. __IO uint32_t wait_loop_index = 0;
  488. HAL_StatusTypeDef status = HAL_OK;
  489. /* Check the COMP handle allocation and lock status */
  490. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  491. {
  492. status = HAL_ERROR;
  493. }
  494. else
  495. {
  496. /* Check the parameter */
  497. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  498. /* Set HAL COMP handle state */
  499. if(hcomp->State == HAL_COMP_STATE_READY)
  500. {
  501. /* Enable the selected comparator */
  502. SET_BIT(hcomp->Instance->CFGR, COMP_CFGRx_EN);
  503. /* Enable the Interrupt comparator */
  504. SET_BIT(hcomp->Instance->CFGR, COMP_CFGRx_ITEN);
  505. hcomp->State = HAL_COMP_STATE_BUSY;
  506. /* Delay for COMP startup time */
  507. /* Wait loop initialization and execution */
  508. /* Note: Variable divided by 2 to compensate partially */
  509. /* CPU processing cycles. */
  510. wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / (1000000 * 2)));
  511. while(wait_loop_index != 0)
  512. {
  513. wait_loop_index--;
  514. }
  515. }
  516. else
  517. {
  518. status = HAL_ERROR;
  519. }
  520. }
  521. return status;
  522. }
  523. /**
  524. * @brief Disable the interrupt and Stop the comparator.
  525. * @param hcomp COMP handle
  526. * @retval HAL status
  527. */
  528. HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
  529. {
  530. HAL_StatusTypeDef status = HAL_OK;
  531. /* Disable the EXTI Line interrupt mode */
  532. CLEAR_BIT(EXTI_D1->IMR1, COMP_GET_EXTI_LINE(hcomp->Instance));
  533. /* Disable the Interrupt comparator */
  534. CLEAR_BIT(hcomp->Instance->CFGR, COMP_CFGRx_ITEN);
  535. status = HAL_COMP_Stop(hcomp);
  536. return status;
  537. }
  538. /**
  539. * @brief Comparator IRQ Handler.
  540. * @param hcomp COMP handle
  541. * @retval HAL status
  542. */
  543. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  544. {
  545. /* Get the EXTI line corresponding to the selected COMP instance */
  546. uint32_t exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  547. /* Check COMP EXTI flag */
  548. if(READ_BIT(EXTI_D1->PR1, exti_line) != RESET)
  549. {
  550. /* Check whether comparator is in independent or window mode */
  551. if(READ_BIT(COMP12_COMMON->CFGR, COMP_CFGRx_WINMODE) != RESET)
  552. {
  553. /* Clear COMP EXTI line pending bit of the pair of comparators */
  554. /* in window mode. */
  555. /* Note: Pair of comparators in window mode can both trig IRQ when */
  556. /* input voltage is changing from "out of window" area */
  557. /* (low or high ) to the other "out of window" area (high or low).*/
  558. /* Both flags must be cleared to call comparator trigger */
  559. /* callback is called once. */
  560. WRITE_REG(EXTI_D1->PR1, (COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
  561. }
  562. else
  563. {
  564. /* Clear COMP EXTI line pending bit */
  565. WRITE_REG(EXTI_D1->PR1, exti_line);
  566. }
  567. /* COMP trigger user callback */
  568. HAL_COMP_TriggerCallback(hcomp);
  569. }
  570. /* Get COMP interrupt source */
  571. if (__HAL_COMP_GET_IT_SOURCE(hcomp, COMP_IT_EN) != RESET)
  572. {
  573. if((__HAL_COMP_GET_FLAG( COMP_FLAG_C1I)) != RESET)
  574. {
  575. /* Clear the COMP channel 1 interrupt flag */
  576. __HAL_COMP_CLEAR_C1IFLAG();
  577. /* Disable COMP interrupt */
  578. __HAL_COMP_DISABLE_IT(hcomp,COMP_IT_EN);
  579. }
  580. if((__HAL_COMP_GET_FLAG( COMP_FLAG_C2I)) != RESET)
  581. {
  582. /* Clear the COMP channel 2 interrupt flag */
  583. __HAL_COMP_CLEAR_C2IFLAG();
  584. /* Disable COMP interrupt */
  585. __HAL_COMP_DISABLE_IT(hcomp,COMP_IT_EN);
  586. }
  587. /* Change COMP state */
  588. hcomp->State = HAL_COMP_STATE_READY;
  589. /* COMP trigger user callback */
  590. HAL_COMP_TriggerCallback(hcomp);
  591. }
  592. }
  593. /**
  594. * @}
  595. */
  596. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  597. * @brief Management functions.
  598. *
  599. @verbatim
  600. ===============================================================================
  601. ##### Peripheral Control functions #####
  602. ===============================================================================
  603. [..]
  604. This subsection provides a set of functions allowing to control the comparators.
  605. @endverbatim
  606. * @{
  607. */
  608. /**
  609. * @brief Lock the selected comparator configuration.
  610. * @note A system reset is required to unlock the comparator configuration.
  611. * @param hcomp COMP handle
  612. * @retval HAL status
  613. */
  614. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  615. {
  616. HAL_StatusTypeDef status = HAL_OK;
  617. /* Check the COMP handle allocation and lock status */
  618. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  619. {
  620. status = HAL_ERROR;
  621. }
  622. else
  623. {
  624. /* Check the parameter */
  625. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  626. /* Set HAL COMP handle state */
  627. hcomp->State = ((HAL_COMP_StateTypeDef)(hcomp->State | COMP_STATE_BITFIELD_LOCK));
  628. }
  629. if(status == HAL_OK)
  630. {
  631. /* Set the lock bit corresponding to selected comparator */
  632. __HAL_COMP_LOCK(hcomp);
  633. }
  634. return status;
  635. }
  636. /**
  637. * @brief Return the output level (high or low) of the selected comparator.
  638. * @note The output level depends on the selected polarity.
  639. * If the polarity is not inverted:
  640. * - Comparator output is low when the input plus is at a lower
  641. * voltage than the input minus
  642. * - Comparator output is high when the input plus is at a higher
  643. * voltage than the input minus
  644. * If the polarity is inverted:
  645. * - Comparator output is high when the input plus is at a lower
  646. * voltage than the input minus
  647. * - Comparator output is low when the input plus is at a higher
  648. * voltage than the input minus
  649. * @param hcomp COMP handle
  650. * @retval Returns the selected comparator output level:
  651. * @arg @ref COMP_OUTPUT_LEVEL_LOW
  652. * @arg @ref COMP_OUTPUT_LEVEL_HIGH
  653. *
  654. */
  655. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  656. {
  657. /* Check the parameter */
  658. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  659. if (hcomp->Instance == COMP1)
  660. {
  661. return (uint32_t)(READ_BIT(COMP12->SR, COMP_SR_C1VAL));
  662. }
  663. else
  664. {
  665. return (uint32_t)((READ_BIT(COMP12->SR, COMP_SR_C2VAL))>> 1);
  666. }
  667. }
  668. /**
  669. * @brief Comparator callback.
  670. * @param hcomp COMP handle
  671. * @retval None
  672. */
  673. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  674. {
  675. /* Prevent unused argument(s) compilation warning */
  676. UNUSED(hcomp);
  677. /* NOTE : This function should not be modified, when the callback is needed,
  678. the HAL_COMP_TriggerCallback should be implemented in the user file
  679. */
  680. }
  681. /**
  682. * @}
  683. */
  684. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  685. * @brief Peripheral State functions.
  686. *
  687. @verbatim
  688. ===============================================================================
  689. ##### Peripheral State functions #####
  690. ===============================================================================
  691. [..]
  692. This subsection permit to get in run-time the status of the peripheral.
  693. @endverbatim
  694. * @{
  695. */
  696. /**
  697. * @brief Return the COMP handle state.
  698. * @param hcomp COMP handle
  699. * @retval HAL state
  700. */
  701. HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  702. {
  703. /* Check the COMP handle allocation */
  704. if(hcomp == NULL)
  705. {
  706. return HAL_COMP_STATE_RESET;
  707. }
  708. /* Check the parameter */
  709. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  710. /* Return HAL COMP handle state */
  711. return hcomp->State;
  712. }
  713. /**
  714. * @}
  715. */
  716. /**
  717. * @}
  718. */
  719. #endif /* HAL_COMP_MODULE_ENABLED */
  720. /**
  721. * @}
  722. */
  723. /**
  724. * @}
  725. */
  726. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/