stm32g0xx_ll_comp.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_ll_comp.c
  4. * @author MCD Application Team
  5. * @brief COMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2018 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32g0xx_ll_comp.h"
  21. #ifdef USE_FULL_ASSERT
  22. #include "stm32_assert.h"
  23. #else
  24. #define assert_param(expr) ((void)0U)
  25. #endif /* USE_FULL_ASSERT */
  26. /** @addtogroup STM32G0xx_LL_Driver
  27. * @{
  28. */
  29. #if defined (COMP1) || defined (COMP2)
  30. /** @addtogroup COMP_LL COMP
  31. * @{
  32. */
  33. /* Private types -------------------------------------------------------------*/
  34. /* Private variables ---------------------------------------------------------*/
  35. /* Private constants ---------------------------------------------------------*/
  36. /* Private macros ------------------------------------------------------------*/
  37. /** @addtogroup COMP_LL_Private_Macros
  38. * @{
  39. */
  40. /* Check of parameters for configuration of COMP hierarchical scope: */
  41. /* COMP instance. */
  42. #define IS_LL_COMP_POWER_MODE(__POWER_MODE__) \
  43. (((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED) \
  44. || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \
  45. )
  46. /* Note: On this STM32 series, comparator input plus parameters are */
  47. /* the same on all COMP instances. */
  48. /* However, comparator instance kept as macro parameter for */
  49. /* compatibility with other STM32 families. */
  50. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  51. (((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  52. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  53. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \
  54. )
  55. /* Note: On this STM32 series, comparator input minus parameters are */
  56. /* the same on all COMP instances. */
  57. /* However, comparator instance kept as macro parameter for */
  58. /* compatibility with other STM32 families. */
  59. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  60. (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  61. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  62. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  63. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  64. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  65. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  66. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  67. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  68. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3) \
  69. )
  70. #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
  71. (((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \
  72. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW) \
  73. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM) \
  74. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH) \
  75. )
  76. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
  77. (((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
  78. || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
  79. )
  80. #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__OUTPUT_BLANKING_SOURCE__) \
  81. (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
  82. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC4) \
  83. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5) \
  84. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3) \
  85. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3) \
  86. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC2) \
  87. )
  88. /**
  89. * @}
  90. */
  91. /* Private function prototypes -----------------------------------------------*/
  92. /* Exported functions --------------------------------------------------------*/
  93. /** @addtogroup COMP_LL_Exported_Functions
  94. * @{
  95. */
  96. /** @addtogroup COMP_LL_EF_Init
  97. * @{
  98. */
  99. /**
  100. * @brief De-initialize registers of the selected COMP instance
  101. * to their default reset values.
  102. * @note If comparator is locked, de-initialization by software is
  103. * not possible.
  104. * The only way to unlock the comparator is a device hardware reset.
  105. * @param COMPx COMP instance
  106. * @retval An ErrorStatus enumeration value:
  107. * - SUCCESS: COMP registers are de-initialized
  108. * - ERROR: COMP registers are not de-initialized
  109. */
  110. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  111. {
  112. ErrorStatus status = SUCCESS;
  113. /* Check the parameters */
  114. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  115. /* Note: Hardware constraint (refer to description of this function): */
  116. /* COMP instance must not be locked. */
  117. if (LL_COMP_IsLocked(COMPx) == 0UL)
  118. {
  119. LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL);
  120. }
  121. else
  122. {
  123. /* Comparator instance is locked: de-initialization by software is */
  124. /* not possible. */
  125. /* The only way to unlock the comparator is a device hardware reset. */
  126. status = ERROR;
  127. }
  128. return status;
  129. }
  130. /**
  131. * @brief Initialize some features of COMP instance.
  132. * @note This function configures features of the selected COMP instance.
  133. * Some features are also available at scope COMP common instance
  134. * (common to several COMP instances).
  135. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  136. * @param COMPx COMP instance
  137. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  138. * @retval An ErrorStatus enumeration value:
  139. * - SUCCESS: COMP registers are initialized
  140. * - ERROR: COMP registers are not initialized
  141. */
  142. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, const LL_COMP_InitTypeDef *COMP_InitStruct)
  143. {
  144. ErrorStatus status = SUCCESS;
  145. /* Check the parameters */
  146. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  147. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  148. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  149. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  150. assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
  151. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  152. assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMP_InitStruct->OutputBlankingSource));
  153. /* Note: Hardware constraint (refer to description of this function) */
  154. /* COMP instance must not be locked. */
  155. if (LL_COMP_IsLocked(COMPx) == 0UL)
  156. {
  157. /* Configuration of comparator instance : */
  158. /* - PowerMode */
  159. /* - InputPlus */
  160. /* - InputMinus */
  161. /* - InputHysteresis */
  162. /* - OutputPolarity */
  163. /* - OutputBlankingSource */
  164. MODIFY_REG(COMPx->CSR,
  165. COMP_CSR_PWRMODE
  166. | COMP_CSR_INPSEL
  167. | COMP_CSR_INMSEL
  168. | COMP_CSR_HYST
  169. | COMP_CSR_POLARITY
  170. | COMP_CSR_BLANKING
  171. ,
  172. COMP_InitStruct->PowerMode
  173. | COMP_InitStruct->InputPlus
  174. | COMP_InitStruct->InputMinus
  175. | COMP_InitStruct->InputHysteresis
  176. | COMP_InitStruct->OutputPolarity
  177. | COMP_InitStruct->OutputBlankingSource
  178. );
  179. }
  180. else
  181. {
  182. /* Initialization error: COMP instance is locked. */
  183. status = ERROR;
  184. }
  185. return status;
  186. }
  187. /**
  188. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  189. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  190. * whose fields will be set to default values.
  191. * @retval None
  192. */
  193. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  194. {
  195. /* Set COMP_InitStruct fields to default values */
  196. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_MEDIUMSPEED;
  197. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  198. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  199. COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE;
  200. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  201. COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
  202. }
  203. /**
  204. * @}
  205. */
  206. /**
  207. * @}
  208. */
  209. /**
  210. * @}
  211. */
  212. #endif /* COMP1 || COMP2 */
  213. /**
  214. * @}
  215. */
  216. #endif /* USE_FULL_LL_DRIVER */