You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1069 lines
36 KiB

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