Skip to content

Commit 998747c

Browse files
committed
Simplify analogRead function
1 parent 411015b commit 998747c

File tree

1 file changed

+6
-88
lines changed

1 file changed

+6
-88
lines changed

Sming/Arch/Esp32/Core/adc.cpp

Lines changed: 6 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,17 @@
11
#include <driver/adc.h>
2-
#include <soc/adc_periph.h>
3-
#if ESP_IDF_VERSION_MAJOR < 5
4-
#include <esp_adc_cal.h>
5-
#endif
62
#include <debug_progmem.h>
7-
#include <Digital.h>
8-
9-
#define DEFAULT_VREF 1100 // Use adc2_vref_to_gpio() to obtain a better estimate
10-
11-
namespace
12-
{
13-
struct AdcInfo {
14-
adc_unit_t adc;
15-
adc_channel_t channel;
16-
};
17-
18-
bool lookupAdc(uint16_t pin, AdcInfo& info)
19-
{
20-
for(unsigned adc = 0; adc < SOC_ADC_PERIPH_NUM; ++adc) {
21-
for(unsigned ch = 0; ch < SOC_ADC_MAX_CHANNEL_NUM; ++ch) {
22-
if(adc_channel_io_map[adc][ch] == pin) {
23-
info.adc = adc_unit_t(adc);
24-
info.channel = adc_channel_t(ch);
25-
return true;
26-
}
27-
}
28-
}
29-
30-
return false;
31-
}
32-
33-
} // namespace
343

354
uint16_t analogRead(uint16_t pin)
365
{
37-
AdcInfo info;
38-
if(!lookupAdc(pin, info)) {
6+
adc_oneshot_unit_init_cfg_t init_config{};
7+
adc_channel_t channel;
8+
esp_err_t err = adc_oneshot_io_to_channel(pin, &init_config.unit_id, &channel);
9+
if(err != ESP_OK) {
3910
debug_e("Pin %u is not ADC pin!", pin);
4011
return 0;
4112
}
4213

43-
pinMode(pin, ANALOG);
44-
45-
#if ESP_IDF_VERSION_MAJOR < 5
46-
47-
constexpr adc_atten_t attenuation{ADC_ATTEN_DB_0};
48-
esp_adc_cal_characteristics_t adcChars{};
49-
50-
auto adcWidth = adc_bits_width_t(ADC_WIDTH_MAX - 1);
51-
52-
// Configure
53-
if(info.adc == ADC_UNIT_2) {
54-
adc2_config_channel_atten(adc2_channel_t(info.channel), attenuation);
55-
} else {
56-
adc1_config_width(adcWidth);
57-
adc1_config_channel_atten(adc1_channel_t(info.channel), attenuation);
58-
}
59-
60-
// Characterize
61-
esp_adc_cal_characterize(info.adc, attenuation, adcWidth, DEFAULT_VREF, &adcChars);
62-
63-
// Sample
64-
if(info.adc == ADC_UNIT_2) {
65-
int value{0};
66-
esp_err_t r = adc2_get_raw(adc2_channel_t(info.channel), adcWidth, &value);
67-
if(r == ESP_OK) {
68-
return value;
69-
} else if(r == ESP_ERR_INVALID_STATE) {
70-
debug_e("GPIO%u: %s: ADC2 not initialized yet.", pin, esp_err_to_name(r));
71-
} else if(r == ESP_ERR_TIMEOUT) {
72-
debug_e("GPIO%u: %s: ADC2 is in use by Wi-Fi.", pin, esp_err_to_name(r));
73-
} else {
74-
debug_e("GPIO%u: %s", pin, esp_err_to_name(r));
75-
}
76-
77-
return value;
78-
}
79-
80-
return adc1_get_raw(adc1_channel_t(info.channel));
81-
82-
#else
83-
8414
// Initialise unit
85-
adc_oneshot_unit_init_cfg_t init_config{
86-
.unit_id = info.adc,
87-
};
8815
adc_oneshot_unit_handle_t adc_handle;
8916
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle));
9017

@@ -93,21 +20,12 @@ uint16_t analogRead(uint16_t pin)
9320
.atten = ADC_ATTEN_DB_0,
9421
.bitwidth = ADC_BITWIDTH_DEFAULT,
9522
};
96-
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, info.channel, &channel_config));
97-
98-
// Calibration
99-
// adc_cali_handle_t adc1_cali_handle = NULL;
100-
// bool do_calibration1 = example_adc_calibration_init(ADC_UNIT_1, ADC_ATTEN_DB_11, &adc1_cali_handle);
23+
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, channel, &channel_config));
10124

10225
int rawSampleValue{0};
103-
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, info.channel, &rawSampleValue));
104-
105-
// ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[0][0], &voltage[0][0]));
106-
// ESP_LOGI(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", ADC_UNIT_1 + 1, EXAMPLE_ADC1_CHAN0, voltage[0][0]);
26+
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, channel, &rawSampleValue));
10727

10828
ESP_ERROR_CHECK(adc_oneshot_del_unit(adc_handle));
10929

11030
return rawSampleValue;
111-
112-
#endif
11331
}

0 commit comments

Comments
 (0)