adc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**************************************************************************//**
  2. * @file adc.h
  3. * @version V1.00
  4. * $Revision: 23 $
  5. * $Date: 15/11/16 2:12p $
  6. * @brief NUC472/NUC442 ADC driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __ADC_H__
  12. #define __ADC_H__
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  18. @{
  19. */
  20. /** @addtogroup NUC472_442_ADC_Driver ADC Driver
  21. @{
  22. */
  23. /** @addtogroup NUC472_442_ADC_EXPORTED_CONSTANTS ADC Exported Constants
  24. @{
  25. */
  26. #define ADC_CH_0_MASK (1UL << 0) /*!< ADC channel 0 mask \hideinitializer */
  27. #define ADC_CH_1_MASK (1UL << 1) /*!< ADC channel 1 mask \hideinitializer */
  28. #define ADC_CH_2_MASK (1UL << 2) /*!< ADC channel 2 mask \hideinitializer */
  29. #define ADC_CH_3_MASK (1UL << 3) /*!< ADC channel 3 mask \hideinitializer */
  30. #define ADC_CH_4_MASK (1UL << 4) /*!< ADC channel 4 mask \hideinitializer */
  31. #define ADC_CH_5_MASK (1UL << 5) /*!< ADC channel 5 mask \hideinitializer */
  32. #define ADC_CH_6_MASK (1UL << 6) /*!< ADC channel 6 mask \hideinitializer */
  33. #define ADC_CH_7_MASK (1UL << 7) /*!< ADC channel 7 mask \hideinitializer */
  34. #define ADC_CH_8_MASK (1UL << 8) /*!< ADC channel 8 mask \hideinitializer */
  35. #define ADC_CH_9_MASK (1UL << 9) /*!< ADC channel 9 mask \hideinitializer */
  36. #define ADC_CH_10_MASK (1UL << 10) /*!< ADC channel 10 mask \hideinitializer */
  37. #define ADC_CH_11_MASK (1UL << 11) /*!< ADC channel 11 mask \hideinitializer */
  38. #define ADC_CH_BG_MASK (1UL << 16) /*!< ADC channel 12 (band-gap ) mask \hideinitializer */
  39. #define ADC_CH_TS_MASK (1UL << 17) /*!< ADC channel 13 (temperature sensor) mask \hideinitializer */
  40. #define ADC_CMP_LESS_THAN (0UL) /*!< ADC compare condition less than \hideinitializer */
  41. #define ADC_CMP_GREATER_OR_EQUAL_TO (ADC_CMP0_CMPCOND_Msk) /*!< ADC compare condition greater or equal to \hideinitializer */
  42. #define ADC_TRIGGER_BY_EXT_PIN (0UL) /*!< ADC trigger by STADC pin \hideinitializer */
  43. #define ADC_TRIGGER_BY_PWM (ADC_CTL_HWTRGSEL_Msk) /*!< ADC trigger by PWM events \hideinitializer */
  44. #define ADC_LOW_LEVEL_TRIGGER (0UL << ADC_CTL_HWTRGCOND_Pos) /*!< External pin low level trigger ADC \hideinitializer */
  45. #define ADC_HIGH_LEVEL_TRIGGER (1UL << ADC_CTL_HWTRGCOND_Pos) /*!< External pin high level trigger ADC \hideinitializer */
  46. #define ADC_FALLING_EDGE_TRIGGER (2UL << ADC_CTL_HWTRGCOND_Pos) /*!< External pin falling edge trigger ADC \hideinitializer */
  47. #define ADC_RISING_EDGE_TRIGGER (3UL << ADC_CTL_HWTRGCOND_Pos) /*!< External pin rising edge trigger ADC \hideinitializer */
  48. #define ADC_ADF_INT (ADC_STATUS0_ADIF_Msk) /*!< ADC convert complete interrupt \hideinitializer */
  49. #define ADC_CMP0_INT (ADC_STATUS0_ADCMPF0_Msk) /*!< ADC comparator 0 interrupt \hideinitializer */
  50. #define ADC_CMP1_INT (ADC_STATUS0_ADCMPF1_Msk) /*!< ADC comparator 1 interrupt \hideinitializer */
  51. #define ADC_INPUT_MODE_SINGLE_END (0UL << ADC_CTL_DIFFEN_Pos) /*!< ADC input mode set to single end \hideinitializer */
  52. #define ADC_INPUT_MODE_DIFFERENTIAL (1UL << ADC_CTL_DIFFEN_Pos) /*!< ADC input mode set to differential \hideinitializer */
  53. #define ADC_OPERATION_MODE_SINGLE (0UL << ADC_CTL_OPMODE_Pos) /*!< ADC operation mode set to single conversion \hideinitializer */
  54. #define ADC_OPERATION_MODE_SINGLE_CYCLE (2UL << ADC_CTL_OPMODE_Pos) /*!< ADC operation mode set to single cycle scan \hideinitializer */
  55. #define ADC_OPERATION_MODE_CONTINUOUS (3UL << ADC_CTL_OPMODE_Pos) /*!< ADC operation mode set to continuous scan \hideinitializer */
  56. #define ADC_DMODE_OUT_FORMAT_UNSIGNED (0UL << ADC_CTL_DMOF_Pos) /*!< ADC differential mode output format with unsigned \hideinitializer */
  57. #define ADC_DMODE_OUT_FORMAT_2COMPLEMENT (1UL << ADC_CTL_DMOF_Pos) /*!< ADC differential mode output format with 2's complement \hideinitializer */
  58. /*@}*/ /* end of group NUC472_442_ADC_EXPORTED_CONSTANTS */
  59. /** @addtogroup NUC472_442_ADC_EXPORTED_FUNCTIONS ADC Exported Functions
  60. @{
  61. */
  62. /**
  63. * @brief Get the latest ADC conversion data
  64. * @param[in] adc Base address of ADC module
  65. * @param[in] u32ChNum Channel number
  66. * @return Latest ADC conversion data
  67. * \hideinitializer
  68. */
  69. #define ADC_GET_CONVERSION_DATA(adc, u32ChNum) ( ADC->DAT[u32ChNum] & ADC_DAT0_RESULT_Msk)
  70. /**
  71. * @brief Return the user-specified interrupt flags
  72. * @param[in] adc Base address of ADC module
  73. * @param[in] u32Mask The combination of following interrupt status bits. Each bit corresponds to a interrupt status.
  74. * - \ref ADC_ADF_INT
  75. * - \ref ADC_CMP0_INT
  76. * - \ref ADC_CMP1_INT
  77. * @return User specified interrupt flags
  78. * \hideinitializer
  79. */
  80. #define ADC_GET_INT_FLAG(adc, u32Mask) (ADC->STATUS0 & (u32Mask))
  81. /**
  82. * @brief This macro clear the selected interrupt status bits
  83. * @param[in] adc Base address of ADC module
  84. * @param[in] u32Mask The combination of following interrupt status bits. Each bit corresponds to a interrupt status.
  85. * - \ref ADC_ADF_INT
  86. * - \ref ADC_CMP0_INT
  87. * - \ref ADC_CMP1_INT
  88. * @return None
  89. * \hideinitializer
  90. */
  91. #define ADC_CLR_INT_FLAG(adc, u32Mask) (ADC->STATUS0 = (ADC->STATUS0 & ~(ADC_STATUS0_ADIF_Msk | \
  92. ADC_STATUS0_ADCMPF0_Msk | \
  93. ADC_STATUS0_ADCMPF1_Msk)) | (u32Mask))
  94. /**
  95. * @brief Get the busy state of ADC
  96. * @param[in] adc Base address of ADC module
  97. * @return busy state of ADC
  98. * @retval 0 ADC is not busy
  99. * @retval 1 ADC is busy
  100. * \hideinitializer
  101. */
  102. #define ADC_IS_BUSY(adc) (ADC->STATUS0 & ADC_STATUS0_BUSY_Msk ? 1 : 0)
  103. /**
  104. * @brief Check if the ADC conversion data is over written or not
  105. * @param[in] adc Base address of ADC module
  106. * @param[in] u32ChNum Currently not used
  107. * @return Over run state of ADC data
  108. * @retval 0 ADC data is not overrun
  109. * @retval 1 ADC data us overrun
  110. * \hideinitializer
  111. */
  112. #define ADC_IS_DATA_OVERRUN(adc, u32ChNum) (ADC->STATUS1 & ( 1 << (ADC_STATUS1_OV_Pos + u32ChNum)) ? 1 : 0)
  113. /**
  114. * @brief Check if the ADC conversion data is valid or not
  115. * @param[in] adc Base address of ADC module
  116. * @param[in] u32ChNum Currently not used
  117. * @return Valid state of ADC data
  118. * @retval 0 ADC data is not valid
  119. * @retval 1 ADC data us valid
  120. * \hideinitializer
  121. */
  122. #define ADC_IS_DATA_VALID(adc, u32ChNum) (ADC->STATUS1 & ( 1 << (ADC_STATUS1_VALID_Pos + u32ChNum)) ? 1 : 0)
  123. /**
  124. * @brief Power down ADC module
  125. * @param[in] adc Base address of ADC module
  126. * @return None
  127. * \hideinitializer
  128. */
  129. #define ADC_POWER_DOWN(adc) (ADC->CTL &= ~ADC_CTL_ADCEN_Msk)
  130. /**
  131. * @brief Power on ADC module
  132. * @param[in] adc Base address of ADC module
  133. * @return None
  134. * \hideinitializer
  135. */
  136. #define ADC_POWER_ON(adc) (ADC->CTL |= ADC_CTL_ADCEN_Msk)
  137. /**
  138. * @brief Configure the comparator 0 and enable it
  139. * @param[in] adc Base address of ADC module
  140. * @param[in] u32ChNum Specifies the source channel, valid value are from 0 to 7
  141. * @param[in] u32Condition Specifies the compare condition
  142. * - \ref ADC_CMP_LESS_THAN
  143. * - \ref ADC_CMP_GREATER_OR_EQUAL_TO
  144. * @param[in] u32Data Specifies the compare value. Valid value are between 0 ~ 0x3FF
  145. * @param[in] u32MatchCount Specifies the match count setting, valid values are between 1~16
  146. * @return None
  147. * @details For example, ADC_ENABLE_CMP0(ADC, 5, ADC_CMP_GREATER_OR_EQUAL_TO, 0x800, 10);
  148. * Means ADC will assert comparator 0 flag if channel 5 conversion result is
  149. * greater or equal to 0x800 for 10 times continuously.
  150. * \hideinitializer
  151. */
  152. #define ADC_ENABLE_CMP0(adc, \
  153. u32ChNum, \
  154. u32Condition, \
  155. u32Data, \
  156. u32MatchCount) (ADC->CMP[0] = ((u32ChNum) << ADC_CMP0_CMPCH_Pos) | \
  157. (u32Condition) | \
  158. ((u32Data) << ADC_CMP0_CMPDAT_Pos) | \
  159. (((u32MatchCount) - 1) << ADC_CMP0_CMPMCNT_Pos) |\
  160. ADC_CMP0_ADCMPEN_Msk)
  161. /**
  162. * @brief Disable comparator 0
  163. * @param[in] adc Base address of ADC module
  164. * \hideinitializer
  165. */
  166. #define ADC_DISABLE_CMP0(adc) (ADC->CMP[0] = 0)
  167. /**
  168. * @brief Configure the comparator 1 and enable it
  169. * @param[in] adc Base address of ADC module
  170. * @param[in] u32ChNum Specifies the source channel, valid value are from 0 to 7
  171. * @param[in] u32Condition Specifies the compare condition
  172. * - \ref ADC_CMP_LESS_THAN
  173. * - \ref ADC_CMP_GREATER_OR_EQUAL_TO
  174. * @param[in] u32Data Specifies the compare value. Valid value are between 0 ~ 0x3FF
  175. * @param[in] u32MatchCount Specifies the match count setting, valid values are between 1~16
  176. * @return None
  177. * @details For example, ADC_ENABLE_CMP1(ADC, 5, ADC_CMP_GREATER_OR_EQUAL_TO, 0x800, 10);
  178. * Means ADC will assert comparator 1 flag if channel 5 conversion result is
  179. * greater or equal to 0x800 for 10 times continuously.
  180. * \hideinitializer
  181. */
  182. #define ADC_ENABLE_CMP1(adc, \
  183. u32ChNum, \
  184. u32Condition, \
  185. u32Data, \
  186. u32MatchCount) (ADC->CMP[1] = ((u32ChNum) << ADC_CMP1_CMPCH_Pos) | \
  187. (u32Condition) | \
  188. ((u32Data) << ADC_CMP1_CMPDAT_Pos) | \
  189. ((u32MatchCount - 1) << ADC_CMP1_CMPMCNT_Pos) |\
  190. ADC_CMP1_ADCMPEN_Msk)
  191. /**
  192. * @brief Disable comparator 1
  193. * @param[in] adc Base address of ADC module
  194. * \hideinitializer
  195. */
  196. #define ADC_DISABLE_CMP1(adc) (ADC->CMP[1] = 0)
  197. /**
  198. * @brief Set ADC input channel. Enabled channel will be converted while ADC starts.
  199. * @param[in] adc Base address of ADC module
  200. * @param[in] u32Mask Channel enable bit. Each bit corresponds to a input channel. Bit 0 is channel 0, bit 1 is channel 1...
  201. * @return None
  202. * \hideinitializer
  203. */
  204. #define ADC_SET_INPUT_CHANNEL(adc, u32Mask) (ADC->CHEN = (ADC->CHEN & ~ADC_CHEN_CHEN_Msk) | (u32Mask))
  205. /**
  206. * @brief Start the A/D conversion.
  207. * @param[in] adc Base address of ADC module
  208. * @return None
  209. * \hideinitializer
  210. */
  211. #define ADC_START_CONV(adc) (ADC->CTL |= ADC_CTL_SWTRG_Msk)
  212. /**
  213. * @brief Stop the A/D conversion.
  214. * @param[in] adc Base address of ADC module
  215. * @return None
  216. * \hideinitializer
  217. */
  218. #define ADC_STOP_CONV(adc) (ADC->CTL &= ~ADC_CTL_SWTRG_Msk)
  219. /**
  220. * @brief Set the output format in differential input mode.
  221. * @param[in] adc Base address of ADC module
  222. * @param[in] u32Format Differential input mode output format. Valid values are:
  223. * - \ref ADC_DMODE_OUT_FORMAT_UNSIGNED
  224. * - \ref ADC_DMODE_OUT_FORMAT_2COMPLEMENT
  225. * @return None
  226. * \hideinitializer
  227. */
  228. #define ADC_SET_DMOF(adc, u32Format) (ADC->CTL = (ADC->CTL & ~ADC_CTL_DMOF_Msk) | u32Format)
  229. /**
  230. * @brief Enable PDMA transfer.
  231. * @param[in] adc Base address of ADC module
  232. * @return None
  233. * \hideinitializer
  234. */
  235. #define ADC_ENABLE_PDMA(adc) (ADC->CTL |= ADC_CTL_PDMAEN_Msk)
  236. /**
  237. * @brief Disable PDMA transfer.
  238. * @param[in] adc Base address of ADC module
  239. * @return None
  240. * \hideinitializer
  241. */
  242. #define ADC_DISABLE_PDMA(adc) (ADC->CTL &= ~ADC_CTL_PDMAEN_Msk)
  243. /**
  244. * @brief Get PDMA current transfer data
  245. * @param[in] adc Base address of ADC module
  246. * @return PDMA current transfer data
  247. * \hideinitializer
  248. */
  249. #define ADC_GET_PDMA_DATA(adc) ( ADC->CURDAT & ADC_CURDAT_CURDAT_Msk)
  250. void ADC_Open(ADC_T *adc,
  251. uint32_t u32InputMode,
  252. uint32_t u32OpMode,
  253. uint32_t u32ChMask);
  254. void ADC_Close(ADC_T *adc);
  255. void ADC_EnableHWTrigger(ADC_T *adc,
  256. uint32_t u32Source,
  257. uint32_t u32Param);
  258. void ADC_DisableHWTrigger(ADC_T *adc);
  259. void ADC_EnableInt(ADC_T *adc, uint32_t u32Mask);
  260. void ADC_DisableInt(ADC_T *adc, uint32_t u32Mask);
  261. /*@}*/ /* end of group NUC472_442_ADC_EXPORTED_FUNCTIONS */
  262. /*@}*/ /* end of group NUC472_442_ADC_Driver */
  263. /*@}*/ /* end of group NUC472_442_Device_Driver */
  264. #ifdef __cplusplus
  265. }
  266. #endif
  267. #endif //__ADC_H__
  268. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/