stm32g4xx_hal_comp.c 33 KB

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