|
| 1 | +/** |
| 2 | + * @file AdcSampler.cpp |
| 3 | + * @brief BSP Layer - Common ADC sampling implementation |
| 4 | + * |
| 5 | + * This file contains platform-independent ADC sampling logic. |
| 6 | + * Platform-specific GPIO-to-channel mapping is in separate files: |
| 7 | + * - AdcSampler_esp32.cpp |
| 8 | + * - AdcSampler_esp32s3.cpp |
| 9 | + */ |
| 10 | + |
| 11 | +#include "AdcSampler.hpp" |
| 12 | + |
| 13 | +#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32) |
| 14 | +#include <esp_log.h> |
| 15 | + |
| 16 | +static const char *TAG = "[AdcSampler]"; |
| 17 | + |
| 18 | +// Static member initialization |
| 19 | +adc_oneshot_unit_handle_t AdcSampler::shared_unit_ = nullptr; |
| 20 | + |
| 21 | +AdcSampler::~AdcSampler() |
| 22 | +{ |
| 23 | + if (cali_handle_) |
| 24 | + { |
| 25 | + #if defined(CONFIG_IDF_TARGET_ESP32S3) |
| 26 | + adc_cali_delete_scheme_curve_fitting(cali_handle_); |
| 27 | + #elif defined(CONFIG_IDF_TARGET_ESP32) |
| 28 | + adc_cali_delete_scheme_line_fitting(cali_handle_); |
| 29 | + #endif |
| 30 | + cali_handle_ = nullptr; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +bool AdcSampler::init(int gpio, adc_atten_t atten, adc_bitwidth_t bitwidth, size_t window_size) |
| 35 | +{ |
| 36 | + // Initialize moving average filter |
| 37 | + if (window_size == 0) |
| 38 | + { |
| 39 | + window_size = 1; |
| 40 | + } |
| 41 | + samples_.assign(window_size, 0); |
| 42 | + sample_sum_ = 0; |
| 43 | + sample_idx_ = 0; |
| 44 | + sample_count_ = 0; |
| 45 | + |
| 46 | + atten_ = atten; |
| 47 | + bitwidth_ = bitwidth; |
| 48 | + |
| 49 | + // Map GPIO to ADC channel (platform-specific) |
| 50 | + if (!map_gpio_to_channel(gpio, unit_, channel_)) |
| 51 | + { |
| 52 | + ESP_LOGW(TAG, "GPIO %d is not a valid ADC1 pin on this chip", gpio); |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + // Initialize shared ADC unit |
| 57 | + if (!ensure_unit()) |
| 58 | + { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + // Configure the ADC channel |
| 63 | + if (!configure_channel(gpio, atten, bitwidth)) |
| 64 | + { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + // Try calibration (requires eFuse data) |
| 69 | + // ESP32-S3 uses curve-fitting, ESP32 uses line-fitting |
| 70 | + esp_err_t cal_err = ESP_FAIL; |
| 71 | + |
| 72 | + #if defined(CONFIG_IDF_TARGET_ESP32S3) |
| 73 | + // ESP32-S3 curve fitting calibration |
| 74 | + adc_cali_curve_fitting_config_t cal_cfg = { |
| 75 | + .unit_id = unit_, |
| 76 | + .chan = channel_, |
| 77 | + .atten = atten_, |
| 78 | + .bitwidth = bitwidth_, |
| 79 | + }; |
| 80 | + cal_err = adc_cali_create_scheme_curve_fitting(&cal_cfg, &cali_handle_); |
| 81 | + #elif defined(CONFIG_IDF_TARGET_ESP32) |
| 82 | + // ESP32 line-fitting calibration is per-unit, not per-channel |
| 83 | + adc_cali_line_fitting_config_t cal_cfg = { |
| 84 | + .unit_id = unit_, |
| 85 | + .atten = atten_, |
| 86 | + .bitwidth = bitwidth_, |
| 87 | + }; |
| 88 | + cal_err = adc_cali_create_scheme_line_fitting(&cal_cfg, &cali_handle_); |
| 89 | + #endif |
| 90 | + |
| 91 | + if (cal_err == ESP_OK) |
| 92 | + { |
| 93 | + cali_inited_ = true; |
| 94 | + ESP_LOGI(TAG, "ADC calibration initialized"); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + cali_inited_ = false; |
| 99 | + ESP_LOGW(TAG, "ADC calibration not available; using raw-to-mV approximation"); |
| 100 | + } |
| 101 | + |
| 102 | + return true; |
| 103 | +} |
| 104 | + |
| 105 | +bool AdcSampler::sampleOnce() |
| 106 | +{ |
| 107 | + if (!shared_unit_) |
| 108 | + { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + int raw = 0; |
| 113 | + esp_err_t err = adc_oneshot_read(shared_unit_, channel_, &raw); |
| 114 | + if (err != ESP_OK) |
| 115 | + { |
| 116 | + ESP_LOGE(TAG, "adc_oneshot_read failed: %s", esp_err_to_name(err)); |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + int mv = 0; |
| 121 | + if (cali_inited_) |
| 122 | + { |
| 123 | + if (adc_cali_raw_to_voltage(cali_handle_, raw, &mv) != ESP_OK) |
| 124 | + { |
| 125 | + mv = 0; |
| 126 | + } |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + // Approximate conversion for 12dB attenuation (~0–3600 mV range) |
| 131 | + // Full-scale raw = (1 << bitwidth_) - 1 |
| 132 | + // For 12-bit: max raw = 4095 → ~3600 mV |
| 133 | + int full_scale_mv = 3600; |
| 134 | + int max_raw = (1 << bitwidth_) - 1; |
| 135 | + if (max_raw > 0) |
| 136 | + { |
| 137 | + mv = (raw * full_scale_mv) / max_raw; |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + mv = 0; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Update moving average filter |
| 146 | + sample_sum_ -= samples_[sample_idx_]; |
| 147 | + samples_[sample_idx_] = mv; |
| 148 | + sample_sum_ += mv; |
| 149 | + sample_idx_ = (sample_idx_ + 1) % samples_.size(); |
| 150 | + if (sample_count_ < samples_.size()) |
| 151 | + { |
| 152 | + sample_count_++; |
| 153 | + } |
| 154 | + filtered_mv_ = sample_sum_ / static_cast<int>(sample_count_ > 0 ? sample_count_ : 1); |
| 155 | + |
| 156 | + return true; |
| 157 | +} |
| 158 | + |
| 159 | +bool AdcSampler::ensure_unit() |
| 160 | +{ |
| 161 | + if (shared_unit_) |
| 162 | + { |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + adc_oneshot_unit_init_cfg_t unit_cfg = { |
| 167 | + .unit_id = ADC_UNIT_1, |
| 168 | + .clk_src = ADC_RTC_CLK_SRC_DEFAULT, |
| 169 | + .ulp_mode = ADC_ULP_MODE_DISABLE, |
| 170 | + }; |
| 171 | + esp_err_t err = adc_oneshot_new_unit(&unit_cfg, &shared_unit_); |
| 172 | + if (err != ESP_OK) |
| 173 | + { |
| 174 | + ESP_LOGE(TAG, "adc_oneshot_new_unit failed: %s", esp_err_to_name(err)); |
| 175 | + shared_unit_ = nullptr; |
| 176 | + return false; |
| 177 | + } |
| 178 | + return true; |
| 179 | +} |
| 180 | + |
| 181 | +bool AdcSampler::configure_channel(int gpio, adc_atten_t atten, adc_bitwidth_t bitwidth) |
| 182 | +{ |
| 183 | + adc_oneshot_chan_cfg_t chan_cfg = { |
| 184 | + .atten = atten, |
| 185 | + .bitwidth = bitwidth, |
| 186 | + }; |
| 187 | + esp_err_t err = adc_oneshot_config_channel(shared_unit_, channel_, &chan_cfg); |
| 188 | + if (err != ESP_OK) |
| 189 | + { |
| 190 | + ESP_LOGE(TAG, "adc_oneshot_config_channel failed (GPIO %d, CH %d): %s", |
| 191 | + gpio, static_cast<int>(channel_), esp_err_to_name(err)); |
| 192 | + return false; |
| 193 | + } |
| 194 | + return true; |
| 195 | +} |
| 196 | + |
| 197 | +#endif // CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32 |
0 commit comments