Skip to content

Commit 0105375

Browse files
committed
refactor(Monitoring): abstract platform-specific ADC code and add ESP32-S2 support
- Move calibration functions to platform-specific files - Add ADC_SAMPLER_SUPPORTED macro for unified platform detection - Add ESP32-S2 support (UNTESTED) - Remove explicit #if defined() macros from common code
1 parent c6584af commit 0105375

File tree

6 files changed

+159
-54
lines changed

6 files changed

+159
-54
lines changed

components/Monitoring/CMakeLists.txt

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,30 @@
99
# +-----------------------+
1010
# | ESP-IDF ADC HAL | ← Espressif official driver
1111
# +-----------------------+
12+
#
13+
# Supported platforms (must support ESP-CAM):
14+
# - ESP32: Tested
15+
# - ESP32-S3: Tested
16+
# - ESP32-S2: UNTESTED - Based on datasheet
1217

1318
set(
1419
requires
1520
Helpers
1621
)
1722

23+
# List of supported ADC platforms (aligned with ESP_CAMERA_SUPPORTED)
24+
set(ADC_SUPPORTED_TARGETS "esp32" "esp32s3" "esp32s2")
25+
26+
# Check if current target supports ADC
27+
list(FIND ADC_SUPPORTED_TARGETS "$ENV{IDF_TARGET}" ADC_TARGET_INDEX)
28+
if (NOT ADC_TARGET_INDEX EQUAL -1)
29+
set(ADC_SAMPLER_SUPPORTED TRUE)
30+
else()
31+
set(ADC_SAMPLER_SUPPORTED FALSE)
32+
endif()
33+
1834
# Platform-specific dependencies
19-
if ("$ENV{IDF_TARGET}" STREQUAL "esp32s3" OR "$ENV{IDF_TARGET}" STREQUAL "esp32")
35+
if (ADC_SAMPLER_SUPPORTED)
2036
list(APPEND requires
2137
driver
2238
esp_adc
@@ -32,22 +48,20 @@ set(
3248
)
3349

3450
# BSP Layer: ADC sampler implementation
35-
if ("$ENV{IDF_TARGET}" STREQUAL "esp32s3" OR "$ENV{IDF_TARGET}" STREQUAL "esp32")
36-
# Common ADC implementation
37-
list(APPEND source_files
38-
"Monitoring/AdcSampler.cpp"
39-
)
40-
41-
# Platform-specific GPIO-to-channel mapping
42-
if ("$ENV{IDF_TARGET}" STREQUAL "esp32s3")
51+
if (ADC_SAMPLER_SUPPORTED)
52+
# Common ADC implementation
4353
list(APPEND source_files
44-
"Monitoring/AdcSampler_esp32s3.cpp"
54+
"Monitoring/AdcSampler.cpp"
4555
)
46-
elseif ("$ENV{IDF_TARGET}" STREQUAL "esp32")
47-
list(APPEND source_files
48-
"Monitoring/AdcSampler_esp32.cpp"
49-
)
50-
endif()
56+
57+
# Platform-specific GPIO-to-channel mapping and calibration
58+
if ("$ENV{IDF_TARGET}" STREQUAL "esp32s3")
59+
list(APPEND source_files "Monitoring/AdcSampler_esp32s3.cpp")
60+
elseif ("$ENV{IDF_TARGET}" STREQUAL "esp32s2")
61+
list(APPEND source_files "Monitoring/AdcSampler_esp32s2.cpp")
62+
elseif ("$ENV{IDF_TARGET}" STREQUAL "esp32")
63+
list(APPEND source_files "Monitoring/AdcSampler_esp32.cpp")
64+
endif()
5165
endif()
5266

5367

components/Monitoring/Monitoring/AdcSampler.cpp

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* @brief BSP Layer - Common ADC sampling implementation
44
*
55
* 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
6+
* Platform-specific implementations are in separate files:
7+
* - AdcSampler_esp32.cpp (Tested)
8+
* - AdcSampler_esp32s3.cpp (Tested)
9+
* - AdcSampler_esp32s2.cpp (UNTESTED)
910
*/
1011

1112
#include "AdcSampler.hpp"
1213

13-
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32)
14+
#if ADC_SAMPLER_SUPPORTED
1415
#include <esp_log.h>
1516

1617
static const char* TAG = "[AdcSampler]";
@@ -22,11 +23,7 @@ AdcSampler::~AdcSampler()
2223
{
2324
if (cali_handle_)
2425
{
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
26+
delete_calibration(cali_handle_);
3027
cali_handle_ = nullptr;
3128
}
3229
}
@@ -66,29 +63,8 @@ bool AdcSampler::init(int gpio, adc_atten_t atten, adc_bitwidth_t bitwidth, size
6663
}
6764

6865
// 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)
66+
// Platform-specific: ESP32-S3/S2 use curve-fitting, ESP32 uses line-fitting
67+
if (create_calibration(&cali_handle_))
9268
{
9369
cali_inited_ = true;
9470
ESP_LOGI(TAG, "ADC calibration initialized");
@@ -193,4 +169,4 @@ bool AdcSampler::configure_channel(int gpio, adc_atten_t atten, adc_bitwidth_t b
193169
return true;
194170
}
195171

196-
#endif // CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32
172+
#endif // ADC_SAMPLER_SUPPORTED

components/Monitoring/Monitoring/AdcSampler.hpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@
2020
#include <cstdint>
2121
#include "sdkconfig.h"
2222

23-
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32)
23+
// Supported ESP32 platforms with ADC1 oneshot driver
24+
// - ESP32: Tested
25+
// - ESP32-S3: Tested
26+
// - ESP32-S2: UNTESTED - GPIO mapping based on datasheet
27+
#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32S2)
28+
#define ADC_SAMPLER_SUPPORTED 1
29+
#else
30+
#define ADC_SAMPLER_SUPPORTED 0
31+
#endif
32+
33+
#if ADC_SAMPLER_SUPPORTED
2434
#include <vector>
2535
#include "esp_adc/adc_cali.h"
2636
#include "esp_adc/adc_cali_scheme.h"
@@ -86,10 +96,22 @@ class AdcSampler
8696

8797
/**
8898
* @brief Platform-specific GPIO to ADC channel mapping
89-
* @note Implemented separately in AdcSampler_esp32.cpp and AdcSampler_esp32s3.cpp
99+
* @note Implemented in AdcSampler_esp32.cpp, AdcSampler_esp32s3 and AdcSampler_esp32s2.cpp
90100
*/
91101
static bool map_gpio_to_channel(int gpio, adc_unit_t& unit, adc_channel_t& channel);
92102

103+
/**
104+
* @brief Platform-specific ADC calibration initialization
105+
* @note Implemented in AdcSampler_esp32.cpp, AdcSampler_esp32s3 and AdcSampler_esp32s2.cpp
106+
*/
107+
bool create_calibration(adc_cali_handle_t* handle);
108+
109+
/**
110+
* @brief Platform-specific ADC calibration cleanup
111+
* @note Implemented in AdcSampler_esp32.cpp, AdcSampler_esp32s3 and AdcSampler_esp32s2.cpp
112+
*/
113+
void delete_calibration(adc_cali_handle_t handle);
114+
93115
// Shared ADC1 oneshot handle (single instance for all AdcSampler objects)
94116
static adc_oneshot_unit_handle_t shared_unit_;
95117

@@ -109,7 +131,7 @@ class AdcSampler
109131
int filtered_mv_{0};
110132
};
111133

112-
#else
134+
#else // !ADC_SAMPLER_SUPPORTED
113135
// Stub for unsupported targets to keep interfaces consistent
114136
class AdcSampler
115137
{
@@ -131,4 +153,4 @@ class AdcSampler
131153
return false;
132154
}
133155
};
134-
#endif
156+
#endif // ADC_SAMPLER_SUPPORTED

components/Monitoring/Monitoring/AdcSampler_esp32.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file AdcSampler_esp32.cpp
3-
* @brief BSP Layer - ESP32 specific GPIO to ADC channel mapping
3+
* @brief BSP Layer - ESP32 specific ADC implementation
44
*
55
* ESP32 ADC1 GPIO mapping:
66
* - GPIO32 → ADC1_CH4
@@ -56,4 +56,20 @@ bool AdcSampler::map_gpio_to_channel(int gpio, adc_unit_t& unit, adc_channel_t&
5656
}
5757
}
5858

59+
bool AdcSampler::create_calibration(adc_cali_handle_t* handle)
60+
{
61+
// ESP32 uses line fitting calibration (per-unit, not per-channel)
62+
adc_cali_line_fitting_config_t cal_cfg = {
63+
.unit_id = unit_,
64+
.atten = atten_,
65+
.bitwidth = bitwidth_,
66+
};
67+
return adc_cali_create_scheme_line_fitting(&cal_cfg, handle) == ESP_OK;
68+
}
69+
70+
void AdcSampler::delete_calibration(adc_cali_handle_t handle)
71+
{
72+
adc_cali_delete_scheme_line_fitting(handle);
73+
}
74+
5975
#endif // CONFIG_IDF_TARGET_ESP32
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @file AdcSampler_esp32s2.cpp
3+
* @brief BSP Layer - ESP32-S2 specific ADC implementation
4+
*
5+
* UNTESTED - This implementation is based on ESP32-S2 datasheet.
6+
* Please verify on actual hardware before production use.
7+
*
8+
* ESP32-S2 ADC1 GPIO mapping:
9+
* - GPIO1 → ADC1_CH0
10+
* - GPIO2 → ADC1_CH1
11+
* - GPIO3 → ADC1_CH2
12+
* - GPIO4 → ADC1_CH3
13+
* - GPIO5 → ADC1_CH4
14+
* - GPIO6 → ADC1_CH5
15+
* - GPIO7 → ADC1_CH6
16+
* - GPIO8 → ADC1_CH7
17+
* - GPIO9 → ADC1_CH8
18+
* - GPIO10 → ADC1_CH9
19+
*
20+
* Note: ADC2 is not used to avoid conflicts with Wi-Fi.
21+
* Same as ESP32-S3 implementation.
22+
*/
23+
24+
#include "AdcSampler.hpp"
25+
26+
#if defined(CONFIG_IDF_TARGET_ESP32S2)
27+
28+
bool AdcSampler::map_gpio_to_channel(int gpio, adc_unit_t& unit, adc_channel_t& channel)
29+
{
30+
unit = ADC_UNIT_1; // Only use ADC1 to avoid Wi-Fi conflict
31+
32+
// ESP32-S2: ADC1 on GPIO1–10 → CH0–CH9
33+
if (gpio >= 1 && gpio <= 10)
34+
{
35+
channel = static_cast<adc_channel_t>(gpio - 1);
36+
return true;
37+
}
38+
39+
channel = ADC_CHANNEL_0;
40+
return false;
41+
}
42+
43+
bool AdcSampler::create_calibration(adc_cali_handle_t* handle)
44+
{
45+
// ESP32-S2 uses curve fitting calibration
46+
adc_cali_curve_fitting_config_t cal_cfg = {
47+
.unit_id = unit_,
48+
.chan = channel_,
49+
.atten = atten_,
50+
.bitwidth = bitwidth_,
51+
};
52+
return adc_cali_create_scheme_curve_fitting(&cal_cfg, handle) == ESP_OK;
53+
}
54+
55+
void AdcSampler::delete_calibration(adc_cali_handle_t handle)
56+
{
57+
adc_cali_delete_scheme_curve_fitting(handle);
58+
}
59+
60+
#endif // CONFIG_IDF_TARGET_ESP32S2

components/Monitoring/Monitoring/AdcSampler_esp32s3.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file AdcSampler_esp32s3.cpp
3-
* @brief BSP Layer - ESP32-S3 specific GPIO to ADC channel mapping
3+
* @brief BSP Layer - ESP32-S3 specific ADC implementation
44
*
55
* ESP32-S3 ADC1 GPIO mapping:
66
* - GPIO1 → ADC1_CH0
@@ -36,4 +36,21 @@ bool AdcSampler::map_gpio_to_channel(int gpio, adc_unit_t& unit, adc_channel_t&
3636
return false;
3737
}
3838

39+
bool AdcSampler::create_calibration(adc_cali_handle_t* handle)
40+
{
41+
// ESP32-S3 uses curve fitting calibration
42+
adc_cali_curve_fitting_config_t cal_cfg = {
43+
.unit_id = unit_,
44+
.chan = channel_,
45+
.atten = atten_,
46+
.bitwidth = bitwidth_,
47+
};
48+
return adc_cali_create_scheme_curve_fitting(&cal_cfg, handle) == ESP_OK;
49+
}
50+
51+
void AdcSampler::delete_calibration(adc_cali_handle_t handle)
52+
{
53+
adc_cali_delete_scheme_curve_fitting(handle);
54+
}
55+
3956
#endif // CONFIG_IDF_TARGET_ESP32S3

0 commit comments

Comments
 (0)