32#ifdef HF_MCU_FAMILY_ESP32
48#include "driver/gpio.h"
49#include "esp_adc/adc_cali.h"
50#include "esp_adc/adc_cali_scheme.h"
51#include "esp_adc/adc_continuous.h"
52#include "esp_adc/adc_filter.h"
53#include "esp_adc/adc_monitor.h"
54#include "esp_adc/adc_oneshot.h"
69#if defined(HF_MCU_ESP32C6)
70#define HF_ESP32_ADC_MAX_UNITS 1
71#define HF_ESP32_ADC_MAX_CHANNELS 7
72#define HF_ESP32_ADC_MAX_FILTERS 2
73#define HF_ESP32_ADC_MAX_MONITORS 2
74#define HF_ESP32_ADC_MAX_RAW_VALUE 4095
75#define HF_ESP32_ADC_REFERENCE_VOLTAGE_MV 1100
76#define HF_ESP32_ADC_MAX_SAMPLING_FREQ 100000
77#define HF_ESP32_ADC_MIN_SAMPLING_FREQ 10
78#define HF_ESP32_ADC_DEFAULT_SAMPLING_FREQ 1000
79#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MIN 256
80#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MAX 4096
81#define HF_ESP32_ADC_DMA_BUFFER_SIZE_DEFAULT 1024
83#define HF_ESP32_ADC_ONESHOT_CLK_SRC ADC_DIGI_CLK_SRC_DEFAULT
84#define HF_ESP32_ADC_CONTINUOUS_CLK_SRC ADC_DIGI_CLK_SRC_DEFAULT
85#define HF_ESP32_ADC_ULP_MODE ADC_ULP_MODE_DISABLE
88#elif defined(HF_MCU_ESP32)
89#define HF_ESP32_ADC_MAX_UNITS 2
90#define HF_ESP32_ADC_MAX_CHANNELS 8
91#define HF_ESP32_ADC_MAX_FILTERS 2
92#define HF_ESP32_ADC_MAX_MONITORS 2
93#define HF_ESP32_ADC_MAX_RAW_VALUE 4095
94#define HF_ESP32_ADC_REFERENCE_VOLTAGE_MV 1100
95#define HF_ESP32_ADC_MAX_SAMPLING_FREQ 200000
96#define HF_ESP32_ADC_MIN_SAMPLING_FREQ 10
97#define HF_ESP32_ADC_DEFAULT_SAMPLING_FREQ 1000
98#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MIN 256
99#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MAX 4096
100#define HF_ESP32_ADC_DMA_BUFFER_SIZE_DEFAULT 1024
102#define HF_ESP32_ADC_ONESHOT_CLK_SRC ADC_RTC_CLK_SRC_DEFAULT
103#define HF_ESP32_ADC_CONTINUOUS_CLK_SRC ADC_DIGI_CLK_SRC_DEFAULT
104#define HF_ESP32_ADC_ULP_MODE ADC_ULP_MODE_DISABLE
107#elif defined(HF_MCU_ESP32S2)
108#define HF_ESP32_ADC_MAX_UNITS 1
109#define HF_ESP32_ADC_MAX_CHANNELS 10
110#define HF_ESP32_ADC_MAX_FILTERS 2
111#define HF_ESP32_ADC_MAX_MONITORS 2
112#define HF_ESP32_ADC_MAX_RAW_VALUE 4095
113#define HF_ESP32_ADC_REFERENCE_VOLTAGE_MV 1100
114#define HF_ESP32_ADC_MAX_SAMPLING_FREQ 200000
115#define HF_ESP32_ADC_MIN_SAMPLING_FREQ 10
116#define HF_ESP32_ADC_DEFAULT_SAMPLING_FREQ 1000
117#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MIN 256
118#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MAX 4096
119#define HF_ESP32_ADC_DMA_BUFFER_SIZE_DEFAULT 1024
121#define HF_ESP32_ADC_ONESHOT_CLK_SRC ADC_RTC_CLK_SRC_DEFAULT
122#define HF_ESP32_ADC_CONTINUOUS_CLK_SRC ADC_DIGI_CLK_SRC_DEFAULT
123#define HF_ESP32_ADC_ULP_MODE ADC_ULP_MODE_DISABLE
126#elif defined(HF_MCU_ESP32S3)
127#define HF_ESP32_ADC_MAX_UNITS 2
128#define HF_ESP32_ADC_MAX_CHANNELS 10
129#define HF_ESP32_ADC_MAX_FILTERS 2
130#define HF_ESP32_ADC_MAX_MONITORS 2
131#define HF_ESP32_ADC_MAX_RAW_VALUE 4095
132#define HF_ESP32_ADC_REFERENCE_VOLTAGE_MV 1100
133#define HF_ESP32_ADC_MAX_SAMPLING_FREQ 200000
134#define HF_ESP32_ADC_MIN_SAMPLING_FREQ 10
135#define HF_ESP32_ADC_DEFAULT_SAMPLING_FREQ 1000
136#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MIN 256
137#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MAX 4096
138#define HF_ESP32_ADC_DMA_BUFFER_SIZE_DEFAULT 1024
140#define HF_ESP32_ADC_ONESHOT_CLK_SRC ADC_RTC_CLK_SRC_DEFAULT
141#define HF_ESP32_ADC_CONTINUOUS_CLK_SRC ADC_DIGI_CLK_SRC_DEFAULT
142#define HF_ESP32_ADC_ULP_MODE ADC_ULP_MODE_DISABLE
145#elif defined(HF_MCU_ESP32C3)
146#define HF_ESP32_ADC_MAX_UNITS 1
147#define HF_ESP32_ADC_MAX_CHANNELS 6
148#define HF_ESP32_ADC_MAX_FILTERS 2
149#define HF_ESP32_ADC_MAX_MONITORS 2
150#define HF_ESP32_ADC_MAX_RAW_VALUE 4095
151#define HF_ESP32_ADC_REFERENCE_VOLTAGE_MV 1100
152#define HF_ESP32_ADC_MAX_SAMPLING_FREQ 100000
153#define HF_ESP32_ADC_MIN_SAMPLING_FREQ 10
154#define HF_ESP32_ADC_DEFAULT_SAMPLING_FREQ 1000
155#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MIN 256
156#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MAX 4096
157#define HF_ESP32_ADC_DMA_BUFFER_SIZE_DEFAULT 1024
159#define HF_ESP32_ADC_ONESHOT_CLK_SRC ADC_RTC_CLK_SRC_DEFAULT
160#define HF_ESP32_ADC_CONTINUOUS_CLK_SRC ADC_DIGI_CLK_SRC_DEFAULT
161#define HF_ESP32_ADC_ULP_MODE ADC_ULP_MODE_DISABLE
164#elif defined(HF_MCU_ESP32C2)
165#define HF_ESP32_ADC_MAX_UNITS 1
166#define HF_ESP32_ADC_MAX_CHANNELS 4
167#define HF_ESP32_ADC_MAX_FILTERS 2
168#define HF_ESP32_ADC_MAX_MONITORS 2
169#define HF_ESP32_ADC_MAX_RAW_VALUE 4095
170#define HF_ESP32_ADC_REFERENCE_VOLTAGE_MV 1100
171#define HF_ESP32_ADC_MAX_SAMPLING_FREQ 100000
172#define HF_ESP32_ADC_MIN_SAMPLING_FREQ 10
173#define HF_ESP32_ADC_DEFAULT_SAMPLING_FREQ 1000
174#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MIN 256
175#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MAX 4096
176#define HF_ESP32_ADC_DMA_BUFFER_SIZE_DEFAULT 1024
178#define HF_ESP32_ADC_ONESHOT_CLK_SRC ADC_RTC_CLK_SRC_DEFAULT
179#define HF_ESP32_ADC_CONTINUOUS_CLK_SRC ADC_DIGI_CLK_SRC_DEFAULT
180#define HF_ESP32_ADC_ULP_MODE ADC_ULP_MODE_DISABLE
183#elif defined(HF_MCU_ESP32H2)
184#define HF_ESP32_ADC_MAX_UNITS 1
185#define HF_ESP32_ADC_MAX_CHANNELS 6
186#define HF_ESP32_ADC_MAX_FILTERS 2
187#define HF_ESP32_ADC_MAX_MONITORS 2
188#define HF_ESP32_ADC_MAX_RAW_VALUE 4095
189#define HF_ESP32_ADC_REFERENCE_VOLTAGE_MV 1100
190#define HF_ESP32_ADC_MAX_SAMPLING_FREQ 100000
191#define HF_ESP32_ADC_MIN_SAMPLING_FREQ 10
192#define HF_ESP32_ADC_DEFAULT_SAMPLING_FREQ 1000
193#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MIN 256
194#define HF_ESP32_ADC_DMA_BUFFER_SIZE_MAX 4096
195#define HF_ESP32_ADC_DMA_BUFFER_SIZE_DEFAULT 1024
197#define HF_ESP32_ADC_ONESHOT_CLK_SRC ADC_RTC_CLK_SRC_DEFAULT
198#define HF_ESP32_ADC_CONTINUOUS_CLK_SRC ADC_DIGI_CLK_SRC_DEFAULT
199#define HF_ESP32_ADC_ULP_MODE ADC_ULP_MODE_DISABLE
203#error "Unsupported ESP32 variant! Please add support for this ESP32 variant in EspAdc.h"
274 ~EspAdc() noexcept override;
278 EspAdc(const EspAdc&) = delete;
279 EspAdc& operator=(const EspAdc&) = delete;
283 EspAdc(EspAdc&& other) = delete;
284 EspAdc& operator=(EspAdc&& other) = delete;
291 static constexpr
hf_u8_t HF_ADC_MAX_UNITS =
292 HF_ESP32_ADC_MAX_UNITS;
293 static constexpr
hf_u8_t HF_ADC_MAX_CHANNELS =
294 HF_ESP32_ADC_MAX_CHANNELS;
295 static constexpr
hf_u8_t HF_ADC_MAX_FILTERS =
296 HF_ESP32_ADC_MAX_FILTERS;
297 static constexpr
hf_u8_t HF_ADC_MAX_MONITORS =
298 HF_ESP32_ADC_MAX_MONITORS;
299 static constexpr
hf_u16_t HF_ADC_MAX_RAW_VALUE_12BIT =
300 HF_ESP32_ADC_MAX_RAW_VALUE;
301 static constexpr
hf_u32_t HF_ADC_REFERENCE_VOLTAGE_MV =
302 HF_ESP32_ADC_REFERENCE_VOLTAGE_MV;
303 static constexpr
hf_u32_t HF_ADC_MAX_SAMPLING_FREQ =
304 HF_ESP32_ADC_MAX_SAMPLING_FREQ;
305 static constexpr
hf_u32_t HF_ADC_MIN_SAMPLING_FREQ =
306 HF_ESP32_ADC_MIN_SAMPLING_FREQ;
308 HF_ESP32_ADC_DEFAULT_SAMPLING_FREQ;
309 static constexpr
size_t HF_ADC_DMA_BUFFER_SIZE_MIN =
310 HF_ESP32_ADC_DMA_BUFFER_SIZE_MIN;
311 static constexpr
size_t HF_ADC_DMA_BUFFER_SIZE_MAX =
312 HF_ESP32_ADC_DMA_BUFFER_SIZE_MAX;
314 HF_ESP32_ADC_DMA_BUFFER_SIZE_DEFAULT;
316 static constexpr adc_oneshot_clk_src_t HF_ADC_ONESHOT_CLK_SRC =
317 HF_ESP32_ADC_ONESHOT_CLK_SRC;
318 static constexpr adc_continuous_clk_src_t HF_ADC_CONTINUOUS_CLK_SRC =
319 HF_ESP32_ADC_CONTINUOUS_CLK_SRC;
320 static constexpr adc_ulp_mode_t HF_ADC_ULP_MODE =
321 HF_ESP32_ADC_ULP_MODE;
331 hf_bool_t Initialize() noexcept override;
337 hf_bool_t Deinitialize() noexcept override;
343 [[nodiscard]]
hf_u8_t GetMaxChannels() const noexcept override;
362 hf_time_t timeBetweenSamples = 0) noexcept override;
374 hf_time_t timeBetweenSamples = 0) noexcept override;
386 float& channel_reading_v,
hf_u8_t numOfSamplesToAvg = 1,
387 hf_time_t timeBetweenSamples = 0) noexcept override;
398 hf_u32_t* readings,
float* voltages) noexcept override;
521 void* user_data =
nullptr) noexcept;
539 [[nodiscard]]
hf_bool_t IsContinuousRunning() const noexcept;
621 void* user_data =
nullptr) noexcept;
658 [[nodiscard]]
hf_adc_err_t GetLastError() const noexcept;
672 struct MonitorContext {
675 uint32_t high_threshold;
676 uint32_t low_threshold;
679 EspAdc* adc_instance;
691 uint64_t GetCurrentTimeUs() const noexcept;
695 static
hf_bool_t IRAM_ATTR ContinuousCallback(adc_continuous_handle_t handle,
696 const adc_continuous_evt_data_t* edata,
697 void* user_data) noexcept;
698 static
hf_bool_t IRAM_ATTR HighThresholdCallback(adc_monitor_handle_t monitor_handle,
699 const adc_monitor_evt_data_t* event_data,
700 void* user_data) noexcept;
701 static
hf_bool_t IRAM_ATTR LowThresholdCallback(adc_monitor_handle_t monitor_handle,
702 const adc_monitor_evt_data_t* event_data,
703 void* user_data) noexcept;
711 std::atomic<
hf_bool_t> continuous_running_;
719 adc_oneshot_unit_handle_t oneshot_handle_;
720 adc_continuous_handle_t continuous_handle_;
721 std::array<adc_cali_handle_t, 4>
722 calibration_handles_;
723 std::array<adc_iir_filter_handle_t, HF_ADC_MAX_FILTERS> filter_handles_;
724 std::array<adc_monitor_handle_t, HF_ADC_MAX_MONITORS> monitor_handles_;
728 void* continuous_user_data_;
731 std::array<
void*, HF_ADC_MAX_MONITORS> monitor_user_data_;
732 std::array<MonitorContext, HF_ADC_MAX_MONITORS> monitor_contexts_;
Abstract base class for ADC implementations in the HardFOC system.
hf_adc_err_t
Definition BaseAdc.h:115
Consolidated MCU-specific type definitions for hardware abstraction (hf_* types).
bool(*)(const hf_adc_continuous_data_t *data, void *user_data) hf_adc_continuous_callback_t
ADC continuous mode data callback function.
Definition EspTypes_ADC.h:218
hf_adc_mode_t
ADC operating modes supported by ESP32.
Definition EspTypes_ADC.h:41
hf_adc_bitwidth_t
ADC resolution/bit width settings for ESP32 Values must match ESP-IDF adc_bitwidth_t enum.
Definition EspTypes_ADC.h:62
@ WIDTH_DEFAULT
Default width (12-bit for ESP32)
hf_adc_atten_t
ADC attenuation levels for ESP32 These control the input voltage range that can be measured Values mu...
Definition EspTypes_ADC.h:51
void(*)(const hf_adc_monitor_event_t *event, void *user_data) hf_adc_monitor_callback_t
ADC threshold monitor callback function.
Definition EspTypes_ADC.h:234
static constexpr uint32_t HF_ADC_DEFAULT_SAMPLING_FREQ
Definition EspTypes_Base.h:40
static constexpr size_t HF_ADC_DMA_BUFFER_SIZE_DEFAULT
Definition EspTypes_Base.h:41
uint32_t hf_u32_t
Platform-agnostic 32-bit unsigned integer type.
Definition HardwareTypes.h:52
hf_u32_t hf_time_t
Platform-agnostic time type in milliseconds.
Definition HardwareTypes.h:173
uint8_t hf_u8_t
Platform-agnostic 8-bit unsigned integer type.
Definition HardwareTypes.h:40
bool hf_bool_t
Platform-agnostic boolean type.
Definition HardwareTypes.h:88
uint16_t hf_u16_t
Platform-agnostic 16-bit unsigned integer type.
Definition HardwareTypes.h:46
hf_u32_t hf_channel_id_t
Platform-agnostic channel identifier type.
Definition HardwareTypes.h:163
Centralized MCU platform selection and configuration header.
Base class for ADCs.
Definition BaseAdc.h:196
ADC continuous mode configuration structure.
Definition EspTypes_ADC.h:110
ADC diagnostic information.
Definition BaseAdc.h:165
ADC filter configuration structure.
Definition EspTypes_ADC.h:124
ADC monitor configuration structure.
Definition EspTypes_ADC.h:136
ADC operation statistics.
Definition BaseAdc.h:145
ADC unit configuration structure.
Definition EspTypes_ADC.h:161