9 static const char *
const TAG =
"adc.esp32";
11 static const adc_bits_width_t ADC_WIDTH_MAX_SOC_BITS =
static_cast<adc_bits_width_t
>(ADC_WIDTH_MAX - 1);
13 #ifndef SOC_ADC_RTC_MAX_BITWIDTH 14 #if USE_ESP32_VARIANT_ESP32S2 15 static const int32_t SOC_ADC_RTC_MAX_BITWIDTH = 13;
17 static const int32_t SOC_ADC_RTC_MAX_BITWIDTH = 12;
18 #endif // USE_ESP32_VARIANT_ESP32S2 19 #endif // SOC_ADC_RTC_MAX_BITWIDTH 21 static const int ADC_MAX = (1 << SOC_ADC_RTC_MAX_BITWIDTH) - 1;
22 static const int ADC_HALF = (1 << SOC_ADC_RTC_MAX_BITWIDTH) >> 1;
25 ESP_LOGCONFIG(TAG,
"Setting up ADC '%s'...", this->
get_name().c_str());
27 if (this->
channel1_ != ADC1_CHANNEL_MAX) {
28 adc1_config_width(ADC_WIDTH_MAX_SOC_BITS);
32 }
else if (this->
channel2_ != ADC2_CHANNEL_MAX) {
38 for (int32_t i = 0; i <= ADC_ATTEN_DB_12_COMPAT; i++) {
39 auto adc_unit = this->
channel1_ != ADC1_CHANNEL_MAX ? ADC_UNIT_1 : ADC_UNIT_2;
40 auto cal_value = esp_adc_cal_characterize(adc_unit, (adc_atten_t) i, ADC_WIDTH_MAX_SOC_BITS,
44 case ESP_ADC_CAL_VAL_EFUSE_VREF:
45 ESP_LOGV(TAG,
"Using eFuse Vref for calibration");
47 case ESP_ADC_CAL_VAL_EFUSE_TP:
48 ESP_LOGV(TAG,
"Using two-point eFuse Vref for calibration");
50 case ESP_ADC_CAL_VAL_DEFAULT_VREF:
58 LOG_SENSOR(
"",
"ADC Sensor",
this);
59 LOG_PIN(
" Pin: ", this->
pin_);
61 ESP_LOGCONFIG(TAG,
" Attenuation: auto");
65 ESP_LOGCONFIG(TAG,
" Attenuation: 0db");
67 case ADC_ATTEN_DB_2_5:
68 ESP_LOGCONFIG(TAG,
" Attenuation: 2.5db");
71 ESP_LOGCONFIG(TAG,
" Attenuation: 6db");
73 case ADC_ATTEN_DB_12_COMPAT:
74 ESP_LOGCONFIG(TAG,
" Attenuation: 12db");
81 LOG_UPDATE_INTERVAL(
this);
89 if (this->
channel1_ != ADC1_CHANNEL_MAX) {
91 }
else if (this->
channel2_ != ADC2_CHANNEL_MAX) {
92 adc2_get_raw(this->
channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw);
99 sum = (sum + (this->sample_count_ >> 1)) / this->
sample_count_;
107 int raw12 = ADC_MAX, raw6 = ADC_MAX, raw2 = ADC_MAX, raw0 = ADC_MAX;
109 if (this->
channel1_ != ADC1_CHANNEL_MAX) {
110 adc1_config_channel_atten(this->
channel1_, ADC_ATTEN_DB_12_COMPAT);
112 if (raw12 < ADC_MAX) {
113 adc1_config_channel_atten(this->
channel1_, ADC_ATTEN_DB_6);
115 if (raw6 < ADC_MAX) {
116 adc1_config_channel_atten(this->
channel1_, ADC_ATTEN_DB_2_5);
118 if (raw2 < ADC_MAX) {
119 adc1_config_channel_atten(this->
channel1_, ADC_ATTEN_DB_0);
124 }
else if (this->
channel2_ != ADC2_CHANNEL_MAX) {
125 adc2_config_channel_atten(this->
channel2_, ADC_ATTEN_DB_12_COMPAT);
126 adc2_get_raw(this->
channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw12);
127 if (raw12 < ADC_MAX) {
128 adc2_config_channel_atten(this->
channel2_, ADC_ATTEN_DB_6);
129 adc2_get_raw(this->
channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw6);
130 if (raw6 < ADC_MAX) {
131 adc2_config_channel_atten(this->
channel2_, ADC_ATTEN_DB_2_5);
132 adc2_get_raw(this->
channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw2);
133 if (raw2 < ADC_MAX) {
134 adc2_config_channel_atten(this->
channel2_, ADC_ATTEN_DB_0);
135 adc2_get_raw(this->
channel2_, ADC_WIDTH_MAX_SOC_BITS, &raw0);
141 if (raw0 == -1 || raw2 == -1 || raw6 == -1 || raw12 == -1) {
145 uint32_t mv12 = esp_adc_cal_raw_to_voltage(raw12, &this->
cal_characteristics_[(int32_t) ADC_ATTEN_DB_12_COMPAT]);
146 uint32_t mv6 = esp_adc_cal_raw_to_voltage(raw6, &this->
cal_characteristics_[(int32_t) ADC_ATTEN_DB_6]);
147 uint32_t mv2 = esp_adc_cal_raw_to_voltage(raw2, &this->
cal_characteristics_[(int32_t) ADC_ATTEN_DB_2_5]);
148 uint32_t mv0 = esp_adc_cal_raw_to_voltage(raw0, &this->
cal_characteristics_[(int32_t) ADC_ATTEN_DB_0]);
150 uint32_t c12 = std::min(raw12, ADC_HALF);
151 uint32_t c6 = ADC_HALF - std::abs(raw6 - ADC_HALF);
152 uint32_t c2 = ADC_HALF - std::abs(raw2 - ADC_HALF);
153 uint32_t c0 = std::min(ADC_MAX - raw0, ADC_HALF);
154 uint32_t csum = c12 + c6 + c2 + c0;
156 uint32_t mv_scaled = (mv12 * c12) + (mv6 * c6) + (mv2 * c2) + (mv0 * c0);
157 return mv_scaled / (float) (csum * 1000U);
void setup() override
Setup ADC.
esp_adc_cal_characteristics_t cal_characteristics_[SOC_ADC_ATTEN_NUM]
Implementation of SPI Controller mode.
void dump_config() override
const StringRef & get_name() const