adc_hal.h 6.5 KB

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