stm32l4xx_hal_comp.c 34 KB

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