py32f0xx_ll_comp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_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 "py32f0xx_ll_comp.h"
  33. #include "py32f0xx_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 PY32F0xx_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_POWER_MODE(__POWER_MODE__) \
  56. ( ((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED) \
  57. || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \
  58. )
  59. /* Note: On this PY32 series, comparator input plus parameters are */
  60. /* the same on all COMP instances. */
  61. /* However, comparator instance kept as macro parameter for */
  62. /* compatibility with other PY32 families. */
  63. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  64. ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  65. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  66. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \
  67. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO4) \
  68. )
  69. /* Note: On this PY32 series, comparator input minus parameters are */
  70. /* the same on all COMP instances. */
  71. /* However, comparator instance kept as macro parameter for */
  72. /* compatibility with other PY32 families. */
  73. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  74. ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  75. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  76. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  77. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  78. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VCC) \
  79. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_TS) \
  80. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  81. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  82. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3) \
  83. )
  84. #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
  85. ( ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_DISABLE) \
  86. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_ENABLE) \
  87. )
  88. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
  89. ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
  90. || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
  91. )
  92. /**
  93. * @}
  94. */
  95. /* Private function prototypes -----------------------------------------------*/
  96. /* Exported functions --------------------------------------------------------*/
  97. /** @addtogroup COMP_LL_Exported_Functions
  98. * @{
  99. */
  100. /** @addtogroup COMP_LL_EF_Init
  101. * @{
  102. */
  103. /**
  104. * @brief De-initialize registers of the selected COMP instance
  105. * to their default reset values.
  106. * @note If comparator is locked, de-initialization by software is
  107. * not possible.
  108. * The only way to unlock the comparator is a device hardware reset.
  109. * @param COMPx COMP instance
  110. * @retval An ErrorStatus enumeration value:
  111. * - SUCCESS: COMP registers are de-initialized
  112. * - ERROR: COMP registers are not de-initialized
  113. */
  114. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  115. {
  116. ErrorStatus status = SUCCESS;
  117. /* Check the parameters */
  118. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  119. /* Note: Hardware constraint (refer to description of this function): */
  120. /* COMP instance must not be locked. */
  121. /* Note: If Deint the COMP1, The HYST and SCALER_EN of COMP2 will be cleared together */
  122. if (LL_COMP_IsLocked(COMPx) == 0U)
  123. {
  124. LL_COMP_WriteReg(COMPx, CSR, 0x00000000U);
  125. LL_COMP_WriteReg(COMPx, FR, 0x00000000U);
  126. }
  127. else
  128. {
  129. /* Comparator instance is locked: de-initialization by software is */
  130. /* not possible. */
  131. /* The only way to unlock the comparator is a device hardware reset. */
  132. status = ERROR;
  133. }
  134. return status;
  135. }
  136. /**
  137. * @brief Initialize some features of COMP instance.
  138. * @note This function configures features of the selected COMP instance.
  139. * Some features are also available at scope COMP common instance
  140. * (common to several COMP instances).
  141. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  142. * @param COMPx COMP instance
  143. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  144. * @retval An ErrorStatus enumeration value:
  145. * - SUCCESS: COMP registers are initialized
  146. * - ERROR: COMP registers are not initialized
  147. */
  148. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  149. {
  150. ErrorStatus status = SUCCESS;
  151. /* Check the parameters */
  152. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  153. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  154. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  155. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  156. assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
  157. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  158. /* Note: Hardware constraint (refer to description of this function) */
  159. /* COMP instance must not be locked. */
  160. if (LL_COMP_IsLocked(COMPx) == 0U)
  161. {
  162. /* Configuration of comparator instance : */
  163. /* - PowerMode */
  164. /* - InputPlus */
  165. /* - InputMinus */
  166. /* - OutputPolarity */
  167. MODIFY_REG(COMPx->CSR,
  168. COMP_CSR_PWRMODE
  169. | COMP_CSR_INPSEL
  170. | COMP_CSR_INMSEL
  171. | COMP_CSR_POLARITY
  172. ,
  173. COMP_InitStruct->PowerMode
  174. | COMP_InitStruct->InputPlus
  175. | COMP_InitStruct->InputMinus
  176. | COMP_InitStruct->OutputPolarity
  177. );
  178. /* Set comparator hysteresis mode */
  179. MODIFY_REG(COMP12_COMMON->CSR_ODD, COMP_CSR_HYST,COMP_InitStruct->InputHysteresis);
  180. /* Set Vrefint Scaler Enable */
  181. if ((COMP_InitStruct->InputMinus==LL_COMP_INPUT_MINUS_1_4VREFINT)||(COMP_InitStruct->InputMinus==LL_COMP_INPUT_MINUS_1_2VREFINT)
  182. ||(COMP_InitStruct->InputMinus==LL_COMP_INPUT_MINUS_3_4VREFINT)||(COMP_InitStruct->InputMinus==LL_COMP_INPUT_MINUS_VREFINT))
  183. {
  184. SET_BIT(COMP12_COMMON->CSR_ODD, COMP_CSR_SCALER_EN);
  185. }
  186. if (COMP_InitStruct->DigitalFilter == 0)
  187. {
  188. /* Disable digital filter */
  189. CLEAR_BIT(COMPx->FR, COMP_FR_FLTEN);
  190. }
  191. else
  192. {
  193. WRITE_REG(COMPx->FR, (COMP_FR_FLTEN | (COMP_InitStruct->DigitalFilter << COMP_FR_FLTCNT_Pos)));
  194. }
  195. }
  196. else
  197. {
  198. /* Initialization error: COMP instance is locked. */
  199. status = ERROR;
  200. }
  201. return status;
  202. }
  203. /**
  204. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  205. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  206. * whose fields will be set to default values.
  207. * @retval None
  208. */
  209. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  210. {
  211. /* Set COMP_InitStruct fields to default values */
  212. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_MEDIUMSPEED;
  213. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  214. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  215. COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_DISABLE;
  216. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  217. COMP_InitStruct->DigitalFilter = 0;
  218. }
  219. /**
  220. * @}
  221. */
  222. /**
  223. * @}
  224. */
  225. /**
  226. * @}
  227. */
  228. #endif /* COMP1 || COMP2 */
  229. /**
  230. * @}
  231. */
  232. #endif /* USE_FULL_LL_DRIVER */
  233. /************************ (C) COPYRIGHT Puya *****END OF FILE****/