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.

stm32l1xx_hal_comp.c 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. *** Callback registration ***
  74. =============================================
  75. [..]
  76. The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
  77. allows the user to configure dynamically the driver callbacks.
  78. Use Functions @ref HAL_COMP_RegisterCallback()
  79. to register an interrupt callback.
  80. [..]
  81. Function @ref HAL_COMP_RegisterCallback() allows to register following callbacks:
  82. (+) TriggerCallback : callback for COMP trigger.
  83. (+) MspInitCallback : callback for Msp Init.
  84. (+) MspDeInitCallback : callback for Msp DeInit.
  85. This function takes as parameters the HAL peripheral handle, the Callback ID
  86. and a pointer to the user callback function.
  87. [..]
  88. Use function @ref HAL_COMP_UnRegisterCallback to reset a callback to the default
  89. weak function.
  90. [..]
  91. @ref HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
  92. and the Callback ID.
  93. This function allows to reset following callbacks:
  94. (+) TriggerCallback : callback for COMP trigger.
  95. (+) MspInitCallback : callback for Msp Init.
  96. (+) MspDeInitCallback : callback for Msp DeInit.
  97. [..]
  98. By default, after the @ref HAL_COMP_Init() and when the state is @ref HAL_COMP_STATE_RESET
  99. all callbacks are set to the corresponding weak functions:
  100. example @ref HAL_COMP_TriggerCallback().
  101. Exception done for MspInit and MspDeInit functions that are
  102. reset to the legacy weak functions in the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit() only when
  103. these callbacks are null (not registered beforehand).
  104. [..]
  105. If MspInit or MspDeInit are not null, the @ref HAL_COMP_Init()/ @ref HAL_COMP_DeInit()
  106. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  107. [..]
  108. Callbacks can be registered/unregistered in @ref HAL_COMP_STATE_READY state only.
  109. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  110. in @ref HAL_COMP_STATE_READY or @ref HAL_COMP_STATE_RESET state,
  111. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  112. [..]
  113. Then, the user first registers the MspInit/MspDeInit user callbacks
  114. using @ref HAL_COMP_RegisterCallback() before calling @ref HAL_COMP_DeInit()
  115. or @ref HAL_COMP_Init() function.
  116. [..]
  117. When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
  118. not defined, the callback registration feature is not available and all callbacks
  119. are set to the corresponding weak functions.
  120. @endverbatim
  121. ******************************************************************************
  122. * @attention
  123. *
  124. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  125. * All rights reserved.</center></h2>
  126. *
  127. * This software component is licensed by ST under BSD 3-Clause license,
  128. * the "License"; You may not use this file except in compliance with the
  129. * License. You may obtain a copy of the License at:
  130. * opensource.org/licenses/BSD-3-Clause
  131. *
  132. ******************************************************************************
  133. */
  134. /*
  135. Additionnal remark:
  136. Table 1. COMP Inputs for the STM32L1xx devices
  137. +----------------------------------------------------------------------+
  138. | | | COMP1 | COMP2 |
  139. |-----------------|--------------------------------|---------|---------|
  140. | | 1/4 VREFINT | -- | OK |
  141. | | 1/2 VREFINT | -- | OK |
  142. | | 3/4 VREFINT | -- | OK |
  143. | Inverting | VREFINT | OK | OK |
  144. | input | DAC Ch1 OUT (PA4) | -- | OK |
  145. | | DAC Ch2 OUT (PA5) | -- | OK |
  146. | | IO: PB3 | -- | OK |
  147. |-----------------|--------------------------------|---------|---------|
  148. | | IO: | | |
  149. | | PB4, 5, 6*, 7* | --- | OK |
  150. | Non-inverting | PA0*, 1*, 2*, 3*, 4, 5, 6, 7 | OK | --- |
  151. | input | PB0, 1, 12, 13, 14, 15 | OK | --- |
  152. | | PC0, 1, 2, 3, 4, 5 | OK | --- |
  153. | | PE7, 8, 9, 10 | OK | --- |
  154. | | PF6, 7, 8, 9, 10 | OK | --- |
  155. | | OPAMP1 output | OK | --- |
  156. | | OPAMP2 output | OK | --- |
  157. | | OPAMP3 output** | OK | --- |
  158. +----------------------------------------------------------------------+
  159. *: Available on devices category Cat.3, Cat.4, Cat.5 only.
  160. **: Available on devices category Cat.4 only.
  161. [..] Table 2. COMP Outputs redirection to embedded timers
  162. +-----------------------------------+
  163. | COMP1 | COMP2 |
  164. |-----------------|-----------------|
  165. | | TIM2 IC4 |
  166. | | TIM2 OCREF CLR |
  167. | (no redirection | TIM3 IC4 |
  168. | to timers) | TIM3 OCREF CLR |
  169. | | TIM4 IC4 |
  170. | | TIM4 OCREF CLR |
  171. | | TIM10 IC1 |
  172. +-----------------------------------+
  173. */
  174. /* Includes ------------------------------------------------------------------*/
  175. #include "stm32l1xx_hal.h"
  176. /** @addtogroup STM32L1xx_HAL_Driver
  177. * @{
  178. */
  179. /** @defgroup COMP COMP
  180. * @brief COMP HAL module driver
  181. * @{
  182. */
  183. #ifdef HAL_COMP_MODULE_ENABLED
  184. /* Private typedef -----------------------------------------------------------*/
  185. /* Private define ------------------------------------------------------------*/
  186. /** @defgroup COMP_Private_Constants COMP Private Constants
  187. * @{
  188. */
  189. /* Delay for COMP start-up time. */
  190. /* Maximum delay is 10us for comparator 1 and 25us for comparator 2 in slow */
  191. /* mode (refer to device datasheet, parameter tSTART). */
  192. /* Delay in CPU cycles, fixed to worst case: maximum CPU frequency 32MHz to */
  193. /* have the minimum number of CPU cycles to fulfill this delay. */
  194. /* - Comparator 1: delay minimum of 320 CPU cycles. Wait loop takes 3 CPU */
  195. /* cycles per iteration, therefore total wait iterations */
  196. /* number must be initialized at 106 iterations. */
  197. /* - Comparator 2: delay minimum of 800 CPU cycles. Wait loop takes 3 CPU */
  198. /* cycles per iteration, therefore total wait iterations */
  199. /* number must be initialized at 266 iterations. */
  200. #define COMP1_START_DELAY_CPU_CYCLES (106U)
  201. #define COMP2_START_DELAY_CPU_CYCLES (266U)
  202. /* Comparator status "locked": to update COMP handle state (software lock */
  203. /* only on COMP of STM32L1xx devices) by bitfield: */
  204. /* states HAL_COMP_STATE_READY_LOCKED, HAL_COMP_STATE_BUSY_LOCKED. */
  205. #define COMP_STATE_BIT_LOCK (0x00000010U)
  206. /**
  207. * @}
  208. */
  209. /* Private macro -------------------------------------------------------------*/
  210. /* Private variables ---------------------------------------------------------*/
  211. /* Private function prototypes -----------------------------------------------*/
  212. /* Private functions ---------------------------------------------------------*/
  213. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  214. * @{
  215. */
  216. /** @defgroup COMP_Exported_Functions_Group1 Initialization and de-initialization functions
  217. * @brief Initialization and Configuration functions
  218. *
  219. @verbatim
  220. ===============================================================================
  221. ##### Initialization and de-initialization functions #####
  222. ===============================================================================
  223. [..] This section provides functions to initialize and de-initialize comparators
  224. @endverbatim
  225. * @{
  226. */
  227. /**
  228. * @brief Initializes the COMP according to the specified
  229. * parameters in the COMP_InitTypeDef and create the associated handle.
  230. * @note If the selected comparator is locked, initialization can't be performed.
  231. * To unlock the configuration, perform a system reset.
  232. * @param hcomp COMP handle
  233. * @retval HAL status
  234. */
  235. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  236. {
  237. HAL_StatusTypeDef status = HAL_OK;
  238. /* Check the COMP handle allocation and lock status */
  239. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  240. {
  241. status = HAL_ERROR;
  242. }
  243. else
  244. {
  245. /* Check the parameter */
  246. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  247. if (hcomp->Instance == COMP1)
  248. {
  249. assert_param(IS_COMP_NONINVERTINGINPUTPULL(hcomp->Init.NonInvertingInputPull));
  250. }
  251. else /* if (hcomp->Instance == COMP2) */
  252. {
  253. assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
  254. assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
  255. assert_param(IS_COMP_MODE(hcomp->Init.Mode));
  256. assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
  257. }
  258. /* In window mode, non-inverting inputs of the 2 comparators are */
  259. /* connected together and are using inputs of COMP2 only. If COMP1 is */
  260. /* selected, this parameter is discarded. */
  261. if ((hcomp->Init.WindowMode == COMP_WINDOWMODE_DISABLE) ||
  262. (hcomp->Instance == COMP2) )
  263. {
  264. assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
  265. }
  266. /* Enable SYSCFG clock and the low level hardware to access comparators */
  267. if(hcomp->State == HAL_COMP_STATE_RESET)
  268. {
  269. /* Allocate lock resource and initialize it */
  270. hcomp->Lock = HAL_UNLOCKED;
  271. /* Enable SYSCFG clock to control the routing Interface (RI) */
  272. __HAL_RCC_SYSCFG_CLK_ENABLE();
  273. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  274. /* Init the COMP Callback settings */
  275. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  276. if (hcomp->MspInitCallback == NULL)
  277. {
  278. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  279. }
  280. /* Init the low level hardware */
  281. hcomp->MspInitCallback(hcomp);
  282. #else
  283. /* Init the low level hardware */
  284. HAL_COMP_MspInit(hcomp);
  285. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  286. }
  287. /* Configuration of comparator: */
  288. /* - Output selection */
  289. /* - Inverting input selection */
  290. /* - Window mode */
  291. /* - Mode fast/slow speed */
  292. /* - Inverting input pull-up/down resistors */
  293. /* Configuration depending on comparator instance */
  294. if (hcomp->Instance == COMP1)
  295. {
  296. MODIFY_REG(COMP->CSR, COMP_CSR_400KPD | COMP_CSR_10KPD | COMP_CSR_400KPU | COMP_CSR_10KPU,
  297. hcomp->Init.NonInvertingInputPull );
  298. }
  299. else /* if (hcomp->Instance == COMP2) */
  300. {
  301. /* Note: If comparator 2 is not enabled, inverting input (parameter */
  302. /* "hcomp->Init.InvertingInput") is configured into function */
  303. /* "HAL_COMP_Start()" since inverting input selection also */
  304. /* enables the comparator 2. */
  305. /* If comparator 2 is already enabled, inverting input is */
  306. /* reconfigured on the fly. */
  307. if (__COMP_IS_ENABLED(hcomp) == RESET)
  308. {
  309. MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL |
  310. COMP_CSR_WNDWE |
  311. COMP_CSR_SPEED ,
  312. hcomp->Init.Output |
  313. hcomp->Init.WindowMode |
  314. hcomp->Init.Mode );
  315. }
  316. else
  317. {
  318. MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL |
  319. COMP_CSR_INSEL |
  320. COMP_CSR_WNDWE |
  321. COMP_CSR_SPEED ,
  322. hcomp->Init.Output |
  323. hcomp->Init.InvertingInput |
  324. hcomp->Init.WindowMode |
  325. hcomp->Init.Mode );
  326. }
  327. }
  328. /* Configure Routing Interface (RI) switches for comparator non-inverting */
  329. /* input. */
  330. /* Except in 2 cases: */
  331. /* - if non-inverting input has no selection: it can be the case for */
  332. /* COMP1 in window mode. */
  333. /* - particular case for PC3: if switch COMP1_SW1 is closed */
  334. /* (by macro "__HAL_OPAMP_OPAMP3OUT_CONNECT_ADC_COMP1()" or */
  335. /* "__HAL_RI_SWITCH_COMP1_SW1_CLOSE()"), connection between pin PC3 */
  336. /* (or OPAMP3, if available) and COMP1 is done directly, without going */
  337. /* through ADC switch matrix. */
  338. #if defined(COMP_CSR_SW1)
  339. if(READ_BIT(COMP->CSR, COMP_CSR_SW1) != RESET)
  340. {
  341. if(hcomp->Init.NonInvertingInput != COMP_NONINVERTINGINPUT_PC3)
  342. {
  343. /* Case of switch COMP1_SW1 closed and non-inverting input different of PC3:
  344. setting of another input is not possible (issue of pin shorted with PC3) */
  345. status = HAL_ERROR;
  346. }
  347. }
  348. else
  349. #endif
  350. {
  351. if (__COMP_ROUTING_INTERFACE_TOBECONFIGURED(hcomp))
  352. {
  353. if (hcomp->Instance == COMP1)
  354. {
  355. /* Enable the switch control mode */
  356. __HAL_RI_SWITCHCONTROLMODE_ENABLE();
  357. /* Close the analog switch of ADC switch matrix to COMP1 (ADC */
  358. /* channel 26: Vcomp) */
  359. __HAL_RI_IOSWITCH_CLOSE(RI_IOSWITCH_VCOMP);
  360. }
  361. /* Close the I/O analog switch corresponding to comparator */
  362. /* non-inverting input selected. */
  363. __HAL_RI_IOSWITCH_CLOSE(hcomp->Init.NonInvertingInput);
  364. }
  365. }
  366. /* Initialize the COMP state*/
  367. if(hcomp->State == HAL_COMP_STATE_RESET)
  368. {
  369. hcomp->State = HAL_COMP_STATE_READY;
  370. }
  371. }
  372. return status;
  373. }
  374. /**
  375. * @brief DeInitializes the COMP peripheral
  376. * @note Deinitialization can't be performed if the COMP configuration is locked.
  377. * To unlock the configuration, perform a system reset.
  378. * @param hcomp COMP handle
  379. * @retval HAL status
  380. */
  381. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  382. {
  383. HAL_StatusTypeDef status = HAL_OK;
  384. /* Check the COMP handle allocation and lock status */
  385. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  386. {
  387. status = HAL_ERROR;
  388. }
  389. else
  390. {
  391. /* Check the parameter */
  392. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  393. /* Reset configuration depending on comparator instance */
  394. if (hcomp->Instance == COMP1)
  395. {
  396. CLEAR_BIT(COMP->CSR , COMP_CSR_400KPD | COMP_CSR_10KPD | COMP_CSR_400KPU | COMP_CSR_10KPU);
  397. }
  398. else /* if (hcomp->Instance == COMP2) */
  399. {
  400. CLEAR_BIT(COMP->CSR , COMP_CSR_OUTSEL |
  401. COMP_CSR_WNDWE |
  402. COMP_CSR_INSEL |
  403. COMP_CSR_SPEED );
  404. }
  405. /* Restore default state of Routing Interface (RI) switches for */
  406. /* comparator non-inverting input. */
  407. if (hcomp->Init.NonInvertingInput != COMP_NONINVERTINGINPUT_NONE)
  408. {
  409. /* Open the I/O analog switch corresponding to comparator */
  410. /* non-inverting input selected. */
  411. __HAL_RI_IOSWITCH_OPEN(hcomp->Init.NonInvertingInput);
  412. }
  413. if (hcomp->Instance == COMP1)
  414. {
  415. /* Open the analog switch of ADC switch matrix to COMP1 (ADC */
  416. /* channel 26: Vcomp) */
  417. __HAL_RI_IOSWITCH_OPEN(RI_IOSWITCH_VCOMP);
  418. /* Disable the switch control mode */
  419. __HAL_RI_SWITCHCONTROLMODE_DISABLE();
  420. }
  421. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  422. if (hcomp->MspDeInitCallback == NULL)
  423. {
  424. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  425. }
  426. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  427. hcomp->MspDeInitCallback(hcomp);
  428. #else
  429. /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK, NVIC */
  430. HAL_COMP_MspDeInit(hcomp);
  431. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  432. hcomp->State = HAL_COMP_STATE_RESET;
  433. /* Process unlocked */
  434. __HAL_UNLOCK(hcomp);
  435. }
  436. return status;
  437. }
  438. /**
  439. * @brief Initializes the COMP MSP.
  440. * @param hcomp COMP handle
  441. * @retval None
  442. */
  443. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  444. {
  445. /* Prevent unused argument(s) compilation warning */
  446. UNUSED(hcomp);
  447. /* NOTE : This function Should not be modified, when the callback is needed,
  448. the HAL_COMP_MspInit could be implenetd in the user file
  449. */
  450. }
  451. /**
  452. * @brief DeInitializes COMP MSP.
  453. * @param hcomp COMP handle
  454. * @retval None
  455. */
  456. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  457. {
  458. /* Prevent unused argument(s) compilation warning */
  459. UNUSED(hcomp);
  460. /* NOTE : This function Should not be modified, when the callback is needed,
  461. the HAL_COMP_MspDeInit could be implenetd in the user file
  462. */
  463. }
  464. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  465. /**
  466. * @brief Register a User COMP Callback
  467. * To be used instead of the weak predefined callback
  468. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  469. * the configuration information for the specified COMP.
  470. * @param CallbackID ID of the callback to be registered
  471. * This parameter can be one of the following values:
  472. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  473. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  474. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  475. * @param pCallback pointer to the Callback function
  476. * @retval HAL status
  477. */
  478. HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
  479. {
  480. HAL_StatusTypeDef status = HAL_OK;
  481. if (pCallback == NULL)
  482. {
  483. /* Update the error code */
  484. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  485. return HAL_ERROR;
  486. }
  487. if (HAL_COMP_STATE_READY == hcomp->State)
  488. {
  489. switch (CallbackID)
  490. {
  491. case HAL_COMP_TRIGGER_CB_ID :
  492. hcomp->TriggerCallback = pCallback;
  493. break;
  494. case HAL_COMP_MSPINIT_CB_ID :
  495. hcomp->MspInitCallback = pCallback;
  496. break;
  497. case HAL_COMP_MSPDEINIT_CB_ID :
  498. hcomp->MspDeInitCallback = pCallback;
  499. break;
  500. default :
  501. /* Update the error code */
  502. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  503. /* Return error status */
  504. status = HAL_ERROR;
  505. break;
  506. }
  507. }
  508. else if (HAL_COMP_STATE_RESET == hcomp->State)
  509. {
  510. switch (CallbackID)
  511. {
  512. case HAL_COMP_MSPINIT_CB_ID :
  513. hcomp->MspInitCallback = pCallback;
  514. break;
  515. case HAL_COMP_MSPDEINIT_CB_ID :
  516. hcomp->MspDeInitCallback = pCallback;
  517. break;
  518. default :
  519. /* Update the error code */
  520. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  521. /* Return error status */
  522. status = HAL_ERROR;
  523. break;
  524. }
  525. }
  526. else
  527. {
  528. /* Update the error code */
  529. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  530. /* Return error status */
  531. status = HAL_ERROR;
  532. }
  533. return status;
  534. }
  535. /**
  536. * @brief Unregister a COMP Callback
  537. * COMP callback is redirected to the weak predefined callback
  538. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  539. * the configuration information for the specified COMP.
  540. * @param CallbackID ID of the callback to be unregistered
  541. * This parameter can be one of the following values:
  542. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  543. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  544. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  545. * @retval HAL status
  546. */
  547. HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
  548. {
  549. HAL_StatusTypeDef status = HAL_OK;
  550. if (HAL_COMP_STATE_READY == hcomp->State)
  551. {
  552. switch (CallbackID)
  553. {
  554. case HAL_COMP_TRIGGER_CB_ID :
  555. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  556. break;
  557. case HAL_COMP_MSPINIT_CB_ID :
  558. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  559. break;
  560. case HAL_COMP_MSPDEINIT_CB_ID :
  561. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  562. break;
  563. default :
  564. /* Update the error code */
  565. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  566. /* Return error status */
  567. status = HAL_ERROR;
  568. break;
  569. }
  570. }
  571. else if (HAL_COMP_STATE_RESET == hcomp->State)
  572. {
  573. switch (CallbackID)
  574. {
  575. case HAL_COMP_MSPINIT_CB_ID :
  576. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  577. break;
  578. case HAL_COMP_MSPDEINIT_CB_ID :
  579. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  580. break;
  581. default :
  582. /* Update the error code */
  583. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  584. /* Return error status */
  585. status = HAL_ERROR;
  586. break;
  587. }
  588. }
  589. else
  590. {
  591. /* Update the error code */
  592. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  593. /* Return error status */
  594. status = HAL_ERROR;
  595. }
  596. return status;
  597. }
  598. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  599. /**
  600. * @}
  601. */
  602. /** @defgroup COMP_Exported_Functions_Group2 I/O operation functions
  603. * @brief I/O operation functions
  604. *
  605. @verbatim
  606. ===============================================================================
  607. ##### IO operation functions #####
  608. ===============================================================================
  609. [..]
  610. This subsection provides a set of functions allowing to manage the COMP
  611. start and stop actions with or without interruption on ExtI line.
  612. @endverbatim
  613. * @{
  614. */
  615. /**
  616. * @brief Start the comparator
  617. * @param hcomp COMP handle
  618. * @retval HAL status
  619. */
  620. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  621. {
  622. HAL_StatusTypeDef status = HAL_OK;
  623. uint32_t wait_loop_cycles = 0;
  624. __IO uint32_t wait_loop_index = 0;
  625. /* Check the COMP handle allocation and lock status */
  626. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  627. {
  628. status = HAL_ERROR;
  629. }
  630. else
  631. {
  632. /* Check the parameter */
  633. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  634. if(hcomp->State == HAL_COMP_STATE_READY)
  635. {
  636. /* Note: For comparator 2, inverting input (parameter */
  637. /* "hcomp->Init.InvertingInput") is configured into this */
  638. /* function instead of function "HAL_COMP_Init()" since */
  639. /* inverting input selection also enables the comparator 2. */
  640. __HAL_COMP_ENABLE(hcomp);
  641. /* Set delay for COMP start-up time */
  642. if (hcomp->Instance == COMP1)
  643. {
  644. wait_loop_cycles = COMP1_START_DELAY_CPU_CYCLES;
  645. }
  646. else /* if (hcomp->Instance == COMP2) */
  647. {
  648. wait_loop_cycles = COMP2_START_DELAY_CPU_CYCLES;
  649. }
  650. /* Delay for COMP start-up time. */
  651. /* Delay fixed to worst case: maximum CPU frequency */
  652. while(wait_loop_index < wait_loop_cycles)
  653. {
  654. wait_loop_index++;
  655. }
  656. /* Update COMP state */
  657. hcomp->State = HAL_COMP_STATE_BUSY;
  658. }
  659. else
  660. {
  661. status = HAL_ERROR;
  662. }
  663. }
  664. return status;
  665. }
  666. /**
  667. * @brief Stop the comparator
  668. * @param hcomp COMP handle
  669. * @retval HAL status
  670. */
  671. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  672. {
  673. HAL_StatusTypeDef status = HAL_OK;
  674. /* Check the COMP handle allocation and lock status */
  675. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  676. {
  677. status = HAL_ERROR;
  678. }
  679. else
  680. {
  681. /* Check the parameter */
  682. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  683. if(hcomp->State == HAL_COMP_STATE_BUSY)
  684. {
  685. /* Disable the selected comparator */
  686. __HAL_COMP_DISABLE(hcomp);
  687. /* Update COMP state */
  688. hcomp->State = HAL_COMP_STATE_READY;
  689. }
  690. else
  691. {
  692. status = HAL_ERROR;
  693. }
  694. }
  695. return status;
  696. }
  697. /**
  698. * @brief Enables the interrupt and starts the comparator
  699. * @param hcomp COMP handle
  700. * @retval HAL status.
  701. */
  702. HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
  703. {
  704. HAL_StatusTypeDef status = HAL_OK;
  705. uint32_t extiline = 0;
  706. status = HAL_COMP_Start(hcomp);
  707. if(status == HAL_OK)
  708. {
  709. /* Check the parameter */
  710. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  711. /* Get the Exti Line output configuration */
  712. extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  713. /* Configure the trigger rising edge */
  714. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
  715. {
  716. SET_BIT(EXTI->RTSR, extiline);
  717. }
  718. else
  719. {
  720. CLEAR_BIT(EXTI->RTSR, extiline);
  721. }
  722. /* Configure the trigger falling edge */
  723. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
  724. {
  725. SET_BIT(EXTI->FTSR, extiline);
  726. }
  727. else
  728. {
  729. CLEAR_BIT(EXTI->FTSR, extiline);
  730. }
  731. /* Clear COMP EXTI pending bit */
  732. WRITE_REG(EXTI->PR, extiline);
  733. /* Enable EXTI interrupt mode */
  734. SET_BIT(EXTI->IMR, extiline);
  735. }
  736. return status;
  737. }
  738. /**
  739. * @brief Disable the interrupt and Stop the comparator
  740. * @param hcomp COMP handle
  741. * @retval HAL status
  742. */
  743. HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
  744. {
  745. HAL_StatusTypeDef status = HAL_OK;
  746. /* Disable the EXTI Line interrupt mode */
  747. CLEAR_BIT(EXTI->IMR, COMP_GET_EXTI_LINE(hcomp->Instance));
  748. status = HAL_COMP_Stop(hcomp);
  749. return status;
  750. }
  751. /**
  752. * @brief Comparator IRQ Handler
  753. * @param hcomp COMP handle
  754. * @retval HAL status
  755. */
  756. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  757. {
  758. uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  759. /* Check COMP Exti flag */
  760. if(READ_BIT(EXTI->PR, extiline) != RESET)
  761. {
  762. /* Clear COMP EXTI pending bit */
  763. WRITE_REG(EXTI->PR, extiline);
  764. /* COMP trigger callback */
  765. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  766. hcomp->TriggerCallback(hcomp);
  767. #else
  768. HAL_COMP_TriggerCallback(hcomp);
  769. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  770. }
  771. }
  772. /**
  773. * @}
  774. */
  775. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  776. * @brief Peripheral Control functions
  777. *
  778. @verbatim
  779. ===============================================================================
  780. ##### Peripheral Control functions #####
  781. ===============================================================================
  782. [..]
  783. This subsection provides a set of functions allowing to control the COMP
  784. management functions: Lock status, comparator output level check, IRQ
  785. callback (in case of usage of comparator with interruption on ExtI line).
  786. @endverbatim
  787. * @{
  788. */
  789. /**
  790. * @brief Lock the selected comparator configuration.
  791. * Caution: On STM32L1, HAL COMP lock is software lock only (not
  792. * hardware lock as on some other STM32 devices)
  793. * @param hcomp COMP handle
  794. * @retval HAL status
  795. */
  796. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  797. {
  798. HAL_StatusTypeDef status = HAL_OK;
  799. /* Check the COMP handle allocation and lock status */
  800. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  801. {
  802. status = HAL_ERROR;
  803. }
  804. else
  805. {
  806. /* Check the parameter */
  807. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  808. /* Set lock flag on state */
  809. switch(hcomp->State)
  810. {
  811. case HAL_COMP_STATE_BUSY:
  812. hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
  813. break;
  814. case HAL_COMP_STATE_READY:
  815. hcomp->State = HAL_COMP_STATE_READY_LOCKED;
  816. break;
  817. default:
  818. /* unexpected state */
  819. status = HAL_ERROR;
  820. break;
  821. }
  822. }
  823. return status;
  824. }
  825. /**
  826. * @brief Return the output level (high or low) of the selected comparator.
  827. * The output level depends on the selected polarity.
  828. * - Comparator output is low when the non-inverting input is at a lower
  829. * voltage than the inverting input
  830. * - Comparator output is high when the non-inverting input is at a higher
  831. * voltage than the inverting input
  832. * @param hcomp COMP handle
  833. * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
  834. *
  835. */
  836. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  837. {
  838. uint32_t level = 0;
  839. /* Check the parameter */
  840. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  841. /* Read output level of the selected comparator */
  842. if(READ_BIT(COMP->CSR, __COMP_CSR_CMPXOUT(hcomp)) == RESET)
  843. {
  844. level = COMP_OUTPUTLEVEL_LOW;
  845. }
  846. else
  847. {
  848. level = COMP_OUTPUTLEVEL_HIGH;
  849. }
  850. return(level);
  851. }
  852. /**
  853. * @brief Comparator trigger callback.
  854. * @param hcomp COMP handle
  855. * @retval None
  856. */
  857. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  858. {
  859. /* Prevent unused argument(s) compilation warning */
  860. UNUSED(hcomp);
  861. /* NOTE : This function should not be modified, when the callback is needed,
  862. the HAL_COMP_TriggerCallback should be implemented in the user file
  863. */
  864. }
  865. /**
  866. * @}
  867. */
  868. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  869. * @brief Peripheral State functions
  870. *
  871. @verbatim
  872. ===============================================================================
  873. ##### Peripheral State functions #####
  874. ===============================================================================
  875. [..]
  876. This subsection permit to get in run-time the status of the peripheral.
  877. @endverbatim
  878. * @{
  879. */
  880. /**
  881. * @brief Return the COMP state
  882. * @param hcomp COMP handle
  883. * @retval HAL state
  884. */
  885. HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  886. {
  887. /* Check the COMP handle allocation */
  888. if(hcomp == NULL)
  889. {
  890. return HAL_COMP_STATE_RESET;
  891. }
  892. /* Check the parameter */
  893. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  894. return hcomp->State;
  895. }
  896. /**
  897. * @brief Return the COMP error code.
  898. * @param hcomp COMP handle
  899. * @retval COMP error code
  900. */
  901. uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
  902. {
  903. /* Check the parameters */
  904. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  905. return hcomp->ErrorCode;
  906. }
  907. /**
  908. * @}
  909. */
  910. /**
  911. * @}
  912. */
  913. #endif /* HAL_COMP_MODULE_ENABLED */
  914. /**
  915. * @}
  916. */
  917. /**
  918. * @}
  919. */
  920. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/