nu_opa.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**************************************************************************//**
  2. * @file nu_opa.h
  3. * @version V3.00
  4. * @brief M480 series OPA driver header file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_OPA_H__
  10. #define __NU_OPA_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup OPA_Driver OPA Driver
  19. @{
  20. */
  21. /** @addtogroup OPA_EXPORTED_CONSTANTS OPA Exported Constants
  22. @{
  23. */
  24. #define OPA_CALIBRATION_CLK_1K (0UL) /*!< OPA calibration clock select 1 KHz \hideinitializer */
  25. #define OPA_CALIBRATION_RV_1_2_AVDD (0UL) /*!< OPA calibration reference voltage select 1/2 AVDD \hideinitializer */
  26. #define OPA_CALIBRATION_RV_H_L_VCM (1UL) /*!< OPA calibration reference voltage select from high vcm to low vcm \hideinitializer */
  27. /*@}*/ /* end of group OPA_EXPORTED_CONSTANTS */
  28. /** @addtogroup OPA_EXPORTED_FUNCTIONS OPA Exported Functions
  29. @{
  30. */
  31. /*---------------------------------------------------------------------------------------------------------*/
  32. /* Define OPA functions prototype */
  33. /*---------------------------------------------------------------------------------------------------------*/
  34. __STATIC_INLINE int32_t OPA_Calibration(OPA_T *opa, uint32_t u32OpaNum, uint32_t u32ClockSel, uint32_t u32LevelSel);
  35. /**
  36. * @brief This macro is used to power on the OPA circuit
  37. * @param[in] opa The pointer of the specified OPA module
  38. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  39. * @return None
  40. * @details This macro will set OPx_EN (x=0, 1) bit of OPACR register to power on the OPA circuit.
  41. * @note Remember to enable HIRC clock while power on the OPA circuit.
  42. * \hideinitializer
  43. */
  44. #define OPA_POWER_ON(opa, u32OpaNum) ((opa)->CTL |= (1UL<<(OPA_CTL_OPEN0_Pos+(u32OpaNum))))
  45. /**
  46. * @brief This macro is used to power down the OPA circuit
  47. * @param[in] opa The pointer of the specified OPA module
  48. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  49. * @return None
  50. * @details This macro will clear OPx_EN (x=0, 1) bit of OPACR register to power down the OPA circuit.
  51. * \hideinitializer
  52. */
  53. #define OPA_POWER_DOWN(opa, u32OpaNum) ((opa)->CTL &= ~(1UL<<(OPA_CTL_OPEN0_Pos+(u32OpaNum))))
  54. /**
  55. * @brief This macro is used to enable the OPA Schmitt trigger buffer
  56. * @param[in] opa The pointer of the specified OPA module
  57. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  58. * @return None
  59. * @details This macro will set OPSCHx_EN (x=0, 1) bit of OPACR register to enable the OPA Schmitt trigger buffer.
  60. * \hideinitializer
  61. */
  62. #define OPA_ENABLE_SCH_TRIGGER(opa, u32OpaNum) ((opa)->CTL |= (1UL<<(OPA_CTL_OPDOEN0_Pos+(u32OpaNum))))
  63. /**
  64. * @brief This macro is used to disable the OPA Schmitt trigger buffer
  65. * @param[in] opa The pointer of the specified OPA module
  66. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  67. * @return None
  68. * @details This macro will clear OPSCHx_EN (x=0, 1) bit of OPACR register to disable the OPA Schmitt trigger buffer.
  69. * \hideinitializer
  70. */
  71. #define OPA_DISABLE_SCH_TRIGGER(opa, u32OpaNum) ((opa)->CTL &= ~(1UL<<(OPA_CTL_OPDOEN0_Pos+(u32OpaNum))))
  72. /**
  73. * @brief This macro is used to enable OPA Schmitt trigger digital output interrupt
  74. * @param[in] opa The pointer of the specified OPA module
  75. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  76. * @return None
  77. * @details This macro will set OPDIEx (x=0, 1) bit of OPACR register to enable the OPA Schmitt trigger digital output interrupt.
  78. * \hideinitializer
  79. */
  80. #define OPA_ENABLE_INT(opa, u32OpaNum) ((opa)->CTL |= (1UL<<(OPA_CTL_OPDOIEN0_Pos+(u32OpaNum))))
  81. /**
  82. * @brief This macro is used to disable OPA Schmitt trigger digital output interrupt
  83. * @param[in] opa The pointer of the specified OPA module
  84. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  85. * @return None
  86. * @details This macro will clear OPDIEx (x=0, 1) bit of OPACR register to disable the OPA Schmitt trigger digital output interrupt.
  87. * \hideinitializer
  88. */
  89. #define OPA_DISABLE_INT(opa, u32OpaNum) ((opa)->CTL &= ~(1UL<<(OPA_CTL_OPDOIEN0_Pos+(u32OpaNum))))
  90. /**
  91. * @brief This macro is used to get OPA digital output state
  92. * @param[in] opa The pointer of the specified OPA module
  93. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  94. * @return OPA digital output state
  95. * @details This macro will return the OPA digital output value.
  96. * \hideinitializer
  97. */
  98. #define OPA_GET_DIGITAL_OUTPUT(opa, u32OpaNum) (((opa)->STATUS & (OPA_STATUS_OPDO0_Msk<<(u32OpaNum)))?1UL:0UL)
  99. /**
  100. * @brief This macro is used to get OPA interrupt flag
  101. * @param[in] opa The pointer of the specified OPA module
  102. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  103. * @retval 0 OPA interrupt does not occur.
  104. * @retval 1 OPA interrupt occurs.
  105. * @details This macro will return the ACMP interrupt flag.
  106. * \hideinitializer
  107. */
  108. #define OPA_GET_INT_FLAG(opa, u32OpaNum) (((opa)->STATUS & (OPA_STATUS_OPDOIF0_Msk<<(u32OpaNum)))?1UL:0UL)
  109. /**
  110. * @brief This macro is used to clear OPA interrupt flag
  111. * @param[in] opa The pointer of the specified OPA module
  112. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  113. * @return None
  114. * @details This macro will write 1 to OPDFx (x=0,1) bit of OPASR register to clear interrupt flag.
  115. * \hideinitializer
  116. */
  117. #define OPA_CLR_INT_FLAG(opa, u32OpaNum) ((opa)->STATUS = (OPA_STATUS_OPDOIF0_Msk<<(u32OpaNum)))
  118. /**
  119. * @brief This function is used to configure and start OPA calibration
  120. * @param[in] opa The pointer of the specified OPA module
  121. * @param[in] u32OpaNum The OPA number. 0 for OPA0; 1 for OPA1; 2 for OPA2.
  122. * @param[in] u32ClockSel Select OPA calibration clock
  123. * - \ref OPA_CALIBRATION_CLK_1K
  124. * @param[in] u32RefVol Select OPA reference voltage
  125. * - \ref OPA_CALIBRATION_RV_1_2_AVDD
  126. * - \ref OPA_CALIBRATION_RV_H_L_VCM
  127. * @retval 0 PMOS and NMOS calibration successfully.
  128. * @retval -1 only PMOS calibration failed.
  129. * @retval -2 only NMOS calibration failed.
  130. * @retval -3 PMOS and NMOS calibration failed.
  131. */
  132. __STATIC_INLINE int32_t OPA_Calibration(OPA_T *opa,
  133. uint32_t u32OpaNum,
  134. uint32_t u32ClockSel,
  135. uint32_t u32RefVol)
  136. {
  137. uint32_t u32CALResult;
  138. int32_t i32Ret = 0L;
  139. (opa)->CALCTL = (((opa)->CALCTL) & ~(OPA_CALCTL_CALCLK0_Msk << (u32OpaNum << 1)));
  140. (opa)->CALCTL = (((opa)->CALCTL) & ~(OPA_CALCTL_CALRVS0_Msk << (u32OpaNum))) | (((u32RefVol) << OPA_CALCTL_CALRVS0_Pos) << (u32OpaNum));
  141. (opa)->CALCTL |= (OPA_CALCTL_CALTRG0_Msk << (u32OpaNum));
  142. while((opa)->CALCTL & (OPA_CALCTL_CALTRG0_Msk << (u32OpaNum))) {}
  143. u32CALResult = ((opa)->CALST >> ((u32OpaNum)*4U)) & (OPA_CALST_CALNS0_Msk|OPA_CALST_CALPS0_Msk);
  144. if (u32CALResult == 0U)
  145. {
  146. i32Ret = 0L;
  147. }
  148. else if (u32CALResult == OPA_CALST_CALNS0_Msk)
  149. {
  150. i32Ret = -2L;
  151. }
  152. else if (u32CALResult == OPA_CALST_CALPS0_Msk)
  153. {
  154. i32Ret = -1L;
  155. }
  156. else if (u32CALResult == (OPA_CALST_CALNS0_Msk|OPA_CALST_CALPS0_Msk))
  157. {
  158. i32Ret = -3L;
  159. }
  160. return i32Ret;
  161. }
  162. /**
  163. * @brief This macro is used to generate asynchronous reset signals to OPA controller
  164. * @param None
  165. * @return None
  166. * \hideinitializer
  167. */
  168. #define OPA_Reset() \
  169. do { \
  170. SYS->IPRST2 |= SYS_IPRST2_OPARST_Msk; \
  171. SYS->IPRST2 &= ~SYS_IPRST2_OPARST_Msk; \
  172. } while(0)
  173. /*@}*/ /* end of group OPA_EXPORTED_FUNCTIONS */
  174. /*@}*/ /* end of group OPA_Driver */
  175. /*@}*/ /* end of group Standard_Driver */
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif /* __NU_OPA_H__ */
  180. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/