nu_adc.h 11 KB

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