n32g45x_opamp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_opamp.c
  29. * @author Nations
  30. * @version v1.0.2
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_opamp.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup OPAMP
  40. * @brief OPAMP driver modules
  41. * @{
  42. */
  43. /** @addtogroup OPAMP_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup OPAMP_Private_Defines
  50. * @{
  51. */
  52. /**
  53. * @}
  54. */
  55. /** @addtogroup OPAMP_Private_Macros
  56. * @{
  57. */
  58. /**
  59. * @}
  60. */
  61. /** @addtogroup OPAMP_Private_Variables
  62. * @{
  63. */
  64. /**
  65. * @}
  66. */
  67. /** @addtogroup OPAMP_Private_FunctionPrototypes
  68. * @{
  69. */
  70. /**
  71. * @}
  72. */
  73. /** @addtogroup OPAMP_Private_Functions
  74. * @{
  75. */
  76. #define SetBitMsk(reg, bit, msk) ((reg) = (((reg) & ~(msk)) | (bit)))
  77. #define ClrBit(reg, bit) ((reg) &= ~(bit))
  78. #define SetBit(reg, bit) ((reg) |= (bit))
  79. #define GetBit(reg, bit) ((reg) & (bit))
  80. /**
  81. * @brief Deinitializes the OPAMP peripheral registers to their default reset values.
  82. */
  83. void OPAMP_DeInit(void)
  84. {
  85. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_OPAMP, ENABLE);
  86. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_OPAMP, DISABLE);
  87. }
  88. void OPAMP_StructInit(OPAMP_InitType* OPAMP_InitStruct)
  89. {
  90. OPAMP_InitStruct->Gain = OPAMP_CS_PGA_GAIN_2;
  91. OPAMP_InitStruct->HighVolRangeEn = ENABLE;
  92. OPAMP_InitStruct->TimeAutoMuxEn = DISABLE;
  93. OPAMP_InitStruct->Mod = OPAMP_CS_PGA_EN;
  94. }
  95. void OPAMP_Init(OPAMPX OPAMPx, OPAMP_InitType* OPAMP_InitStruct)
  96. {
  97. __IO uint32_t* pCs = &OPAMP->CS1 + OPAMPx;
  98. __IO uint32_t tmp = *pCs;
  99. SetBitMsk(tmp, OPAMP_InitStruct->Gain, OPAMP_CS_PGA_GAIN_MASK);
  100. if(OPAMP_InitStruct->HighVolRangeEn==ENABLE)
  101. SetBitMsk(tmp, OPAMP_CS_RANGE_MASK, OPAMP_CS_RANGE_MASK);
  102. else
  103. ClrBit(tmp,OPAMP_CS_RANGE_MASK);
  104. if(OPAMP_InitStruct->TimeAutoMuxEn==ENABLE)
  105. SetBitMsk(tmp,OPAMP_CS_TCMEN_MASK, OPAMP_CS_TCMEN_MASK);
  106. else
  107. ClrBit(tmp,OPAMP_CS_TCMEN_MASK);
  108. SetBitMsk(tmp, OPAMP_InitStruct->Mod, OPAMP_CS_MOD_MASK);
  109. *pCs = tmp;
  110. }
  111. void OPAMP_Enable(OPAMPX OPAMPx, FunctionalState en)
  112. {
  113. __IO uint32_t* pCs = &OPAMP->CS1 + OPAMPx;
  114. if (en)
  115. SetBit(*pCs, OPAMP_CS_EN_MASK);
  116. else
  117. ClrBit(*pCs, OPAMP_CS_EN_MASK);
  118. }
  119. void OPAMP_SetPgaGain(OPAMPX OPAMPx, OPAMP_CS_PGA_GAIN Gain)
  120. {
  121. __IO uint32_t* pCs = &OPAMP->CS1 + OPAMPx;
  122. __IO uint32_t tmp = *pCs;
  123. SetBitMsk(tmp, Gain, OPAMP_CS_PGA_GAIN_MASK);
  124. *pCs = tmp;
  125. }
  126. void OPAMP_SetVpSecondSel(OPAMPX OPAMPx, OPAMP_CS_VPSSEL VpSSel)
  127. {
  128. __IO uint32_t* pCs = &OPAMP->CS1 + OPAMPx;
  129. __IO uint32_t tmp = *pCs;
  130. SetBitMsk(tmp, VpSSel, OPAMP_CS_VPSEL_SECOND_MASK);
  131. *pCs = tmp;
  132. }
  133. void OPAMP_SetVmSecondSel(OPAMPX OPAMPx, OPAMP_CS_VMSSEL VmSSel)
  134. {
  135. __IO uint32_t* pCs = &OPAMP->CS1 + OPAMPx;
  136. __IO uint32_t tmp = *pCs;
  137. SetBitMsk(tmp, VmSSel, OPAMP_CS_VMSEL_SECOND_MASK);
  138. *pCs = tmp;
  139. }
  140. void OPAMP_SetVpSel(OPAMPX OPAMPx, OPAMP_CS_VPSEL VpSel)
  141. {
  142. __IO uint32_t* pCs = &OPAMP->CS1 + OPAMPx;
  143. __IO uint32_t tmp = *pCs;
  144. SetBitMsk(tmp, VpSel, OPAMP_CS_VPSEL_MASK);
  145. *pCs = tmp;
  146. }
  147. void OPAMP_SetVmSel(OPAMPX OPAMPx, OPAMP_CS_VMSEL VmSel)
  148. {
  149. __IO uint32_t* pCs = &OPAMP->CS1 + OPAMPx;
  150. __IO uint32_t tmp = *pCs;
  151. SetBitMsk(tmp, VmSel, OPAMP_CS_VMSEL_MASK);
  152. *pCs = tmp;
  153. }
  154. bool OPAMP_IsCalOutHigh(OPAMPX OPAMPx)
  155. {
  156. __IO uint32_t* pCs = &OPAMP->CS1 + OPAMPx;
  157. return (GetBit(*pCs, OPAMP_CS_CALOUT_MASK)) ? true : false;
  158. }
  159. void OPAMP_CalibrationEnable(OPAMPX OPAMPx, FunctionalState en)
  160. {
  161. __IO uint32_t* pCs = &OPAMP->CS1 + OPAMPx;
  162. if (en)
  163. SetBit(*pCs, OPAMP_CS_CALON_MASK);
  164. else
  165. ClrBit(*pCs, OPAMP_CS_CALON_MASK);
  166. }
  167. // Lock see @OPAMP_LOCK
  168. void OPAMP_SetLock(uint32_t Lock)
  169. {
  170. OPAMP->LOCK = Lock;
  171. }
  172. /**
  173. * @}
  174. */
  175. /**
  176. * @}
  177. */
  178. /**
  179. * @}
  180. */