opa.h 8.0 KB

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