stm32f3xx_hal_comp.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. * + Start/Stop operation functions in polling mode.
  10. * + Start/Stop operation functions in interrupt mode.
  11. * + Peripheral Control functions
  12. * + Peripheral State functions
  13. *
  14. @verbatim
  15. ================================================================================
  16. ##### COMP Peripheral features #####
  17. ================================================================================
  18. [..]
  19. The STM32F3xx device family integrates up to 7 analog comparators COMP1, COMP2...COMP7:
  20. (#) The non inverting input and inverting input can be set to GPIO pins.
  21. For STM32F3xx devices please refer to the COMP peripheral section in corresponding
  22. Reference Manual.
  23. (#) The COMP output is available using HAL_COMP_GetOutputLevel()
  24. and can be set on GPIO pins.
  25. For STM32F3xx devices please refer to the COMP peripheral section in corresponding
  26. Reference Manual.
  27. (#) The COMP output can be redirected to embedded timers (TIM1, TIM2, TIM3...).
  28. For STM32F3xx devices please refer to the COMP peripheral section in corresponding
  29. Reference Manual.
  30. (#) Each couple of comparators COMP1 and COMP2, COMP3 and COMP4, COMP5 and COMP6 can be combined in window
  31. mode and respectively COMP1, COMP3 and COMP5 non inverting input is used as common non-inverting input.
  32. (#) The seven comparators have interrupt capability with wake-up
  33. from Sleep and Stop modes (through the EXTI controller):
  34. (++) COMP1 is internally connected to EXTI Line 21
  35. (++) COMP2 is internally connected to EXTI Line 22
  36. (++) COMP3 is internally connected to EXTI Line 29
  37. (++) COMP4 is internally connected to EXTI Line 30
  38. (++) COMP5 is internally connected to EXTI Line 31
  39. (++) COMP6 is internally connected to EXTI Line 32
  40. (++) COMP7 is internally connected to EXTI Line 33.
  41. From the corresponding IRQ handler, the right interrupt source can be retrieved with the
  42. adequate macro __HAL_COMP_COMPx_EXTI_GET_FLAG().
  43. ##### How to use this driver #####
  44. ================================================================================
  45. [..]
  46. This driver provides functions to configure and program the Comparators of all STM32F3xx devices.
  47. To use the comparator, perform the following steps:
  48. (#) Fill in the HAL_COMP_MspInit() to
  49. (++) Configure the comparator input in analog mode using HAL_GPIO_Init()
  50. (++) Configure the comparator output in alternate function mode using HAL_GPIO_Init() to map the comparator
  51. output to the GPIO pin
  52. (++) If required enable the COMP interrupt (EXTI line Interrupt): by configuring and enabling EXTI line in Interrupt mode and
  53. selecting the desired sensitivity level using HAL_GPIO_Init() function. After that enable the comparator
  54. interrupt vector using HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ() functions.
  55. (#) Configure the comparator using HAL_COMP_Init() function:
  56. (++) Select the inverting input (input minus)
  57. (++) Select the non-inverting input (input plus)
  58. (++) Select the output polarity
  59. (++) Select the output redirection
  60. (++) Select the hysteresis level
  61. (++) Select the power mode
  62. (++) Select the event/interrupt mode
  63. -@@- HAL_COMP_Init() calls internally __HAL_RCC_SYSCFG_CLK_ENABLE() in order
  64. to enable the comparator(s).
  65. (#) On-the-fly reconfiguration of comparator(s) may be done by calling again HAL_COMP_Init(
  66. function with new input parameter values; HAL_COMP_MspInit() function shall be adapted
  67. to support multi configurations.
  68. (#) Enable the comparator using HAL_COMP_Start() or HAL_COMP_Start_IT() functions.
  69. (#) Use HAL_COMP_TriggerCallback() and/or HAL_COMP_GetOutputLevel() functions
  70. to manage comparator outputs (events and output level).
  71. (#) Disable the comparator using HAL_COMP_Stop() or HAL_COMP_Stop_IT()
  72. function.
  73. (#) De-initialize the comparator using HAL_COMP_DeInit() function.
  74. (#) For safety purposes comparator(s) can be locked using HAL_COMP_Lock() function.
  75. Only a MCU reset can reset that protection.
  76. @endverbatim
  77. ******************************************************************************
  78. * @attention
  79. *
  80. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  81. *
  82. * Redistribution and use in source and binary forms, with or without modification,
  83. * are permitted provided that the following conditions are met:
  84. * 1. Redistributions of source code must retain the above copyright notice,
  85. * this list of conditions and the following disclaimer.
  86. * 2. Redistributions in binary form must reproduce the above copyright notice,
  87. * this list of conditions and the following disclaimer in the documentation
  88. * and/or other materials provided with the distribution.
  89. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  90. * may be used to endorse or promote products derived from this software
  91. * without specific prior written permission.
  92. *
  93. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  94. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  95. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  96. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  97. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  98. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  99. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  100. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  101. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  102. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  103. *
  104. ******************************************************************************
  105. */
  106. /*
  107. Additional Tables:
  108. Table 1. COMP Inputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
  109. +------------------------------------------------------------------------------------------+
  110. | | | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  111. |-----------------|----------------|---------------|---------------------------------------|
  112. | | 1U/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
  113. | | 1U/2 VREFINT | OK | OK | OK | OK | OK | OK | OK |
  114. | | 3U/4 VREFINT | OK | OK | OK | OK | OK | OK | OK |
  115. | Inverting Input | VREFINT | OK | OK | OK | OK | OK | OK | OK |
  116. | | DAC1 OUT (PA4) | OK | OK | OK | OK | OK | OK | OK |
  117. | | DAC2 OUT (PA5) | OK | OK | OK | OK | OK | OK | OK |
  118. | | IO1 | PA0 | PA2 | PD15U | PE8 | PD13U | PD10U | PC0 |
  119. | | IO2 | --- | --- | PB12U | PB2 | PB10U | PB15U | --- |
  120. |-----------------|----------------|-------|-------|-------|-------|-------|-------|-------|
  121. | Non Inverting | IO1 | PA1 | PA7 | PB14U | PB0 | PD12U | PD11U | PA0 |
  122. | Input | IO2 | --- | PA3 | PD14U | PE7 | PB13U | PB11U | PC1 |
  123. +------------------------------------------------------------------------------------------+
  124. Table 2. COMP Outputs for the STM32F303xB/STM32F303xC/STM32F303xE devices
  125. +-------------------------------------------------------+
  126. | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  127. |-------|-------|-------|-------|-------|-------|-------|
  128. | PA0 | PA2 | PB1 | PC8 | PC7 | PA10U | PC2 |
  129. | PF4 | PA7 | --- | PA8 | PA9 | PC6 | --- |
  130. | PA6 | PA12U | --- | --- | --- | --- | --- |
  131. | PA11U | PB9 | --- | --- | --- | --- | --- |
  132. | PB8 | --- | --- | --- | --- | --- | --- |
  133. +-------------------------------------------------------+
  134. Table 3. COMP Outputs redirection to embedded timers for the STM32F303xB/STM32F303xC devices
  135. +----------------------------------------------------------------------------------------------------------------------+
  136. | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  137. |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
  138. | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN |
  139. | | | | | | | |
  140. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  141. | | | | | | | |
  142. | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN |
  143. | | | | | | | |
  144. | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
  145. | | | | | | | |
  146. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  147. | + | + | + | + | + | + | + |
  148. | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
  149. | | | | | | | |
  150. | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
  151. | | | | | | | |
  152. | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
  153. | | | | | | | |
  154. | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
  155. | | | | | | | |
  156. | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
  157. | | | | | | | |
  158. | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
  159. | | | | | | | |
  160. | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
  161. +----------------------------------------------------------------------------------------------------------------------+
  162. Table 4. COMP Outputs redirection to embedded timers for the STM32F303xE devices
  163. +----------------------------------------------------------------------------------------------------------------------+
  164. | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  165. |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
  166. | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1U) | TIM1 BKIN | TIM1 BKIN | TIM1 BKIN (1U) |
  167. | | | | | | | |
  168. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  169. | | | | | | | |
  170. | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1U) | TIM8 BKIN | TIM8 BKIN | TIM8 BKIN (1U) |
  171. | | | | | | | |
  172. | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
  173. | | | | | | | |
  174. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  175. | + | + | + | + | + | + | + |
  176. | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 | TIM8BKIN2 |
  177. | | | | | | | |
  178. | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM1 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM8 OCREFCLR | TIM1 OCREFCLR |
  179. | | | | | | | |
  180. | TIM1 IC1 | TIM1 IC1 | TIM2 OCREFCLR | TIM3 IC3 | TIM2 IC1 | TIM2 IC2 | TIM8 OCREFCLR |
  181. | | | | | | | |
  182. | TIM2 IC4 | TIM2 IC4 | TIM3 IC2 | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM2 OCREFCLR | TIM2 IC3 |
  183. | | | | | | | |
  184. | TIM2 OCREFCLR | TIM2 OCREFCLR | TIM4 IC1 | TIM4 IC2 | TIM4 IC3 | TIM16 OCREFCLR| TIM1 IC2 |
  185. | | | | | | | |
  186. | TIM3 IC1 | TIM3 IC1 | TIM15 IC1 | TIM15 OCREFCLR| TIM16 BKIN | TIM16 IC1 | TIM17 OCREFCLR|
  187. | | | | | | | |
  188. | TIM3 OCREFCLR | TIM3 OCREFCLR | TIM15 BKIN | TIM15 IC2 | TIM17 IC1 | TIM4 IC4 | TIM17 BKIN |
  189. | | | | | | | |
  190. | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1U)| TIM20 BKIN | TIM20 BKIN | TIM20 BKIN (1U)|
  191. | | | | | | | |
  192. | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
  193. | | | | | | | |
  194. | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 | TIM1 BKIN2 |
  195. | + | + | + | + | + | + | + |
  196. | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 | TIM8 BKIN2 |
  197. | + | + | + | + | + | + | + |
  198. | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 | TIM20 BKIN2 |
  199. | | | | | | | |
  200. +----------------------------------------------------------------------------------------------------------------------+
  201. (1U): This connection consists of connecting both GPIO and COMP output to TIM1/8U/20 BRK input through an OR gate, instead
  202. 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
  203. add a digital filter (3 bits) on the COMP output.
  204. Table 5. COMP Outputs blanking sources for the STM32F303xB/STM32F303xC/STM32F303xE devices
  205. +----------------------------------------------------------------------------------------------------------------------+
  206. | COMP1 | COMP2 | COMP3 | COMP4 | COMP5 | COMP6 | COMP7 |
  207. |----------------|----------------|----------------|----------------|----------------|----------------|----------------|
  208. | TIM1 OC5 | TIM1 OC5 | TIM1 OC5 | TIM3 OC4 | -------- | TIM8 OC5 | TIM1 OC5 |
  209. | | | | | | | |
  210. | TIM2 OC3 | TIM2 OC3 | -------- | TIM8 OC5 | TIM3 OC3 | TIM2 OC4 | TIM8 OC5 |
  211. | | | | | | | |
  212. | TIM3 OC3 | TIM3 OC3 | TIM2 OC4 | TIM15 OC1 | TIM8 OC5 | TIM15 OC2 | TIM15 OC2 |
  213. | | | | | | | |
  214. +----------------------------------------------------------------------------------------------------------------------+
  215. */
  216. /* Includes ------------------------------------------------------------------*/
  217. #include "stm32f3xx_hal.h"
  218. /** @addtogroup STM32F3xx_HAL_Driver
  219. * @{
  220. */
  221. /** @defgroup COMP COMP
  222. * @brief COMP HAL module driver
  223. * @{
  224. */
  225. #ifdef HAL_COMP_MODULE_ENABLED
  226. /* Private typedef -----------------------------------------------------------*/
  227. /* Private define ------------------------------------------------------------*/
  228. /** @defgroup COMP_Private_Constants COMP Private Constants
  229. * @{
  230. */
  231. #define COMP_LOCK_DISABLE (0x00000000U)
  232. #define COMP_LOCK_ENABLE COMP_CSR_COMPxLOCK
  233. /**
  234. * @}
  235. */
  236. /* Private macro -------------------------------------------------------------*/
  237. /* Private variables ---------------------------------------------------------*/
  238. /* Private function prototypes -----------------------------------------------*/
  239. /* Exported functions --------------------------------------------------------*/
  240. /** @defgroup COMP_Exported_Functions COMP Exported Functions
  241. * @{
  242. */
  243. /** @defgroup COMP_Exported_Functions_Group1 Initialization/de-initialization functions
  244. * @brief Initialization and de-initialization functions.
  245. *
  246. @verbatim
  247. ===============================================================================
  248. ##### Initialization and de-initialization functions #####
  249. ===============================================================================
  250. [..] This section provides functions to initialize and de-initialize comparators.
  251. @endverbatim
  252. * @{
  253. */
  254. /**
  255. * @brief Initialize the COMP peripheral according to the specified
  256. * parameters in the COMP_InitTypeDef and initialize the associated handle.
  257. * @note If the selected comparator is locked, initialization cannot be performed.
  258. * To unlock the configuration, perform a system reset.
  259. * @param hcomp COMP handle
  260. * @retval HAL status
  261. */
  262. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp)
  263. {
  264. HAL_StatusTypeDef status = HAL_OK;
  265. /* Check the COMP handle allocation and lock status */
  266. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  267. {
  268. status = HAL_ERROR;
  269. }
  270. else
  271. {
  272. /* Check the parameters */
  273. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  274. assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput));
  275. assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput));
  276. assert_param(IS_COMP_NONINVERTINGINPUT_INSTANCE(hcomp->Instance, hcomp->Init.NonInvertingInput));
  277. assert_param(IS_COMP_OUTPUT(hcomp->Init.Output));
  278. assert_param(IS_COMP_OUTPUT_INSTANCE(hcomp->Instance, hcomp->Init.Output));
  279. assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol));
  280. assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis));
  281. assert_param(IS_COMP_MODE(hcomp->Init.Mode));
  282. assert_param(IS_COMP_BLANKINGSRCE(hcomp->Init.BlankingSrce));
  283. assert_param(IS_COMP_BLANKINGSRCE_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce));
  284. assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode));
  285. if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE)
  286. {
  287. assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance));
  288. assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode));
  289. }
  290. /* Init SYSCFG and the low level hardware to access comparators */
  291. __HAL_RCC_SYSCFG_CLK_ENABLE();
  292. /* Init the low level hardware : SYSCFG to access comparators */
  293. HAL_COMP_MspInit(hcomp);
  294. if(hcomp->State == HAL_COMP_STATE_RESET)
  295. {
  296. /* Allocate lock resource and initialize it */
  297. hcomp->Lock = HAL_UNLOCKED;
  298. }
  299. /* Manage inverting input comparator inverting input connected to a GPIO */
  300. /* for STM32F302x, STM32F32xx, STM32F33x. */
  301. hcomp->Init.InvertingInput = COMP_INVERTINGINPUT_SELECTION(hcomp->Instance, hcomp->Init.InvertingInput);
  302. /* Set COMP parameters */
  303. /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */
  304. /* Set COMPxNONINSEL bits according to hcomp->Init.NonInvertingInput value */
  305. /* Set COMPxBLANKING bits according to hcomp->Init.BlankingSrce value */
  306. /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */
  307. /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */
  308. /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */
  309. /* Set COMPxMODE bits according to hcomp->Init.Mode value */
  310. COMP_INIT(hcomp);
  311. /* Initialize the COMP state*/
  312. hcomp->State = HAL_COMP_STATE_READY;
  313. }
  314. return status;
  315. }
  316. /**
  317. * @brief DeInitialize the COMP peripheral.
  318. * @note If the selected comparator is locked, deinitialization cannot be performed.
  319. * To unlock the configuration, perform a system reset.
  320. * @param hcomp COMP handle
  321. * @retval HAL status
  322. */
  323. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef *hcomp)
  324. {
  325. HAL_StatusTypeDef status = HAL_OK;
  326. /* Check the COMP handle allocation and lock status */
  327. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  328. {
  329. status = HAL_ERROR;
  330. }
  331. else
  332. {
  333. /* Check the parameter */
  334. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  335. /* Set COMP_CSR register to reset value */
  336. COMP_DEINIT(hcomp);
  337. /* DeInit the low level hardware: SYSCFG, GPIO, CLOCK and NVIC */
  338. HAL_COMP_MspDeInit(hcomp);
  339. hcomp->State = HAL_COMP_STATE_RESET;
  340. /* Release Lock */
  341. __HAL_UNLOCK(hcomp);
  342. }
  343. return status;
  344. }
  345. /**
  346. * @brief Initialize the COMP MSP.
  347. * @param hcomp COMP handle
  348. * @retval None
  349. */
  350. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef *hcomp)
  351. {
  352. /* Prevent unused argument(s) compilation warning */
  353. UNUSED(hcomp);
  354. /* NOTE : This function should not be modified, when the callback is needed,
  355. the HAL_COMP_MspInit could be implemented in the user file
  356. */
  357. }
  358. /**
  359. * @brief DeInitialize the COMP MSP.
  360. * @param hcomp COMP handle
  361. * @retval None
  362. */
  363. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef *hcomp)
  364. {
  365. /* Prevent unused argument(s) compilation warning */
  366. UNUSED(hcomp);
  367. /* NOTE : This function should not be modified, when the callback is needed,
  368. the HAL_COMP_MspDeInit could be implemented in the user file
  369. */
  370. }
  371. /**
  372. * @}
  373. */
  374. /** @defgroup COMP_Exported_Functions_Group2 Start Stop operation functions
  375. * @brief Start-Stop operation functions.
  376. *
  377. @verbatim
  378. ===============================================================================
  379. ##### Start Stop operation functions #####
  380. ===============================================================================
  381. [..] This section provides functions allowing to:
  382. (+) Start a comparator without interrupt generation.
  383. (+) Stop a comparator without interrupt generation.
  384. (+) Start a comparator with interrupt generation.
  385. (+) Stop a comparator with interrupt generation.
  386. (+) Handle interrupts from a comparator with associated callback function.
  387. @endverbatim
  388. * @{
  389. */
  390. /**
  391. * @brief Start the comparator.
  392. * @param hcomp COMP handle
  393. * @retval HAL status
  394. */
  395. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  396. {
  397. HAL_StatusTypeDef status = HAL_OK;
  398. uint32_t extiline = 0U;
  399. /* Check the COMP handle allocation and lock status */
  400. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  401. {
  402. status = HAL_ERROR;
  403. }
  404. else
  405. {
  406. /* Check the parameter */
  407. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  408. if(hcomp->State == HAL_COMP_STATE_READY)
  409. {
  410. /* Get the EXTI Line output configuration */
  411. extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  412. /* Configure the event generation */
  413. if((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_EVENT_RISING|COMP_TRIGGERMODE_EVENT_FALLING)) != RESET)
  414. {
  415. /* Configure the event trigger rising edge */
  416. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_RISING) != RESET)
  417. {
  418. COMP_EXTI_RISING_ENABLE(extiline);
  419. }
  420. else
  421. {
  422. COMP_EXTI_RISING_DISABLE(extiline);
  423. }
  424. /* Configure the trigger falling edge */
  425. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_FALLING) != RESET)
  426. {
  427. COMP_EXTI_FALLING_ENABLE(extiline);
  428. }
  429. else
  430. {
  431. COMP_EXTI_FALLING_DISABLE(extiline);
  432. }
  433. /* Enable EXTI event generation */
  434. COMP_EXTI_ENABLE_EVENT(extiline);
  435. /* Clear COMP EXTI pending bit */
  436. COMP_EXTI_CLEAR_FLAG(extiline);
  437. }
  438. /* Enable the selected comparator */
  439. __HAL_COMP_ENABLE(hcomp);
  440. hcomp->State = HAL_COMP_STATE_BUSY;
  441. }
  442. else
  443. {
  444. status = HAL_ERROR;
  445. }
  446. }
  447. return status;
  448. }
  449. /**
  450. * @brief Stop the comparator.
  451. * @param hcomp COMP handle
  452. * @retval HAL status
  453. */
  454. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  455. {
  456. HAL_StatusTypeDef status = HAL_OK;
  457. /* Check the COMP handle allocation and lock status */
  458. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  459. {
  460. status = HAL_ERROR;
  461. }
  462. else
  463. {
  464. /* Check the parameter */
  465. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  466. if(hcomp->State == HAL_COMP_STATE_BUSY)
  467. {
  468. /* Disable the EXTI Line event mode if any */
  469. COMP_EXTI_DISABLE_EVENT(COMP_GET_EXTI_LINE(hcomp->Instance));
  470. /* Disable the selected comparator */
  471. __HAL_COMP_DISABLE(hcomp);
  472. hcomp->State = HAL_COMP_STATE_READY;
  473. }
  474. else
  475. {
  476. status = HAL_ERROR;
  477. }
  478. }
  479. return status;
  480. }
  481. /**
  482. * @brief Start the comparator in Interrupt mode.
  483. * @param hcomp COMP handle
  484. * @retval HAL status.
  485. */
  486. HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp)
  487. {
  488. HAL_StatusTypeDef status = HAL_OK;
  489. uint32_t extiline = 0U;
  490. /* Check the COMP handle allocation and lock status */
  491. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  492. {
  493. status = HAL_ERROR;
  494. }
  495. else
  496. {
  497. /* Check the parameter */
  498. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  499. if(hcomp->State == HAL_COMP_STATE_READY)
  500. {
  501. /* Configure the EXTI event generation */
  502. if((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_IT_RISING|COMP_TRIGGERMODE_IT_FALLING)) != RESET)
  503. {
  504. /* Get the EXTI Line output configuration */
  505. extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  506. /* Configure the trigger rising edge */
  507. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET)
  508. {
  509. COMP_EXTI_RISING_ENABLE(extiline);
  510. }
  511. else
  512. {
  513. COMP_EXTI_RISING_DISABLE(extiline);
  514. }
  515. /* Configure the trigger falling edge */
  516. if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
  517. {
  518. COMP_EXTI_FALLING_ENABLE(extiline);
  519. }
  520. else
  521. {
  522. COMP_EXTI_FALLING_DISABLE(extiline);
  523. }
  524. /* Clear COMP EXTI pending bit if any */
  525. COMP_EXTI_CLEAR_FLAG(extiline);
  526. /* Enable EXTI interrupt mode */
  527. COMP_EXTI_ENABLE_IT(extiline);
  528. /* Enable the selected comparator */
  529. __HAL_COMP_ENABLE(hcomp);
  530. hcomp->State = HAL_COMP_STATE_BUSY;
  531. }
  532. else
  533. {
  534. status = HAL_ERROR;
  535. }
  536. }
  537. else
  538. {
  539. status = HAL_ERROR;
  540. }
  541. }
  542. return status;
  543. }
  544. /**
  545. * @brief Stop the comparator in Interrupt mode.
  546. * @param hcomp COMP handle
  547. * @retval HAL status
  548. */
  549. HAL_StatusTypeDef HAL_COMP_Stop_IT(COMP_HandleTypeDef *hcomp)
  550. {
  551. HAL_StatusTypeDef status = HAL_OK;
  552. /* Disable the EXTI Line interrupt mode */
  553. COMP_EXTI_DISABLE_IT(COMP_GET_EXTI_LINE(hcomp->Instance));
  554. status = HAL_COMP_Stop(hcomp);
  555. return status;
  556. }
  557. /**
  558. * @brief Comparator IRQ Handler.
  559. * @param hcomp COMP handle
  560. * @retval HAL status
  561. */
  562. void HAL_COMP_IRQHandler(COMP_HandleTypeDef *hcomp)
  563. {
  564. uint32_t extiline = COMP_GET_EXTI_LINE(hcomp->Instance);
  565. /* Check COMP EXTI flag */
  566. if(COMP_EXTI_GET_FLAG(extiline) != RESET)
  567. {
  568. /* Clear COMP EXTI pending bit */
  569. COMP_EXTI_CLEAR_FLAG(extiline);
  570. /* COMP trigger user callback */
  571. HAL_COMP_TriggerCallback(hcomp);
  572. }
  573. }
  574. /**
  575. * @brief Comparator callback.
  576. * @param hcomp COMP handle
  577. * @retval None
  578. */
  579. __weak void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp)
  580. {
  581. /* Prevent unused argument(s) compilation warning */
  582. UNUSED(hcomp);
  583. /* NOTE : This function should not be modified, when the callback is needed,
  584. the HAL_COMP_TriggerCallback should be implemented in the user file
  585. */
  586. }
  587. /**
  588. * @}
  589. */
  590. /** @defgroup COMP_Exported_Functions_Group3 Peripheral Control functions
  591. * @brief Management functions.
  592. *
  593. @verbatim
  594. ===============================================================================
  595. ##### Peripheral Control functions #####
  596. ===============================================================================
  597. [..]
  598. This subsection provides a set of functions allowing to control the comparators.
  599. @endverbatim
  600. * @{
  601. */
  602. /**
  603. * @brief Lock the selected comparator configuration.
  604. * @note A system reset is required to unlock the comparator configuration.
  605. * @param hcomp COMP handle
  606. * @retval HAL status
  607. */
  608. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp)
  609. {
  610. HAL_StatusTypeDef status = HAL_OK;
  611. /* Check the COMP handle allocation and lock status */
  612. if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET))
  613. {
  614. status = HAL_ERROR;
  615. }
  616. else
  617. {
  618. /* Check the parameter */
  619. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  620. /* Set lock flag on state */
  621. switch(hcomp->State)
  622. {
  623. case HAL_COMP_STATE_BUSY:
  624. hcomp->State = HAL_COMP_STATE_BUSY_LOCKED;
  625. break;
  626. case HAL_COMP_STATE_READY:
  627. hcomp->State = HAL_COMP_STATE_READY_LOCKED;
  628. break;
  629. default:
  630. /* unexpected state */
  631. status = HAL_ERROR;
  632. break;
  633. }
  634. }
  635. if(status == HAL_OK)
  636. {
  637. /* Set the lock bit corresponding to selected comparator */
  638. __HAL_COMP_LOCK(hcomp);
  639. }
  640. return status;
  641. }
  642. /**
  643. * @brief Return the output level (high or low) of the selected comparator.
  644. * The output level depends on the selected polarity.
  645. * If the polarity is not inverted:
  646. * - Comparator output is low when the non-inverting input is at a lower
  647. * voltage than the inverting input
  648. * - Comparator output is high when the non-inverting input is at a higher
  649. * voltage than the inverting input
  650. * If the polarity is inverted:
  651. * - Comparator output is high when the non-inverting input is at a lower
  652. * voltage than the inverting input
  653. * - Comparator output is low when the non-inverting input is at a higher
  654. * voltage than the inverting input
  655. * @param hcomp COMP handle
  656. * @retval Returns the selected comparator output level:
  657. * @arg @ref COMP_OUTPUTLEVEL_LOW
  658. * @arg @ref COMP_OUTPUTLEVEL_HIGH
  659. *
  660. */
  661. uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp)
  662. {
  663. uint32_t level=0U;
  664. /* Check the parameter */
  665. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  666. level = READ_BIT(hcomp->Instance->CSR, COMP_CSR_COMPxOUT);
  667. if(level != 0U)
  668. {
  669. return(COMP_OUTPUTLEVEL_HIGH);
  670. }
  671. return(COMP_OUTPUTLEVEL_LOW);
  672. }
  673. /**
  674. * @}
  675. */
  676. /** @defgroup COMP_Exported_Functions_Group4 Peripheral State functions
  677. * @brief Peripheral State functions.
  678. *
  679. @verbatim
  680. ===============================================================================
  681. ##### Peripheral State functions #####
  682. ===============================================================================
  683. [..]
  684. This subsection permits to get in run-time the status of the peripheral.
  685. @endverbatim
  686. * @{
  687. */
  688. /**
  689. * @brief Return the COMP handle state.
  690. * @param hcomp COMP handle
  691. * @retval HAL state
  692. */
  693. HAL_COMP_StateTypeDef HAL_COMP_GetState(COMP_HandleTypeDef *hcomp)
  694. {
  695. /* Check the COMP handle allocation */
  696. if(hcomp == NULL)
  697. {
  698. return HAL_COMP_STATE_RESET;
  699. }
  700. /* Check the parameter */
  701. assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance));
  702. /* Return COMP handle state */
  703. return hcomp->State;
  704. }
  705. /**
  706. * @}
  707. */
  708. /**
  709. * @}
  710. */
  711. #endif /* HAL_COMP_MODULE_ENABLED */
  712. /**
  713. * @}
  714. */
  715. /**
  716. * @}
  717. */
  718. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/