stm32l1xx_hal_comp.c 28 KB

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