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.
 
 
 

745 lines
23 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_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/de-initialization functions
  9. * + I/O operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. @verbatim
  14. ================================================================================
  15. ##### COMP Peripheral features #####
  16. ================================================================================
  17. [..]
  18. The STM32F0xx device family integrates up to 2 analog comparators COMP1 and COMP2:
  19. (+) The non inverting input and inverting input can be set to GPIO pins.
  20. (+) The COMP output is available using HAL_COMP_GetOutputLevel()
  21. and can be set on GPIO pins.
  22. (+) The COMP output can be redirected to embedded timers (TIM1, TIM2 and TIM3).
  23. (+) The comparators COMP1 and COMP2 can be combined in window mode.
  24. (+) The comparators have interrupt capability with wake-up
  25. from Sleep and Stop modes (through the EXTI controller):
  26. (++) COMP1 is internally connected to EXTI Line 21
  27. (++) COMP2 is internally connected to EXTI Line 22
  28. (+) From the corresponding IRQ handler, the right interrupt source can be retrieved with the
  29. macros __HAL_COMP_COMP1_EXTI_GET_FLAG() and __HAL_COMP_COMP2_EXTI_GET_FLAG().
  30. ##### How to use this driver #####
  31. ================================================================================
  32. [..]
  33. This driver provides functions to configure and program the Comparators of STM32F05x, STM32F07x and STM32F09x devices.
  34. To use the comparator, perform the following steps:
  35. (#) Fill in the HAL_COMP_MspInit() to
  36. (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
  37. (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
  38. output to the GPIO pin
  39. (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
  40. selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
  41. interrupt vector using HAL_NVIC_EnableIRQ() function.
  42. (#) Configure the comparator using HAL_COMP_Init() function:
  43. (++) Select the inverting input (input minus)
  44. (++) Select the non inverting input (input plus)
  45. (++) Select the output polarity
  46. (++) Select the output redirection
  47. (++) Select the hysteresis level
  48. (++) Select the power mode
  49. (++) Select the event/interrupt mode
  50. (++) Select the window mode
  51. -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order
  52. to access the comparator(s) registers.
  53. (#) Enable the comparator using HAL_COMP_Start() function or HAL_COMP_Start_IT() function for interrupt mode.
  54. (#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions
  55. to manage comparator outputs (event/interrupt triggered and output level).
  56. (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
  57. function.
  58. (#) De-initialize the comparator using HAL_COMP_DeInit() function.
  59. (#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function.
  60. Only a MCU reset can reset that protection.
  61. @endverbatim
  62. ******************************************************************************
  63. * @attention
  64. *
  65. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  66. *
  67. * Redistribution and use in source and binary forms, with or without modification,
  68. * are permitted provided that the following conditions are met:
  69. * 1. Redistributions of source code must retain the above copyright notice,
  70. * this list of conditions and the following disclaimer.
  71. * 2. Redistributions in binary form must reproduce the above copyright notice,
  72. * this list of conditions and the following disclaimer in the documentation
  73. * and/or other materials provided with the distribution.
  74. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  75. * may be used to endorse or promote products derived from this software
  76. * without specific prior written permission.
  77. *
  78. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  79. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  80. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  81. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  82. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  83. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  84. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  85. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  86. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  87. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  88. *
  89. ******************************************************************************
  90. */
  91. /*
  92. Additional Tables:
  93. Table 1. COMP Inputs for the STM32F05x, STM32F07x and STM32F09x devices
  94. +--------------------------------------------------+
  95. | | | COMP1 | COMP2 |
  96. |-----------------|----------------|---------------|
  97. | | 1/4 VREFINT | OK | OK |
  98. | | 1/2 VREFINT | OK | OK |
  99. | | 3/4 VREFINT | OK | OK |
  100. | Inverting Input | VREFINT | OK | OK |
  101. | | DAC1 OUT (PA4) | OK | OK |
  102. | | DAC2 OUT (PA5) | OK | OK |
  103. | | IO1 | PA0 | PA2 |
  104. |-----------------|----------------|-------|-------|
  105. | Non Inverting | | PA1 | PA3 |
  106. | Input | | | |
  107. +--------------------------------------------------+
  108. Table 2. COMP Outputs for the STM32F05x, STM32F07x and STM32F09x devices
  109. +---------------+
  110. | COMP1 | COMP2 |
  111. |-------|-------|
  112. | PA0 | PA2 |
  113. | PA6 | PA7 |
  114. | PA11 | PA12 |
  115. +---------------+
  116. Table 3. COMP Outputs redirection to embedded timers for the STM32F05x, STM32F07x and STM32F09x devices
  117. +---------------------------------+
  118. | COMP1 | COMP2 |
  119. |----------------|----------------|
  120. | TIM1 BKIN | TIM1 BKIN |
  121. | | |
  122. | TIM1 OCREFCLR | TIM1 OCREFCLR |
  123. | | |
  124. | TIM1 IC1 | TIM1 IC1 |
  125. | | |
  126. | TIM2 IC4 | TIM2 IC4 |
  127. | | |
  128. | TIM2 OCREFCLR | TIM2 OCREFCLR |
  129. | | |
  130. | TIM3 IC1 | TIM3 IC1 |
  131. | | |
  132. | TIM3 OCREFCLR | TIM3 OCREFCLR |
  133. +---------------------------------+
  134. */
  135. /* Includes ------------------------------------------------------------------*/
  136. #include "stm32f0xx_hal.h"
  137. #ifdef HAL_COMP_MODULE_ENABLED
  138. #if defined(STM32F051x8) || defined(STM32F058xx) || \
  139. defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || \
  140. defined(STM32F091xC) || defined (STM32F098xx)
  141. /** @addtogroup STM32F0xx_HAL_Driver
  142. * @{
  143. */
  144. /** @defgroup COMP COMP
  145. * @brief COMP HAL module driver
  146. * @{
  147. */
  148. /* Private typedef -----------------------------------------------------------*/
  149. /* Private define ------------------------------------------------------------*/
  150. /** @defgroup COMP_Private_Constants COMP Private Constants
  151. * @{
  152. */
  153. /* Delay for COMP startup time. */
  154. /* Note: Delay required to reach propagation delay specification. */
  155. /* Literal set to maximum value (refer to device datasheet, */
  156. /* parameter "tSTART"). */
  157. /* Unit: us */
  158. #define COMP_DELAY_STARTUP_US (60U) /*!< Delay for COMP startup time */
  159. /* CSR register reset value */
  160. #define COMP_CSR_RESET_VALUE (0x00000000U)
  161. /* CSR register masks */
  162. #define COMP_CSR_RESET_PARAMETERS_MASK (0x00003FFFU)
  163. #define COMP_CSR_UPDATE_PARAMETERS_MASK (0x00003FFEU)
  164. /* CSR COMPx non inverting input mask */
  165. #define COMP_CSR_COMPxNONINSEL_MASK ((uint16_t)COMP_CSR_COMP1SW1)
  166. /* CSR COMP2 shift */
  167. #define COMP_CSR_COMP1_SHIFT 0U
  168. #define COMP_CSR_COMP2_SHIFT 16U
  169. /**
  170. * @}
  171. */
  172. /* Private macro -------------------------------------------------------------*/
  173. /* Private variables ---------------------------------------------------------*/
  174. /* Private function prototypes -----------------------------------------------*/
  175. /* Private functions ---------------------------------------------------------*/
  176. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  177. * @{
  178. */
  179. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  180. * @brief Initialization and Configuration functions
  181. *
  182. @verbatim
  183. ===============================================================================
  184. ##### Initialization and Configuration functions #####
  185. ===============================================================================
  186. [..] This section provides functions to initialize and de-initialize comparators
  187. @endverbatim
  188. * @{
  189. */
  190. /**
  191. * @brief Initializes the COMP according to the specified
  192. * parameters in the COMP_InitTypeDef and create the associated handle.
  193. * @note If the selected comparator is locked, initialization can't be performed.
  194. * To unlock the configuration, perform a system reset.
  195. * @param hcomp COMP handle
  196. * @retval HAL status
  197. */
  198. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  199. {
  200. HAL_StatusTypeDef status = HAL_OK;
  201. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  202. /* Check the COMP handle allocation and lock status */
  203. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  204. {
  205. status = HAL_ERROR;
  206. }
  207. else
  208. {
  209. /* Check the parameter */
  210. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  211. assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
  212. assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
  213. assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
  214. assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  215. assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  216. assert_param(IS_COMP_MODE(hcomp->Init.Mode));
  217. if(hcomp->Init.NonInvertingInput == COMP_NONINVERTINGINPUT_DAC1SWITCHCLOSED)
  218. {
  219. assert_param(IS_COMP_DAC1SWITCH_INSTANCE(hcomp->Instance));
  220. }
  221. if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
  222. {
  223. assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
  224. }
  225. /* Init SYSCFG and the low level hardware to access comparators */
  226. __HAL_RCC_SYSCFG_CLK_ENABLE();
  227. /* Init the low level hardware : SYSCFG to access comparators */
  228. HAL_COMP_MspInit(hcomp);
  229. if(hcomp->State == HAL_COMP_STATE_RESET)
  230. {
  231. /* Allocate lock resource and initialize it */
  232. hcomp->Lock = HAL_UNLOCKED;
  233. }
  234. /* Change COMP peripheral state */
  235. hcomp->State = HAL_COMP_STATE_BUSY;
  236. /* Set COMP parameters */
  237. /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
  238. /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */
  239. /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
  240. /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */
  241. /* Set COMPxMODE bits according to hcomp->Init.Mode value */
  242. if(hcomp->Instance == COMP2)
  243. {
  244. regshift = COMP_CSR_COMP2_SHIFT;
  245. }
  246. MODIFY_REG(COMP->CSR,
  247. (COMP_CSR_COMPxINSEL | COMP_CSR_COMPxNONINSEL_MASK | \
  248. COMP_CSR_COMPxOUTSEL | COMP_CSR_COMPxPOL | \
  249. COMP_CSR_COMPxHYST | COMP_CSR_COMPxMODE) << regshift,
  250. (hcomp->Init.InvertingInput | \
  251. hcomp->Init.NonInvertingInput | \
  252. hcomp->Init.Output | \
  253. hcomp->Init.OutputPol | \
  254. hcomp->Init.Hysteresis | \
  255. hcomp->Init.Mode) << regshift);
  256. if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
  257. {
  258. COMP->CSR |= COMP_CSR_WNDWEN;
  259. }
  260. /* Initialize the COMP state*/
  261. hcomp->State = HAL_COMP_STATE_READY;
  262. }
  263. return status;
  264. }
  265. /**
  266. * @brief DeInitializes the COMP peripheral
  267. * @note Deinitialization can't be performed if the COMP configuration is locked.
  268. * To unlock the configuration, perform a system reset.
  269. * @param hcomp COMP handle
  270. * @retval HAL status
  271. */
  272. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  273. {
  274. HAL_StatusTypeDef status = HAL_OK;
  275. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  276. /* Check the COMP handle allocation and lock status */
  277. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  278. {
  279. status = HAL_ERROR;
  280. }
  281. else
  282. {
  283. /* Check the parameter */
  284. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  285. /* Set COMP_CSR register to reset value for the corresponding COMP instance */
  286. if(hcomp->Instance == COMP2)
  287. {
  288. regshift = COMP_CSR_COMP2_SHIFT;
  289. }
  290. MODIFY_REG(COMP->CSR,
  291. COMP_CSR_RESET_PARAMETERS_MASK << regshift,
  292. COMP_CSR_RESET_VALUE << regshift);
  293. /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  294. HAL_COMP_MspDeInit(hcomp);
  295. hcomp->State = HAL_COMP_STATE_RESET;
  296. /* Release Lock */
  297. __HAL_UNLOCK(hcomp);
  298. }
  299. return status;
  300. }
  301. /**
  302. * @brief Initializes the COMP MSP.
  303. * @param hcomp COMP handle
  304. * @retval None
  305. */
  306. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  307. {
  308. /* Prevent unused argument(s) compilation warning */
  309. UNUSED(hcomp);
  310. /* NOTE : This function Should not be modified, when the callback is needed,
  311. the HAL_COMP_MspInit could be implenetd in the user file
  312. */
  313. }
  314. /**
  315. * @brief DeInitializes COMP MSP.
  316. * @param hcomp COMP handle
  317. * @retval None
  318. */
  319. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  320. {
  321. /* Prevent unused argument(s) compilation warning */
  322. UNUSED(hcomp);
  323. /* NOTE : This function Should not be modified, when the callback is needed,
  324. the HAL_COMP_MspDeInit could be implenetd in the user file
  325. */
  326. }
  327. /**
  328. * @}
  329. */
  330. /** @defgroup COMP_Exported_Functions_Group2 I/O operation functions
  331. * @brief Data transfers functions
  332. *
  333. @verbatim
  334. ===============================================================================
  335. ##### IO operation functions #####
  336. ===============================================================================
  337. [..]
  338. This subsection provides a set of functions allowing to manage the COMP data
  339. transfers.
  340. @endverbatim
  341. * @{
  342. */
  343. /**
  344. * @brief Start the comparator
  345. * @param hcomp COMP handle
  346. * @retval HAL status
  347. */
  348. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  349. {
  350. uint32_t wait_loop_index = 0U;
  351. HAL_StatusTypeDef status = HAL_OK;
  352. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  353. /* Check the COMP handle allocation and lock status */
  354. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  355. {
  356. status = HAL_ERROR;
  357. }
  358. else
  359. {
  360. /* Check the parameter */
  361. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  362. if(hcomp->State == HAL_COMP_STATE_READY)
  363. {
  364. /* Enable the selected comparator */
  365. if(hcomp->Instance == COMP2)
  366. {
  367. regshift = COMP_CSR_COMP2_SHIFT;
  368. }
  369. SET_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift);
  370. /* Set HAL COMP handle state */
  371. hcomp->State = HAL_COMP_STATE_BUSY;
  372. /* Delay for COMP startup time */
  373. wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / 1000000U));
  374. while(wait_loop_index != 0U)
  375. {
  376. wait_loop_index--;
  377. }
  378. }
  379. else
  380. {
  381. status = HAL_ERROR;
  382. }
  383. }
  384. return status;
  385. }
  386. /**
  387. * @brief Stop the comparator
  388. * @param hcomp COMP handle
  389. * @retval HAL status
  390. */
  391. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  392. {
  393. HAL_StatusTypeDef status = HAL_OK;
  394. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  395. /* Check the COMP handle allocation and lock status */
  396. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  397. {
  398. status = HAL_ERROR;
  399. }
  400. else
  401. {
  402. /* Check the parameter */
  403. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  404. if(hcomp->State == HAL_COMP_STATE_BUSY)
  405. {
  406. /* Disable the selected comparator */
  407. if(hcomp->Instance == COMP2)
  408. {
  409. regshift = COMP_CSR_COMP2_SHIFT;
  410. }
  411. CLEAR_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift);
  412. hcomp->State = HAL_COMP_STATE_READY;
  413. }
  414. else
  415. {
  416. status = HAL_ERROR;
  417. }
  418. }
  419. return status;
  420. }
  421. /**
  422. * @brief Enables the interrupt and starts the comparator
  423. * @param hcomp COMP handle
  424. * @retval HAL status.
  425. */
  426. HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
  427. {
  428. HAL_StatusTypeDef status = HAL_OK;
  429. uint32_t extiline = 0U;
  430. /* Check the parameter */
  431. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  432. status = HAL_COMP_Start(hcomp);
  433. if(status == HAL_OK)
  434. {
  435. /* Check the Exti Line output configuration */
  436. extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  437. /* Configure the rising edge */
  438. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
  439. {
  440. SET_BIT(EXTI->RTSR, extiline);
  441. }
  442. else
  443. {
  444. CLEAR_BIT(EXTI->RTSR, extiline);
  445. }
  446. /* Configure the falling edge */
  447. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
  448. {
  449. SET_BIT(EXTI->FTSR, extiline);
  450. }
  451. else
  452. {
  453. CLEAR_BIT(EXTI->FTSR, extiline);
  454. }
  455. /* Clear COMP EXTI pending bit */
  456. WRITE_REG(EXTI->PR, extiline);
  457. /* Enable Exti interrupt mode */
  458. SET_BIT(EXTI->IMR, extiline);
  459. }
  460. return status;
  461. }
  462. /**
  463. * @brief Disable the interrupt and Stop the comparator
  464. * @param hcomp COMP handle
  465. * @retval HAL status
  466. */
  467. HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
  468. {
  469. HAL_StatusTypeDef status = HAL_OK;
  470. /* Disable the Exti Line interrupt mode */
  471. CLEAR_BIT(EXTI->IMR, COMP_GET_EXTI_LINE(hcomp->Instance));
  472. status = HAL_COMP_Stop(hcomp);
  473. return status;
  474. }
  475. /**
  476. * @brief Comparator IRQ Handler
  477. * @param hcomp COMP handle
  478. * @retval HAL status
  479. */
  480. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  481. {
  482. uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  483. /* Check COMP Exti flag */
  484. if(READ_BIT(EXTI->PR, extiline) != RESET)
  485. {
  486. /* Clear COMP Exti pending bit */
  487. WRITE_REG(EXTI->PR, extiline);
  488. /* COMP trigger user callback */
  489. HAL_COMP_TriggerCallback(hcomp);
  490. }
  491. }
  492. /**
  493. * @}
  494. */
  495. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  496. * @brief management functions
  497. *
  498. @verbatim
  499. ===============================================================================
  500. ##### Peripheral Control functions #####
  501. ===============================================================================
  502. [..]
  503. This subsection provides a set of functions allowing to control the COMP data
  504. transfers.
  505. @endverbatim
  506. * @{
  507. */
  508. /**
  509. * @brief Lock the selected comparator configuration.
  510. * @param hcomp COMP handle
  511. * @retval HAL status
  512. */
  513. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  514. {
  515. HAL_StatusTypeDef status = HAL_OK;
  516. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  517. /* Check the COMP handle allocation and lock status */
  518. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  519. {
  520. status = HAL_ERROR;
  521. }
  522. else
  523. {
  524. /* Check the parameter */
  525. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  526. /* Set lock flag */
  527. hcomp->State |= COMP_STATE_BIT_LOCK;
  528. /* Set the lock bit corresponding to selected comparator */
  529. if(hcomp->Instance == COMP2)
  530. {
  531. regshift = COMP_CSR_COMP2_SHIFT;
  532. }
  533. SET_BIT(COMP->CSR, COMP_CSR_COMPxLOCK << regshift);
  534. }
  535. return status;
  536. }
  537. /**
  538. * @brief Return the output level (high or low) of the selected comparator.
  539. * The output level depends on the selected polarity.
  540. * If the polarity is not inverted:
  541. * - Comparator output is low when the non-inverting input is at a lower
  542. * voltage than the inverting input
  543. * - Comparator output is high when the non-inverting input is at a higher
  544. * voltage than the inverting input
  545. * If the polarity is inverted:
  546. * - Comparator output is high when the non-inverting input is at a lower
  547. * voltage than the inverting input
  548. * - Comparator output is low when the non-inverting input is at a higher
  549. * voltage than the inverting input
  550. * @param hcomp COMP handle
  551. * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
  552. *
  553. */
  554. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  555. {
  556. uint32_t level=0;
  557. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  558. /* Check the parameter */
  559. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  560. if(hcomp->Instance == COMP2)
  561. {
  562. regshift = COMP_CSR_COMP2_SHIFT;
  563. }
  564. level = READ_BIT(COMP->CSR, COMP_CSR_COMPxOUT << regshift);
  565. if(level != 0U)
  566. {
  567. return(COMP_OUTPUTLEVEL_HIGH);
  568. }
  569. return(COMP_OUTPUTLEVEL_LOW);
  570. }
  571. /**
  572. * @brief Comparator callback.
  573. * @param hcomp COMP handle
  574. * @retval None
  575. */
  576. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  577. {
  578. /* Prevent unused argument(s) compilation warning */
  579. UNUSED(hcomp);
  580. /* NOTE : This function should not be modified, when the callback is needed,
  581. the HAL_COMP_TriggerCallback should be implemented in the user file
  582. */
  583. }
  584. /**
  585. * @}
  586. */
  587. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  588. * @brief Peripheral State functions
  589. *
  590. @verbatim
  591. ===============================================================================
  592. ##### Peripheral State functions #####
  593. ===============================================================================
  594. [..]
  595. This subsection permit to get in run-time the status of the peripheral
  596. and the data flow.
  597. @endverbatim
  598. * @{
  599. */
  600. /**
  601. * @brief Return the COMP state
  602. * @param hcomp COMP handle
  603. * @retval HAL state
  604. */
  605. uint32_t HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  606. {
  607. /* Check the COMP handle allocation */
  608. if(hcomp == NULL)
  609. {
  610. return HAL_COMP_STATE_RESET;
  611. }
  612. /* Check the parameter */
  613. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  614. return hcomp->State;
  615. }
  616. /**
  617. * @}
  618. */
  619. /**
  620. * @}
  621. */
  622. /**
  623. * @}
  624. */
  625. /**
  626. * @}
  627. */
  628. #endif /* STM32F051x8 || STM32F058xx || */
  629. /* STM32F071xB || STM32F072xB || STM32F078xx || */
  630. /* STM32F091xC || defined (STM32F098xx) */
  631. #endif /* HAL_COMP_MODULE_ENABLED */
  632. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/