nu_adc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**************************************************************************//**
  2. * @file nu_adc.h
  3. * @brief ADC driver header file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright(C) 2020 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #ifndef __NU_ADC_H__
  9. #define __NU_ADC_H__
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. /** @addtogroup Standard_Driver Standard Driver
  15. @{
  16. */
  17. /** @addtogroup ADC_Driver ADC Driver
  18. @{
  19. */
  20. /** @addtogroup ADC_EXPORTED_CONSTANTS ADC Exported Constants
  21. @{
  22. */
  23. #include "adc_reg.h"
  24. #define ADC_CH_0_MASK (1UL << 0) /*!< ADC channel 0 mask \hideinitializer */
  25. #define ADC_CH_1_MASK (1UL << 1) /*!< ADC channel 1 mask \hideinitializer */
  26. #define ADC_CH_2_MASK (1UL << 2) /*!< ADC channel 2 mask \hideinitializer */
  27. #define ADC_CH_3_MASK (1UL << 3) /*!< ADC channel 3 mask \hideinitializer */
  28. #define ADC_CH_4_MASK (1UL << 4) /*!< ADC channel 4 mask \hideinitializer */
  29. #define ADC_CH_5_MASK (1UL << 5) /*!< ADC channel 5 mask \hideinitializer */
  30. #define ADC_CH_6_MASK (1UL << 6) /*!< ADC channel 6 mask \hideinitializer */
  31. #define ADC_CH_7_MASK (1UL << 7) /*!< ADC channel 7 mask \hideinitializer */
  32. #define ADC_CH_NUM 8 /*!< Total Channel number \hideinitializer */
  33. #define ADC_HIGH_SPEED_MODE ADC_CONF_SPEED_Msk /*!< ADC working in high speed mode (3.2MHz <= ECLK <= 16MHz) \hideinitializer */
  34. #define ADC_NORMAL_SPEED_MODE 0 /*!< ADC working in normal speed mode (ECLK < 3.2MHz) \hideinitializer */
  35. #define ADC_REFSEL_VREF 0 /*!< ADC reference voltage source selection set to VREF \hideinitializer */
  36. #define ADC_REFSEL_AVDD (3UL << ADC_CONF_REFSEL_Pos) /*!< ADC reference voltage source selection set to AVDD \hideinitializer */
  37. #define ADC_INPUT_MODE_NORMAL_CONV 0 /*!< ADC works in normal conversion mode \hideinitializer */
  38. #define ADC_INPUT_MODE_4WIRE_TOUCH 1 /*!< ADC works in 4-wire touch screen mode \hideinitializer */
  39. #define ADC_INPUT_MODE_5WIRE_TOUCH 2 /*!< ADC works in 5-wire touch screen mode \hideinitializer */
  40. /*@}*/ /* end of group ADC_EXPORTED_CONSTANTS */
  41. /** @addtogroup ADC_EXPORTED_FUNCTIONS ADC Exported Functions
  42. @{
  43. */
  44. /**
  45. * @brief Get the latest ADC conversion data
  46. * @param[in] adc Base address of ADC module
  47. * @param[in] u32ChNum Currently not used
  48. * @return Latest ADC conversion data
  49. * \hideinitializer
  50. */
  51. #define ADC_GET_CONVERSION_DATA(adc, u32ChNum) ((adc)->DATA)
  52. /**
  53. * @brief Get the latest ADC conversion X data
  54. * @param[in] adc Base address of ADC module
  55. * @return Latest ADC conversion X data
  56. * \hideinitializer
  57. */
  58. #define ADC_GET_CONVERSION_XDATA(adc) ((adc)->XYDATA & ADC_XYDATA_XDATA_Msk)
  59. /**
  60. * @brief Get the latest ADC conversion Y data
  61. * @param[in] adc Base address of ADC module
  62. * @return Latest ADC conversion Y data
  63. * \hideinitializer
  64. */
  65. #define ADC_GET_CONVERSION_YDATA(adc) ((adc)->XYDATA >> ADC_XYDATA_YDATA_Pos)
  66. /**
  67. * @brief Get the latest ADC conversion Z1 data
  68. * @param[in] adc Base address of ADC module
  69. * @return Latest ADC conversion Z1 data
  70. * \hideinitializer
  71. */
  72. #define ADC_GET_CONVERSION_Z1DATA(adc) ((adc)->ZDATA & ADC_ZDATA_Z1DATA_Msk)
  73. /**
  74. * @brief Get the latest ADC conversion Z2 data
  75. * @param[in] adc Base address of ADC module
  76. * @return Latest ADC conversion Z2 data
  77. * \hideinitializer
  78. */
  79. #define ADC_GET_CONVERSION_Z2DATA(adc) ((adc)->ZDATA >> ADC_ZDATA_Z2DATA_Pos)
  80. /**
  81. * @brief Return the user-specified interrupt flags
  82. * @param[in] adc Base address of ADC module
  83. * @param[in] u32Mask Could be \ref ADC_IER_MIEN_Msk
  84. * @return User specified interrupt flags
  85. * \hideinitializer
  86. */
  87. #define ADC_GET_INT_FLAG(adc, u32Mask) ((adc)->ISR & (u32Mask))
  88. /**
  89. * @brief This macro clear the selected interrupt status bits
  90. * @param[in] adc Base address of ADC module
  91. * @param[in] u32Mask Could be \ref ADC_IER_MIEN_Msk
  92. * @return None
  93. * \hideinitializer
  94. */
  95. #define ADC_CLR_INT_FLAG(adc, u32Mask) ((adc)->ISR = (u32Mask))
  96. /**
  97. * @brief Return the user-specified interrupt flags
  98. * @param[in] adc Base address of ADC module
  99. * @param[in] u32Mask Could be \ref ADC_IER_MIEN_Msk
  100. * @return User specified interrupt flags
  101. * \hideinitializer
  102. */
  103. #define ADC_GET_WKINT_FLAG(adc, u32Mask) ((adc)->WKISR & (u32Mask))
  104. /**
  105. * @brief Enable the interrupt(s) selected by u32Mask parameter.
  106. * @param[in] adc Base address of ADC module
  107. * @param[in] u32Mask Could be \ref ADC_IER_MIEN_Msk
  108. * @return None
  109. */
  110. #define ADC_ENABLE_INT(adc, u32Mask) ((adc)->IER |= u32Mask)
  111. /**
  112. * @brief Disable the interrupt(s) selected by u32Mask parameter.
  113. * @param[in] adc Base address of ADC module
  114. * @param[in] u32Mask Could be \ref ADC_IER_MIEN_Msk
  115. * @return None
  116. */
  117. #define ADC_DISABLE_INT(adc, u32Mask) ((adc)->IER &= ~u32Mask)
  118. /**
  119. * @brief Power down ADC module
  120. * @param[in] adc Base address of ADC module
  121. * @return None
  122. * \hideinitializer
  123. */
  124. #define ADC_POWER_DOWN(adc) ((adc)->CTL &= ~ADC_CTL_ADEN_Msk)
  125. /**
  126. * @brief Power on ADC module
  127. * @param[in] adc Base address of ADC module
  128. * @return None
  129. * \hideinitializer
  130. */
  131. #define ADC_POWER_ON(adc) ((adc)->CTL |= ADC_CTL_ADEN_Msk)
  132. /**
  133. * @brief Set ADC input channel. Enabled channel will be converted while ADC starts.
  134. * @param[in] adc Base address of ADC module
  135. * @param[in] u32Mask Channel enable bit. Each bit corresponds to a input channel. Bit 0 is channel 0, bit 1 is channel 1...
  136. * @return None
  137. * @note ADC can only convert 1 channel at a time. If more than 1 channels are enabled, only channel
  138. * with smallest number will be convert.
  139. * \hideinitializer
  140. */
  141. #define ADC_SET_INPUT_CHANNEL(adc, u32Mask) do {uint32_t u32Ch = 0, i;\
  142. for(i = 0; i < ADC_CH_NUM; i++) {\
  143. if((u32Mask) & (1 << i)) {\
  144. u32Ch = i;\
  145. break;\
  146. }\
  147. }\
  148. (adc)->CONF = ((adc)->CONF & ~ADC_CONF_CHSEL_Msk) | (u32Ch << ADC_CONF_CHSEL_Pos);\
  149. }while(0)
  150. /**
  151. * @brief Start the A/D conversion.
  152. * @param[in] adc Base address of ADC module
  153. * @return None
  154. * \hideinitializer
  155. */
  156. #define ADC_START_CONV(adc) ((adc)->CTL |= ADC_CTL_MST_Msk)
  157. /**
  158. * @brief Set the reference voltage selection.
  159. * @param[in] adc Base address of ADC module
  160. * @param[in] u32Ref The reference voltage selection. Valid values are:
  161. * - \ref ADC_REFSEL_VREF
  162. * - \ref ADC_REFSEL_AVDD
  163. * @return None
  164. * \hideinitializer
  165. */
  166. #define ADC_SET_REF_VOLTAGE(adc, u32Ref) ((adc)->CONF = ((adc)->CONF & ~ADC_CONF_REFSEL_Msk) | (u32Ref))
  167. /**
  168. * @brief Set ADC to convert X/Y coordinate
  169. * @param[in] adc Base address of ADC module
  170. * @return None
  171. * \hideinitializer
  172. */
  173. #define ADC_CONVERT_XY_MODE(adc) do {(adc)->CTL &= ~ADC_CTL_PEDEEN_Msk;\
  174. (adc)->CONF |= ADC_CONF_TEN_Msk | ADC_CONF_ZEN_Msk;} while(0)
  175. /**
  176. * @brief Set ADC to detect pen down event
  177. * @param[in] adc Base address of ADC module
  178. * @return None
  179. * \hideinitializer
  180. */
  181. #define ADC_DETECT_PD_MODE(adc) do {(adc)->CONF &= ~(ADC_CONF_TEN_Msk | ADC_CONF_ZEN_Msk);\
  182. (adc)->CTL |= ADC_CTL_PEDEEN_Msk;} while(0)
  183. #define ADC_CONF_REFSEL_VREF (0<<ADC_CONF_REFSEL_Pos) /*!< ADC reference select VREF input or 2.5v buffer output */
  184. #define ADC_CONF_REFSEL_YMYP (1<<ADC_CONF_REFSEL_Pos) /*!< ADC reference select YM vs YP */
  185. #define ADC_CONF_REFSEL_XMXP (2<<ADC_CONF_REFSEL_Pos) /*!< ADC reference select XM vs XP */
  186. #define ADC_CONF_REFSEL_AVDD33 (3<<ADC_CONF_REFSEL_Pos) /*!< ADC reference select AGND33 vs AVDD33 */
  187. /** \brief Structure type of ADC_CMD
  188. */
  189. typedef enum
  190. {
  191. START_MST, /*!<Menu Start Conversion with interrupt */
  192. START_MST_POLLING, /*!<Menu Start Conversion with polling */
  193. VBPOWER_ON, /*!<Enable ADC Internal Bandgap Power */
  194. VBPOWER_OFF, /*!<Disable ADC Internal Bandgap Power */
  195. VBAT_ON, /*!<Enable Voltage Battery conversion function */
  196. VBAT_OFF, /*!<Disable Voltage Battery conversion function */
  197. KPPOWER_ON, /*!<Enable ADC Keypad power */
  198. KPPOWER_OFF, /*!<Disable ADC Keypad power */
  199. KPCONV_ON, /*!<Enable Keypad conversion function */
  200. KPCONV_OFF, /*!<Disable Keypad conversion function */
  201. KPPRESS_ON, /*!<Enable Keypad press event */
  202. KPPRESS_OFF, /*!<Disable Keypad press event */
  203. KPUP_ON, /*!<Enable Keypad up event */
  204. KPUP_OFF, /*!<Disable Keypad up event */
  205. PEPOWER_ON, /*!<Enable Pen Down Power ,It can control pen down event */
  206. PEPOWER_OFF, /*!<Disable Pen Power */
  207. PEDEF_ON, /*!<Enable Pen Down Event Flag */
  208. PEDEF_OFF, /*!<Disable Pen Down Event Flag */
  209. WKP_ON, /*!<Enable Keypad Press Wake Up */
  210. WKP_OFF, /*!<Disable Keypad Press Wake Up */
  211. WKT_ON, /*!<Enable Pen Down Wake Up */
  212. WKT_OFF, /*!<Disable Pen Down Wake Up */
  213. SWITCH_5WIRE_ON, /*!<Wire Mode Switch to 5-Wire Configuration */
  214. SWITCH_5WIRE_OFF, /*!<Wire Mode Switch to 4-Wire Configuration */
  215. T_ON, /*!<Enable Touch detection function */
  216. T_OFF, /*!<Disable Touch detection function */
  217. TAVG_ON, /*!<Enable Touch Mean average for X and Y function */
  218. TAVG_OFF, /*!<Disable Touch Mean average for X and Y function */
  219. Z_ON, /*!<Enable Press measure function */
  220. Z_OFF, /*!<Disable Press measure function */
  221. TZAVG_ON, /*!<Enable Pressure Mean average for Z1 and Z2 function */
  222. TZAVG_OFF, /*!<Disable Pressure Mean average for Z1 and Z2 function */
  223. NAC_ON, /*!<Enable Normal AD Conversion */
  224. NAC_OFF, /*!<Disable Normal AD Conversion */
  225. SWITCH_CH, /*!<Switch Channel */
  226. } ADC_CMD;
  227. typedef int32_t(*ADC_CALLBACK)(uint32_t status, uint32_t userData);
  228. void ADC_Open(ADC_T *adc,
  229. uint32_t u32InputMode,
  230. uint32_t u32OpMode,
  231. uint32_t u32ChMask);
  232. void ADC_Close(ADC_T *adc);
  233. void ADC_EnableInt(ADC_T *adc, uint32_t u32Mask);
  234. void ADC_DisableInt(ADC_T *adc, uint32_t u32Mask);
  235. /*@}*/ /* end of group ADC_EXPORTED_FUNCTIONS */
  236. /*@}*/ /* end of group ADC_Driver */
  237. /*@}*/ /* end of group Standard_Driver */
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. #endif //__NU_ADC_H__