stm32f0xx_hal_comp.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_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/de-initialization functions
  9. * + Peripheral Control functions
  10. * + Peripheral State functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ================================================================================
  25. ##### COMP Peripheral features #####
  26. ================================================================================
  27. [..]
  28. The STM32F0xx device family integrates up to 2 analog comparators COMP1 and COMP2:
  29. (+) The non inverting input and inverting input can be set to GPIO pins.
  30. (+) The COMP output is available using HAL_COMP_GetOutputLevel()
  31. and can be set on GPIO pins.
  32. (+) The COMP output can be redirected to embedded timers (TIM1, TIM2 and TIM3).
  33. (+) The comparators COMP1 and COMP2 can be combined in window mode.
  34. (+) The comparators have interrupt capability with wake-up
  35. from Sleep and Stop modes (through the EXTI controller):
  36. (++) COMP1 is internally connected to EXTI Line 21
  37. (++) COMP2 is internally connected to EXTI Line 22
  38. (+) From the corresponding IRQ handler, the right interrupt source can be retrieved with the
  39. macros __HAL_COMP_COMP1_EXTI_GET_FLAG() and __HAL_COMP_COMP2_EXTI_GET_FLAG().
  40. ##### How to use this driver #####
  41. ================================================================================
  42. [..]
  43. This driver provides functions to configure and program the Comparators of STM32F05x, STM32F07x and STM32F09x devices.
  44. To use the comparator, perform the following steps:
  45. (#) Fill in the HAL_COMP_MspInit() to
  46. (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
  47. (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
  48. output to the GPIO pin
  49. (++) If required enable the COMP interrupt by configuring and enabling EXTI line in Interrupt mode and
  50. selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
  51. interrupt vector using HAL_NVIC_EnableIRQ() function.
  52. (#) Configure the comparator using HAL_COMP_Init() function:
  53. (++) Select the inverting input (input minus)
  54. (++) Select the non inverting input (input plus)
  55. (++) Select the output polarity
  56. (++) Select the output redirection
  57. (++) Select the hysteresis level
  58. (++) Select the power mode
  59. (++) Select the event/interrupt mode
  60. (++) Select the window mode
  61. -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order
  62. to access the comparator(s) registers.
  63. (#) Enable the comparator using HAL_COMP_Start() function or HAL_COMP_Start_IT() function for interrupt mode.
  64. (#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions
  65. to manage comparator outputs (event/interrupt triggered and output level).
  66. (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
  67. function.
  68. (#) De-initialize the comparator using HAL_COMP_DeInit() function.
  69. (#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function.
  70. Only a MCU reset can reset that protection.
  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 HAL_COMP_RegisterCallback()
  77. to register an interrupt callback.
  78. [..]
  79. Function HAL_COMP_RegisterCallback() allows to register following callbacks:
  80. (+) OperationCpltCallback : callback for End of operation.
  81. (+) ErrorCallback : callback for error detection.
  82. (+) MspInitCallback : callback for Msp Init.
  83. (+) MspDeInitCallback : callback for Msp DeInit.
  84. This function takes as parameters the HAL peripheral handle, the Callback ID
  85. and a pointer to the user callback function.
  86. [..]
  87. Use function HAL_COMP_UnRegisterCallback to reset a callback to the default
  88. weak function.
  89. [..]
  90. HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
  91. and the Callback ID.
  92. This function allows to reset following callbacks:
  93. (+) OperationCpltCallback : callback for End of operation.
  94. (+) ErrorCallback : callback for error detection.
  95. (+) MspInitCallback : callback for Msp Init.
  96. (+) MspDeInitCallback : callback for Msp DeInit.
  97. [..]
  98. By default, after the HAL_COMP_Init() and when the state is HAL_COMP_STATE_RESET
  99. all callbacks are set to the corresponding weak functions:
  100. examples HAL_COMP_OperationCpltCallback(), HAL_COMP_ErrorCallback().
  101. Exception done for MspInit and MspDeInit functions that are
  102. reset to the legacy weak functions in the HAL_COMP_Init()/ HAL_COMP_DeInit() only when
  103. these callbacks are null (not registered beforehand).
  104. [..]
  105. If MspInit or MspDeInit are not null, the HAL_COMP_Init()/ HAL_COMP_DeInit()
  106. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  107. [..]
  108. Callbacks can be registered/unregistered in HAL_COMP_STATE_READY state only.
  109. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  110. in HAL_COMP_STATE_READY or HAL_COMP_STATE_RESET state,
  111. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  112. [..]
  113. Then, the user first registers the MspInit/MspDeInit user callbacks
  114. using HAL_COMP_RegisterCallback() before calling HAL_COMP_DeInit()
  115. or HAL_COMP_Init() function.
  116. [..]
  117. When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
  118. not defined, the callback registration feature is not available and all callbacks
  119. are set to the corresponding weak functions.
  120. @endverbatim
  121. ******************************************************************************
  122. */
  123. /*
  124. Additional Tables:
  125. Table 1. COMP Inputs for the STM32F05x, STM32F07x and STM32F09x devices
  126. +--------------------------------------------------+
  127. | | | COMP1 | COMP2 |
  128. |-----------------|----------------|---------------|
  129. | | 1/4 VREFINT | OK | OK |
  130. | | 1/2 VREFINT | OK | OK |
  131. | | 3/4 VREFINT | OK | OK |
  132. | Inverting Input | VREFINT | OK | OK |
  133. | | DAC1 OUT (PA4) | OK | OK |
  134. | | DAC2 OUT (PA5) | OK | OK |
  135. | | IO1 | PA0 | PA2 |
  136. |-----------------|----------------|-------|-------|
  137. | Non Inverting | | PA1 | PA3 |
  138. | Input | | | |
  139. +--------------------------------------------------+
  140. Table 2. COMP Outputs for the STM32F05x, STM32F07x and STM32F09x devices
  141. +---------------+
  142. | COMP1 | COMP2 |
  143. |-------|-------|
  144. | PA0 | PA2 |
  145. | PA6 | PA7 |
  146. | PA11 | PA12 |
  147. +---------------+
  148. Table 3. COMP Outputs redirection to embedded timers for the STM32F05x, STM32F07x and STM32F09x devices
  149. +---------------------------------+
  150. | COMP1 | COMP2 |
  151. |----------------|----------------|
  152. | TIM1 BKIN | TIM1 BKIN |
  153. | | |
  154. | TIM1 OCREFCLR | TIM1 OCREFCLR |
  155. | | |
  156. | TIM1 IC1 | TIM1 IC1 |
  157. | | |
  158. | TIM2 IC4 | TIM2 IC4 |
  159. | | |
  160. | TIM2 OCREFCLR | TIM2 OCREFCLR |
  161. | | |
  162. | TIM3 IC1 | TIM3 IC1 |
  163. | | |
  164. | TIM3 OCREFCLR | TIM3 OCREFCLR |
  165. +---------------------------------+
  166. */
  167. /* Includes ------------------------------------------------------------------*/
  168. #include "stm32f0xx_hal.h"
  169. #ifdef HAL_COMP_MODULE_ENABLED
  170. #if defined (COMP1) || defined (COMP2)
  171. /** @addtogroup STM32F0xx_HAL_Driver
  172. * @{
  173. */
  174. /** @defgroup COMP COMP
  175. * @brief COMP HAL module driver
  176. * @{
  177. */
  178. /* Private typedef -----------------------------------------------------------*/
  179. /* Private define ------------------------------------------------------------*/
  180. /** @defgroup COMP_Private_Constants COMP Private Constants
  181. * @{
  182. */
  183. /* Delay for COMP startup time. */
  184. /* Note: Delay required to reach propagation delay specification. */
  185. /* Literal set to maximum value (refer to device datasheet, */
  186. /* parameter "tSTART"). */
  187. /* Unit: us */
  188. #define COMP_DELAY_STARTUP_US (60U) /*!< Delay for COMP startup time */
  189. /* CSR register reset value */
  190. #define COMP_CSR_RESET_VALUE (0x00000000U)
  191. /* CSR register masks */
  192. #define COMP_CSR_RESET_PARAMETERS_MASK (0x00003FFFU)
  193. #define COMP_CSR_UPDATE_PARAMETERS_MASK (0x00003FFEU)
  194. /* CSR COMPx non inverting input mask */
  195. #define COMP_CSR_COMPxNONINSEL_MASK ((uint16_t)COMP_CSR_COMP1SW1)
  196. /* CSR COMP2 shift */
  197. #define COMP_CSR_COMP1_SHIFT 0U
  198. #define COMP_CSR_COMP2_SHIFT 16U
  199. /**
  200. * @}
  201. */
  202. /* Private macro -------------------------------------------------------------*/
  203. /* Private variables ---------------------------------------------------------*/
  204. /* Private function prototypes -----------------------------------------------*/
  205. /* Private functions ---------------------------------------------------------*/
  206. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  207. * @{
  208. */
  209. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  210. * @brief Initialization and Configuration functions
  211. *
  212. @verbatim
  213. ===============================================================================
  214. ##### Initialization and Configuration functions #####
  215. ===============================================================================
  216. [..] This section provides functions to initialize and de-initialize comparators
  217. @endverbatim
  218. * @{
  219. */
  220. /**
  221. * @brief Initializes the COMP according to the specified
  222. * parameters in the COMP_InitTypeDef and create the associated handle.
  223. * @note If the selected comparator is locked, initialization can't be performed.
  224. * To unlock the configuration, perform a system reset.
  225. * @param hcomp COMP handle
  226. * @retval HAL status
  227. */
  228. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  229. {
  230. HAL_StatusTypeDef status = HAL_OK;
  231. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  232. /* Check the COMP handle allocation and lock status */
  233. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  234. {
  235. status = HAL_ERROR;
  236. }
  237. else
  238. {
  239. /* Check the parameter */
  240. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  241. assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
  242. assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
  243. assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
  244. assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  245. assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  246. assert_param(IS_COMP_MODE(hcomp->Init.Mode));
  247. if(hcomp->Init.NonInvertingInput == COMP_NONINVERTINGINPUT_DAC1SWITCHCLOSED)
  248. {
  249. assert_param(IS_COMP_DAC1SWITCH_INSTANCE(hcomp->Instance));
  250. }
  251. if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
  252. {
  253. assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
  254. }
  255. /* Init SYSCFG and the low level hardware to access comparators */
  256. __HAL_RCC_SYSCFG_CLK_ENABLE();
  257. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  258. /* Init the COMP Callback settings */
  259. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  260. if (hcomp->MspInitCallback == NULL)
  261. {
  262. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  263. }
  264. /* Init the low level hardware */
  265. hcomp->MspInitCallback(hcomp);
  266. #else
  267. /* Init the low level hardware : SYSCFG to access comparators */
  268. HAL_COMP_MspInit(hcomp);
  269. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  270. if(hcomp->State == HAL_COMP_STATE_RESET)
  271. {
  272. /* Allocate lock resource and initialize it */
  273. hcomp->Lock = HAL_UNLOCKED;
  274. }
  275. /* Change COMP peripheral state */
  276. hcomp->State = HAL_COMP_STATE_BUSY;
  277. /* Set COMP parameters */
  278. /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
  279. /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */
  280. /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
  281. /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */
  282. /* Set COMPxMODE bits according to hcomp->Init.Mode value */
  283. if(hcomp->Instance == COMP2)
  284. {
  285. regshift = COMP_CSR_COMP2_SHIFT;
  286. }
  287. MODIFY_REG(COMP->CSR,
  288. (COMP_CSR_COMPxINSEL | COMP_CSR_COMPxNONINSEL_MASK | \
  289. COMP_CSR_COMPxOUTSEL | COMP_CSR_COMPxPOL | \
  290. COMP_CSR_COMPxHYST | COMP_CSR_COMPxMODE) << regshift,
  291. (hcomp->Init.InvertingInput | \
  292. hcomp->Init.NonInvertingInput | \
  293. hcomp->Init.Output | \
  294. hcomp->Init.OutputPol | \
  295. hcomp->Init.Hysteresis | \
  296. hcomp->Init.Mode) << regshift);
  297. if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
  298. {
  299. COMP->CSR |= COMP_CSR_WNDWEN;
  300. }
  301. /* Initialize the COMP state*/
  302. hcomp->State = HAL_COMP_STATE_READY;
  303. }
  304. return status;
  305. }
  306. /**
  307. * @brief DeInitializes the COMP peripheral
  308. * @note Deinitialization can't be performed if the COMP configuration is locked.
  309. * To unlock the configuration, perform a system reset.
  310. * @param hcomp COMP handle
  311. * @retval HAL status
  312. */
  313. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  314. {
  315. HAL_StatusTypeDef status = HAL_OK;
  316. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  317. /* Check the COMP handle allocation and lock status */
  318. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  319. {
  320. status = HAL_ERROR;
  321. }
  322. else
  323. {
  324. /* Check the parameter */
  325. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  326. /* Set COMP_CSR register to reset value for the corresponding COMP instance */
  327. if(hcomp->Instance == COMP2)
  328. {
  329. regshift = COMP_CSR_COMP2_SHIFT;
  330. }
  331. MODIFY_REG(COMP->CSR,
  332. COMP_CSR_RESET_PARAMETERS_MASK << regshift,
  333. COMP_CSR_RESET_VALUE << regshift);
  334. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  335. if (hcomp->MspDeInitCallback == NULL)
  336. {
  337. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  338. }
  339. /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  340. hcomp->MspDeInitCallback(hcomp);
  341. #else
  342. /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  343. HAL_COMP_MspDeInit(hcomp);
  344. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  345. hcomp->State = HAL_COMP_STATE_RESET;
  346. /* Release Lock */
  347. __HAL_UNLOCK(hcomp);
  348. }
  349. return status;
  350. }
  351. /**
  352. * @brief Initializes the COMP MSP.
  353. * @param hcomp COMP handle
  354. * @retval None
  355. */
  356. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  357. {
  358. /* Prevent unused argument(s) compilation warning */
  359. UNUSED(hcomp);
  360. /* NOTE : This function Should not be modified, when the callback is needed,
  361. the HAL_COMP_MspInit could be implenetd in the user file
  362. */
  363. }
  364. /**
  365. * @brief DeInitializes COMP MSP.
  366. * @param hcomp COMP handle
  367. * @retval None
  368. */
  369. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  370. {
  371. /* Prevent unused argument(s) compilation warning */
  372. UNUSED(hcomp);
  373. /* NOTE : This function Should not be modified, when the callback is needed,
  374. the HAL_COMP_MspDeInit could be implenetd in the user file
  375. */
  376. }
  377. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  378. /**
  379. * @brief Register a User COMP Callback
  380. * To be used instead of the weak predefined callback
  381. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  382. * the configuration information for the specified COMP.
  383. * @param CallbackID ID of the callback to be registered
  384. * This parameter can be one of the following values:
  385. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  386. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  387. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  388. * @param pCallback pointer to the Callback function
  389. * @retval HAL status
  390. */
  391. HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
  392. {
  393. HAL_StatusTypeDef status = HAL_OK;
  394. if (pCallback == NULL)
  395. {
  396. /* Update the error code */
  397. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  398. return HAL_ERROR;
  399. }
  400. if (HAL_COMP_STATE_READY == hcomp->State)
  401. {
  402. switch (CallbackID)
  403. {
  404. case HAL_COMP_TRIGGER_CB_ID :
  405. hcomp->TriggerCallback = pCallback;
  406. break;
  407. case HAL_COMP_MSPINIT_CB_ID :
  408. hcomp->MspInitCallback = pCallback;
  409. break;
  410. case HAL_COMP_MSPDEINIT_CB_ID :
  411. hcomp->MspDeInitCallback = pCallback;
  412. break;
  413. default :
  414. /* Update the error code */
  415. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  416. /* Return error status */
  417. status = HAL_ERROR;
  418. break;
  419. }
  420. }
  421. else if (HAL_COMP_STATE_RESET == hcomp->State)
  422. {
  423. switch (CallbackID)
  424. {
  425. case HAL_COMP_MSPINIT_CB_ID :
  426. hcomp->MspInitCallback = pCallback;
  427. break;
  428. case HAL_COMP_MSPDEINIT_CB_ID :
  429. hcomp->MspDeInitCallback = pCallback;
  430. break;
  431. default :
  432. /* Update the error code */
  433. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  434. /* Return error status */
  435. status = HAL_ERROR;
  436. break;
  437. }
  438. }
  439. else
  440. {
  441. /* Update the error code */
  442. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  443. /* Return error status */
  444. status = HAL_ERROR;
  445. }
  446. return status;
  447. }
  448. /**
  449. * @brief Unregister a COMP Callback
  450. * COMP callback is redirected to the weak predefined callback
  451. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  452. * the configuration information for the specified COMP.
  453. * @param CallbackID ID of the callback to be unregistered
  454. * This parameter can be one of the following values:
  455. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  456. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  457. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  458. * @retval HAL status
  459. */
  460. HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
  461. {
  462. HAL_StatusTypeDef status = HAL_OK;
  463. if (HAL_COMP_STATE_READY == hcomp->State)
  464. {
  465. switch (CallbackID)
  466. {
  467. case HAL_COMP_TRIGGER_CB_ID :
  468. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  469. break;
  470. case HAL_COMP_MSPINIT_CB_ID :
  471. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  472. break;
  473. case HAL_COMP_MSPDEINIT_CB_ID :
  474. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  475. break;
  476. default :
  477. /* Update the error code */
  478. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  479. /* Return error status */
  480. status = HAL_ERROR;
  481. break;
  482. }
  483. }
  484. else if (HAL_COMP_STATE_RESET == hcomp->State)
  485. {
  486. switch (CallbackID)
  487. {
  488. case HAL_COMP_MSPINIT_CB_ID :
  489. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  490. break;
  491. case HAL_COMP_MSPDEINIT_CB_ID :
  492. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  493. break;
  494. default :
  495. /* Update the error code */
  496. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  497. /* Return error status */
  498. status = HAL_ERROR;
  499. break;
  500. }
  501. }
  502. else
  503. {
  504. /* Update the error code */
  505. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  506. /* Return error status */
  507. status = HAL_ERROR;
  508. }
  509. return status;
  510. }
  511. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  512. /**
  513. * @}
  514. */
  515. /** @defgroup COMP_Exported_Functions_Group2 I/O operation functions
  516. * @brief Data transfers functions
  517. *
  518. @verbatim
  519. ===============================================================================
  520. ##### IO operation functions #####
  521. ===============================================================================
  522. [..]
  523. This subsection provides a set of functions allowing to manage the COMP data
  524. transfers.
  525. @endverbatim
  526. * @{
  527. */
  528. /**
  529. * @brief Start the comparator
  530. * @param hcomp COMP handle
  531. * @retval HAL status
  532. */
  533. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  534. {
  535. uint32_t wait_loop_index = 0U;
  536. HAL_StatusTypeDef status = HAL_OK;
  537. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  538. /* Check the COMP handle allocation and lock status */
  539. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  540. {
  541. status = HAL_ERROR;
  542. }
  543. else
  544. {
  545. /* Check the parameter */
  546. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  547. if(hcomp->State == HAL_COMP_STATE_READY)
  548. {
  549. /* Enable the selected comparator */
  550. if(hcomp->Instance == COMP2)
  551. {
  552. regshift = COMP_CSR_COMP2_SHIFT;
  553. }
  554. SET_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift);
  555. /* Set HAL COMP handle state */
  556. hcomp->State = HAL_COMP_STATE_BUSY;
  557. /* Delay for COMP startup time */
  558. wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / 1000000U));
  559. while(wait_loop_index != 0U)
  560. {
  561. wait_loop_index--;
  562. }
  563. }
  564. else
  565. {
  566. status = HAL_ERROR;
  567. }
  568. }
  569. return status;
  570. }
  571. /**
  572. * @brief Stop the comparator
  573. * @param hcomp COMP handle
  574. * @retval HAL status
  575. */
  576. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  577. {
  578. HAL_StatusTypeDef status = HAL_OK;
  579. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  580. /* Check the COMP handle allocation and lock status */
  581. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  582. {
  583. status = HAL_ERROR;
  584. }
  585. else
  586. {
  587. /* Check the parameter */
  588. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  589. if(hcomp->State == HAL_COMP_STATE_BUSY)
  590. {
  591. /* Disable the selected comparator */
  592. if(hcomp->Instance == COMP2)
  593. {
  594. regshift = COMP_CSR_COMP2_SHIFT;
  595. }
  596. CLEAR_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift);
  597. hcomp->State = HAL_COMP_STATE_READY;
  598. }
  599. else
  600. {
  601. status = HAL_ERROR;
  602. }
  603. }
  604. return status;
  605. }
  606. /**
  607. * @brief Enables the interrupt and starts the comparator
  608. * @param hcomp COMP handle
  609. * @retval HAL status.
  610. */
  611. HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
  612. {
  613. HAL_StatusTypeDef status = HAL_OK;
  614. uint32_t extiline = 0U;
  615. /* Check the parameter */
  616. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  617. status = HAL_COMP_Start(hcomp);
  618. if(status == HAL_OK)
  619. {
  620. /* Check the Exti Line output configuration */
  621. extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  622. /* Configure the rising edge */
  623. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
  624. {
  625. SET_BIT(EXTI->RTSR, extiline);
  626. }
  627. else
  628. {
  629. CLEAR_BIT(EXTI->RTSR, extiline);
  630. }
  631. /* Configure the falling edge */
  632. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
  633. {
  634. SET_BIT(EXTI->FTSR, extiline);
  635. }
  636. else
  637. {
  638. CLEAR_BIT(EXTI->FTSR, extiline);
  639. }
  640. /* Clear COMP EXTI pending bit */
  641. WRITE_REG(EXTI->PR, extiline);
  642. /* Enable Exti interrupt mode */
  643. SET_BIT(EXTI->IMR, extiline);
  644. }
  645. return status;
  646. }
  647. /**
  648. * @brief Disable the interrupt and Stop the comparator
  649. * @param hcomp COMP handle
  650. * @retval HAL status
  651. */
  652. HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
  653. {
  654. HAL_StatusTypeDef status = HAL_OK;
  655. /* Disable the Exti Line interrupt mode */
  656. CLEAR_BIT(EXTI->IMR, COMP_GET_EXTI_LINE(hcomp->Instance));
  657. status = HAL_COMP_Stop(hcomp);
  658. return status;
  659. }
  660. /**
  661. * @brief Comparator IRQ Handler
  662. * @param hcomp COMP handle
  663. * @retval HAL status
  664. */
  665. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  666. {
  667. uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  668. /* Check COMP Exti flag */
  669. if(READ_BIT(EXTI->PR, extiline) != RESET)
  670. {
  671. /* Clear COMP Exti pending bit */
  672. WRITE_REG(EXTI->PR, extiline);
  673. /* COMP trigger callback */
  674. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  675. hcomp->TriggerCallback(hcomp);
  676. #else
  677. HAL_COMP_TriggerCallback(hcomp);
  678. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  679. }
  680. }
  681. /**
  682. * @}
  683. */
  684. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  685. * @brief management functions
  686. *
  687. @verbatim
  688. ===============================================================================
  689. ##### Peripheral Control functions #####
  690. ===============================================================================
  691. [..]
  692. This subsection provides a set of functions allowing to control the COMP data
  693. transfers.
  694. @endverbatim
  695. * @{
  696. */
  697. /**
  698. * @brief Lock the selected comparator configuration.
  699. * @param hcomp COMP handle
  700. * @retval HAL status
  701. */
  702. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  703. {
  704. HAL_StatusTypeDef status = HAL_OK;
  705. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  706. /* Check the COMP handle allocation and lock status */
  707. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  708. {
  709. status = HAL_ERROR;
  710. }
  711. else
  712. {
  713. /* Check the parameter */
  714. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  715. /* Set lock flag */
  716. hcomp->State |= COMP_STATE_BIT_LOCK;
  717. /* Set the lock bit corresponding to selected comparator */
  718. if(hcomp->Instance == COMP2)
  719. {
  720. regshift = COMP_CSR_COMP2_SHIFT;
  721. }
  722. SET_BIT(COMP->CSR, COMP_CSR_COMPxLOCK << regshift);
  723. }
  724. return status;
  725. }
  726. /**
  727. * @brief Return the output level (high or low) of the selected comparator.
  728. * The output level depends on the selected polarity.
  729. * If the polarity is not inverted:
  730. * - Comparator output is low when the non-inverting input is at a lower
  731. * voltage than the inverting input
  732. * - Comparator output is high when the non-inverting input is at a higher
  733. * voltage than the inverting input
  734. * If the polarity is inverted:
  735. * - Comparator output is high when the non-inverting input is at a lower
  736. * voltage than the inverting input
  737. * - Comparator output is low when the non-inverting input is at a higher
  738. * voltage than the inverting input
  739. * @param hcomp COMP handle
  740. * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH.
  741. *
  742. */
  743. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  744. {
  745. uint32_t level=0;
  746. uint32_t regshift = COMP_CSR_COMP1_SHIFT;
  747. /* Check the parameter */
  748. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  749. if(hcomp->Instance == COMP2)
  750. {
  751. regshift = COMP_CSR_COMP2_SHIFT;
  752. }
  753. level = READ_BIT(COMP->CSR, COMP_CSR_COMPxOUT << regshift);
  754. if(level != 0U)
  755. {
  756. return(COMP_OUTPUTLEVEL_HIGH);
  757. }
  758. return(COMP_OUTPUTLEVEL_LOW);
  759. }
  760. /**
  761. * @brief Comparator trigger callback.
  762. * @param hcomp COMP handle
  763. * @retval None
  764. */
  765. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  766. {
  767. /* Prevent unused argument(s) compilation warning */
  768. UNUSED(hcomp);
  769. /* NOTE : This function should not be modified, when the callback is needed,
  770. the HAL_COMP_TriggerCallback should be implemented in the user file
  771. */
  772. }
  773. /**
  774. * @}
  775. */
  776. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  777. * @brief Peripheral State functions
  778. *
  779. @verbatim
  780. ===============================================================================
  781. ##### Peripheral State functions #####
  782. ===============================================================================
  783. [..]
  784. This subsection permit to get in run-time the status of the peripheral
  785. and the data flow.
  786. @endverbatim
  787. * @{
  788. */
  789. /**
  790. * @brief Return the COMP state
  791. * @param hcomp COMP handle
  792. * @retval HAL state
  793. */
  794. uint32_t HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  795. {
  796. /* Check the COMP handle allocation */
  797. if(hcomp == NULL)
  798. {
  799. return HAL_COMP_STATE_RESET;
  800. }
  801. /* Check the parameter */
  802. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  803. return hcomp->State;
  804. }
  805. /**
  806. * @brief Return the COMP error code.
  807. * @param hcomp COMP handle
  808. * @retval COMP error code
  809. */
  810. uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
  811. {
  812. /* Check the parameters */
  813. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  814. return hcomp->ErrorCode;
  815. }
  816. /**
  817. * @}
  818. */
  819. /**
  820. * @}
  821. */
  822. /**
  823. * @}
  824. */
  825. /**
  826. * @}
  827. */
  828. #endif /* COMP1 || COMP2 */
  829. #endif /* HAL_COMP_MODULE_ENABLED */