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.
 
 
 

281 lines
11 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_ll_comp.c
  4. * @author MCD Application Team
  5. * @brief COMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2019 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 "stm32wbxx_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 STM32WBxx_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 series, 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(LL_COMP_INPUT_PLUS_IO1)
  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_IO3) \
  57. )
  58. #else
  59. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  60. ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  61. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \
  62. )
  63. #endif
  64. /* Note: On this STM32 series, comparator input minus parameters are */
  65. /* the same on all COMP instances. */
  66. /* However, comparator instance kept as macro parameter for */
  67. /* compatibility with other STM32 families. */
  68. #if defined(LL_COMP_INPUT_MINUS_IO2)
  69. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  70. ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  71. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  72. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  73. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  74. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  75. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  76. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3) \
  77. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO4) \
  78. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO5) \
  79. )
  80. #else
  81. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  82. ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  83. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  84. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  85. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  86. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  87. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3) \
  88. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO4) \
  89. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO5) \
  90. )
  91. #endif
  92. #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
  93. ( ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \
  94. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW) \
  95. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM) \
  96. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH) \
  97. )
  98. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
  99. ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
  100. || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
  101. )
  102. #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__OUTPUT_BLANKING_SOURCE__) \
  103. ( ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
  104. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5) \
  105. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3) \
  106. )
  107. /**
  108. * @}
  109. */
  110. /* Private function prototypes -----------------------------------------------*/
  111. /* Exported functions --------------------------------------------------------*/
  112. /** @addtogroup COMP_LL_Exported_Functions
  113. * @{
  114. */
  115. /** @addtogroup COMP_LL_EF_Init
  116. * @{
  117. */
  118. /**
  119. * @brief De-initialize registers of the selected COMP instance
  120. * to their default reset values.
  121. * @note If comparator is locked, de-initialization by software is
  122. * not possible.
  123. * The only way to unlock the comparator is a device hardware reset.
  124. * @param COMPx COMP instance
  125. * @retval An ErrorStatus enumeration value:
  126. * - SUCCESS: COMP registers are de-initialized
  127. * - ERROR: COMP registers are not de-initialized
  128. */
  129. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  130. {
  131. ErrorStatus status = SUCCESS;
  132. /* Check the parameters */
  133. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  134. /* Note: Hardware constraint (refer to description of this function): */
  135. /* COMP instance must not be locked. */
  136. if (LL_COMP_IsLocked(COMPx) == 0UL)
  137. {
  138. LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL);
  139. }
  140. else
  141. {
  142. /* Comparator instance is locked: de-initialization by software is */
  143. /* not possible. */
  144. /* The only way to unlock the comparator is a device hardware reset. */
  145. status = ERROR;
  146. }
  147. return status;
  148. }
  149. /**
  150. * @brief Initialize some features of COMP instance.
  151. * @note This function configures features of the selected COMP instance.
  152. * Some features are also available at scope COMP common instance
  153. * (common to several COMP instances).
  154. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  155. * @param COMPx COMP instance
  156. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  157. * @retval An ErrorStatus enumeration value:
  158. * - SUCCESS: COMP registers are initialized
  159. * - ERROR: COMP registers are not initialized
  160. */
  161. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  162. {
  163. ErrorStatus status = SUCCESS;
  164. /* Check the parameters */
  165. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  166. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  167. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  168. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  169. assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
  170. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  171. assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMP_InitStruct->OutputBlankingSource));
  172. /* Note: Hardware constraint (refer to description of this function) */
  173. /* COMP instance must not be locked. */
  174. if (LL_COMP_IsLocked(COMPx) == 0UL)
  175. {
  176. /* Configuration of comparator instance : */
  177. /* - PowerMode */
  178. /* - InputPlus */
  179. /* - InputMinus */
  180. /* - InputHysteresis */
  181. /* - OutputPolarity */
  182. /* - OutputBlankingSource */
  183. MODIFY_REG(COMPx->CSR,
  184. COMP_CSR_PWRMODE
  185. | COMP_CSR_INPSEL
  186. | COMP_CSR_SCALEN
  187. | COMP_CSR_BRGEN
  188. | COMP_CSR_INMESEL
  189. | COMP_CSR_INMSEL
  190. | COMP_CSR_HYST
  191. | COMP_CSR_POLARITY
  192. | COMP_CSR_BLANKING
  193. ,
  194. COMP_InitStruct->PowerMode
  195. | COMP_InitStruct->InputPlus
  196. | COMP_InitStruct->InputMinus
  197. | COMP_InitStruct->InputHysteresis
  198. | COMP_InitStruct->OutputPolarity
  199. | COMP_InitStruct->OutputBlankingSource
  200. );
  201. }
  202. else
  203. {
  204. /* Initialization error: COMP instance is locked. */
  205. status = ERROR;
  206. }
  207. return status;
  208. }
  209. /**
  210. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  211. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  212. * whose fields will be set to default values.
  213. * @retval None
  214. */
  215. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  216. {
  217. /* Set COMP_InitStruct fields to default values */
  218. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_ULTRALOWPOWER;
  219. #if defined(LL_COMP_INPUT_PLUS_IO1)
  220. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  221. #else
  222. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO2;
  223. #endif
  224. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  225. COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE;
  226. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  227. COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
  228. }
  229. /**
  230. * @}
  231. */
  232. /**
  233. * @}
  234. */
  235. /**
  236. * @}
  237. */
  238. #endif /* COMP1 || COMP2 */
  239. /**
  240. * @}
  241. */
  242. #endif /* USE_FULL_LL_DRIVER */
  243. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/