py32f002b_ll_comp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_ll_comp.c
  4. * @author MCU Application Team
  5. * @brief COMP LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya 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. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. #if defined(USE_FULL_LL_DRIVER)
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "py32f002b_ll_comp.h"
  33. #include "py32f002b_ll_bus.h"
  34. #ifdef USE_FULL_ASSERT
  35. #include "py32_assert.h"
  36. #else
  37. #define assert_param(expr) ((void)0U)
  38. #endif /* USE_FULL_ASSERT */
  39. /** @addtogroup PY32F002B_LL_Driver
  40. * @{
  41. */
  42. #if defined (COMP1) || defined (COMP2)
  43. /** @addtogroup COMP_LL COMP
  44. * @{
  45. */
  46. /* Private types -------------------------------------------------------------*/
  47. /* Private variables ---------------------------------------------------------*/
  48. /* Private constants ---------------------------------------------------------*/
  49. /* Private macros ------------------------------------------------------------*/
  50. /** @addtogroup COMP_LL_Private_Macros
  51. * @{
  52. */
  53. /* Check of parameters for configuration of COMP hierarchical scope: */
  54. /* COMP instance. */
  55. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  56. ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  57. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  58. )
  59. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  60. ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  61. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  62. )
  63. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
  64. ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
  65. || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
  66. )
  67. /**
  68. * @}
  69. */
  70. /* Private function prototypes -----------------------------------------------*/
  71. /* Exported functions --------------------------------------------------------*/
  72. /** @addtogroup COMP_LL_Exported_Functions
  73. * @{
  74. */
  75. /** @addtogroup COMP_LL_EF_Init
  76. * @{
  77. */
  78. /**
  79. * @brief De-initialize registers of the selected COMP instance
  80. * to their default reset values.
  81. * @note If comparator is locked, de-initialization by software is
  82. * not possible.
  83. * The only way to unlock the comparator is a device hardware reset.
  84. * @param COMPx COMP instance
  85. * @retval An ErrorStatus enumeration value:
  86. * - SUCCESS: COMP registers are de-initialized
  87. * - ERROR: COMP registers are not de-initialized
  88. */
  89. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  90. {
  91. ErrorStatus status = SUCCESS;
  92. /* Check the parameters */
  93. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  94. LL_COMP_WriteReg(COMPx, CSR, 0x00000000U);
  95. LL_COMP_WriteReg(COMPx, FR, 0x00000000U);
  96. return status;
  97. }
  98. /**
  99. * @brief Initialize some features of COMP instance.
  100. * @note This function configures features of the selected COMP instance.
  101. * Some features are also available at scope COMP common instance
  102. * (common to several COMP instances).
  103. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  104. * @param COMPx COMP instance
  105. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  106. * @retval An ErrorStatus enumeration value:
  107. * - SUCCESS: COMP registers are initialized
  108. * - ERROR: COMP registers are not initialized
  109. */
  110. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  111. {
  112. ErrorStatus status = SUCCESS;
  113. /* Check the parameters */
  114. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  115. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  116. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  117. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  118. /* Configuration of comparator instance : */
  119. /* - InputPlus */
  120. /* - InputMinus */
  121. /* - OutputPolarity */
  122. MODIFY_REG(COMPx->CSR,
  123. COMP_CSR_INPSEL
  124. | COMP_CSR_INNSEL
  125. | COMP_CSR_POLARITY
  126. ,
  127. COMP_InitStruct->InputPlus
  128. | COMP_InitStruct->InputMinus
  129. | COMP_InitStruct->OutputPolarity
  130. );
  131. if (COMP_InitStruct->DigitalFilter == 0)
  132. {
  133. /* Disable digital filter */
  134. CLEAR_BIT(COMPx->FR, COMP_FR_FLTEN);
  135. }
  136. else
  137. {
  138. WRITE_REG(COMPx->FR, (COMP_FR_FLTEN | (COMP_InitStruct->DigitalFilter << COMP_FR_FLTCNT_Pos)));
  139. }
  140. return status;
  141. }
  142. /**
  143. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  144. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  145. * whose fields will be set to default values.
  146. * @retval None
  147. */
  148. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  149. {
  150. /* Set COMP_InitStruct fields to default values */
  151. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  152. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_IO1;
  153. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  154. COMP_InitStruct->DigitalFilter = 0;
  155. }
  156. /**
  157. * @}
  158. */
  159. /**
  160. * @}
  161. */
  162. /**
  163. * @}
  164. */
  165. #endif /* COMP1 || COMP2 */
  166. /**
  167. * @}
  168. */
  169. #endif /* USE_FULL_LL_DRIVER */
  170. /************************ (C) COPYRIGHT Puya *****END OF FILE****/