adc_hal.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. #include "hal/adc_types.h"
  3. #include "hal/adc_ll.h"
  4. /*---------------------------------------------------------------
  5. Common setting
  6. ---------------------------------------------------------------*/
  7. /**
  8. * ADC module initialization.
  9. */
  10. void adc_hal_init(void);
  11. /**
  12. * ADC module deinitialization.
  13. */
  14. void adc_hal_deinit(void);
  15. /**
  16. * Set adc sample cycle.
  17. *
  18. * @note Normally, please use default value.
  19. * @param sample_cycle The number of ADC sampling cycles. Range: 1 ~ 7.
  20. */
  21. #define adc_hal_set_sample_cycle(sample_cycle) adc_ll_set_sample_cycle(sample_cycle)
  22. /**
  23. * Set ADC module power management.
  24. *
  25. * @prarm manage Set ADC power status.
  26. */
  27. #define adc_hal_set_power_manage(manage) adc_ll_set_power_manage(manage)
  28. /**
  29. * Get ADC module power management.
  30. *
  31. * @return
  32. * - ADC power status.
  33. */
  34. #define adc_hal_get_power_manage() adc_ll_get_power_manage()
  35. /**
  36. * ADC module clock division factor setting. ADC clock devided from APB clock.
  37. *
  38. * @prarm div Division factor.
  39. */
  40. #define adc_hal_digi_set_clk_div(div) adc_ll_digi_set_clk_div(div)
  41. /**
  42. * ADC SAR clock division factor setting. ADC SAR clock devided from `RTC_FAST_CLK`.
  43. *
  44. * @prarm div Division factor.
  45. */
  46. #define adc_hal_set_sar_clk_div(adc_n, div) adc_ll_set_sar_clk_div(adc_n, div)
  47. /**
  48. * Set ADC module controller.
  49. * There are five SAR ADC controllers:
  50. * Two digital controller: Continuous conversion mode (DMA). High performance with multiple channel scan modes;
  51. * Two RTC controller: Single conversion modes (Polling). For low power purpose working during deep sleep;
  52. * the other is dedicated for Power detect (PWDET / PKDET), Only support ADC2.
  53. *
  54. * @prarm adc_n ADC unit.
  55. * @prarm ctrl ADC controller.
  56. */
  57. #define adc_hal_set_controller(adc_n, ctrl) adc_ll_set_controller(adc_n, ctrl)
  58. /**
  59. * Set the attenuation of a particular channel on ADCn.
  60. *
  61. * @note For any given channel, this function must be called before the first time conversion.
  62. *
  63. * The default ADC full-scale voltage is 1.1V. To read higher voltages (up to the pin maximum voltage,
  64. * usually 3.3V) requires setting >0dB signal attenuation for that ADC channel.
  65. *
  66. * When VDD_A is 3.3V:
  67. *
  68. * - 0dB attenuaton (ADC_ATTEN_DB_0) gives full-scale voltage 1.1V
  69. * - 2.5dB attenuation (ADC_ATTEN_DB_2_5) gives full-scale voltage 1.5V
  70. * - 6dB attenuation (ADC_ATTEN_DB_6) gives full-scale voltage 2.2V
  71. * - 11dB attenuation (ADC_ATTEN_DB_11) gives full-scale voltage 3.9V (see note below)
  72. *
  73. * @note The full-scale voltage is the voltage corresponding to a maximum reading (depending on ADC1 configured
  74. * bit width, this value is: 4095 for 12-bits, 2047 for 11-bits, 1023 for 10-bits, 511 for 9 bits.)
  75. *
  76. * @note At 11dB attenuation the maximum voltage is limited by VDD_A, not the full scale voltage.
  77. *
  78. * Due to ADC characteristics, most accurate results are obtained within the following approximate voltage ranges:
  79. *
  80. * - 0dB attenuaton (ADC_ATTEN_DB_0) between 100 and 950mV
  81. * - 2.5dB attenuation (ADC_ATTEN_DB_2_5) between 100 and 1250mV
  82. * - 6dB attenuation (ADC_ATTEN_DB_6) between 150 to 1750mV
  83. * - 11dB attenuation (ADC_ATTEN_DB_11) between 150 to 2450mV
  84. *
  85. * For maximum accuracy, use the ADC calibration APIs and measure voltages within these recommended ranges.
  86. *
  87. * @prarm adc_n ADC unit.
  88. * @prarm channel ADCn channel number.
  89. * @prarm atten The attenuation option.
  90. */
  91. #define adc_hal_set_atten(adc_n, channel, atten) adc_ll_set_atten(adc_n, channel, atten)
  92. /**
  93. * Get the attenuation of a particular channel on ADCn.
  94. *
  95. * @param adc_n ADC unit.
  96. * @param channel ADCn channel number.
  97. * @return atten The attenuation option.
  98. */
  99. #define adc_hal_get_atten(adc_n, channel) adc_ll_get_atten(adc_n, channel)
  100. /**
  101. * Close ADC AMP module if don't use it for power save.
  102. */
  103. #define adc_hal_amp_disable() adc_ll_amp_disable()
  104. /*---------------------------------------------------------------
  105. PWDET(Power detect) controller setting
  106. ---------------------------------------------------------------*/
  107. /**
  108. * Set adc cct for PWDET controller.
  109. *
  110. * @note Capacitor tuning of the PA power monitor. cct set to the same value with PHY.
  111. * @prarm cct Range: 0 ~ 7.
  112. */
  113. #define adc_hal_pwdet_set_cct(cct) adc_ll_pwdet_set_cct(cct)
  114. /**
  115. * Get adc cct for PWDET controller.
  116. *
  117. * @note Capacitor tuning of the PA power monitor. cct set to the same value with PHY.
  118. * @return cct Range: 0 ~ 7.
  119. */
  120. #define adc_hal_pwdet_get_cct() adc_ll_pwdet_get_cct()
  121. /*---------------------------------------------------------------
  122. RTC controller setting
  123. ---------------------------------------------------------------*/
  124. /**
  125. * Get the converted value for each ADCn for RTC controller.
  126. *
  127. * @note It may be block to wait conversion finish.
  128. *
  129. * @prarm adc_n ADC unit.
  130. * @param channel adc channel number.
  131. * @param value Pointer for touch value.
  132. *
  133. * @return
  134. * - 0: The value is valid.
  135. * - ~0: The value is invalid.
  136. */
  137. int adc_hal_convert(adc_ll_num_t adc_n, int channel, int *value);
  138. /**
  139. * Set adc output data format for RTC controller.
  140. *
  141. * @prarm adc_n ADC unit.
  142. * @prarm bits Output data bits width option.
  143. */
  144. #define adc_hal_rtc_set_output_format(adc_n, bits) adc_ll_rtc_set_output_format(adc_n, bits)
  145. /**
  146. * ADC module output data invert or not.
  147. *
  148. * @prarm adc_n ADC unit.
  149. */
  150. #define adc_hal_rtc_output_invert(adc_n, inv_en) adc_ll_rtc_output_invert(adc_n, inv_en)
  151. /**
  152. * Enable/disable the output of ADCn's internal reference voltage to one of ADC2's channels.
  153. *
  154. * This function routes the internal reference voltage of ADCn to one of
  155. * ADC2's channels. This reference voltage can then be manually measured
  156. * for calibration purposes.
  157. *
  158. * @note ESP32 only supports output of ADC2's internal reference voltage.
  159. * @param[in] adc ADC unit select
  160. * @param[in] channel ADC2 channel number
  161. * @param[in] en Enable/disable the reference voltage output
  162. */
  163. #define adc_hal_vref_output(adc, channel, en) adc_ll_vref_output(adc, channel, en)
  164. /*---------------------------------------------------------------
  165. Digital controller setting
  166. ---------------------------------------------------------------*/
  167. /**
  168. * Digital controller initialization.
  169. */
  170. void adc_hal_digi_init(void);
  171. /**
  172. * Digital controller deinitialization.
  173. */
  174. void adc_hal_digi_deinit(void);
  175. /**
  176. * Setting the digital controller.
  177. *
  178. * @param cfg Pointer to digital controller paramter.
  179. */
  180. void adc_hal_digi_controller_config(const adc_digi_config_t *cfg);
  181. /**
  182. * Reset the pattern table pointer, then take the measurement rule from table header in next measurement.
  183. *
  184. * @param adc_n ADC unit.
  185. */
  186. #define adc_hal_digi_clear_pattern_table(adc_n) adc_ll_digi_clear_pattern_table(adc_n)