fm33lc0xx_fl_comp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. ****************************************************************************************************
  3. * @file fm33lc0xx_fl_comp.c
  4. * @author FMSH Application Team
  5. * @brief Src file of COMP FL Module
  6. ****************************************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) [2019] [Fudan Microelectronics]
  10. * THIS SOFTWARE is licensed under the Mulan PSL v1.
  11. * can use this software according to the terms and conditions of the Mulan PSL v1.
  12. * You may obtain a copy of Mulan PSL v1 at:
  13. * http://license.coscl.org.cn/MulanPSL
  14. * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
  16. * PURPOSE.
  17. * See the Mulan PSL v1 for more details.
  18. *
  19. ****************************************************************************************************
  20. */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "fm33lc0xx_fl_comp.h"
  23. #include "fm33lc0xx_fl_rmu.h"
  24. #include "fm33lc0xx_fl_rcc.h"
  25. #include "fm33lc0xx_fl_opa.h"
  26. #include "fm33_assert.h"
  27. /** @addtogroup FM33LC0XX_FL_Driver
  28. * @{
  29. */
  30. /** @addtogroup COMP
  31. * @{
  32. */
  33. /* Private macros ------------------------------------------------------------*/
  34. /** @addtogroup COMP_FL_Private_Macros
  35. * @{
  36. */
  37. #define IS_COMP_ALL_INSTANCE(INTENCE) (((INTENCE) == COMP1)||\
  38. ((INTENCE) == COMP2))
  39. #define IS_FL_COMP_POSITIVEINPUT(__VALUE__) (((__VALUE__) == FL_COMP_INP_SOURCE_INP1)||\
  40. ((__VALUE__) == FL_COMP_INP_SOURCE_INP2)||\
  41. ((__VALUE__) == FL_COMP_INP_SOURCE_INP3))
  42. #define IS_FL_COMP_NEGATIVEINPUT(__VALUE__) (((__VALUE__) == FL_COMP_INN_SOURCE_INN1)||\
  43. ((__VALUE__) == FL_COMP_INN_SOURCE_INN2)||\
  44. ((__VALUE__) == FL_COMP_INN_SOURCE_VREF)||\
  45. ((__VALUE__) == FL_COMP_INN_SOURCE_VREF_DIV_2))
  46. #define IS_FL_COMP_POLARITY(__VALUE__) (((__VALUE__) == FL_COMP_OUTPUT_POLARITY_NORMAL)||\
  47. ((__VALUE__) == FL_COMP_OUTPUT_POLARITY_INVERT))
  48. #define IS_FL_COMP_EDGE(__VALUE__) (((__VALUE__) == FL_COMP_INTERRUPT_EDGE_BOTH)||\
  49. ((__VALUE__) == FL_COMP_INTERRUPT_EDGE_RISING )||\
  50. ((__VALUE__) == FL_COMP_INTERRUPT_EDGE_FALLING))
  51. #define IS_FL_COMP_DIGITAL_FILTER(__VALUE__) (((__VALUE__) == FL_DISABLE)||\
  52. ((__VALUE__) == FL_ENABLE))
  53. /**
  54. * @}
  55. */
  56. /** @addtogroup COMP_FL_EF_Init
  57. * @{
  58. */
  59. /**
  60. * @brief 复位COMP外设
  61. * @param COMPx 外设入口地址
  62. * @retval 错误状态,可能值:
  63. * -FL_PASS 外设寄存器值恢复复位值
  64. * -FL_FAIL 未成功执行
  65. */
  66. FL_ErrorStatus FL_COMP_DeInit(COMP_Type *COMPx)
  67. {
  68. /* 入口参数检查 */
  69. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  70. /* 恢复寄存器值为默认值 */
  71. COMPx->CR = 0x00000000U;
  72. /* 关闭外设总线时钟和工作时钟 */
  73. FL_RCC_DisableGroup1BusClock(FL_RCC_GROUP1_BUSCLK_ANAC);
  74. /* 锁定外设复位 */
  75. FL_RCC_DisablePeripheralReset();
  76. return FL_PASS;
  77. }
  78. /**
  79. * @brief 根据 COMP_InitStruct 的配置信息初始化对应外设.
  80. * @param COMPx 外设入口地址
  81. * @param initStruct 指向 @ref FL_COMP_InitTypeDef 结构体的指针
  82. * @retval 错误状态,可能值:
  83. * -FL_PASS 配置成功
  84. * -FL_FAIL 配置过程发生错误
  85. */
  86. FL_ErrorStatus FL_COMP_Init(COMP_Type *COMPx, FL_COMP_InitTypeDef *initStruct)
  87. {
  88. /* 入口参数检查 */
  89. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  90. assert_param(IS_FL_COMP_EDGE(initStruct->edge));
  91. assert_param(IS_FL_COMP_POLARITY(initStruct->polarity));
  92. assert_param(IS_FL_COMP_POSITIVEINPUT(initStruct->positiveInput));
  93. assert_param(IS_FL_COMP_NEGATIVEINPUT(initStruct->negativeInput));
  94. assert_param(IS_FL_COMP_DIGITAL_FILTER(initStruct->digitalFilter));
  95. /* 使能时钟总线 */
  96. FL_RCC_EnableGroup1BusClock(FL_RCC_GROUP1_BUSCLK_ANAC);
  97. /* 比较器输出极性选择 */
  98. FL_COMP_SetOutputPolarity(COMPx, initStruct->polarity);
  99. /* 比较器正向输入选择 */
  100. FL_COMP_SetINPSource(COMPx, initStruct->positiveInput);
  101. /* 比较器反向输入选择 */
  102. FL_COMP_SetINNSource(COMPx, initStruct->negativeInput);
  103. /* 比较器使用vref 打开vref_buf */
  104. if((initStruct->negativeInput == FL_COMP_INN_SOURCE_VREF) || (initStruct->negativeInput == FL_COMP_INN_SOURCE_VREF_DIV_2))
  105. {
  106. FL_OPA_EnableVrefBuffer(OPA1);//使能
  107. FL_OPA_DisableBypassVrefBuffer(OPA1);//不bypass
  108. }
  109. if(COMPx == COMP1)
  110. {
  111. /* 比较器中断边沿选择 */
  112. FL_COMP_SetComparator1InterruptEdge(COMP, initStruct->edge);
  113. }
  114. else
  115. {
  116. /* 比较器中断边沿选择 */
  117. FL_COMP_SetComparator2InterruptEdge(COMP, initStruct->edge);
  118. }
  119. /* 滤波 */
  120. if(initStruct->digitalFilter)
  121. {
  122. if(COMPx == COMP1)
  123. { FL_COMP_EnableComparator1OutputFilter(COMP); }
  124. else
  125. { FL_COMP_EnableComparator2OutputFilter(COMP); }
  126. }
  127. else
  128. {
  129. if(COMPx == COMP1)
  130. { FL_COMP_DisableComparator1OutputFilter(COMP); }
  131. else
  132. { FL_COMP_DisableComparator2OutputFilter(COMP); }
  133. }
  134. return FL_PASS;
  135. }
  136. /**
  137. * @brief 将 @ref FL_COMP_InitTypeDef 结构体初始化为默认配置
  138. * @param initStruct 指向 @ref FL_COMP_InitTypeDef 结构体的指针
  139. *
  140. * @retval None
  141. */
  142. void FL_COMP_StructInit(FL_COMP_InitTypeDef *initStruct)
  143. {
  144. /* 复位配置信息 */
  145. initStruct->edge = FL_COMP_INTERRUPT_EDGE_BOTH;
  146. initStruct->polarity = FL_COMP_OUTPUT_POLARITY_NORMAL;
  147. initStruct->negativeInput = FL_COMP_INN_SOURCE_INN1;
  148. initStruct->positiveInput = FL_COMP_INP_SOURCE_INP1;
  149. initStruct->digitalFilter = FL_ENABLE;
  150. }
  151. /**
  152. * @}
  153. */
  154. /**
  155. * @}
  156. */
  157. /**
  158. * @}
  159. */
  160. /*************************************************************END OF FILE************************************************************/