stm32l1xx_hal_comp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_comp.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief COMP HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the COMP peripheral:
  11. * + Initialization and de-initialization functions
  12. * + I/O operation functions
  13. * + Peripheral Control functions
  14. * + Peripheral State functions
  15. *
  16. @verbatim
  17. ================================================================================
  18. ##### COMP Peripheral features #####
  19. ================================================================================
  20. [..]
  21. The STM32L1xx device family integrates 2 analog comparators COMP1 and
  22. COMP2:
  23. (#) The non inverting input and inverting input can be set to GPIO pins.
  24. Refer to "table1. COMP Inputs" below.
  25. HAL COMP driver configures the Routing Interface (RI) to connect the
  26. selected I/O pins to comparator input.
  27. Caution: Comparator COMP1 and ADC cannot be used at the same time as
  28. ADC since they share the ADC switch matrix: COMP1 non-inverting
  29. input is routed through ADC switch matrix. Except if ADC is intented
  30. to measure voltage on COMP1 non-inverting input: it can be performed
  31. on ADC channel VCOMP.
  32. (#) The COMP output is available using HAL_COMP_GetOutputLevel().
  33. (#) The COMP output can be redirected to embedded timers (TIM2, TIM3,
  34. TIM4, TIM10).
  35. COMP output cannot be redirected to any I/O pin.
  36. Refer to "table 2. COMP Outputs redirection to embedded timers" below.
  37. (#) The comparators COMP1 and COMP2 can be combined in window mode.
  38. In this mode, COMP2 non inverting input is used as common
  39. non-inverting input.
  40. (#) The 2 comparators have interrupt capability with wake-up
  41. from Sleep and Stop modes (through the EXTI controller):
  42. (++) COMP1 is internally connected to EXTI Line 21
  43. (++) COMP2 is internally connected to EXTI Line 22
  44. From the corresponding IRQ handler, the right interrupt source can be
  45. retrieved with macro __HAL_COMP_EXTI_GET_FLAG(). Possible values are:
  46. (++) COMP_EXTI_LINE_COMP1_EVENT
  47. (++) COMP_EXTI_LINE_COMP2_EVENT
  48. (#) The comparators also offer the possibility to ouput the voltage
  49. reference (VrefInt), used on inverting inputs, on I/O pin through
  50. a buffer. To use it, refer to macro "__HAL_VREFINT_OUT_ENABLE()".
  51. [..] Table 1. COMP Inputs for the STM32L1xx devices
  52. +----------------------------------------------------------------------+
  53. | | | COMP1 | COMP2 |
  54. |-----------------|--------------------------------|---------|---------|
  55. | | 1/4 VREFINT | -- | OK |
  56. | | 1/2 VREFINT | -- | OK |
  57. | | 3/4 VREFINT | -- | OK |
  58. | Inverting | VREFINT | OK | OK |
  59. | input | DAC Ch1 OUT (PA4) | -- | OK |
  60. | | DAC Ch2 OUT (PA5) | -- | OK |
  61. | | IO: PB3 | -- | OK |
  62. |-----------------|--------------------------------|---------|---------|
  63. | | IO: | | |
  64. | | PB4, 5, 6*, 7* | --- | OK |
  65. | Non-inverting | PA0*, 1*, 2*, 3*, 4, 5, 6, 7 | OK | --- |
  66. | input | PB0, 1, 12, 13, 14, 15 | OK | --- |
  67. | | PC0, 1, 2, 3, 4, 5 | OK | --- |
  68. | | PE7, 8, 9, 10 | OK | --- |
  69. | | PF6, 7, 8, 9, 10 | OK | --- |
  70. | | OPAMP1 output | OK | --- |
  71. | | OPAMP2 output | OK | --- |
  72. | | OPAMP3 output** | OK | --- |
  73. +----------------------------------------------------------------------+
  74. *: Available on devices category Cat.3, Cat.4, Cat.5 only.
  75. **: Available on devices category Cat.4 only.
  76. [..] Table 2. COMP Outputs redirection to embedded timers
  77. +-----------------------------------+
  78. | COMP1 | COMP2 |
  79. |-----------------|-----------------|
  80. | | TIM2 IC4 |
  81. | | TIM2 OCREF CLR |
  82. | (no redirection | TIM3 IC4 |
  83. | to timers) | TIM3 OCREF CLR |
  84. | | TIM4 IC4 |
  85. | | TIM4 OCREF CLR |
  86. | | TIM10 IC1 |
  87. +-----------------------------------+
  88. ##### How to use this driver #####
  89. ================================================================================
  90. [..]
  91. This driver provides functions to configure and program the Comparators of all STM32L1xx devices.
  92. To use the comparator, perform the following steps:
  93. (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit().
  94. (++) Configure the comparator input I/O pin using HAL_GPIO_Init():
  95. - For all inputs: I/O pin in analog mode (Schmitt trigger disabled)
  96. - Possible alternate configuration, for non-inverting inputs of comparator 2: I/O pin in floating mode (Schmitt trigger enabled).
  97. It is recommended to use analog configuration to avoid any overconsumption around VDD/2.
  98. (++) Enable COMP Peripheral clock using macro __COMP_CLK_ENABLE()
  99. (++) If required enable the COMP interrupt (EXTI line Interrupt): enable
  100. the comparator interrupt vector using HAL_NVIC_EnableIRQ(COMP_IRQn)
  101. and HAL_NVIC_SetPriority(COMP_IRQn, xxx, xxx) functions.
  102. (#) Configure the comparator using HAL_COMP_Init() function:
  103. (++) Select the inverting input (COMP2 only)
  104. (++) Select the non-inverting input
  105. (++) Select the output redirection to timers (COMP2 only)
  106. (++) Select the speed mode (COMP2 only)
  107. (++) Select the window mode (related to COMP1 and COMP2, but selected
  108. by COMP2 only)
  109. (++) Select the pull-up/down resistors on non-inverting input (COMP1 only)
  110. (#) Enable the comparator using HAL_COMP_Start() or HAL_COMP_Start_IT()
  111. function
  112. (#) If needed, use HAL_COMP_GetOutputLevel() or HAL_COMP_TriggerCallback()
  113. functions to manage comparator actions (output level or events)
  114. (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
  115. function
  116. (#) De-initialize the comparator using HAL_COMP_DeInit() function
  117. @endverbatim
  118. ******************************************************************************
  119. * @attention
  120. *
  121. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  122. *
  123. * Redistribution and use in source and binary forms, with or without modification,
  124. * are permitted provided that the following conditions are met:
  125. * 1. Redistributions of source code must retain the above copyright notice,
  126. * this list of conditions and the following disclaimer.
  127. * 2. Redistributions in binary form must reproduce the above copyright notice,
  128. * this list of conditions and the following disclaimer in the documentation
  129. * and/or other materials provided with the distribution.
  130. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  131. * may be used to endorse or promote products derived from this software
  132. * without specific prior written permission.
  133. *
  134. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  135. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  136. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  137. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  138. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  139. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  140. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  141. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  142. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  143. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  144. *
  145. ******************************************************************************
  146. */
  147. /* Includes ------------------------------------------------------------------*/
  148. #include "stm32l1xx_hal.h"
  149. /** @addtogroup STM32L1xx_HAL_Driver
  150. * @{
  151. */
  152. /** @defgroup COMP COMP
  153. * @brief COMP HAL module driver
  154. * @{
  155. */
  156. #ifdef HAL_COMP_MODULE_ENABLED
  157. /* Private typedef -----------------------------------------------------------*/
  158. /* Private define ------------------------------------------------------------*/
  159. /** @defgroup COMP_Private_Constants COMP Private Constants
  160. * @{
  161. */
  162. /* Delay for COMP startup time. */
  163. /* Maximum delay is 10us for comparator 1 and 25us for comparator 2 in slow */
  164. /* mode (refer to device datasheet, parameter tSTART). */
  165. /* Delay in CPU cycles, fixed to worst case: maximum CPU frequency 32MHz to */
  166. /* have the minimum number of CPU cycles to fulfill this delay. */
  167. /* - Comparator 1: delay minimum of 320 CPU cyles. Wait loop takes 3 CPU */
  168. /* cycles per iteration, therefore total wait iterations */
  169. /* number must be initialized at 106 iterations. */
  170. /* - Comparator 2: delay minimum of 800 CPU cyles. Wait loop takes 3 CPU */
  171. /* cycles per iteration, therefore total wait iterations */
  172. /* number must be initialized at 266 iterations. */
  173. #define COMP1_START_DELAY_CPU_CYCLES ((uint32_t)106)
  174. #define COMP2_START_DELAY_CPU_CYCLES ((uint32_t)266)
  175. /* Comparator status "locked": to update COMP handle state (software lock */
  176. /* only on COMP of STM32L1xx devices) by bitfield: */
  177. /* states HAL_COMP_STATE_READY_LOCKED, HAL_COMP_STATE_BUSY_LOCKED. */
  178. #define COMP_STATE_BIT_LOCK ((uint32_t) 0x00000010)
  179. /**
  180. * @}
  181. */
  182. /* Private macro -------------------------------------------------------------*/
  183. /* Private variables ---------------------------------------------------------*/
  184. /* Private function prototypes -----------------------------------------------*/
  185. /* Private functions ---------------------------------------------------------*/
  186. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  187. * @{
  188. */
  189. /** @defgroup COMP_Exported_Functions_Group1 Initialization and de-initialization functions
  190. * @brief Initialization and Configuration functions
  191. *
  192. @verbatim
  193. ===============================================================================
  194. ##### Initialization and de-initialization functions #####
  195. ===============================================================================
  196. [..] This section provides functions to initialize and de-initialize comparators
  197. @endverbatim
  198. * @{
  199. */
  200. /**
  201. * @brief Initializes the COMP according to the specified
  202. * parameters in the COMP_InitTypeDef and create the associated handle.
  203. * @note If the selected comparator is locked, initialization can't be performed.
  204. * To unlock the configuration, perform a system reset.
  205. * @param hcomp: COMP handle
  206. * @retval HAL status
  207. */
  208. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  209. {
  210. HAL_StatusTypeDef status = HAL_OK;
  211. /* Check the COMP handle allocation and lock status */
  212. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  213. {
  214. status = HAL_ERROR;
  215. }
  216. else
  217. {
  218. /* Check the parameter */
  219. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  220. if (hcomp->Instance == COMP1)
  221. {
  222. assert_param(IS_COMP_NONINVERTINGINPUTPULL(hcomp->Init.NonInvertingInputPull));
  223. }
  224. else /* if (hcomp->Instance == COMP2) */
  225. {
  226. assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
  227. assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
  228. assert_param(IS_COMP_MODE(hcomp->Init.Mode));
  229. assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
  230. }
  231. /* In window mode, non-inverting inputs of the 2 comparators are */
  232. /* connected together and are using inputs of COMP2 only. If COMP1 is */
  233. /* selected, this parameter is discarded. */
  234. if ((hcomp->Init.WindowMode == COMP_WINDOWMODE_DISABLED) ||
  235. (hcomp->Instance == COMP2) )
  236. {
  237. assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
  238. }
  239. /* Enable SYSCFG clock and the low level hardware to access comparators */
  240. if(hcomp->State == HAL_COMP_STATE_RESET)
  241. {
  242. /* Enable SYSCFG clock to control the routing Interface (RI) */
  243. __SYSCFG_CLK_ENABLE();
  244. /* Init the low level hardware */
  245. HAL_COMP_MspInit(hcomp);
  246. }
  247. /* Configuration of comparator: */
  248. /* - Output selection */
  249. /* - Inverting input selection */
  250. /* - Window mode */
  251. /* - Mode fast/slow speed */
  252. /* - Inverting input pull-up/down resistors */
  253. /* Configuration depending on comparator instance */
  254. if (hcomp->Instance == COMP1)
  255. {
  256. MODIFY_REG(COMP->CSR, COMP_CSR_400KPD | COMP_CSR_10KPD | COMP_CSR_400KPU | COMP_CSR_10KPU,
  257. hcomp->Init.NonInvertingInputPull );
  258. }
  259. else /* if (hcomp->Instance == COMP2) */
  260. {
  261. /* Note: If comparator 2 is not enabled, inverting input (parameter */
  262. /* "hcomp->Init.InvertingInput") is configured into function */
  263. /* "HAL_COMP_Start()" since inverting input selection also */
  264. /* enables the comparator 2. */
  265. /* If comparator 2 is already enabled, inverting input is */
  266. /* reconfigured on the fly. */
  267. if (__COMP_IS_ENABLED(hcomp) == RESET)
  268. {
  269. MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL |
  270. COMP_CSR_WNDWE |
  271. COMP_CSR_SPEED ,
  272. hcomp->Init.Output |
  273. hcomp->Init.WindowMode |
  274. hcomp->Init.Mode );
  275. }
  276. else
  277. {
  278. MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL |
  279. COMP_CSR_INSEL |
  280. COMP_CSR_WNDWE |
  281. COMP_CSR_SPEED ,
  282. hcomp->Init.Output |
  283. hcomp->Init.InvertingInput |
  284. hcomp->Init.WindowMode |
  285. hcomp->Init.Mode );
  286. }
  287. }
  288. /* Configure Routing Interface (RI) switches for comparator non-inverting */
  289. /* input. */
  290. /* Except in 2 cases: */
  291. /* - if non-inverting input has no selection: it can be the case for */
  292. /* COMP1 in window mode. */
  293. /* - particular case for PC3: if switch COMP1_SW1 is closed */
  294. /* (by macro "__HAL_OPAMP_OPAMP3OUT_CONNECT_ADC_COMP1()" or */
  295. /* "__HAL_RI_SWITCH_COMP1_SW1_CLOSE()"), connection between pin PC3 */
  296. /* (or OPAMP3, if available) and COMP1 is done directly, without going */
  297. /* through ADC switch matrix. */
  298. if (__COMP_ROUTING_INTERFACE_TOBECONFIGURED(hcomp))
  299. {
  300. if (hcomp->Instance == COMP1)
  301. {
  302. /* Enable the switch control mode */
  303. __HAL_RI_SWITCHCONTROLMODE_ENABLE();
  304. /* Close the analog switch of ADC switch matrix to COMP1 (ADC */
  305. /* channel 26: Vcomp) */
  306. __HAL_RI_IOSWITCH_CLOSE(RI_IOSWITCH_VCOMP);
  307. }
  308. /* Close the I/O analog switch corresponding to comparator */
  309. /* non-inverting input selected. */
  310. __HAL_RI_IOSWITCH_CLOSE(hcomp->Init.NonInvertingInput);
  311. }
  312. /* Initialize the COMP state*/
  313. if(hcomp->State == HAL_COMP_STATE_RESET)
  314. {
  315. hcomp->State = HAL_COMP_STATE_READY;
  316. }
  317. }
  318. return status;
  319. }
  320. /**
  321. * @brief DeInitializes the COMP peripheral
  322. * @note Deinitialization can't be performed if the COMP configuration is locked.
  323. * To unlock the configuration, perform a system reset.
  324. * @param hcomp: COMP handle
  325. * @retval HAL status
  326. */
  327. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  328. {
  329. HAL_StatusTypeDef status = HAL_OK;
  330. /* Check the COMP handle allocation and lock status */
  331. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  332. {
  333. status = HAL_ERROR;
  334. }
  335. else
  336. {
  337. /* Check the parameter */
  338. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  339. /* Reset configuration depending on comparator instance */
  340. if (hcomp->Instance == COMP1)
  341. {
  342. CLEAR_BIT(COMP->CSR , COMP_CSR_400KPD | COMP_CSR_10KPD | COMP_CSR_400KPU | COMP_CSR_10KPU);
  343. }
  344. else /* if (hcomp->Instance == COMP2) */
  345. {
  346. CLEAR_BIT(COMP->CSR , COMP_CSR_OUTSEL |
  347. COMP_CSR_WNDWE |
  348. COMP_CSR_INSEL |
  349. COMP_CSR_SPEED );
  350. }
  351. /* Restore default state of Routing Interface (RI) switches for */
  352. /* comparator non-inverting input. */
  353. if (hcomp->Init.NonInvertingInput != COMP_NONINVERTINGINPUT_NONE)
  354. {
  355. /* Open the I/O analog switch corresponding to comparator */
  356. /* non-inverting input selected. */
  357. __HAL_RI_IOSWITCH_OPEN(hcomp->Init.NonInvertingInput);
  358. }
  359. if (hcomp->Instance == COMP1)
  360. {
  361. /* Open the analog switch of ADC switch matrix to COMP1 (ADC */
  362. /* channel 26: Vcomp) */
  363. __HAL_RI_IOSWITCH_OPEN(RI_IOSWITCH_VCOMP);
  364. /* Disable the switch control mode */
  365. __HAL_RI_SWITCHCONTROLMODE_DISABLE();
  366. }
  367. /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  368. HAL_COMP_MspDeInit(hcomp);
  369. hcomp->State = HAL_COMP_STATE_RESET;
  370. /* Process unlocked */
  371. __HAL_UNLOCK(hcomp);
  372. }
  373. return status;
  374. }
  375. /**
  376. * @brief Initializes the COMP MSP.
  377. * @param hcomp: COMP handle
  378. * @retval None
  379. */
  380. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  381. {
  382. /* NOTE : This function Should not be modified, when the callback is needed,
  383. the HAL_COMP_MspInit could be implenetd in the user file
  384. */
  385. }
  386. /**
  387. * @brief DeInitializes COMP MSP.
  388. * @param hcomp: COMP handle
  389. * @retval None
  390. */
  391. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  392. {
  393. /* NOTE : This function Should not be modified, when the callback is needed,
  394. the HAL_COMP_MspDeInit could be implenetd in the user file
  395. */
  396. }
  397. /**
  398. * @}
  399. */
  400. /** @defgroup COMP_Exported_Functions_Group2 I/O operation functions
  401. * @brief I/O operation functions
  402. *
  403. @verbatim
  404. ===============================================================================
  405. ##### IO operation functions #####
  406. ===============================================================================
  407. [..]
  408. This subsection provides a set of functions allowing to manage the COMP
  409. start and stop actions with or without interruption on ExtI line.
  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. HAL_StatusTypeDef status = HAL_OK;
  421. uint32_t wait_loop_cycles = 0;
  422. __IO uint32_t wait_loop_index = 0;
  423. /* Check the COMP handle allocation and lock status */
  424. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  425. {
  426. status = HAL_ERROR;
  427. }
  428. else
  429. {
  430. /* Check the parameter */
  431. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  432. if(hcomp->State == HAL_COMP_STATE_READY)
  433. {
  434. /* Note: For comparator 2, inverting input (parameter */
  435. /* "hcomp->Init.InvertingInput") is configured into this */
  436. /* function instead of function "HAL_COMP_Init()" since */
  437. /* inverting input selection also enables the comparator 2. */
  438. __HAL_COMP_ENABLE(hcomp);
  439. /* Set delay for COMP startup time */
  440. if (hcomp->Instance == COMP1)
  441. {
  442. wait_loop_cycles = COMP1_START_DELAY_CPU_CYCLES;
  443. }
  444. else /* if (hcomp->Instance == COMP2) */
  445. {
  446. wait_loop_cycles = COMP2_START_DELAY_CPU_CYCLES;
  447. }
  448. /* Delay for COMP startup time. */
  449. /* Delay fixed to worst case: maximum CPU frequency */
  450. while(wait_loop_index < wait_loop_cycles)
  451. {
  452. wait_loop_index++;
  453. }
  454. /* Update COMP state */
  455. hcomp->State = HAL_COMP_STATE_BUSY;
  456. }
  457. else
  458. {
  459. status = HAL_ERROR;
  460. }
  461. }
  462. return status;
  463. }
  464. /**
  465. * @brief Stop the comparator
  466. * @param hcomp: COMP handle
  467. * @retval HAL status
  468. */
  469. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  470. {
  471. HAL_StatusTypeDef status = HAL_OK;
  472. /* Check the COMP handle allocation and lock status */
  473. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  474. {
  475. status = HAL_ERROR;
  476. }
  477. else
  478. {
  479. /* Check the parameter */
  480. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  481. if(hcomp->State == HAL_COMP_STATE_BUSY)
  482. {
  483. /* Disable the selected comparator */
  484. __HAL_COMP_DISABLE(hcomp);
  485. /* Update COMP state */
  486. hcomp->State = HAL_COMP_STATE_READY;
  487. }
  488. else
  489. {
  490. status = HAL_ERROR;
  491. }
  492. }
  493. return status;
  494. }
  495. /**
  496. * @brief Enables the interrupt and starts the comparator
  497. * @param hcomp: COMP handle
  498. * @retval HAL status.
  499. */
  500. HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
  501. {
  502. HAL_StatusTypeDef status = HAL_OK;
  503. uint32_t extiline = 0;
  504. status = HAL_COMP_Start(hcomp);
  505. if(status == HAL_OK)
  506. {
  507. /* Check the parameter */
  508. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  509. /* Get the Exti Line output configuration */
  510. extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
  511. /* Configure the rising edge */
  512. /* COMP TriggerMode set to COMP_TRIGGERMODE_IT_RISING or */
  513. /* COMP_TRIGGERMODE_IT_RISING_FALLING. */
  514. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
  515. {
  516. __HAL_COMP_EXTI_RISING_IT_ENABLE(extiline);
  517. }
  518. else
  519. {
  520. __HAL_COMP_EXTI_RISING_IT_DISABLE(extiline);
  521. }
  522. /* Configure the falling edge */
  523. /* COMP TriggerMode set to COMP_TRIGGERMODE_IT_FALLING or */
  524. /* COMP_TRIGGERMODE_IT_RISING_FALLING. */
  525. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
  526. {
  527. __HAL_COMP_EXTI_FALLING_IT_ENABLE(extiline);
  528. }
  529. else
  530. {
  531. __HAL_COMP_EXTI_FALLING_IT_DISABLE(extiline);
  532. }
  533. /* Enable Exti interrupt mode */
  534. __HAL_COMP_EXTI_ENABLE_IT(extiline);
  535. /* Clear COMP Exti pending bit */
  536. __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
  537. }
  538. return status;
  539. }
  540. /**
  541. * @brief Disable the interrupt and Stop the comparator
  542. * @param hcomp: COMP handle
  543. * @retval HAL status
  544. */
  545. HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
  546. {
  547. HAL_StatusTypeDef status = HAL_OK;
  548. /* Disable the Exti Line interrupt mode */
  549. __HAL_COMP_EXTI_DISABLE_IT(__HAL_COMP_GET_EXTI_LINE(hcomp->Instance));
  550. status = HAL_COMP_Stop(hcomp);
  551. return status;
  552. }
  553. /**
  554. * @brief Comparator IRQ Handler
  555. * @param hcomp: COMP handle
  556. * @retval HAL status
  557. */
  558. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  559. {
  560. uint32_t extiline = __HAL_COMP_GET_EXTI_LINE(hcomp->Instance);
  561. /* Check COMP Exti flag */
  562. if(__HAL_COMP_EXTI_GET_FLAG(extiline) != RESET)
  563. {
  564. /* Clear COMP Exti pending bit */
  565. __HAL_COMP_EXTI_CLEAR_FLAG(extiline);
  566. /* COMP trigger user callback */
  567. HAL_COMP_TriggerCallback(hcomp);
  568. }
  569. }
  570. /**
  571. * @}
  572. */
  573. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  574. * @brief Peripheral Control functions
  575. *
  576. @verbatim
  577. ===============================================================================
  578. ##### Peripheral Control functions #####
  579. ===============================================================================
  580. [..]
  581. This subsection provides a set of functions allowing to control the COMP
  582. management functions: Lock status, comparator output level check, IRQ
  583. callback (in case of usage of comparator with interruption on ExtI line).
  584. @endverbatim
  585. * @{
  586. */
  587. /**
  588. * @brief Lock the selected comparator configuration.
  589. * Caution: On STM32L1, HAL COMP lock is software lock only (not
  590. * hardware lock as on some other STM32 devices)
  591. * @param hcomp: COMP handle
  592. * @retval HAL status
  593. */
  594. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  595. {
  596. HAL_StatusTypeDef status = HAL_OK;
  597. /* Check the COMP handle allocation and lock status */
  598. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  599. {
  600. status = HAL_ERROR;
  601. }
  602. else
  603. {
  604. /* Check the parameter */
  605. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  606. /* Set lock flag */
  607. hcomp->State |= COMP_STATE_BIT_LOCK;
  608. }
  609. return status;
  610. }
  611. /**
  612. * @brief Return the output level (high or low) of the selected comparator.
  613. * The output level depends on the selected polarity.
  614. * - Comparator output is low when the non-inverting input is at a lower
  615. * voltage than the inverting input
  616. * - Comparator output is high when the non-inverting input is at a higher
  617. * voltage than the inverting input
  618. * @param hcomp: COMP handle
  619. * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
  620. *
  621. */
  622. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  623. {
  624. uint32_t level = 0;
  625. /* Check the parameter */
  626. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  627. /* Read output level of the selected comparator */
  628. if(READ_BIT(COMP->CSR, __COMP_CSR_CMPXOUT(hcomp)) == RESET)
  629. {
  630. level = COMP_OUTPUTLEVEL_LOW;
  631. }
  632. else
  633. {
  634. level = COMP_OUTPUTLEVEL_HIGH;
  635. }
  636. return(level);
  637. }
  638. /**
  639. * @brief Comparator callback.
  640. * @param hcomp: COMP handle
  641. * @retval None
  642. */
  643. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  644. {
  645. /* NOTE : This function should not be modified, when the callback is needed,
  646. the HAL_COMP_TriggerCallback should be implemented in the user file
  647. */
  648. }
  649. /**
  650. * @}
  651. */
  652. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  653. * @brief Peripheral State functions
  654. *
  655. @verbatim
  656. ===============================================================================
  657. ##### Peripheral State functions #####
  658. ===============================================================================
  659. [..]
  660. This subsection permit to get in run-time the status of the peripheral.
  661. @endverbatim
  662. * @{
  663. */
  664. /**
  665. * @brief Return the COMP state
  666. * @param hcomp : COMP handle
  667. * @retval HAL state
  668. */
  669. HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  670. {
  671. /* Check the COMP handle allocation */
  672. if(hcomp == NULL)
  673. {
  674. return HAL_COMP_STATE_RESET;
  675. }
  676. /* Check the parameter */
  677. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  678. return hcomp->State;
  679. }
  680. /**
  681. * @}
  682. */
  683. /**
  684. * @}
  685. */
  686. #endif /* HAL_COMP_MODULE_ENABLED */
  687. /**
  688. * @}
  689. */
  690. /**
  691. * @}
  692. */
  693. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/