stm32l4xx_hal_comp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 STM32L4xx 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 STM32L4xx 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. @endverbatim
  72. ******************************************************************************
  73. Table 1. COMP inputs and output for STM32L4xx devices
  74. +---------------------------------------------------------+
  75. | | | COMP1 | COMP2 |
  76. |----------------|----------------|-----------|-----------|
  77. | | IO1 | PC5 | PB4 |
  78. | Input plus | IO2 | PB2 | PB6 |
  79. | | IO3 (3) | PA1 | PA3 |
  80. |----------------|----------------|-----------------------|
  81. | | 1/4 VrefInt | Available | Available |
  82. | | 1/2 VrefInt | Available | Available |
  83. | | 3/4 VrefInt | Available | Available |
  84. | Input minus | VrefInt | Available | Available |
  85. | | DAC1 channel 1 | Available | Available |
  86. | | DAC1 channel 2 | Available | Available |
  87. | | IO1 | PB1 | PB3 |
  88. | | IO2 | PC4 | PB7 |
  89. | | IO3 (3) | PA0 | PA2 |
  90. | | IO4 (3) | PA4 | PA4 |
  91. | | IO5 (3) | PA5 | PA5 |
  92. +---------------------------------------------------------+
  93. | Output | | PB0 (1) | PB5 (1) |
  94. | | | PB10 (1) | PB11 (1) |
  95. | | | TIM (2) | TIM (2) |
  96. +---------------------------------------------------------+
  97. (1) GPIO must be set to alternate function for comparator
  98. (2) Comparators output to timers is set in timers instances.
  99. (3) Only STM32L43x/L44x
  100. ******************************************************************************
  101. * @attention
  102. *
  103. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  104. *
  105. * Redistribution and use in source and binary forms, with or without modification,
  106. * are permitted provided that the following conditions are met:
  107. * 1. Redistributions of source code must retain the above copyright notice,
  108. * this list of conditions and the following disclaimer.
  109. * 2. Redistributions in binary form must reproduce the above copyright notice,
  110. * this list of conditions and the following disclaimer in the documentation
  111. * and/or other materials provided with the distribution.
  112. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  113. * may be used to endorse or promote products derived from this software
  114. * without specific prior written permission.
  115. *
  116. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  117. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  118. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  119. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  120. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  121. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  122. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  123. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  124. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  125. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  126. *
  127. ******************************************************************************
  128. */
  129. /* Includes ------------------------------------------------------------------*/
  130. #include "stm32l4xx_hal.h"
  131. #ifdef HAL_COMP_MODULE_ENABLED
  132. #if defined (COMP1) || defined (COMP2)
  133. /** @addtogroup STM32L4xx_HAL_Driver
  134. * @{
  135. */
  136. /** @defgroup COMP COMP
  137. * @brief COMP HAL module driver
  138. * @{
  139. */
  140. /* Private typedef -----------------------------------------------------------*/
  141. /* Private define ------------------------------------------------------------*/
  142. /** @addtogroup COMP_Private_Constants
  143. * @{
  144. */
  145. /* Delay for COMP startup time. */
  146. /* Note: Delay required to reach propagation delay specification. */
  147. /* Literal set to maximum value (refer to device datasheet, */
  148. /* parameter "tSTART"). */
  149. /* Unit: us */
  150. #define COMP_DELAY_STARTUP_US (80U) /*!< Delay for COMP startup time */
  151. /* Delay for COMP voltage scaler stabilization time. */
  152. /* Literal set to maximum value (refer to device datasheet, */
  153. /* parameter "tSTART_SCALER"). */
  154. /* Unit: us */
  155. #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200U) /*!< Delay for COMP voltage scaler stabilization time */
  156. #define COMP_OUTPUT_LEVEL_BITOFFSET_POS (30U)
  157. /**
  158. * @}
  159. */
  160. /* Private macro -------------------------------------------------------------*/
  161. /* Private variables ---------------------------------------------------------*/
  162. /* Private function prototypes -----------------------------------------------*/
  163. /* Exported functions --------------------------------------------------------*/
  164. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  165. * @{
  166. */
  167. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  168. * @brief Initialization and de-initialization functions.
  169. *
  170. @verbatim
  171. ===============================================================================
  172. ##### Initialization and de-initialization functions #####
  173. ===============================================================================
  174. [..] This section provides functions to initialize and de-initialize comparators
  175. @endverbatim
  176. * @{
  177. */
  178. /**
  179. * @brief Initialize the COMP according to the specified
  180. * parameters in the COMP_InitTypeDef and initialize the associated handle.
  181. * @note If the selected comparator is locked, initialization can't be performed.
  182. * To unlock the configuration, perform a system reset.
  183. * @param hcomp COMP handle
  184. * @retval HAL status
  185. */
  186. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  187. {
  188. uint32_t tmp_csr = 0U;
  189. uint32_t exti_line = 0U;
  190. uint32_t comp_voltage_scaler_not_initialized = 0U;
  191. __IO uint32_t wait_loop_index = 0U;
  192. HAL_StatusTypeDef status = HAL_OK;
  193. /* Check the COMP handle allocation and lock status */
  194. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  195. {
  196. status = HAL_ERROR;
  197. }
  198. else
  199. {
  200. /* Check the parameters */
  201. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  202. assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput));
  203. assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput));
  204. assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  205. assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode));
  206. assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  207. assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
  208. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  209. assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
  210. if(hcomp->State == HAL_COMP_STATE_RESET)
  211. {
  212. /* Allocate lock resource and initialize it */
  213. hcomp->Lock = HAL_UNLOCKED;
  214. /* Init SYSCFG and the low level hardware to access comparators */
  215. /* Note: HAL_COMP_Init() calls __HAL_RCC_SYSCFG_CLK_ENABLE() */
  216. /* to enable internal control clock of the comparators. */
  217. /* However, this is a legacy strategy. In future STM32 families, */
  218. /* COMP clock enable must be implemented by user */
  219. /* in "HAL_COMP_MspInit()". */
  220. /* Therefore, for compatibility anticipation, it is recommended */
  221. /* to implement __HAL_RCC_SYSCFG_CLK_ENABLE() */
  222. /* in "HAL_COMP_MspInit()". */
  223. __HAL_RCC_SYSCFG_CLK_ENABLE();
  224. /* Init the low level hardware */
  225. HAL_COMP_MspInit(hcomp);
  226. }
  227. /* Memorize voltage scaler state before initialization */
  228. comp_voltage_scaler_not_initialized = (READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) == 0);
  229. /* Set COMP parameters */
  230. tmp_csr = ( hcomp->Init.NonInvertingInput
  231. | hcomp->Init.InvertingInput
  232. | hcomp->Init.BlankingSrce
  233. | hcomp->Init.Hysteresis
  234. | hcomp->Init.OutputPol
  235. | hcomp->Init.Mode
  236. );
  237. /* Set parameters in COMP register */
  238. /* Note: Update all bits except read-only, lock and enable bits */
  239. #if defined (COMP_CSR_INMESEL)
  240. MODIFY_REG(hcomp->Instance->CSR,
  241. COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
  242. COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST |
  243. COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL,
  244. tmp_csr
  245. );
  246. #else
  247. MODIFY_REG(hcomp->Instance->CSR,
  248. COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
  249. COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST |
  250. COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN,
  251. tmp_csr
  252. );
  253. #endif
  254. /* Set window mode */
  255. /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */
  256. /* instances. Therefore, this function can update another COMP */
  257. /* instance that the one currently selected. */
  258. if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON)
  259. {
  260. SET_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
  261. }
  262. else
  263. {
  264. CLEAR_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
  265. }
  266. /* Delay for COMP scaler bridge voltage stabilization */
  267. /* Apply the delay if voltage scaler bridge is enabled for the first time */
  268. if ((READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) != 0U) &&
  269. (comp_voltage_scaler_not_initialized != 0U) )
  270. {
  271. /* Wait loop initialization and execution */
  272. /* Note: Variable divided by 2 to compensate partially */
  273. /* CPU processing cycles. */
  274. wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000 * 2U)));
  275. while(wait_loop_index != 0U)
  276. {
  277. wait_loop_index--;
  278. }
  279. }
  280. /* Get the EXTI line corresponding to the selected COMP instance */
  281. exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  282. /* Manage EXTI settings */
  283. if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != RESET)
  284. {
  285. /* Configure EXTI rising edge */
  286. if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != RESET)
  287. {
  288. LL_EXTI_EnableRisingTrig_0_31(exti_line);
  289. }
  290. else
  291. {
  292. LL_EXTI_DisableRisingTrig_0_31(exti_line);
  293. }
  294. /* Configure EXTI falling edge */
  295. if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != RESET)
  296. {
  297. LL_EXTI_EnableFallingTrig_0_31(exti_line);
  298. }
  299. else
  300. {
  301. LL_EXTI_DisableFallingTrig_0_31(exti_line);
  302. }
  303. /* Clear COMP EXTI pending bit (if any) */
  304. LL_EXTI_ClearFlag_0_31(exti_line);
  305. /* Configure EXTI event mode */
  306. if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != RESET)
  307. {
  308. LL_EXTI_EnableEvent_0_31(exti_line);
  309. }
  310. else
  311. {
  312. LL_EXTI_DisableEvent_0_31(exti_line);
  313. }
  314. /* Configure EXTI interrupt mode */
  315. if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != RESET)
  316. {
  317. LL_EXTI_EnableIT_0_31(exti_line);
  318. }
  319. else
  320. {
  321. LL_EXTI_DisableIT_0_31(exti_line);
  322. }
  323. }
  324. else
  325. {
  326. /* Disable EXTI event mode */
  327. LL_EXTI_DisableEvent_0_31(exti_line);
  328. /* Disable EXTI interrupt mode */
  329. LL_EXTI_DisableIT_0_31(exti_line);
  330. }
  331. /* Set HAL COMP handle state */
  332. /* Note: Transition from state reset to state ready, */
  333. /* otherwise (coming from state ready or busy) no state update. */
  334. if (hcomp->State == HAL_COMP_STATE_RESET)
  335. {
  336. hcomp->State = HAL_COMP_STATE_READY;
  337. }
  338. }
  339. return status;
  340. }
  341. /**
  342. * @brief DeInitialize the COMP peripheral.
  343. * @note Deinitialization cannot be performed if the COMP configuration is locked.
  344. * To unlock the configuration, perform a system reset.
  345. * @param hcomp COMP handle
  346. * @retval HAL status
  347. */
  348. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  349. {
  350. HAL_StatusTypeDef status = HAL_OK;
  351. /* Check the COMP handle allocation and lock status */
  352. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  353. {
  354. status = HAL_ERROR;
  355. }
  356. else
  357. {
  358. /* Check the parameter */
  359. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  360. /* Set COMP_CSR register to reset value */
  361. WRITE_REG(hcomp->Instance->CSR, 0x00000000U);
  362. /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  363. HAL_COMP_MspDeInit(hcomp);
  364. /* Set HAL COMP handle state */
  365. hcomp->State = HAL_COMP_STATE_RESET;
  366. /* Release Lock */
  367. __HAL_UNLOCK(hcomp);
  368. }
  369. return status;
  370. }
  371. /**
  372. * @brief Initialize the COMP MSP.
  373. * @param hcomp COMP handle
  374. * @retval None
  375. */
  376. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  377. {
  378. /* Prevent unused argument(s) compilation warning */
  379. UNUSED(hcomp);
  380. /* NOTE : This function should not be modified, when the callback is needed,
  381. the HAL_COMP_MspInit could be implemented in the user file
  382. */
  383. }
  384. /**
  385. * @brief DeInitialize the COMP MSP.
  386. * @param hcomp COMP handle
  387. * @retval None
  388. */
  389. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  390. {
  391. /* Prevent unused argument(s) compilation warning */
  392. UNUSED(hcomp);
  393. /* NOTE : This function should not be modified, when the callback is needed,
  394. the HAL_COMP_MspDeInit could be implemented in the user file
  395. */
  396. }
  397. /**
  398. * @}
  399. */
  400. /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions
  401. * @brief Start-Stop operation functions.
  402. *
  403. @verbatim
  404. ===============================================================================
  405. ##### IO operation functions #####
  406. ===============================================================================
  407. [..] This section provides functions allowing to:
  408. (+) Start a comparator instance.
  409. (+) Stop a comparator instance.
  410. @endverbatim
  411. * @{
  412. */
  413. /**
  414. * @brief Start the comparator.
  415. * @param hcomp COMP handle
  416. * @retval HAL status
  417. */
  418. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  419. {
  420. __IO uint32_t wait_loop_index = 0U;
  421. HAL_StatusTypeDef status = HAL_OK;
  422. /* Check the COMP handle allocation and lock status */
  423. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  424. {
  425. status = HAL_ERROR;
  426. }
  427. else
  428. {
  429. /* Check the parameter */
  430. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  431. if(hcomp->State == HAL_COMP_STATE_READY)
  432. {
  433. /* Enable the selected comparator */
  434. SET_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
  435. /* Set HAL COMP handle state */
  436. hcomp->State = HAL_COMP_STATE_BUSY;
  437. /* Delay for COMP startup time */
  438. /* Wait loop initialization and execution */
  439. /* Note: Variable divided by 2 to compensate partially */
  440. /* CPU processing cycles. */
  441. wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / (1000000U * 2U)));
  442. while(wait_loop_index != 0U)
  443. {
  444. wait_loop_index--;
  445. }
  446. }
  447. else
  448. {
  449. status = HAL_ERROR;
  450. }
  451. }
  452. return status;
  453. }
  454. /**
  455. * @brief Stop the comparator.
  456. * @param hcomp COMP handle
  457. * @retval HAL status
  458. */
  459. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  460. {
  461. HAL_StatusTypeDef status = HAL_OK;
  462. /* Check the COMP handle allocation and lock status */
  463. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  464. {
  465. status = HAL_ERROR;
  466. }
  467. else
  468. {
  469. /* Check the parameter */
  470. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  471. if((hcomp->State == HAL_COMP_STATE_BUSY) ||
  472. (hcomp->State == HAL_COMP_STATE_READY) )
  473. {
  474. /* Disable the selected comparator */
  475. CLEAR_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
  476. /* Set HAL COMP handle state */
  477. hcomp->State = HAL_COMP_STATE_READY;
  478. }
  479. else
  480. {
  481. status = HAL_ERROR;
  482. }
  483. }
  484. return status;
  485. }
  486. /**
  487. * @brief Comparator IRQ handler.
  488. * @param hcomp COMP handle
  489. * @retval None
  490. */
  491. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  492. {
  493. /* Get the EXTI line corresponding to the selected COMP instance */
  494. uint32_t exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  495. /* Check COMP EXTI flag */
  496. if(LL_EXTI_IsActiveFlag_0_31(exti_line) != RESET)
  497. {
  498. /* Check whether comparator is in independent or window mode */
  499. if(READ_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE) != RESET)
  500. {
  501. /* Clear COMP EXTI line pending bit of the pair of comparators */
  502. /* in window mode. */
  503. /* Note: Pair of comparators in window mode can both trig IRQ when */
  504. /* input voltage is changing from "out of window" area */
  505. /* (low or high ) to the other "out of window" area (high or low).*/
  506. /* Both flags must be cleared to call comparator trigger */
  507. /* callback is called once. */
  508. LL_EXTI_ClearFlag_0_31((COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
  509. }
  510. else
  511. {
  512. /* Clear COMP EXTI line pending bit */
  513. LL_EXTI_ClearFlag_0_31(exti_line);
  514. }
  515. /* COMP trigger user callback */
  516. HAL_COMP_TriggerCallback(hcomp);
  517. }
  518. }
  519. /**
  520. * @}
  521. */
  522. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  523. * @brief Management functions.
  524. *
  525. @verbatim
  526. ===============================================================================
  527. ##### Peripheral Control functions #####
  528. ===============================================================================
  529. [..]
  530. This subsection provides a set of functions allowing to control the comparators.
  531. @endverbatim
  532. * @{
  533. */
  534. /**
  535. * @brief Lock the selected comparator configuration.
  536. * @note A system reset is required to unlock the comparator configuration.
  537. * @note Locking the comparator from reset state is possible
  538. * if __HAL_RCC_SYSCFG_CLK_ENABLE() is being called before.
  539. * @param hcomp COMP handle
  540. * @retval HAL status
  541. */
  542. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  543. {
  544. HAL_StatusTypeDef status = HAL_OK;
  545. /* Check the COMP handle allocation and lock status */
  546. if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp)))
  547. {
  548. status = HAL_ERROR;
  549. }
  550. else
  551. {
  552. /* Check the parameter */
  553. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  554. /* Set HAL COMP handle state */
  555. hcomp->State = ((HAL_COMP_StateTypeDef)(hcomp->State | COMP_STATE_BITFIELD_LOCK));
  556. }
  557. if(status == HAL_OK)
  558. {
  559. /* Set the lock bit corresponding to selected comparator */
  560. __HAL_COMP_LOCK(hcomp);
  561. }
  562. return status;
  563. }
  564. /**
  565. * @brief Return the output level (high or low) of the selected comparator.
  566. * The output level depends on the selected polarity.
  567. * If the polarity is not inverted:
  568. * - Comparator output is low when the input plus is at a lower
  569. * voltage than the input minus
  570. * - Comparator output is high when the input plus is at a higher
  571. * voltage than the input minus
  572. * If the polarity is inverted:
  573. * - Comparator output is high when the input plus is at a lower
  574. * voltage than the input minus
  575. * - Comparator output is low when the input plus is at a higher
  576. * voltage than the input minus
  577. * @param hcomp COMP handle
  578. * @retval Returns the selected comparator output level:
  579. * @arg COMP_OUTPUT_LEVEL_LOW
  580. * @arg COMP_OUTPUT_LEVEL_HIGH
  581. *
  582. */
  583. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  584. {
  585. /* Check the parameter */
  586. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  587. return (uint32_t)(READ_BIT(hcomp->Instance->CSR, COMP_CSR_VALUE)
  588. >> COMP_OUTPUT_LEVEL_BITOFFSET_POS);
  589. }
  590. /**
  591. * @brief Comparator callback.
  592. * @param hcomp COMP handle
  593. * @retval None
  594. */
  595. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  596. {
  597. /* Prevent unused argument(s) compilation warning */
  598. UNUSED(hcomp);
  599. /* NOTE : This function should not be modified, when the callback is needed,
  600. the HAL_COMP_TriggerCallback should be implemented in the user file
  601. */
  602. }
  603. /**
  604. * @}
  605. */
  606. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  607. * @brief Peripheral State functions.
  608. *
  609. @verbatim
  610. ===============================================================================
  611. ##### Peripheral State functions #####
  612. ===============================================================================
  613. [..]
  614. This subsection permit to get in run-time the status of the peripheral.
  615. @endverbatim
  616. * @{
  617. */
  618. /**
  619. * @brief Return the COMP handle state.
  620. * @param hcomp COMP handle
  621. * @retval HAL state
  622. */
  623. HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  624. {
  625. /* Check the COMP handle allocation */
  626. if(hcomp == NULL)
  627. {
  628. return HAL_COMP_STATE_RESET;
  629. }
  630. /* Check the parameter */
  631. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  632. /* Return HAL COMP handle state */
  633. return hcomp->State;
  634. }
  635. /**
  636. * @}
  637. */
  638. /**
  639. * @}
  640. */
  641. /**
  642. * @}
  643. */
  644. /**
  645. * @}
  646. */
  647. #endif /* COMP1 || COMP2 */
  648. #endif /* HAL_COMP_MODULE_ENABLED */
  649. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/