stm32l4xx_hal_comp.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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. * + Peripheral control functions
  10. * + Peripheral state functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2017 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ================================================================================
  25. ##### COMP Peripheral features #####
  26. ================================================================================
  27. [..]
  28. The STM32L4xx device family integrates two analog comparators instances:
  29. COMP1, COMP2 except for the STM32L412xx/STM32L422xx products featuring only
  30. one instance: COMP1.
  31. In the rest of the file, all comments related to a pair of comparators are not
  32. applicable to STM32L412xx/STM32L422xx.
  33. (#) Comparators input minus (inverting input) and input plus (non inverting input)
  34. can be set to internal references or to GPIO pins
  35. (refer to GPIO list in reference manual).
  36. (#) Comparators output level is available using HAL_COMP_GetOutputLevel()
  37. and can be redirected to other peripherals: GPIO pins (in mode
  38. alternate functions for comparator), timers.
  39. (refer to GPIO list in reference manual).
  40. (#) The comparators have interrupt capability through the EXTI controller
  41. with wake-up from sleep and stop modes.
  42. (#) Pairs of comparators instances can be combined in window mode
  43. (2 consecutive instances odd and even COMP<x> and COMP<x+1>).
  44. From the corresponding IRQ handler, the right interrupt source can be retrieved
  45. using macro __HAL_COMP_COMPx_EXTI_GET_FLAG().
  46. ##### How to use this driver #####
  47. ================================================================================
  48. [..]
  49. This driver provides functions to configure and program the comparator instances
  50. of STM32L4xx devices.
  51. To use the comparator, perform the following steps:
  52. (#) Initialize the COMP low level resources by implementing the HAL_COMP_MspInit():
  53. (++) Configure the GPIO connected to comparator inputs plus and minus in analog mode
  54. using HAL_GPIO_Init().
  55. (++) If needed, configure the GPIO connected to comparator output in alternate function mode
  56. using HAL_GPIO_Init().
  57. (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
  58. selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
  59. interrupt vector using HAL_NVIC_EnableIRQ() function.
  60. (#) Configure the comparator using HAL_COMP_Init() function:
  61. (++) Select the input minus (inverting input)
  62. (++) Select the input plus (non-inverting input)
  63. (++) Select the hysteresis
  64. (++) Select the blanking source
  65. (++) Select the output polarity
  66. (++) Select the power mode
  67. (++) Select the window mode
  68. -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE()
  69. to enable internal control clock of the comparators.
  70. However, this is a legacy strategy. In future STM32 families,
  71. COMP clock enable must be implemented by user in "HAL_COMP_MspInit()".
  72. Therefore, for compatibility anticipation, it is recommended to
  73. implement __HAL_RCC_SYSCFG_CLK_ENABLE() in "HAL_COMP_MspInit()".
  74. (#) Reconfiguration on-the-fly of comparator can be done by calling again
  75. function HAL_COMP_Init() with new input structure parameters values.
  76. (#) Enable the comparator using HAL_COMP_Start() function.
  77. (#) Use HAL_COMP_TriggerCallback() or HAL_COMP_GetOutputLevel() functions
  78. to manage comparator outputs (events and output level).
  79. (#) Disable the comparator using HAL_COMP_Stop() function.
  80. (#) De-initialize the comparator using HAL_COMP_DeInit() function.
  81. (#) For safety purpose, comparator configuration can be locked using HAL_COMP_Lock() function.
  82. The only way to unlock the comparator is a device hardware reset.
  83. *** Callback registration ***
  84. =============================================
  85. [..]
  86. The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
  87. allows the user to configure dynamically the driver callbacks.
  88. Use Functions HAL_COMP_RegisterCallback()
  89. to register an interrupt callback.
  90. [..]
  91. Function HAL_COMP_RegisterCallback() allows to register following callbacks:
  92. (+) TriggerCallback : callback for COMP trigger.
  93. (+) MspInitCallback : callback for Msp Init.
  94. (+) MspDeInitCallback : callback for Msp DeInit.
  95. This function takes as parameters the HAL peripheral handle, the Callback ID
  96. and a pointer to the user callback function.
  97. [..]
  98. Use function HAL_COMP_UnRegisterCallback to reset a callback to the default
  99. weak function.
  100. [..]
  101. HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
  102. and the Callback ID.
  103. This function allows to reset following callbacks:
  104. (+) TriggerCallback : callback for COMP trigger.
  105. (+) MspInitCallback : callback for Msp Init.
  106. (+) MspDeInitCallback : callback for Msp DeInit.
  107. [..]
  108. By default, after the HAL_COMP_Init() and when the state is HAL_COMP_STATE_RESET
  109. all callbacks are set to the corresponding weak functions:
  110. example HAL_COMP_TriggerCallback().
  111. Exception done for MspInit and MspDeInit functions that are
  112. reset to the legacy weak functions in the HAL_COMP_Init()/ HAL_COMP_DeInit() only when
  113. these callbacks are null (not registered beforehand).
  114. [..]
  115. If MspInit or MspDeInit are not null, the HAL_COMP_Init()/ HAL_COMP_DeInit()
  116. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  117. [..]
  118. Callbacks can be registered/unregistered in HAL_COMP_STATE_READY state only.
  119. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  120. in HAL_COMP_STATE_READY or HAL_COMP_STATE_RESET state,
  121. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  122. [..]
  123. Then, the user first registers the MspInit/MspDeInit user callbacks
  124. using HAL_COMP_RegisterCallback() before calling HAL_COMP_DeInit()
  125. or HAL_COMP_Init() function.
  126. [..]
  127. When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
  128. not defined, the callback registration feature is not available and all callbacks
  129. are set to the corresponding weak functions.
  130. @endverbatim
  131. ******************************************************************************
  132. Table 1. COMP inputs and output for STM32L4xx devices
  133. +-----------------------------------------------------------------+
  134. | | | COMP1 | COMP2 (4) |
  135. |----------------|----------------|---------------|---------------+
  136. | | IO1 | PC5 | PB4 |
  137. | Input plus | IO2 | PB2 | PB6 |
  138. | | IO3 (3) | PA1 | PA3 |
  139. |----------------|----------------|---------------|---------------+
  140. | | 1/4 VrefInt | Available | Available |
  141. | | 1/2 VrefInt | Available | Available |
  142. | | 3/4 VrefInt | Available | Available |
  143. | Input minus | VrefInt | Available | Available |
  144. | | DAC1 channel 1 | Available | Available (4) |
  145. | | DAC1 channel 2 | Available | Available (4) |
  146. | | IO1 | PB1 | PB3 |
  147. | | IO2 | PC4 | PB7 |
  148. | | IO3 (3) | PA0 | PA2 |
  149. | | IO4 (3) | PA4 | PA4 |
  150. | | IO5 (3) | PA5 | PA5 |
  151. +----------------|----------------|---------------|---------------+
  152. | Output | | PB0 (1) | PB5 (1) |
  153. | | | PB10 (1) | PB11 (1) |
  154. | | | TIM (2) | TIM (2) |
  155. +-----------------------------------------------------------------+
  156. (1) GPIO must be set to alternate function for comparator
  157. (2) Comparators output to timers is set in timers instances.
  158. (3) Only STM32L43x/L44x
  159. (4) Not applicable to STM32L412x/L422x
  160. */
  161. /* Includes ------------------------------------------------------------------*/
  162. #include "stm32l4xx_hal.h"
  163. /** @addtogroup STM32L4xx_HAL_Driver
  164. * @{
  165. */
  166. #ifdef HAL_COMP_MODULE_ENABLED
  167. #if defined (COMP1) || defined (COMP2)
  168. /** @defgroup COMP COMP
  169. * @brief COMP HAL module driver
  170. * @{
  171. */
  172. /* Private typedef -----------------------------------------------------------*/
  173. /* Private define ------------------------------------------------------------*/
  174. /** @addtogroup COMP_Private_Constants
  175. * @{
  176. */
  177. /* Delay for COMP startup time. */
  178. /* Note: Delay required to reach propagation delay specification. */
  179. /* Literal set to maximum value (refer to device datasheet, */
  180. /* parameter "tSTART"). */
  181. /* Unit: us */
  182. #define COMP_DELAY_STARTUP_US (80UL) /*!< Delay for COMP startup time */
  183. /* Delay for COMP voltage scaler stabilization time. */
  184. /* Literal set to maximum value (refer to device datasheet, */
  185. /* parameter "tSTART_SCALER"). */
  186. /* Unit: us */
  187. #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200UL) /*!< Delay for COMP voltage scaler stabilization time */
  188. #define COMP_OUTPUT_LEVEL_BITOFFSET_POS (30UL)
  189. /**
  190. * @}
  191. */
  192. /* Private macro -------------------------------------------------------------*/
  193. /* Private variables ---------------------------------------------------------*/
  194. /* Private function prototypes -----------------------------------------------*/
  195. /* Exported functions --------------------------------------------------------*/
  196. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  197. * @{
  198. */
  199. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  200. * @brief Initialization and de-initialization functions.
  201. *
  202. @verbatim
  203. ===============================================================================
  204. ##### Initialization and de-initialization functions #####
  205. ===============================================================================
  206. [..] This section provides functions to initialize and de-initialize comparators
  207. @endverbatim
  208. * @{
  209. */
  210. /**
  211. * @brief Initialize the COMP according to the specified
  212. * parameters in the COMP_InitTypeDef and initialize the associated handle.
  213. * @note If the selected comparator is locked, initialization can't be performed.
  214. * To unlock the configuration, perform a system reset.
  215. * @param hcomp COMP handle
  216. * @retval HAL status
  217. */
  218. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  219. {
  220. uint32_t tmp_csr;
  221. uint32_t exti_line;
  222. uint32_t comp_voltage_scaler_initialized; /* Value "0" if comparator voltage scaler is not initialized */
  223. __IO uint32_t wait_loop_index = 0UL;
  224. HAL_StatusTypeDef status = HAL_OK;
  225. /* Check the COMP handle allocation and lock status */
  226. if(hcomp == NULL)
  227. {
  228. status = HAL_ERROR;
  229. }
  230. else if(__HAL_COMP_IS_LOCKED(hcomp))
  231. {
  232. status = HAL_ERROR;
  233. }
  234. else
  235. {
  236. /* Check the parameters */
  237. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  238. assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput));
  239. assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput));
  240. assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  241. assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode));
  242. assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  243. assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
  244. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  245. #if defined(COMP2)
  246. assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
  247. #endif
  248. if(hcomp->State == HAL_COMP_STATE_RESET)
  249. {
  250. /* Allocate lock resource and initialize it */
  251. hcomp->Lock = HAL_UNLOCKED;
  252. /* Set COMP error code to none */
  253. COMP_CLEAR_ERRORCODE(hcomp);
  254. /* Init SYSCFG and the low level hardware to access comparators */
  255. /* Note: HAL_COMP_Init() calls __HAL_RCC_SYSCFG_CLK_ENABLE() */
  256. /* to enable internal control clock of the comparators. */
  257. /* However, this is a legacy strategy. In future STM32 families, */
  258. /* COMP clock enable must be implemented by user */
  259. /* in "HAL_COMP_MspInit()". */
  260. /* Therefore, for compatibility anticipation, it is recommended */
  261. /* to implement __HAL_RCC_SYSCFG_CLK_ENABLE() */
  262. /* in "HAL_COMP_MspInit()". */
  263. __HAL_RCC_SYSCFG_CLK_ENABLE();
  264. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  265. /* Init the COMP Callback settings */
  266. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  267. if (hcomp->MspInitCallback == NULL)
  268. {
  269. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  270. }
  271. /* Init the low level hardware */
  272. hcomp->MspInitCallback(hcomp);
  273. #else
  274. /* Init the low level hardware */
  275. HAL_COMP_MspInit(hcomp);
  276. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  277. }
  278. /* Memorize voltage scaler state before initialization */
  279. comp_voltage_scaler_initialized = READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN);
  280. /* Set COMP parameters */
  281. tmp_csr = ( hcomp->Init.NonInvertingInput
  282. | hcomp->Init.InvertingInput
  283. | hcomp->Init.BlankingSrce
  284. | hcomp->Init.Hysteresis
  285. | hcomp->Init.OutputPol
  286. | hcomp->Init.Mode
  287. );
  288. /* Set parameters in COMP register */
  289. /* Note: Update all bits except read-only, lock and enable bits */
  290. #if defined (COMP_CSR_INMESEL)
  291. #if defined (COMP_CSR_WINMODE)
  292. MODIFY_REG(hcomp->Instance->CSR,
  293. COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
  294. COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST |
  295. COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL,
  296. tmp_csr
  297. );
  298. #else
  299. MODIFY_REG(hcomp->Instance->CSR,
  300. COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
  301. COMP_CSR_POLARITY | COMP_CSR_HYST |
  302. COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL,
  303. tmp_csr
  304. );
  305. #endif /* COMP_CSR_WINMODE */
  306. #else
  307. MODIFY_REG(hcomp->Instance->CSR,
  308. COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL |
  309. COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST |
  310. COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN,
  311. tmp_csr
  312. );
  313. #endif /* COMP_CSR_INMESEL */
  314. #if defined(COMP2)
  315. /* Set window mode */
  316. /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */
  317. /* instances. Therefore, this function can update another COMP */
  318. /* instance that the one currently selected. */
  319. if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON)
  320. {
  321. SET_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
  322. }
  323. else
  324. {
  325. CLEAR_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE);
  326. }
  327. #endif /* COMP2 */
  328. /* Delay for COMP scaler bridge voltage stabilization */
  329. /* Apply the delay if voltage scaler bridge is required and not already enabled */
  330. if ((READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) != 0UL) &&
  331. (comp_voltage_scaler_initialized == 0UL) )
  332. {
  333. /* Wait loop initialization and execution */
  334. /* Note: Variable divided by 2 to compensate partially */
  335. /* CPU processing cycles, scaling in us split to not */
  336. /* exceed 32 bits register capacity and handle low frequency. */
  337. wait_loop_index = ((COMP_DELAY_VOLTAGE_SCALER_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  338. while(wait_loop_index != 0UL)
  339. {
  340. wait_loop_index--;
  341. }
  342. }
  343. /* Get the EXTI line corresponding to the selected COMP instance */
  344. exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  345. /* Manage EXTI settings */
  346. if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != 0UL)
  347. {
  348. /* Configure EXTI rising edge */
  349. if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != 0UL)
  350. {
  351. LL_EXTI_EnableRisingTrig_0_31(exti_line);
  352. }
  353. else
  354. {
  355. LL_EXTI_DisableRisingTrig_0_31(exti_line);
  356. }
  357. /* Configure EXTI falling edge */
  358. if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != 0UL)
  359. {
  360. LL_EXTI_EnableFallingTrig_0_31(exti_line);
  361. }
  362. else
  363. {
  364. LL_EXTI_DisableFallingTrig_0_31(exti_line);
  365. }
  366. /* Clear COMP EXTI pending bit (if any) */
  367. LL_EXTI_ClearFlag_0_31(exti_line);
  368. /* Configure EXTI event mode */
  369. if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != 0UL)
  370. {
  371. LL_EXTI_EnableEvent_0_31(exti_line);
  372. }
  373. else
  374. {
  375. LL_EXTI_DisableEvent_0_31(exti_line);
  376. }
  377. /* Configure EXTI interrupt mode */
  378. if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != 0UL)
  379. {
  380. LL_EXTI_EnableIT_0_31(exti_line);
  381. }
  382. else
  383. {
  384. LL_EXTI_DisableIT_0_31(exti_line);
  385. }
  386. }
  387. else
  388. {
  389. /* Disable EXTI event mode */
  390. LL_EXTI_DisableEvent_0_31(exti_line);
  391. /* Disable EXTI interrupt mode */
  392. LL_EXTI_DisableIT_0_31(exti_line);
  393. }
  394. /* Set HAL COMP handle state */
  395. /* Note: Transition from state reset to state ready, */
  396. /* otherwise (coming from state ready or busy) no state update. */
  397. if (hcomp->State == HAL_COMP_STATE_RESET)
  398. {
  399. hcomp->State = HAL_COMP_STATE_READY;
  400. }
  401. }
  402. return status;
  403. }
  404. /**
  405. * @brief DeInitialize the COMP peripheral.
  406. * @note Deinitialization cannot be performed if the COMP configuration is locked.
  407. * To unlock the configuration, perform a system reset.
  408. * @param hcomp COMP handle
  409. * @retval HAL status
  410. */
  411. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  412. {
  413. HAL_StatusTypeDef status = HAL_OK;
  414. /* Check the COMP handle allocation and lock status */
  415. if(hcomp == NULL)
  416. {
  417. status = HAL_ERROR;
  418. }
  419. else if(__HAL_COMP_IS_LOCKED(hcomp))
  420. {
  421. status = HAL_ERROR;
  422. }
  423. else
  424. {
  425. /* Check the parameter */
  426. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  427. /* Set COMP_CSR register to reset value */
  428. WRITE_REG(hcomp->Instance->CSR, 0x00000000UL);
  429. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  430. if (hcomp->MspDeInitCallback == NULL)
  431. {
  432. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  433. }
  434. /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
  435. hcomp->MspDeInitCallback(hcomp);
  436. #else
  437. /* DeInit the low level hardware: GPIO, RCC clock, NVIC */
  438. HAL_COMP_MspDeInit(hcomp);
  439. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  440. /* Set HAL COMP handle state */
  441. hcomp->State = HAL_COMP_STATE_RESET;
  442. /* Release Lock */
  443. __HAL_UNLOCK(hcomp);
  444. }
  445. return status;
  446. }
  447. /**
  448. * @brief Initialize the COMP MSP.
  449. * @param hcomp COMP handle
  450. * @retval None
  451. */
  452. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  453. {
  454. /* Prevent unused argument(s) compilation warning */
  455. UNUSED(hcomp);
  456. /* NOTE : This function should not be modified, when the callback is needed,
  457. the HAL_COMP_MspInit could be implemented in the user file
  458. */
  459. }
  460. /**
  461. * @brief DeInitialize the COMP MSP.
  462. * @param hcomp COMP handle
  463. * @retval None
  464. */
  465. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  466. {
  467. /* Prevent unused argument(s) compilation warning */
  468. UNUSED(hcomp);
  469. /* NOTE : This function should not be modified, when the callback is needed,
  470. the HAL_COMP_MspDeInit could be implemented in the user file
  471. */
  472. }
  473. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  474. /**
  475. * @brief Register a User COMP Callback
  476. * To be used instead of the weak predefined callback
  477. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  478. * the configuration information for the specified COMP.
  479. * @param CallbackID ID of the callback to be registered
  480. * This parameter can be one of the following values:
  481. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  482. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  483. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  484. * @param pCallback pointer to the Callback function
  485. * @retval HAL status
  486. */
  487. HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
  488. {
  489. HAL_StatusTypeDef status = HAL_OK;
  490. if (pCallback == NULL)
  491. {
  492. /* Update the error code */
  493. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  494. return HAL_ERROR;
  495. }
  496. if (HAL_COMP_STATE_READY == hcomp->State)
  497. {
  498. switch (CallbackID)
  499. {
  500. case HAL_COMP_TRIGGER_CB_ID :
  501. hcomp->TriggerCallback = pCallback;
  502. break;
  503. case HAL_COMP_MSPINIT_CB_ID :
  504. hcomp->MspInitCallback = pCallback;
  505. break;
  506. case HAL_COMP_MSPDEINIT_CB_ID :
  507. hcomp->MspDeInitCallback = pCallback;
  508. break;
  509. default :
  510. /* Update the error code */
  511. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  512. /* Return error status */
  513. status = HAL_ERROR;
  514. break;
  515. }
  516. }
  517. else if (HAL_COMP_STATE_RESET == hcomp->State)
  518. {
  519. switch (CallbackID)
  520. {
  521. case HAL_COMP_MSPINIT_CB_ID :
  522. hcomp->MspInitCallback = pCallback;
  523. break;
  524. case HAL_COMP_MSPDEINIT_CB_ID :
  525. hcomp->MspDeInitCallback = pCallback;
  526. break;
  527. default :
  528. /* Update the error code */
  529. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  530. /* Return error status */
  531. status = HAL_ERROR;
  532. break;
  533. }
  534. }
  535. else
  536. {
  537. /* Update the error code */
  538. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  539. /* Return error status */
  540. status = HAL_ERROR;
  541. }
  542. return status;
  543. }
  544. /**
  545. * @brief Unregister a COMP Callback
  546. * COMP callback is redirected to the weak predefined callback
  547. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  548. * the configuration information for the specified COMP.
  549. * @param CallbackID ID of the callback to be unregistered
  550. * This parameter can be one of the following values:
  551. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  552. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  553. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  554. * @retval HAL status
  555. */
  556. HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
  557. {
  558. HAL_StatusTypeDef status = HAL_OK;
  559. if (HAL_COMP_STATE_READY == hcomp->State)
  560. {
  561. switch (CallbackID)
  562. {
  563. case HAL_COMP_TRIGGER_CB_ID :
  564. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  565. break;
  566. case HAL_COMP_MSPINIT_CB_ID :
  567. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  568. break;
  569. case HAL_COMP_MSPDEINIT_CB_ID :
  570. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  571. break;
  572. default :
  573. /* Update the error code */
  574. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  575. /* Return error status */
  576. status = HAL_ERROR;
  577. break;
  578. }
  579. }
  580. else if (HAL_COMP_STATE_RESET == hcomp->State)
  581. {
  582. switch (CallbackID)
  583. {
  584. case HAL_COMP_MSPINIT_CB_ID :
  585. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  586. break;
  587. case HAL_COMP_MSPDEINIT_CB_ID :
  588. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  589. break;
  590. default :
  591. /* Update the error code */
  592. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  593. /* Return error status */
  594. status = HAL_ERROR;
  595. break;
  596. }
  597. }
  598. else
  599. {
  600. /* Update the error code */
  601. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  602. /* Return error status */
  603. status = HAL_ERROR;
  604. }
  605. return status;
  606. }
  607. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  608. /**
  609. * @}
  610. */
  611. /** @defgroup COMP_Exported_Functions_Group2 Start-Stop operation functions
  612. * @brief Start-Stop operation functions.
  613. *
  614. @verbatim
  615. ===============================================================================
  616. ##### IO operation functions #####
  617. ===============================================================================
  618. [..] This section provides functions allowing to:
  619. (+) Start a comparator instance.
  620. (+) Stop a comparator instance.
  621. @endverbatim
  622. * @{
  623. */
  624. /**
  625. * @brief Start the comparator.
  626. * @param hcomp COMP handle
  627. * @retval HAL status
  628. */
  629. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  630. {
  631. __IO uint32_t wait_loop_index = 0UL;
  632. HAL_StatusTypeDef status = HAL_OK;
  633. /* Check the COMP handle allocation and lock status */
  634. if(hcomp == NULL)
  635. {
  636. status = HAL_ERROR;
  637. }
  638. else if(__HAL_COMP_IS_LOCKED(hcomp))
  639. {
  640. status = HAL_ERROR;
  641. }
  642. else
  643. {
  644. /* Check the parameter */
  645. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  646. if(hcomp->State == HAL_COMP_STATE_READY)
  647. {
  648. /* Enable the selected comparator */
  649. SET_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
  650. /* Set HAL COMP handle state */
  651. hcomp->State = HAL_COMP_STATE_BUSY;
  652. /* Delay for COMP startup time */
  653. /* Wait loop initialization and execution */
  654. /* Note: Variable divided by 2 to compensate partially */
  655. /* CPU processing cycles, scaling in us split to not */
  656. /* exceed 32 bits register capacity and handle low frequency. */
  657. wait_loop_index = ((COMP_DELAY_STARTUP_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  658. while(wait_loop_index != 0UL)
  659. {
  660. wait_loop_index--;
  661. }
  662. }
  663. else
  664. {
  665. status = HAL_ERROR;
  666. }
  667. }
  668. return status;
  669. }
  670. /**
  671. * @brief Stop the comparator.
  672. * @param hcomp COMP handle
  673. * @retval HAL status
  674. */
  675. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  676. {
  677. HAL_StatusTypeDef status = HAL_OK;
  678. /* Check the COMP handle allocation and lock status */
  679. if(hcomp == NULL)
  680. {
  681. status = HAL_ERROR;
  682. }
  683. else if(__HAL_COMP_IS_LOCKED(hcomp))
  684. {
  685. status = HAL_ERROR;
  686. }
  687. else
  688. {
  689. /* Check the parameter */
  690. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  691. /* Check compliant states: HAL_COMP_STATE_READY or HAL_COMP_STATE_BUSY */
  692. /* (all states except HAL_COMP_STATE_RESET and except locked status. */
  693. if(hcomp->State != HAL_COMP_STATE_RESET)
  694. {
  695. /* Disable the selected comparator */
  696. CLEAR_BIT(hcomp->Instance->CSR, COMP_CSR_EN);
  697. /* Set HAL COMP handle state */
  698. hcomp->State = HAL_COMP_STATE_READY;
  699. }
  700. else
  701. {
  702. status = HAL_ERROR;
  703. }
  704. }
  705. return status;
  706. }
  707. /**
  708. * @brief Comparator IRQ handler.
  709. * @param hcomp COMP handle
  710. * @retval None
  711. */
  712. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  713. {
  714. /* Get the EXTI line corresponding to the selected COMP instance */
  715. uint32_t exti_line = COMP_GET_EXTI_LINE(hcomp->Instance);
  716. /* Check COMP EXTI flag */
  717. if(LL_EXTI_IsActiveFlag_0_31(exti_line) != 0UL)
  718. {
  719. #if defined(COMP2)
  720. /* Check whether comparator is in independent or window mode */
  721. if(READ_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE) != 0UL)
  722. {
  723. /* Clear COMP EXTI line pending bit of the pair of comparators */
  724. /* in window mode. */
  725. /* Note: Pair of comparators in window mode can both trig IRQ when */
  726. /* input voltage is changing from "out of window" area */
  727. /* (low or high ) to the other "out of window" area (high or low).*/
  728. /* Both flags must be cleared to call comparator trigger */
  729. /* callback is called once. */
  730. LL_EXTI_ClearFlag_0_31((COMP_EXTI_LINE_COMP1 | COMP_EXTI_LINE_COMP2));
  731. }
  732. else
  733. #endif /* COMP2 */
  734. {
  735. /* Clear COMP EXTI line pending bit */
  736. LL_EXTI_ClearFlag_0_31(exti_line);
  737. }
  738. /* COMP trigger user callback */
  739. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  740. hcomp->TriggerCallback(hcomp);
  741. #else
  742. HAL_COMP_TriggerCallback(hcomp);
  743. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  744. }
  745. }
  746. /**
  747. * @}
  748. */
  749. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  750. * @brief Management functions.
  751. *
  752. @verbatim
  753. ===============================================================================
  754. ##### Peripheral Control functions #####
  755. ===============================================================================
  756. [..]
  757. This subsection provides a set of functions allowing to control the comparators.
  758. @endverbatim
  759. * @{
  760. */
  761. /**
  762. * @brief Lock the selected comparator configuration.
  763. * @note A system reset is required to unlock the comparator configuration.
  764. * @note Locking the comparator from reset state is possible
  765. * if __HAL_RCC_SYSCFG_CLK_ENABLE() is being called before.
  766. * @param hcomp COMP handle
  767. * @retval HAL status
  768. */
  769. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  770. {
  771. HAL_StatusTypeDef status = HAL_OK;
  772. /* Check the COMP handle allocation and lock status */
  773. if(hcomp == NULL)
  774. {
  775. status = HAL_ERROR;
  776. }
  777. else if(__HAL_COMP_IS_LOCKED(hcomp))
  778. {
  779. status = HAL_ERROR;
  780. }
  781. else
  782. {
  783. /* Check the parameter */
  784. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  785. /* Set HAL COMP handle state */
  786. switch(hcomp->State)
  787. {
  788. case HAL_COMP_STATE_RESET:
  789. hcomp->State = HAL_COMP_STATE_RESET_LOCKED;
  790. break;
  791. case HAL_COMP_STATE_READY:
  792. hcomp->State = HAL_COMP_STATE_READY_LOCKED;
  793. break;
  794. default: /* HAL_COMP_STATE_BUSY */
  795. hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
  796. break;
  797. }
  798. }
  799. if(status == HAL_OK)
  800. {
  801. /* Set the lock bit corresponding to selected comparator */
  802. __HAL_COMP_LOCK(hcomp);
  803. }
  804. return status;
  805. }
  806. /**
  807. * @brief Return the output level (high or low) of the selected comparator.
  808. * The output level depends on the selected polarity.
  809. * If the polarity is not inverted:
  810. * - Comparator output is low when the input plus is at a lower
  811. * voltage than the input minus
  812. * - Comparator output is high when the input plus is at a higher
  813. * voltage than the input minus
  814. * If the polarity is inverted:
  815. * - Comparator output is high when the input plus is at a lower
  816. * voltage than the input minus
  817. * - Comparator output is low when the input plus is at a higher
  818. * voltage than the input minus
  819. * @param hcomp COMP handle
  820. * @retval Returns the selected comparator output level:
  821. * @arg COMP_OUTPUT_LEVEL_LOW
  822. * @arg COMP_OUTPUT_LEVEL_HIGH
  823. *
  824. */
  825. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  826. {
  827. /* Check the parameter */
  828. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  829. return (uint32_t)(READ_BIT(hcomp->Instance->CSR, COMP_CSR_VALUE)
  830. >> COMP_OUTPUT_LEVEL_BITOFFSET_POS);
  831. }
  832. /**
  833. * @brief Comparator trigger callback.
  834. * @param hcomp COMP handle
  835. * @retval None
  836. */
  837. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  838. {
  839. /* Prevent unused argument(s) compilation warning */
  840. UNUSED(hcomp);
  841. /* NOTE : This function should not be modified, when the callback is needed,
  842. the HAL_COMP_TriggerCallback should be implemented in the user file
  843. */
  844. }
  845. /**
  846. * @}
  847. */
  848. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  849. * @brief Peripheral State functions.
  850. *
  851. @verbatim
  852. ===============================================================================
  853. ##### Peripheral State functions #####
  854. ===============================================================================
  855. [..]
  856. This subsection permit to get in run-time the status of the peripheral.
  857. @endverbatim
  858. * @{
  859. */
  860. /**
  861. * @brief Return the COMP handle state.
  862. * @param hcomp COMP handle
  863. * @retval HAL state
  864. */
  865. HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  866. {
  867. /* Check the COMP handle allocation */
  868. if(hcomp == NULL)
  869. {
  870. return HAL_COMP_STATE_RESET;
  871. }
  872. /* Check the parameter */
  873. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  874. /* Return HAL COMP handle state */
  875. return hcomp->State;
  876. }
  877. /**
  878. * @brief Return the COMP error code.
  879. * @param hcomp COMP handle
  880. * @retval COMP error code
  881. */
  882. uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
  883. {
  884. /* Check the parameters */
  885. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  886. return hcomp->ErrorCode;
  887. }
  888. /**
  889. * @}
  890. */
  891. /**
  892. * @}
  893. */
  894. /**
  895. * @}
  896. */
  897. #endif /* COMP1 || COMP2 */
  898. #endif /* HAL_COMP_MODULE_ENABLED */
  899. /**
  900. * @}
  901. */