stm32f0xx_hal_comp.c 23 KB

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