adc.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**************************************************************************//**
  2. * @file adc.h
  3. * @version V3.0
  4. * $Revision: 10 $
  5. * $Date: 14/02/10 2:27p $
  6. * @brief M051 series ADC driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2014 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 M051_Device_Driver M051 Device Driver
  18. @{
  19. */
  20. /** @addtogroup M051_ADC_Driver ADC Driver
  21. @{
  22. */
  23. /** @addtogroup M051_ADC_EXPORTED_CONSTANTS ADC Exported Constants
  24. @{
  25. */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. /* ADCR Constant Definitions */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. #define ADC_ADCR_ADEN_CONVERTER_DISABLE (0UL<<ADC_ADCR_ADEN_Pos) /*!< ADC converter disable */
  30. #define ADC_ADCR_ADEN_CONVERTER_ENABLE (1UL<<ADC_ADCR_ADEN_Pos) /*!< ADC converter enable */
  31. #define ADC_ADCR_ADMD_SINGLE (0UL<<ADC_ADCR_ADMD_Pos) /*!< Single mode */
  32. #define ADC_ADCR_ADMD_BURST (1UL<<ADC_ADCR_ADMD_Pos) /*!< Burst mode */
  33. #define ADC_ADCR_ADMD_SINGLE_CYCLE (2UL<<ADC_ADCR_ADMD_Pos) /*!< Single cycle scan mode */
  34. #define ADC_ADCR_ADMD_CONTINUOUS (3UL<<ADC_ADCR_ADMD_Pos) /*!< Continuous scan mode */
  35. #define ADC_ADCR_DIFFEN_SINGLE_END (0UL<<ADC_ADCR_DIFFEN_Pos) /*!< Single end input mode */
  36. #define ADC_ADCR_DIFFEN_DIFFERENTIAL (1UL<<ADC_ADCR_DIFFEN_Pos) /*!< Differential input type */
  37. #define ADC_ADCR_DMOF_UNSIGNED_OUTPUT (0UL<<ADC_ADCR_DMOF_Pos) /*!< Select the straight binary format as the output format of the conversion result */
  38. #define ADC_ADCR_DMOF_TWOS_COMPLEMENT (1UL<<ADC_ADCR_DMOF_Pos) /*!< Select the 2's complement format as the output format of the conversion result */
  39. #define ADC_ADCR_TRGEN_DISABLE (0UL<<ADC_ADCR_TRGEN_Pos) /*!< Disable triggering of A/D conversion by external STADC pin or PWM */
  40. #define ADC_ADCR_TRGEN_ENABLE (1UL<<ADC_ADCR_TRGEN_Pos) /*!< Enable triggering of A/D conversion by external STADC pin or PWM */
  41. #define ADC_ADCR_TRGS_STADC (0UL<<ADC_ADCR_TRGS_Pos) /*!< A/D conversion is started by external STADC pin */
  42. #define ADC_ADCR_TRGS_PWM (3UL<<ADC_ADCR_TRGS_Pos) /*!< A/D conversion is started by PWM */
  43. #define ADC_ADCR_TRGCOND_LOW_LEVEL (0UL<<ADC_ADCR_TRGCOND_Pos) /*!< STADC Low level active */
  44. #define ADC_ADCR_TRGCOND_HIGH_LEVEL (1UL<<ADC_ADCR_TRGCOND_Pos) /*!< STADC High level active */
  45. #define ADC_ADCR_TRGCOND_FALLING_EDGE (2UL<<ADC_ADCR_TRGCOND_Pos) /*!< STADC Falling edge active */
  46. #define ADC_ADCR_TRGCOND_RISING_EDGE (3UL<<ADC_ADCR_TRGCOND_Pos) /*!< STADC Rising edge active */
  47. /*---------------------------------------------------------------------------------------------------------*/
  48. /* ADCHER Constant Definitions */
  49. /*---------------------------------------------------------------------------------------------------------*/
  50. #define ADC_ADCHER_PRESEL_EXT_INPUT_SIGNAL (0UL<<ADC_ADCHER_PRESEL_Pos) /*!< The input source of channel 7 is the external analog input */
  51. #define ADC_ADCHER_PRESEL_INT_BANDGAP (1UL<<ADC_ADCHER_PRESEL_Pos) /*!< The input source of channel 7 is the internal bandgap voltage */
  52. #define ADC_ADCHER_PRESEL_INT_TEMPERATURE_SENSOR (2UL<<ADC_ADCHER_PRESEL_Pos) /*!< The input source of channel 7 is the output of internal temperature sensor */
  53. /*---------------------------------------------------------------------------------------------------------*/
  54. /* ADCMPR Constant Definitions */
  55. /*---------------------------------------------------------------------------------------------------------*/
  56. #define ADC_ADCMPR_CMPD(x) ((x) << ADC_ADCMPR_CMPD_Pos) /*!< Compare value for compare function */
  57. #define ADC_ADCMPR_CMPMATCNT(x) (((x)-1) << ADC_ADCMPR_CMPMATCNT_Pos) /*!< Match count for compare function */
  58. #define ADC_ADCMPR_CMPCH(x) ((x) << ADC_ADCMPR_CMPCH_Pos) /*!< Compare channel for compare function */
  59. #define ADC_ADCMPR_CMPCOND_LESS_THAN (0<<ADC_ADCMPR_CMPCOND_Pos) /*!< The compare condition is "less than" */
  60. #define ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL (1<<ADC_ADCMPR_CMPCOND_Pos) /*!< The compare condition is "greater than or equal to" */
  61. #define ADC_ADCMPR_CMPIE_INTERRUPT_ENABLE (ADC_ADCMPR_CMPIE_Msk) /*!< The compare function interrupt enable */
  62. /*---------------------------------------------------------------------------------------------------------*/
  63. /* ADC Interrupt Constant Definitions */
  64. /*---------------------------------------------------------------------------------------------------------*/
  65. #define ADC_ADF_INT (ADC_ADSR_ADF_Msk) /*!< ADC convert complete interrupt */
  66. #define ADC_CMP0_INT (ADC_ADSR_CMPF0_Msk) /*!< ADC comparator 0 interrupt */
  67. #define ADC_CMP1_INT (ADC_ADSR_CMPF1_Msk) /*!< ADC comparator 1 interrupt */
  68. /*---------------------------------------------------------------------------------------------------------*/
  69. /* ADC Operation Mode Constant Definitions */
  70. /*---------------------------------------------------------------------------------------------------------*/
  71. #define ADC_SINGLE_MODE 0 /*!< ADC single mode */
  72. #define ADC_BURST_MODE 1 /*!< ADC burst mode */
  73. #define ADC_SINGLE_CYCLE_MODE 2 /*!< ADC single-cycle scan mode */
  74. #define ADC_CONTINUOUS_MODE 3 /*!< ADC continuous scan mode */
  75. /*---------------------------------------------------------------------------------------------------------*/
  76. /* ADC Trigger Condition Constant Definitions */
  77. /*---------------------------------------------------------------------------------------------------------*/
  78. #define ADC_LOW_LEVEL 0 /*!< ADC external trigger condition is low level trigger */
  79. #define ADC_HIGH_LEVEL 1 /*!< ADC external trigger condition is high level trigger */
  80. #define ADC_FALLING_EDGE 2 /*!< ADC external trigger condition is falling edge trigger */
  81. #define ADC_RISING_EDGE 3 /*!< ADC external trigger condition is rising edge trigger */
  82. /*---------------------------------------------------------------------------------------------------------*/
  83. /* ADC Compare Condition Constant Definitions */
  84. /*---------------------------------------------------------------------------------------------------------*/
  85. #define ADC_LESS_THAN 0 /*!< ADC compare condition is "less than the compare value" */
  86. #define ADC_GREATER_OR_EQUAL 1 /*!< ADC compare condition is "greater than or equal to the compare value" */
  87. /*---------------------------------------------------------------------------------------------------------*/
  88. /* Constant Definitions of ADC Channel 7 Input Source */
  89. /*---------------------------------------------------------------------------------------------------------*/
  90. #define ADC_CH7_EXT_INPUT_SIGNAL 0 /*!< External input signal */
  91. #define ADC_CH7_INT_BANDGAP 1 /*!< Internal band-gap voltage */
  92. #define ADC_CH7_INT_TEMPERATURE_SENSOR 2 /*!< Internal temperature sensor */
  93. /*@}*/ /* end of group M051_ADC_EXPORTED_CONSTANTS */
  94. /** @addtogroup M051_ADC_EXPORTED_FUNCTIONS ADC Exported Functions
  95. @{
  96. */
  97. /**
  98. * @brief Configure the analog input source of channel 7.
  99. * @param[in] adc Base address of ADC module.
  100. * @param[in] u32Source Decides the analog input source of channel 7. Valid values are:
  101. * - \ref ADC_ADCHER_PRESEL_EXT_INPUT_SIGNAL : External analog input.
  102. * - \ref ADC_ADCHER_PRESEL_INT_BANDGAP : Internal bandgap voltage.
  103. * - \ref ADC_ADCHER_PRESEL_INT_TEMPERATURE_SENSOR : Output of internal temperature sensor.
  104. * @return None
  105. * @note While using VBG as channel 7 source, ADC module clock must /b not exceed 300kHz.
  106. */
  107. #define ADC_CONFIG_CH7(adc, u32Source) (ADC->ADCHER = (ADC->ADCHER & ~ADC_ADCHER_PRESEL_Msk) | (u32Source))
  108. /**
  109. * @brief Get conversion data of specified channel.
  110. * @param[in] adc Base address of ADC module.
  111. * @param[in] u32ChNum ADC Channel, valid value are from 0 to 7.
  112. * @return Read RSLT bit field to get conversion data.
  113. */
  114. #define ADC_GET_CONVERSION_DATA(adc, u32ChNum) ((ADC->ADDR[(u32ChNum)] & ADC_ADDR_RSLT_Msk)>>ADC_ADDR_RSLT_Pos)
  115. /**
  116. * @brief Return the user-specified interrupt flags.
  117. * @param[in] adc Base address of ADC module.
  118. * @param[in] u32Mask The combination of following interrupt status bits. Each bit corresponds to a interrupt status.
  119. * Valid values are:
  120. * - \ref ADC_ADF_INT :Convert complete interrupt flag.
  121. * - \ref ADC_CMP0_INT :Comparator 0 interrupt flag.
  122. * - \ref ADC_CMP1_INT :Comparator 1 interrupt flag.
  123. * @return User specified interrupt flags.
  124. */
  125. #define ADC_GET_INT_FLAG(adc, u32Mask) (ADC->ADSR & (u32Mask))
  126. /**
  127. * @brief This macro clear the selected interrupt status bits.
  128. * @param[in] adc Base address of ADC module.
  129. * @param[in] u32Mask The combination of following interrupt status bits. Each bit corresponds to a interrupt status.
  130. * Valid values are:
  131. * - \ref ADC_ADF_INT :Convert complete interrupt flag
  132. * - \ref ADC_CMP0_INT :Comparator 0 interrupt flag
  133. * - \ref ADC_CMP1_INT :Comparator 1 interrupt flag
  134. * @return None
  135. */
  136. #define ADC_CLR_INT_FLAG(adc, u32Mask) (ADC->ADSR = u32Mask)
  137. /**
  138. * @brief Get the busy state of ADC.
  139. * @param[in] adc Base address of ADC module.
  140. * @return busy state of ADC.
  141. * @retval 0 ADC is not busy.
  142. * @retval 1 ADC is busy.
  143. */
  144. #define ADC_IS_BUSY(adc) (ADC->ADSR & ADC_ADSR_BUSY_Msk ? 1 : 0)
  145. /**
  146. * @brief Check if the ADC conversion data is over written or not.
  147. * @param[in] adc Base address of ADC module.
  148. * @param[in] u32ChNum ADC Channel, valid value are from 0 to 7.
  149. * @return Over run state of ADC data.
  150. * @retval 0 ADC data is not overrun.
  151. * @retval 1 ADC data us overrun.
  152. */
  153. #define ADC_IS_DATA_OVERRUN(adc, u32ChNum) (ADC->ADSR & (u32ChNum<<ADC_ADSR_OVERRUN_Pos) ? 1 : 0)
  154. /**
  155. * @brief Check if the ADC conversion data is valid or not.
  156. * @param[in] adc Base address of ADC module.
  157. * @param[in] u32ChNum ADC Channel, valid value are from 0 to 7.
  158. * @return Valid state of ADC data.
  159. * @retval 0 ADC data is not valid.
  160. * @retval 1 ADC data us valid.
  161. */
  162. #define ADC_IS_DATA_VALID(adc, u32ChNum) (ADC->ADSR & (0x1<<(ADC_ADSR_VALID_Pos+u32ChNum)) ? 1 : 0)
  163. /**
  164. * @brief Power down ADC module.
  165. * @param[in] adc Base address of ADC module.
  166. * @return None
  167. */
  168. #define ADC_POWER_DOWN(adc) (ADC->ADCR &= ~ADC_ADCR_ADEN_Msk)
  169. /**
  170. * @brief Power on ADC module.
  171. * @param[in] adc Base address of ADC module.
  172. * @return None
  173. */
  174. #define ADC_POWER_ON(adc) (ADC->ADCR |= ADC_ADCR_ADEN_Msk)
  175. /**
  176. * @brief Configure the comparator 0 and enable it.
  177. * @param[in] adc Base address of ADC module.
  178. * @param[in] u32ChNum Specifies the source channel, valid value are from 0 to 7.
  179. * @param[in] u32Condition Specifies the compare condition. Valid values are:
  180. * - \ref ADC_ADCMPR_CMPCOND_LESS_THAN :The compare condition is "less than the compare value"
  181. * - \ref ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL :The compare condition is "greater than or equal to the compare value
  182. * @param[in] u32Data Specifies the compare value, valid value are between 0 ~ 0xFFF.
  183. * @param[in] u32MatchCount Specifies the match count setting, valid values are between 1~16.
  184. * @return None
  185. * @details For example, ADC_ENABLE_CMP0(ADC, 5, ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL, 0x800, 10);
  186. * Means ADC will assert comparator 0 flag if channel 5 conversion result is greater or
  187. * equal to 0x800 for 10 times continuously.
  188. * @note When DIFFEN bit is set to 1, ADC comparator compares CMPD with conversion result with unsigned
  189. * format (M05xxBN only). u32Data should be filled in unsigned format (straight binary format).
  190. */
  191. #define ADC_ENABLE_CMP0(adc, \
  192. u32ChNum, \
  193. u32Condition, \
  194. u32Data, \
  195. u32MatchCount) (ADC->ADCMPR[0] = ((u32ChNum) << ADC_ADCMPR_CMPCH_Pos) | \
  196. u32Condition | \
  197. ((u32Data) << ADC_ADCMPR_CMPD_Pos) | \
  198. (((u32MatchCount) - 1) << ADC_ADCMPR_CMPMATCNT_Pos) |\
  199. ADC_ADCMPR_CMPEN_Msk)
  200. /**
  201. * @brief Disable comparator 0
  202. * @param[in] adc Base address of ADC module
  203. */
  204. #define ADC_DISABLE_CMP0(adc) (ADC->ADCMPR[0] = 0)
  205. /**
  206. * @brief Configure the comparator 1 and enable it.
  207. * @param[in] adc Base address of ADC module.
  208. * @param[in] u32ChNum Specifies the source channel, valid value are from 0 to 7.
  209. * @param[in] u32Condition Specifies the compare condition. Valid values are:
  210. * - \ref ADC_ADCMPR_CMPCOND_LESS_THAN :The compare condition is "less than the compare value"
  211. * - \ref ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL :The compare condition is "greater than or equal to the compare value
  212. * @param[in] u32Data Specifies the compare value, valid value are between 0 ~ 0xFFF.
  213. * @param[in] u32MatchCount Specifies the match count setting, valid values are between 1~16.
  214. * @return None
  215. * @details For example, ADC_ENABLE_CMP1(ADC, 5, ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL, 0x800, 10);
  216. * Means ADC will assert comparator 1 flag if channel 5 conversion result is greater or
  217. * equal to 0x800 for 10 times continuously.
  218. */
  219. #define ADC_ENABLE_CMP1(adc, \
  220. u32ChNum, \
  221. u32Condition, \
  222. u32Data, \
  223. u32MatchCount) (ADC->ADCMPR[1] = ((u32ChNum) << ADC_ADCMPR_CMPCH_Pos) | \
  224. u32Condition | \
  225. ((u32Data) << ADC_ADCMPR_CMPD_Pos) | \
  226. (((u32MatchCount) - 1) << ADC_ADCMPR_CMPMATCNT_Pos) |\
  227. ADC_ADCMPR_CMPEN_Msk)
  228. /**
  229. * @brief Disable comparator 1.
  230. * @param[in] adc Base address of ADC module.
  231. */
  232. #define ADC_DISABLE_CMP1(adc) (ADC->ADCMPR[1] = 0)
  233. /**
  234. * @brief Set ADC input channel. Enabled channel will be converted while ADC starts.
  235. * @param[in] adc Base address of ADC module.
  236. * @param[in] u32Mask Channel enable bit. Each bit corresponds to a input channel. Bit 0 is channel 0, bit 1 is channel 1..., bit 7 is channel 7.
  237. * @return None
  238. * @note M051 series MCU ADC can only convert 1 channel at a time. If more than 1 channels are enabled, only channel
  239. * with smallest number will be convert.
  240. */
  241. #define ADC_SET_INPUT_CHANNEL(adc, u32Mask) (ADC->ADCHER = (ADC->ADCHER & ~ADC_ADCHER_CHEN_Msk) | (u32Mask))
  242. /**
  243. * @brief Set the output format mode.
  244. * @param[in] adc Base address of ADC module.
  245. * @param[in] u32Format Decides the output format. Valid values are:
  246. * - \ref ADC_ADCR_DMOF_UNSIGNED_OUTPUT :Select the straight binary format as the output format of the conversion result.
  247. * - \ref ADC_ADCR_DMOF_TWOS_COMPLEMENT :Select the 2's complement format as the output format of the conversion result.
  248. * @return None
  249. * @note Burst mode and ADC compare function can not support 2's complement output format,
  250. * this u32Format must be 0 (M05xxBN only).
  251. */
  252. #define ADC_SET_DMOF(adc, u32Format) (ADC->ADCR = (ADC->ADCR & ~ADC_ADCR_DMOF_Msk) | (u32Format))
  253. /**
  254. * @brief Start the A/D conversion.
  255. * @param[in] adc Base address of ADC module.
  256. * @return None
  257. */
  258. #define ADC_START_CONV(adc) (ADC->ADCR |= ADC_ADCR_ADST_Msk)
  259. /**
  260. * @brief Stop the A/D conversion.
  261. * @param[in] adc Base address of ADC module.
  262. * @return None
  263. * @note When hardware clears the ADST bit, the ADST bit must be kept at 0 at least one ADC peripheral clock
  264. * period before setting it to 1 again, otherwise the A/D converter may not work (M05xxBN only). If ADST
  265. * bit is cleared to 0 when ADC is in converting, the BUSY bit will be cleared to 0 immediately, ADC will
  266. * finish the current conversion and save the result to the ADDRx register of the enabled channel (M05xxBN only).
  267. * But, ADC can not finish the current conversion and A/D converter enters idle state directly (M05xxDN only).
  268. */
  269. #define ADC_STOP_CONV(adc) (ADC->ADCR &= ~ADC_ADCR_ADST_Msk)
  270. void ADC_Open(ADC_T *adc,
  271. uint32_t u32InputMode,
  272. uint32_t u32OpMode,
  273. uint32_t u32ChMask);
  274. void ADC_Close(ADC_T *adc);
  275. void ADC_EnableHWTrigger(ADC_T *adc,
  276. uint32_t u32Source,
  277. uint32_t u32Param);
  278. void ADC_DisableHWTrigger(ADC_T *adc);
  279. void ADC_EnableInt(ADC_T *adc, uint32_t u32Mask);
  280. void ADC_DisableInt(ADC_T *adc, uint32_t u32Mask);
  281. /*@}*/ /* end of group M051_ADC_EXPORTED_FUNCTIONS */
  282. /*@}*/ /* end of group M051_ADC_Driver */
  283. /*@}*/ /* end of group M051_Device_Driver */
  284. #ifdef __cplusplus
  285. }
  286. #endif
  287. #endif //__ADC_H__
  288. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/