stm32f3xx_hal_comp.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_hal_comp.c
  4. * @author MCD Application Team
  5. * @brief COMP HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the COMP peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. * + Peripheral State functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 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 STM32F3xx device family integrates up to 7 analog comparators COMP1, COMP2...COMP7:
  29. (#) The non inverting input and inverting input can be set to GPIO pins.
  30. For STM32F3xx devices please refer to the COMP peripheral section in corresponding
  31. Reference Manual.
  32. (#) The COMP output is available using HAL_COMP_GetOutputLevel()
  33. and can be set on GPIO pins.
  34. For STM32F3xx devices please refer to the COMP peripheral section in corresponding
  35. Reference Manual.
  36. (#) The COMP output can be redirected to embedded timers (TIM1, TIM2, TIM3...).
  37. For STM32F3xx devices please refer to the COMP peripheral section in corresponding
  38. Reference Manual.
  39. (#) Each couple of comparators COMP1 and COMP2, COMP3 and COMP4, COMP5 and COMP6 can be combined in window
  40. mode and respectively COMP1, COMP3 and COMP5 non inverting input is used as common non-inverting input.
  41. (#) The seven comparators have interrupt capability with wake-up
  42. from Sleep and Stop modes (through the EXTI controller):
  43. (++) COMP1 is internally connected to EXTI Line 21
  44. (++) COMP2 is internally connected to EXTI Line 22
  45. (++) COMP3 is internally connected to EXTI Line 29
  46. (++) COMP4 is internally connected to EXTI Line 30
  47. (++) COMP5 is internally connected to EXTI Line 31
  48. (++) COMP6 is internally connected to EXTI Line 32
  49. (++) COMP7 is internally connected to EXTI Line 33.
  50. From the corresponding IRQ handler, the right interrupt source can be retrieved with the
  51. adequate macro __HAL_COMP_COMPx_EXTI_GET_FLAG().
  52. ##### How to use this driver #####
  53. ================================================================================
  54. [..]
  55. This driver provides functions to configure and program the Comparators of all STM32F3xx devices.
  56. To use the comparator, perform the following steps:
  57. (#) Fill in the HAL_COMP_MspInit() to
  58. (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
  59. (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
  60. output to the GPIO pin
  61. (++) If required enable the COMP interrupt (EXTI line Interrupt): by configuring and enabling EXTI line in Interrupt mode and
  62. selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
  63. interrupt vector using HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() functions.
  64. (#) Configure the comparator using HAL_COMP_Init() function:
  65. (++) Select the inverting input (input minus)
  66. (++) Select the non-inverting input (input plus)
  67. (++) Select the output polarity
  68. (++) Select the output redirection
  69. (++) Select the hysteresis level
  70. (++) Select the power mode
  71. (++) Select the event/interrupt mode
  72. -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order
  73. to enable the comparator(s).
  74. (#) On-the-fly reconfiguration of comparator(s) may be done by calling again HAL_COMP_Init(
  75. function with new input parameter values; HAL_COMP_MspInit() function shall be adapted
  76. to support multi configurations.
  77. (#) Enable the comparator using HAL_COMP_Start() or HAL_COMP_Start_IT() functions.
  78. (#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions
  79. to manage comparator outputs (events and output level).
  80. (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
  81. function.
  82. (#) De-initialize the comparator using HAL_COMP_DeInit() function.
  83. (#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function.
  84. Only a MCU reset can reset that protection.
  85. *** Callback registration ***
  86. =============================================
  87. [..]
  88. The compilation flag USE_HAL_COMP_REGISTER_CALLBACKS, when set to 1,
  89. allows the user to configure dynamically the driver callbacks.
  90. Use Functions HAL_COMP_RegisterCallback()
  91. to register an interrupt callback.
  92. [..]
  93. Function HAL_COMP_RegisterCallback() allows to register following callbacks:
  94. (+) OperationCpltCallback : callback for End of operation.
  95. (+) ErrorCallback : callback for error detection.
  96. (+) MspInitCallback : callback for Msp Init.
  97. (+) MspDeInitCallback : callback for Msp DeInit.
  98. This function takes as parameters the HAL peripheral handle, the Callback ID
  99. and a pointer to the user callback function.
  100. [..]
  101. Use function HAL_COMP_UnRegisterCallback to reset a callback to the default
  102. weak function.
  103. [..]
  104. HAL_COMP_UnRegisterCallback takes as parameters the HAL peripheral handle,
  105. and the Callback ID.
  106. This function allows to reset following callbacks:
  107. (+) OperationCpltCallback : callback for End of operation.
  108. (+) ErrorCallback : callback for error detection.
  109. (+) MspInitCallback : callback for Msp Init.
  110. (+) MspDeInitCallback : callback for Msp DeInit.
  111. [..]
  112. By default, after the HAL_COMP_Init() and when the state is HAL_COMP_STATE_RESET
  113. all callbacks are set to the corresponding weak functions:
  114. examples HAL_COMP_OperationCpltCallback(), HAL_COMP_ErrorCallback().
  115. Exception done for MspInit and MspDeInit functions that are
  116. reset to the legacy weak functions in the HAL_COMP_Init()/ HAL_COMP_DeInit() only when
  117. these callbacks are null (not registered beforehand).
  118. [..]
  119. If MspInit or MspDeInit are not null, the HAL_COMP_Init()/ HAL_COMP_DeInit()
  120. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  121. [..]
  122. Callbacks can be registered/unregistered in HAL_COMP_STATE_READY state only.
  123. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  124. in HAL_COMP_STATE_READY or HAL_COMP_STATE_RESET state,
  125. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  126. [..]
  127. Then, the user first registers the MspInit/MspDeInit user callbacks
  128. using HAL_COMP_RegisterCallback() before calling HAL_COMP_DeInit()
  129. or HAL_COMP_Init() function.
  130. [..]
  131. When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or
  132. not defined, the callback registration feature is not available and all callbacks
  133. are set to the corresponding weak functions.
  134. ******************************************************************************
  135. */
  136. /*
  137. Additional Tables:
  138. Table 1. COMP Inputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
  139. +------------------------------------------------------------------------------------------+
  140. | | | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  141. |-----------------|----------------|---------------|---------------------------------------|
  142. | | 1U/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
  143. | | 1U/2 VREFINT | OK | OK | OK | OK | OK | OK | OK |
  144. | | 3U/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
  145. | Inverting Input | VREFINT | OK | OK | OK | OK | OK | OK | OK |
  146. | | DAC1 OUT (PA4) | OK | OK | OK | OK | OK | OK | OK |
  147. | | DAC2 OUT (PA5) | OK | OK | OK | OK | OK | OK | OK |
  148. | | IO1 | PA0 | PA2 | PD15U | PE8 | PD13U | PD10U | PC0 |
  149. | | IO2 | --- | --- | PB12U | PB2 | PB10U | PB15U | --- |
  150. |-----------------|----------------|-------|-------|-------|-------|-------|-------|-------|
  151. | Non Inverting | IO1 | PA1 | PA7 | PB14U | PB0 | PD12U | PD11U | PA0 |
  152. | Input | IO2 | --- | PA3 | PD14U | PE7 | PB13U | PB11U | PC1 |
  153. +------------------------------------------------------------------------------------------+
  154. Table 2. COMP Outputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
  155. +-------------------------------------------------------+
  156. | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  157. |-------|-------|-------|-------|-------|-------|-------|
  158. | PA0 | PA2 | PB1 | PC8 | PC7 | PA10U | PC2 |
  159. | PF4 | PA7 | --- | PA8 | PA9 | PC6 | --- |
  160. | PA6 | PA12U | --- | --- | --- | --- | --- |
  161. | PA11U | PB9 | --- | --- | --- | --- | --- |
  162. | PB8 | --- | --- | --- | --- | --- | --- |
  163. +-------------------------------------------------------+
  164. Table 3. COMP Outputs redirection to embedded timers for the STM32F303xB/STM32F303xC devices
  165. +----------------------------------------------------------------------------------------------------------------------+
  166. | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  167. |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
  168. | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN |
  169. | | | | | | | |
  170. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  171. | | | | | | | |
  172. | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN |
  173. | | | | | | | |
  174. | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
  175. | | | | | | | |
  176. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  177. | + | + | + | + | + | + | + |
  178. | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
  179. | | | | | | | |
  180. | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
  181. | | | | | | | |
  182. | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
  183. | | | | | | | |
  184. | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
  185. | | | | | | | |
  186. | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
  187. | | | | | | | |
  188. | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
  189. | | | | | | | |
  190. | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
  191. +----------------------------------------------------------------------------------------------------------------------+
  192. Table 4. COMP Outputs redirection to embedded timers for the STM32F303xE devices
  193. +----------------------------------------------------------------------------------------------------------------------+
  194. | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  195. |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
  196. | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1U) | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1U) |
  197. | | | | | | | |
  198. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  199. | | | | | | | |
  200. | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1U) | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1U) |
  201. | | | | | | | |
  202. | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
  203. | | | | | | | |
  204. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  205. | + | + | + | + | + | + | + |
  206. | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
  207. | | | | | | | |
  208. | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
  209. | | | | | | | |
  210. | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
  211. | | | | | | | |
  212. | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
  213. | | | | | | | |
  214. | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
  215. | | | | | | | |
  216. | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
  217. | | | | | | | |
  218. | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
  219. | | | | | | | |
  220. | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1U)| TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1U)|
  221. | | | | | | | |
  222. | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
  223. | | | | | | | |
  224. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  225. | + | + | + | + | + | + | + |
  226. | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
  227. | + | + | + | + | + | + | + |
  228. | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
  229. | | | | | | | |
  230. +----------------------------------------------------------------------------------------------------------------------+
  231. (1U): This connection consists of connecting both GPIO and COMP output to TIM1/8U/20 BRK input through an OR gate, instead
  232. of connecting the GPIO to the TIM1/8U/20 BRK input and the COMP output to the TIM1/8U/20 BRK_ACTH input. The aim is to
  233. add a digital filter (3 bits) on the COMP output.
  234. Table 5. COMP Outputs blanking sources for the STM32F303xB/STM32F303xC/STM32F303xE devices
  235. +----------------------------------------------------------------------------------------------------------------------+
  236. | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  237. |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
  238. | TIM1 OC5 | TIM1 OC5 | TIM1 OC5 | TIM3 OC4 | -------- | TIM8 OC5 | TIM1 OC5 |
  239. | | | | | | | |
  240. | TIM2 OC3 | TIM2 OC3 | -------- | TIM8 OC5 | TIM3 OC3 | TIM2 OC4 | TIM8 OC5 |
  241. | | | | | | | |
  242. | TIM3 OC3 | TIM3 OC3 | TIM2 OC4 | TIM15 OC1 | TIM8 OC5 | TIM15 OC2 | TIM15 OC2 |
  243. | | | | | | | |
  244. +----------------------------------------------------------------------------------------------------------------------+
  245. */
  246. /* Includes ------------------------------------------------------------------*/
  247. #include "stm32f3xx_hal.h"
  248. /** @addtogroup STM32F3xx_HAL_Driver
  249. * @{
  250. */
  251. /** @defgroup COMP COMP
  252. * @brief COMP HAL module driver
  253. * @{
  254. */
  255. #ifdef HAL_COMP_MODULE_ENABLED
  256. /* Private typedef -----------------------------------------------------------*/
  257. /* Private define ------------------------------------------------------------*/
  258. /** @defgroup COMP_Private_Constants COMP Private Constants
  259. * @{
  260. */
  261. #define COMP_LOCK_DISABLE (0x00000000U)
  262. #define COMP_LOCK_ENABLE COMP_CSR_COMPxLOCK
  263. /* Delay for COMP startup time. */
  264. /* Note: Delay required to reach propagation delay specification. */
  265. /* Literal set to maximum value (refer to device datasheet, */
  266. /* parameter "tSTART"). */
  267. /* Unit: us */
  268. #define COMP_DELAY_STARTUP_US (80UL) /*!< Delay for COMP startup time */
  269. /* Delay for COMP voltage scaler stabilization time. */
  270. /* Literal set to maximum value (refer to device datasheet, */
  271. /* parameter "tSTART_SCALER"). */
  272. /* Unit: us */
  273. #define COMP_DELAY_VOLTAGE_SCALER_STAB_US (200UL) /*!< Delay for COMP voltage scaler stabilization time */
  274. /**
  275. * @}
  276. */
  277. /* Private macro -------------------------------------------------------------*/
  278. /* Private variables ---------------------------------------------------------*/
  279. /* Private function prototypes -----------------------------------------------*/
  280. /* Exported functions --------------------------------------------------------*/
  281. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  282. * @{
  283. */
  284. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  285. * @brief Initialization and de-initialization functions.
  286. *
  287. @verbatim
  288. ===============================================================================
  289. ##### Initialization and de-initialization functions #####
  290. ===============================================================================
  291. [..] This section provides functions to initialize and de-initialize comparators.
  292. @endverbatim
  293. * @{
  294. */
  295. /**
  296. * @brief Initialize the COMP peripheral according to the specified
  297. * parameters in the COMP_InitTypeDef and initialize the associated handle.
  298. * @note If the selected comparator is locked, initialization cannot be performed.
  299. * To unlock the configuration, perform a system reset.
  300. * @param hcomp COMP handle
  301. * @retval HAL status
  302. */
  303. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  304. {
  305. uint32_t comp_voltage_scaler_initialized; /* Value "0" if comparator voltage scaler is not initialized */
  306. __IO uint32_t wait_loop_index = 0UL;
  307. HAL_StatusTypeDef status = HAL_OK;
  308. /* Check the COMP handle allocation and lock status */
  309. if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  310. {
  311. status = HAL_ERROR;
  312. }
  313. else
  314. {
  315. /* Check the parameters */
  316. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  317. assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
  318. assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
  319. assert_param(IS_COMP_NONINVERTINGINPUT_INSTANCE(hcomp->Instance, hcomp->Init.NonInvertingInput));
  320. assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
  321. assert_param(IS_COMP_OUTPUT_INSTANCE(hcomp->Instance, hcomp->Init.Output));
  322. assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  323. assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  324. assert_param(IS_COMP_MODE(hcomp->Init.Mode));
  325. assert_param(IS_COMP_BLANKINGSRCE(hcomp->Init.BlankingSrce));
  326. assert_param(IS_COMP_BLANKINGSRCE_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
  327. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  328. if (hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
  329. {
  330. assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
  331. assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
  332. }
  333. /* Init SYSCFG and the low level hardware to access comparators */
  334. __HAL_RCC_SYSCFG_CLK_ENABLE();
  335. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  336. /* Init the COMP Callback settings */
  337. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  338. if (hcomp->MspInitCallback == NULL)
  339. {
  340. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  341. }
  342. /* Init the low level hardware */
  343. hcomp->MspInitCallback(hcomp);
  344. #else
  345. /* Init the low level hardware : SYSCFG to access comparators */
  346. HAL_COMP_MspInit(hcomp);
  347. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  348. /* Memorize voltage scaler state before initialization */
  349. comp_voltage_scaler_initialized = READ_BIT(hcomp->Instance->CSR, (COMP_CSR_COMPxINSEL_1 | COMP_CSR_COMPxINSEL_0));
  350. if (hcomp->State == HAL_COMP_STATE_RESET)
  351. {
  352. /* Allocate lock resource and initialize it */
  353. hcomp->Lock = HAL_UNLOCKED;
  354. }
  355. /* Manage inverting input comparator inverting input connected to a GPIO */
  356. /* for STM32F302x, STM32F32xx, STM32F33x. */
  357. hcomp->Init.InvertingInput = COMP_INVERTINGINPUT_SELECTION(hcomp->Instance, hcomp->Init.InvertingInput);
  358. /* Set COMP parameters */
  359. /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
  360. /* Set COMPxNONINSEL bits according to hcomp->Init.NonInvertingInput value */
  361. /* Set COMPxBLANKING bits according to hcomp->Init.BlankingSrce value */
  362. /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */
  363. /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
  364. /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */
  365. /* Set COMPxMODE bits according to hcomp->Init.Mode value */
  366. COMP_INIT(hcomp);
  367. /* Delay for COMP scaler bridge voltage stabilization */
  368. /* Apply the delay if voltage scaler bridge is required and not already enabled */
  369. if ((READ_BIT(hcomp->Instance->CSR, (COMP_CSR_COMPxINSEL_1 | COMP_CSR_COMPxINSEL_0)) != 0UL) &&
  370. (comp_voltage_scaler_initialized == 0UL))
  371. {
  372. /* Wait loop initialization and execution */
  373. /* Note: Variable divided by 2 to compensate partially */
  374. /* CPU processing cycles, scaling in us split to not */
  375. /* exceed 32 bits register capacity and handle low frequency. */
  376. wait_loop_index = ((COMP_DELAY_VOLTAGE_SCALER_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  377. while (wait_loop_index != 0UL)
  378. {
  379. wait_loop_index--;
  380. }
  381. }
  382. /* Initialize the COMP state*/
  383. hcomp->State = HAL_COMP_STATE_READY;
  384. }
  385. return status;
  386. }
  387. /**
  388. * @brief DeInitialize the COMP peripheral.
  389. * @note If the selected comparator is locked, deinitialization cannot be performed.
  390. * To unlock the configuration, perform a system reset.
  391. * @param hcomp COMP handle
  392. * @retval HAL status
  393. */
  394. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  395. {
  396. HAL_StatusTypeDef status = HAL_OK;
  397. /* Check the COMP handle allocation and lock status */
  398. if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  399. {
  400. status = HAL_ERROR;
  401. }
  402. else
  403. {
  404. /* Check the parameter */
  405. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  406. /* Set COMP_CSR register to reset value */
  407. COMP_DEINIT(hcomp);
  408. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  409. if (hcomp->MspDeInitCallback == NULL)
  410. {
  411. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  412. }
  413. /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  414. hcomp->MspDeInitCallback(hcomp);
  415. #else
  416. /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  417. HAL_COMP_MspDeInit(hcomp);
  418. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  419. hcomp->State = HAL_COMP_STATE_RESET;
  420. /* Release Lock */
  421. __HAL_UNLOCK(hcomp);
  422. }
  423. return status;
  424. }
  425. /**
  426. * @brief Initialize the COMP MSP.
  427. * @param hcomp COMP handle
  428. * @retval None
  429. */
  430. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  431. {
  432. /* Prevent unused argument(s) compilation warning */
  433. UNUSED(hcomp);
  434. /* NOTE : This function should not be modified, when the callback is needed,
  435. the HAL_COMP_MspInit could be implemented in the user file
  436. */
  437. }
  438. /**
  439. * @brief DeInitialize the COMP MSP.
  440. * @param hcomp COMP handle
  441. * @retval None
  442. */
  443. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  444. {
  445. /* Prevent unused argument(s) compilation warning */
  446. UNUSED(hcomp);
  447. /* NOTE : This function should not be modified, when the callback is needed,
  448. the HAL_COMP_MspDeInit could be implemented in the user file
  449. */
  450. }
  451. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  452. /**
  453. * @brief Register a User COMP Callback
  454. * To be used instead of the weak predefined callback
  455. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  456. * the configuration information for the specified COMP.
  457. * @param CallbackID ID of the callback to be registered
  458. * This parameter can be one of the following values:
  459. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  460. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  461. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  462. * @param pCallback pointer to the Callback function
  463. * @retval HAL status
  464. */
  465. HAL_StatusTypeDef HAL_COMP_RegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID, pCOMP_CallbackTypeDef pCallback)
  466. {
  467. HAL_StatusTypeDef status = HAL_OK;
  468. if (pCallback == NULL)
  469. {
  470. /* Update the error code */
  471. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  472. return HAL_ERROR;
  473. }
  474. if (HAL_COMP_STATE_READY == hcomp->State)
  475. {
  476. switch (CallbackID)
  477. {
  478. case HAL_COMP_TRIGGER_CB_ID :
  479. hcomp->TriggerCallback = pCallback;
  480. break;
  481. case HAL_COMP_MSPINIT_CB_ID :
  482. hcomp->MspInitCallback = pCallback;
  483. break;
  484. case HAL_COMP_MSPDEINIT_CB_ID :
  485. hcomp->MspDeInitCallback = pCallback;
  486. break;
  487. default :
  488. /* Update the error code */
  489. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  490. /* Return error status */
  491. status = HAL_ERROR;
  492. break;
  493. }
  494. }
  495. else if (HAL_COMP_STATE_RESET == hcomp->State)
  496. {
  497. switch (CallbackID)
  498. {
  499. case HAL_COMP_MSPINIT_CB_ID :
  500. hcomp->MspInitCallback = pCallback;
  501. break;
  502. case HAL_COMP_MSPDEINIT_CB_ID :
  503. hcomp->MspDeInitCallback = pCallback;
  504. break;
  505. default :
  506. /* Update the error code */
  507. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  508. /* Return error status */
  509. status = HAL_ERROR;
  510. break;
  511. }
  512. }
  513. else
  514. {
  515. /* Update the error code */
  516. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  517. /* Return error status */
  518. status = HAL_ERROR;
  519. }
  520. return status;
  521. }
  522. /**
  523. * @brief Unregister a COMP Callback
  524. * COMP callback is redirected to the weak predefined callback
  525. * @param hcomp Pointer to a COMP_HandleTypeDef structure that contains
  526. * the configuration information for the specified COMP.
  527. * @param CallbackID ID of the callback to be unregistered
  528. * This parameter can be one of the following values:
  529. * @arg @ref HAL_COMP_TRIGGER_CB_ID Trigger callback ID
  530. * @arg @ref HAL_COMP_MSPINIT_CB_ID MspInit callback ID
  531. * @arg @ref HAL_COMP_MSPDEINIT_CB_ID MspDeInit callback ID
  532. * @retval HAL status
  533. */
  534. HAL_StatusTypeDef HAL_COMP_UnRegisterCallback(COMP_HandleTypeDef *hcomp, HAL_COMP_CallbackIDTypeDef CallbackID)
  535. {
  536. HAL_StatusTypeDef status = HAL_OK;
  537. if (HAL_COMP_STATE_READY == hcomp->State)
  538. {
  539. switch (CallbackID)
  540. {
  541. case HAL_COMP_TRIGGER_CB_ID :
  542. hcomp->TriggerCallback = HAL_COMP_TriggerCallback; /* Legacy weak callback */
  543. break;
  544. case HAL_COMP_MSPINIT_CB_ID :
  545. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  546. break;
  547. case HAL_COMP_MSPDEINIT_CB_ID :
  548. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  549. break;
  550. default :
  551. /* Update the error code */
  552. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  553. /* Return error status */
  554. status = HAL_ERROR;
  555. break;
  556. }
  557. }
  558. else if (HAL_COMP_STATE_RESET == hcomp->State)
  559. {
  560. switch (CallbackID)
  561. {
  562. case HAL_COMP_MSPINIT_CB_ID :
  563. hcomp->MspInitCallback = HAL_COMP_MspInit; /* Legacy weak MspInit */
  564. break;
  565. case HAL_COMP_MSPDEINIT_CB_ID :
  566. hcomp->MspDeInitCallback = HAL_COMP_MspDeInit; /* Legacy weak MspDeInit */
  567. break;
  568. default :
  569. /* Update the error code */
  570. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  571. /* Return error status */
  572. status = HAL_ERROR;
  573. break;
  574. }
  575. }
  576. else
  577. {
  578. /* Update the error code */
  579. hcomp->ErrorCode |= HAL_COMP_ERROR_INVALID_CALLBACK;
  580. /* Return error status */
  581. status = HAL_ERROR;
  582. }
  583. return status;
  584. }
  585. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  586. /**
  587. * @}
  588. */
  589. /** @defgroup COMP_Exported_Functions_Group2 Start Stop operation functions
  590. * @brief Start-Stop operation functions.
  591. *
  592. @verbatim
  593. ===============================================================================
  594. ##### Start Stop operation functions #####
  595. ===============================================================================
  596. [..] This section provides functions allowing to:
  597. (+) Start a comparator without interrupt generation.
  598. (+) Stop a comparator without interrupt generation.
  599. (+) Start a comparator with interrupt generation.
  600. (+) Stop a comparator with interrupt generation.
  601. (+) Handle interrupts from a comparator with associated callback function.
  602. @endverbatim
  603. * @{
  604. */
  605. /**
  606. * @brief Start the comparator.
  607. * @param hcomp COMP handle
  608. * @retval HAL status
  609. */
  610. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  611. {
  612. __IO uint32_t wait_loop_index = 0UL;
  613. HAL_StatusTypeDef status = HAL_OK;
  614. uint32_t extiline = 0U;
  615. /* Check the COMP handle allocation and lock status */
  616. if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  617. {
  618. status = HAL_ERROR;
  619. }
  620. else
  621. {
  622. /* Check the parameter */
  623. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  624. if (hcomp->State == HAL_COMP_STATE_READY)
  625. {
  626. /* Get the EXTI Line output configuration */
  627. extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  628. /* Configure the event generation */
  629. if ((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_EVENT_RISING | COMP_TRIGGERMODE_EVENT_FALLING)) != RESET)
  630. {
  631. /* Configure the event trigger rising edge */
  632. if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_RISING) != RESET)
  633. {
  634. COMP_EXTI_RISING_ENABLE(extiline);
  635. }
  636. else
  637. {
  638. COMP_EXTI_RISING_DISABLE(extiline);
  639. }
  640. /* Configure the trigger falling edge */
  641. if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_FALLING) != RESET)
  642. {
  643. COMP_EXTI_FALLING_ENABLE(extiline);
  644. }
  645. else
  646. {
  647. COMP_EXTI_FALLING_DISABLE(extiline);
  648. }
  649. /* Enable EXTI event generation */
  650. COMP_EXTI_ENABLE_EVENT(extiline);
  651. /* Clear COMP EXTI pending bit */
  652. COMP_EXTI_CLEAR_FLAG(extiline);
  653. }
  654. /* Enable the selected comparator */
  655. __HAL_COMP_ENABLE(hcomp);
  656. hcomp->State = HAL_COMP_STATE_BUSY;
  657. /* Delay for COMP startup time */
  658. /* Wait loop initialization and execution */
  659. /* Note: Variable divided by 2 to compensate partially */
  660. /* CPU processing cycles, scaling in us split to not */
  661. /* exceed 32 bits register capacity and handle low frequency. */
  662. wait_loop_index = ((COMP_DELAY_STARTUP_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  663. while (wait_loop_index != 0UL)
  664. {
  665. wait_loop_index--;
  666. }
  667. }
  668. else
  669. {
  670. status = HAL_ERROR;
  671. }
  672. }
  673. return status;
  674. }
  675. /**
  676. * @brief Stop the comparator.
  677. * @param hcomp COMP handle
  678. * @retval HAL status
  679. */
  680. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  681. {
  682. HAL_StatusTypeDef status = HAL_OK;
  683. /* Check the COMP handle allocation and lock status */
  684. if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  685. {
  686. status = HAL_ERROR;
  687. }
  688. else
  689. {
  690. /* Check the parameter */
  691. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  692. if (hcomp->State == HAL_COMP_STATE_BUSY)
  693. {
  694. /* Disable the EXTI Line event mode if any */
  695. COMP_EXTI_DISABLE_EVENT(COMP_GET_EXTI_LINE(hcomp->Instance));
  696. /* Disable the selected comparator */
  697. __HAL_COMP_DISABLE(hcomp);
  698. hcomp->State = HAL_COMP_STATE_READY;
  699. }
  700. else
  701. {
  702. status = HAL_ERROR;
  703. }
  704. }
  705. return status;
  706. }
  707. /**
  708. * @brief Start the comparator in Interrupt mode.
  709. * @param hcomp COMP handle
  710. * @retval HAL status.
  711. */
  712. HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
  713. {
  714. HAL_StatusTypeDef status = HAL_OK;
  715. uint32_t extiline = 0U;
  716. /* Check the COMP handle allocation and lock status */
  717. if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  718. {
  719. status = HAL_ERROR;
  720. }
  721. else
  722. {
  723. /* Check the parameter */
  724. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  725. if (hcomp->State == HAL_COMP_STATE_READY)
  726. {
  727. /* Configure the EXTI event generation */
  728. if ((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_IT_RISING | COMP_TRIGGERMODE_IT_FALLING)) != RESET)
  729. {
  730. /* Get the EXTI Line output configuration */
  731. extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  732. /* Configure the trigger rising edge */
  733. if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
  734. {
  735. COMP_EXTI_RISING_ENABLE(extiline);
  736. }
  737. else
  738. {
  739. COMP_EXTI_RISING_DISABLE(extiline);
  740. }
  741. /* Configure the trigger falling edge */
  742. if ((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
  743. {
  744. COMP_EXTI_FALLING_ENABLE(extiline);
  745. }
  746. else
  747. {
  748. COMP_EXTI_FALLING_DISABLE(extiline);
  749. }
  750. /* Clear COMP EXTI pending bit if any */
  751. COMP_EXTI_CLEAR_FLAG(extiline);
  752. /* Enable EXTI interrupt mode */
  753. COMP_EXTI_ENABLE_IT(extiline);
  754. /* Enable the selected comparator */
  755. __HAL_COMP_ENABLE(hcomp);
  756. hcomp->State = HAL_COMP_STATE_BUSY;
  757. }
  758. else
  759. {
  760. status = HAL_ERROR;
  761. }
  762. }
  763. else
  764. {
  765. status = HAL_ERROR;
  766. }
  767. }
  768. return status;
  769. }
  770. /**
  771. * @brief Stop the comparator in Interrupt mode.
  772. * @param hcomp COMP handle
  773. * @retval HAL status
  774. */
  775. HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
  776. {
  777. HAL_StatusTypeDef status = HAL_OK;
  778. /* Disable the EXTI Line interrupt mode */
  779. COMP_EXTI_DISABLE_IT(COMP_GET_EXTI_LINE(hcomp->Instance));
  780. status = HAL_COMP_Stop(hcomp);
  781. return status;
  782. }
  783. /**
  784. * @brief Comparator IRQ Handler.
  785. * @param hcomp COMP handle
  786. * @retval HAL status
  787. */
  788. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  789. {
  790. uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  791. /* Check COMP EXTI flag */
  792. if (COMP_EXTI_GET_FLAG(extiline) != RESET)
  793. {
  794. /* Clear COMP EXTI pending bit */
  795. COMP_EXTI_CLEAR_FLAG(extiline);
  796. /* COMP trigger callback */
  797. #if (USE_HAL_COMP_REGISTER_CALLBACKS == 1)
  798. hcomp->TriggerCallback(hcomp);
  799. #else
  800. HAL_COMP_TriggerCallback(hcomp);
  801. #endif /* USE_HAL_COMP_REGISTER_CALLBACKS */
  802. }
  803. }
  804. /**
  805. * @brief Comparator trigger callback.
  806. * @param hcomp COMP handle
  807. * @retval None
  808. */
  809. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  810. {
  811. /* Prevent unused argument(s) compilation warning */
  812. UNUSED(hcomp);
  813. /* NOTE : This function should not be modified, when the callback is needed,
  814. the HAL_COMP_TriggerCallback should be implemented in the user file
  815. */
  816. }
  817. /**
  818. * @}
  819. */
  820. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  821. * @brief Management functions.
  822. *
  823. @verbatim
  824. ===============================================================================
  825. ##### Peripheral Control functions #####
  826. ===============================================================================
  827. [..]
  828. This subsection provides a set of functions allowing to control the comparators.
  829. @endverbatim
  830. * @{
  831. */
  832. /**
  833. * @brief Lock the selected comparator configuration.
  834. * @note A system reset is required to unlock the comparator configuration.
  835. * @param hcomp COMP handle
  836. * @retval HAL status
  837. */
  838. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  839. {
  840. HAL_StatusTypeDef status = HAL_OK;
  841. /* Check the COMP handle allocation and lock status */
  842. if ((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  843. {
  844. status = HAL_ERROR;
  845. }
  846. else
  847. {
  848. /* Check the parameter */
  849. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  850. /* Set lock flag on state */
  851. switch (hcomp->State)
  852. {
  853. case HAL_COMP_STATE_BUSY:
  854. hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
  855. break;
  856. case HAL_COMP_STATE_READY:
  857. hcomp->State = HAL_COMP_STATE_READY_LOCKED;
  858. break;
  859. default:
  860. /* unexpected state */
  861. status = HAL_ERROR;
  862. break;
  863. }
  864. }
  865. if (status == HAL_OK)
  866. {
  867. /* Set the lock bit corresponding to selected comparator */
  868. __HAL_COMP_LOCK(hcomp);
  869. }
  870. return status;
  871. }
  872. /**
  873. * @brief Return the output level (high or low) of the selected comparator.
  874. * The output level depends on the selected polarity.
  875. * If the polarity is not inverted:
  876. * - Comparator output is low when the non-inverting input is at a lower
  877. * voltage than the inverting input
  878. * - Comparator output is high when the non-inverting input is at a higher
  879. * voltage than the inverting input
  880. * If the polarity is inverted:
  881. * - Comparator output is high when the non-inverting input is at a lower
  882. * voltage than the inverting input
  883. * - Comparator output is low when the non-inverting input is at a higher
  884. * voltage than the inverting input
  885. * @param hcomp COMP handle
  886. * @retval Returns the selected comparator output level:
  887. * @arg @ref COMP_OUTPUTLEVEL_LOW
  888. * @arg @ref COMP_OUTPUTLEVEL_HIGH
  889. *
  890. */
  891. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  892. {
  893. uint32_t level = 0U;
  894. /* Check the parameter */
  895. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  896. level = READ_BIT(hcomp->Instance->CSR, COMP_CSR_COMPxOUT);
  897. if (level != 0U)
  898. {
  899. return (COMP_OUTPUTLEVEL_HIGH);
  900. }
  901. return (COMP_OUTPUTLEVEL_LOW);
  902. }
  903. /**
  904. * @}
  905. */
  906. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  907. * @brief Peripheral State functions.
  908. *
  909. @verbatim
  910. ===============================================================================
  911. ##### Peripheral State functions #####
  912. ===============================================================================
  913. [..]
  914. This subsection permits to get in run-time the status of the peripheral.
  915. @endverbatim
  916. * @{
  917. */
  918. /**
  919. * @brief Return the COMP handle state.
  920. * @param hcomp COMP handle
  921. * @retval HAL state
  922. */
  923. HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  924. {
  925. /* Check the COMP handle allocation */
  926. if (hcomp == NULL)
  927. {
  928. return HAL_COMP_STATE_RESET;
  929. }
  930. /* Check the parameter */
  931. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  932. /* Return COMP handle state */
  933. return hcomp->State;
  934. }
  935. /**
  936. * @brief Return the COMP error code.
  937. * @param hcomp COMP handle
  938. * @retval COMP error code
  939. */
  940. uint32_t HAL_COMP_GetError(COMP_HandleTypeDef *hcomp)
  941. {
  942. /* Check the parameters */
  943. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  944. return hcomp->ErrorCode;
  945. }
  946. /**
  947. * @}
  948. */
  949. /**
  950. * @}
  951. */
  952. #endif /* HAL_COMP_MODULE_ENABLED */
  953. /**
  954. * @}
  955. */
  956. /**
  957. * @}
  958. */