py32f0xx_hal_comp.c 33 KB

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