stm32l5xx_hal_comp.c 32 KB

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