fm33lc0xx_fl_opa.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. *******************************************************************************************************
  3. * @file fm33lc0xx_fl_opa.c
  4. * @author FMSH Application Team
  5. * @brief Src file of OPA 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. #include "fm33lc0xx_fl_rcc.h"
  22. #include "fm33lc0xx_fl_rmu.h"
  23. #include "fm33lc0xx_fl_opa.h"
  24. #include "fm33_assert.h"
  25. /** @addtogroup FM33LC0XX_FL_Driver
  26. * @{
  27. */
  28. /** @addtogroup OPA
  29. * @{
  30. */
  31. /* Private macros ------------------------------------------------------------*/
  32. /** @addtogroup OPA_FL_Private_Macros
  33. * @{
  34. */
  35. #define IS_OPA_ALL_INSTANCE(INTENCE) (((INTENCE) == OPA1)||\
  36. ((INTENCE) == OPA2))
  37. #define IS_FL_OPA_INP_CHANNAL(__VALUE__) (((__VALUE__) == FL_OPA_INP_SOURCE_INP1)||\
  38. ((__VALUE__) == FL_OPA_INP_SOURCE_INP2))
  39. #define IS_FL_OPA_INN_CHANNAL(__VALUE__) (((__VALUE__) == FL_OPA_INN_SOURCE_INN1)||\
  40. ((__VALUE__) == FL_OPA_INN_SOURCE_INN2)||\
  41. ((__VALUE__) == FL_OPA_INN_SOURCE_VREF)||\
  42. ((__VALUE__) == FL_OPA_INN_SOURCE_THREE_QUARTERS_VREF)||\
  43. ((__VALUE__) == FL_OPA_INN_SOURCE_HALF_VREF)||\
  44. ((__VALUE__) == FL_OPA_INN_SOURCE_ONE_QUARTER_VREF)||\
  45. ((__VALUE__) == FL_OPA_INN_SOURCE_ONE_EIGHTH_VREF))
  46. #define IS_FL_OPA_MODE(__VALUE__) (((__VALUE__) == FL_OPA_MODE_STANDALONE)||\
  47. ((__VALUE__) == FL_OPA_MODE_COMPARATOR)||\
  48. ((__VALUE__) == FL_OPA_MODE_PGA)||\
  49. ((__VALUE__) == FL_OPA_MODE_BUFFER))
  50. #define IS_FL_OPA_DIGITALFILTER(__VALUE__) (((__VALUE__) == FL_DISABLE)||\
  51. ((__VALUE__) == FL_ENABLE))
  52. #define IS_FL_OPA_NEGTIVE_TO_PIN(__VALUE__) (((__VALUE__) == FL_DISABLE)||\
  53. ((__VALUE__) == FL_ENABLE))
  54. #define IS_FL_OPA_LOW_POWER_MODE(__VALUE__) (((__VALUE__) == FL_DISABLE)||\
  55. ((__VALUE__) == FL_ENABLE))
  56. #define IS_FL_OPA_GAIN(__VALUE__) (((__VALUE__) == FL_OPA_GAIN_X2)||\
  57. ((__VALUE__) == FL_OPA_GAIN_X4)||\
  58. ((__VALUE__) == FL_OPA_GAIN_X8)||\
  59. ((__VALUE__) == FL_OPA_GAIN_X16))
  60. /**
  61. * @}
  62. */
  63. /** @addtogroup OPA_FL_EF_Init
  64. * @{
  65. */
  66. /**
  67. * @brief 复位OPA
  68. * @param OPAx 外设入口地址
  69. * @retval 错误状态,可能值:
  70. * -FL_PASS 外设寄存器值恢复复位值
  71. * -FL_FAIL 未成功执行
  72. */
  73. FL_ErrorStatus FL_OPA_DeInit(OPA_Type *OPAx)
  74. {
  75. /* 入口参数合法性断言 */
  76. assert_param(IS_OPA_ALL_INSTANCE(OPAx));
  77. /* 使能外设复位 */
  78. FL_RCC_EnablePeripheralReset();
  79. /* 复位外设寄存器 */
  80. FL_RCC_EnableResetAPB2Peripheral(FL_RCC_RSTAPB_OPA);
  81. FL_RCC_DisableResetAPB2Peripheral(FL_RCC_RSTAPB_OPA);
  82. /* 关闭外设总线时钟和工作时钟 */
  83. FL_RCC_DisableGroup1BusClock(FL_RCC_GROUP1_BUSCLK_ANAC);
  84. /* 锁定外设复位 */
  85. FL_RCC_DisablePeripheralReset();
  86. return FL_PASS;
  87. }
  88. /**
  89. * @brief 配置OPA
  90. *
  91. * @param OPAx 外设入口地址
  92. * @param initStruct 指向 @ref FL_OPA_InitTypeDef 结构体的指针
  93. *
  94. * @retval 错误状态,可能值:
  95. * -FL_PASS 配置成功
  96. * -FL_FAIL 配置过程发生错误
  97. */
  98. FL_ErrorStatus FL_OPA_Init(OPA_Type *OPAx, FL_OPA_InitTypeDef *initStruct)
  99. {
  100. FL_ErrorStatus status = FL_PASS;
  101. /* 入口参数检查 */
  102. assert_param(IS_OPA_ALL_INSTANCE(OPAx));
  103. assert_param(IS_FL_OPA_INP_CHANNAL(initStruct->INP));
  104. assert_param(IS_FL_OPA_INN_CHANNAL(initStruct->INN));
  105. assert_param(IS_FL_OPA_MODE(initStruct->mode));
  106. assert_param(IS_FL_OPA_DIGITALFILTER(initStruct->digitalFilter));
  107. assert_param(IS_FL_OPA_NEGTIVE_TO_PIN(initStruct->negtiveToPin));
  108. assert_param(IS_FL_OPA_LOW_POWER_MODE(initStruct->lowPowermode));
  109. assert_param(IS_FL_OPA_GAIN(initStruct->gain));
  110. /*总线时钟使能*/
  111. FL_RCC_EnableGroup1BusClock(FL_RCC_GROUP1_BUSCLK_ANAC);
  112. /*配置模式*/
  113. FL_OPA_SetMode(OPAx, initStruct->mode);
  114. /*配置反向输入*/
  115. FL_OPA_SetINNSource(OPAx, initStruct->INN);
  116. /*配置正向输入*/
  117. FL_OPA_SetINPSource(OPAx, initStruct->INP);
  118. /*配置低功耗模式*/
  119. if(initStruct->lowPowermode == FL_ENABLE)
  120. {
  121. FL_OPA_EnableLowPowerMode(OPAx);
  122. }
  123. if(initStruct->mode == FL_OPA_MODE_PGA)
  124. {
  125. /*配置PGA增益*/
  126. FL_OPA_PGA_SetGain(OPAx, initStruct->gain);
  127. if(initStruct->negtiveToPin == FL_ENABLE)
  128. {
  129. /*配置PGA模式下反向输入端接到PIN*/
  130. FL_OPA_PGA_EnableINNConnectToPin(OPAx);
  131. }
  132. }
  133. return status;
  134. }
  135. /**
  136. * @brief 将 @ref FL_OPA_InitTypeDef 结构体初始化为默认配置
  137. * @param initStruct 指向 @ref FL_OPA_InitTypeDef 结构体的指针
  138. *
  139. * @retval None
  140. */
  141. void FL_OPA_StructInit(FL_OPA_InitTypeDef *initStruct)
  142. {
  143. initStruct->INP = FL_OPA_INP_SOURCE_INP1;
  144. initStruct->INN = FL_OPA_INN_SOURCE_INN1;
  145. initStruct->mode = FL_OPA_MODE_STANDALONE;
  146. initStruct->digitalFilter = FL_DISABLE;
  147. initStruct->negtiveToPin = FL_DISABLE;
  148. initStruct->gain = FL_OPA_GAIN_X2;
  149. initStruct->lowPowermode = FL_DISABLE;
  150. }
  151. /**
  152. *@}
  153. */
  154. /**
  155. *@}
  156. */
  157. /**
  158. *@}
  159. */
  160. /******************************************* END OF FILE *******************************************/