py32f002b_hal_comp.c 30 KB

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