You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

297 lines
12 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_ll_comp.c
  4. * @author MCD Application Team
  5. * @brief COMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32h7xx_ll_comp.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif
  27. /** @addtogroup STM32H7xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (COMP1) || defined (COMP2)
  31. /** @addtogroup COMP_LL COMP
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private macros ------------------------------------------------------------*/
  38. /** @addtogroup COMP_LL_Private_Macros
  39. * @{
  40. */
  41. /* Check of parameters for configuration of COMP hierarchical scope: */
  42. /* COMP instance. */
  43. #define IS_LL_COMP_POWER_MODE(__POWER_MODE__) \
  44. ( ((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED) \
  45. || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \
  46. || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER) \
  47. )
  48. /* Note: On this STM32 serie, comparator input plus parameters are */
  49. /* the same on all COMP instances. */
  50. /* However, comparator instance kept as macro parameter for */
  51. /* compatibility with other STM32 families. */
  52. #if defined (COMP_CFGRx_INP2SEL)
  53. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  54. ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  55. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  56. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_DAC2_CH1))
  57. #else
  58. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  59. ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  60. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2))
  61. #endif
  62. /* Note: On this STM32 serie, comparator input minus parameters are */
  63. /* the same on all COMP instances. */
  64. /* However, comparator instance kept as macro parameter for */
  65. /* compatibility with other STM32 families. */
  66. #if defined (COMP_CFGRx_INMSEL_3)
  67. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  68. ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  69. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  70. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  71. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  72. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  73. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  74. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  75. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  76. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_TPSENS_DAC2CH1) \
  77. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VBAT_VDDAP))
  78. #else
  79. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  80. ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  81. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  82. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  83. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  84. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  85. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  86. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  87. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2))
  88. #endif
  89. #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
  90. ( ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \
  91. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW) \
  92. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM) \
  93. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH) \
  94. )
  95. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
  96. ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
  97. || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
  98. )
  99. #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__OUTPUT_BLANKING_SOURCE__) \
  100. ( ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
  101. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5) \
  102. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3) \
  103. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3) \
  104. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4) \
  105. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5) \
  106. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1) \
  107. )
  108. /**
  109. * @}
  110. */
  111. /* Private function prototypes -----------------------------------------------*/
  112. /* Exported functions --------------------------------------------------------*/
  113. /** @addtogroup COMP_LL_Exported_Functions
  114. * @{
  115. */
  116. /** @addtogroup COMP_LL_EF_Init
  117. * @{
  118. */
  119. /**
  120. * @brief De-initialize registers of the selected COMP instance
  121. * to their default reset values.
  122. * @note If comparator is locked, de-initialization by software is
  123. * not possible.
  124. * The only way to unlock the comparator is a device hardware reset.
  125. * @param COMPx COMP instance
  126. * @retval An ErrorStatus enumeration value:
  127. * - SUCCESS: COMP registers are de-initialized
  128. * - ERROR: COMP registers are not de-initialized
  129. */
  130. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  131. {
  132. ErrorStatus status = SUCCESS;
  133. /* Check the parameters */
  134. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  135. /* Note: Hardware constraint (refer to description of this function): */
  136. /* COMP instance must not be locked. */
  137. if(LL_COMP_IsLocked(COMPx) == 0UL)
  138. {
  139. LL_COMP_WriteReg((COMPx), CFGR, 0x00000000UL);
  140. }
  141. else
  142. {
  143. /* Comparator instance is locked: de-initialization by software is */
  144. /* not possible. */
  145. /* The only way to unlock the comparator is a device hardware reset. */
  146. status = ERROR;
  147. }
  148. return status;
  149. }
  150. /**
  151. * @brief Initialize some features of COMP instance.
  152. * @note This function configures features of the selected COMP instance.
  153. * Some features are also available at scope COMP common instance
  154. * (common to several COMP instances).
  155. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  156. * @param COMPx COMP instance
  157. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  158. * @retval An ErrorStatus enumeration value:
  159. * - SUCCESS: COMP registers are initialized
  160. * - ERROR: COMP registers are not initialized
  161. */
  162. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  163. {
  164. ErrorStatus status = SUCCESS;
  165. /* Check the parameters */
  166. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  167. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  168. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  169. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  170. assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
  171. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  172. assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMP_InitStruct->OutputBlankingSource));
  173. /* Note: Hardware constraint (refer to description of this function) */
  174. /* COMP instance must not be locked. */
  175. if(LL_COMP_IsLocked(COMPx) == 0UL)
  176. {
  177. /* Configuration of comparator instance : */
  178. /* - PowerMode */
  179. /* - InputPlus */
  180. /* - InputMinus */
  181. /* - InputHysteresis */
  182. /* - OutputPolarity */
  183. /* - OutputBlankingSource */
  184. #if defined (COMP_CFGRx_INP2SEL)
  185. MODIFY_REG(COMPx->CFGR,
  186. COMP_CFGRx_PWRMODE
  187. | COMP_CFGRx_INPSEL
  188. | COMP_CFGRx_INP2SEL
  189. | COMP_CFGRx_SCALEN
  190. | COMP_CFGRx_BRGEN
  191. | COMP_CFGRx_INMSEL
  192. | COMP_CFGRx_HYST
  193. | COMP_CFGRx_POLARITY
  194. | COMP_CFGRx_BLANKING
  195. ,
  196. COMP_InitStruct->PowerMode
  197. | COMP_InitStruct->InputPlus
  198. | COMP_InitStruct->InputMinus
  199. | COMP_InitStruct->InputHysteresis
  200. | COMP_InitStruct->OutputPolarity
  201. | COMP_InitStruct->OutputBlankingSource
  202. );
  203. #else
  204. MODIFY_REG(COMPx->CFGR,
  205. COMP_CFGRx_PWRMODE
  206. | COMP_CFGRx_INPSEL
  207. | COMP_CFGRx_SCALEN
  208. | COMP_CFGRx_BRGEN
  209. | COMP_CFGRx_INMSEL
  210. | COMP_CFGRx_HYST
  211. | COMP_CFGRx_POLARITY
  212. | COMP_CFGRx_BLANKING
  213. ,
  214. COMP_InitStruct->PowerMode
  215. | COMP_InitStruct->InputPlus
  216. | COMP_InitStruct->InputMinus
  217. | COMP_InitStruct->InputHysteresis
  218. | COMP_InitStruct->OutputPolarity
  219. | COMP_InitStruct->OutputBlankingSource
  220. );
  221. #endif
  222. }
  223. else
  224. {
  225. /* Initialization error: COMP instance is locked. */
  226. status = ERROR;
  227. }
  228. return status;
  229. }
  230. /**
  231. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  232. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  233. * whose fields will be set to default values.
  234. * @retval None
  235. */
  236. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  237. {
  238. /* Set COMP_InitStruct fields to default values */
  239. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_ULTRALOWPOWER;
  240. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  241. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  242. COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE;
  243. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  244. COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
  245. }
  246. /**
  247. * @}
  248. */
  249. /**
  250. * @}
  251. */
  252. /**
  253. * @}
  254. */
  255. #endif /* COMP1 || COMP2 */
  256. /**
  257. * @}
  258. */
  259. #endif /* USE_FULL_LL_DRIVER */
  260. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/