HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
EspTypes_ADC.h File Reference

ESP32 ADC type definitions for hardware abstraction. More...

#include "BaseAdc.h"
#include "EspTypes_Base.h"
#include "HardwareTypes.h"
#include "McuSelect.h"
#include <esp_adc/adc_continuous.h>
#include <esp_adc/adc_oneshot.h>
Include dependency graph for EspTypes_ADC.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  hf_adc_channel_config_t
 ADC channel configuration structure. More...
 
struct  hf_adc_continuous_config_t
 ADC continuous mode configuration structure. More...
 
struct  hf_adc_filter_config_t
 ADC filter configuration structure. More...
 
struct  hf_adc_monitor_config_t
 ADC monitor configuration structure. More...
 
struct  hf_adc_calibration_config_t
 ADC calibration configuration structure. More...
 
struct  hf_adc_unit_config_t
 ADC unit configuration structure. More...
 
struct  hf_adc_continuous_data_t
 ADC continuous data structure. More...
 
struct  hf_adc_monitor_event_t
 ADC monitor event structure. More...
 

Typedefs

using hf_adc_continuous_callback_t
 ADC continuous mode data callback function.
 
using hf_adc_monitor_callback_t = void (*)(const hf_adc_monitor_event_t* event, void* user_data)
 ADC threshold monitor callback function.
 

Enumerations

enum class  hf_adc_mode_t : uint8_t { ONESHOT = 0 , CONTINUOUS = 1 }
 ADC operating modes supported by ESP32. More...
 
enum class  hf_adc_atten_t : uint8_t { ATTEN_DB_0 = ADC_ATTEN_DB_0 , ATTEN_DB_2_5 = ADC_ATTEN_DB_2_5 , ATTEN_DB_6 = ADC_ATTEN_DB_6 , ATTEN_DB_12 = ADC_ATTEN_DB_12 }
 ADC attenuation levels for ESP32 These control the input voltage range that can be measured Values must match ESP-IDF adc_atten_t enum. More...
 
enum class  hf_adc_bitwidth_t : uint8_t {
  WIDTH_9BIT = ADC_BITWIDTH_9 , WIDTH_10BIT = ADC_BITWIDTH_10 , WIDTH_11BIT = ADC_BITWIDTH_11 , WIDTH_12BIT = ADC_BITWIDTH_12 ,
  WIDTH_13BIT = ADC_BITWIDTH_13 , WIDTH_DEFAULT = ADC_BITWIDTH_DEFAULT
}
 ADC resolution/bit width settings for ESP32 Values must match ESP-IDF adc_bitwidth_t enum. More...
 
enum class  hf_adc_filter_coeff_t : uint8_t {
  COEFF_2 = 0 , COEFF_4 = 1 , COEFF_8 = 2 , COEFF_16 = 3 ,
  COEFF_64 = 4
}
 ADC filter coefficient enumeration. More...
 
enum class  hf_adc_monitor_event_type_t : uint8_t { HIGH_THRESH = 0 , LOW_THRESH = 1 }
 ADC monitor event type enumeration. More...
 

Functions

constexpr uint32_t CalcFrameSize (uint32_t samples_per_frame, uint32_t enabled_channels) noexcept
 Calculate frame size in bytes based on samples per frame and enabled channels.
 
constexpr uint32_t CalcBufferPoolSize (uint32_t samples_per_frame, uint32_t enabled_channels, uint32_t max_store_frames) noexcept
 Calculate total buffer pool size based on frames and enabled channels.
 
constexpr bool IsValidContinuousConfig (uint32_t samples_per_frame, uint32_t enabled_channels, uint32_t max_store_frames) noexcept
 Validate continuous mode configuration parameters.
 
constexpr bool IsValidFrameSize (uint32_t frame_size) noexcept
 Validate that frame size is properly aligned.
 
constexpr uint32_t GetFrameResultCount (uint32_t frame_size) noexcept
 Calculate number of conversion results that fit in a frame.
 
constexpr hf_channel_id_t GpioToAdcChannel (hf_pin_num_t gpio_pin) noexcept
 Convert GPIO pin to ADC channel for ESP32.
 
constexpr hf_pin_num_t AdcChannelToGpio (hf_channel_id_t channel_id) noexcept
 Convert ADC channel to GPIO pin for ESP32.
 
constexpr uint32_t GetMaxInputVoltage (hf_adc_atten_t atten) noexcept
 Get maximum input voltage for given attenuation.
 
constexpr uint32_t GetMaxRawValue (hf_adc_bitwidth_t bitwidth) noexcept
 Get maximum raw value for given bit width.
 

Variables

constexpr uint32_t HF_ESP32_ADC_DATA_BYTES_PER_CONV
 Example of ISR-safe ADC callback implementation.
 
constexpr uint32_t HF_ESP32_ADC_MIN_FRAME_SIZE = 64
 Minimum frame size.
 
constexpr uint32_t HF_ESP32_ADC_MAX_FRAME_SIZE = 1024
 Maximum frame size.
 
constexpr uint32_t HF_ESP32_ADC_DEFAULT_FRAME_SIZE = 256
 Default frame size.
 

Detailed Description

ESP32 ADC type definitions for hardware abstraction.

This header defines only the essential ADC-specific types and constants used by the EspAdc implementation. It follows a clean, minimal pattern providing only necessary types without redundant or duplicate definitions.

Author
Nebiyu Tadesse
Date
2025

Typedef Documentation

◆ hf_adc_continuous_callback_t

Initial value:
bool (*)(const hf_adc_continuous_data_t* data,
void* user_data)
ADC continuous data structure.
Definition EspTypes_ADC.h:176

ADC continuous mode data callback function.

Warning
This callback is executed in ISR context and must be ISR-safe:
  • Use only ISR-safe functions (no malloc, free, printf, etc.)
  • Keep execution time as short as possible
  • Avoid calling blocking functions or FreeRTOS APIs that are not ISR-safe
  • Use only stack variables or pre-allocated memory
  • Consider using xQueueSendFromISR() or similar to defer processing
Parameters
dataContinuous data structure containing sampled data
user_dataUser-provided data pointer
Returns
true to yield to higher priority task, false to continue

◆ hf_adc_monitor_callback_t

using hf_adc_monitor_callback_t = void (*)(const hf_adc_monitor_event_t* event, void* user_data)

ADC threshold monitor callback function.

Warning
This callback is executed in ISR context and must be ISR-safe:
  • Use only ISR-safe functions (no malloc, free, printf, etc.)
  • Keep execution time as short as possible
  • Avoid calling blocking functions or FreeRTOS APIs that are not ISR-safe
  • Use only stack variables or pre-allocated memory
  • Consider using xQueueSendFromISR() or similar to defer processing
Parameters
eventMonitor event structure containing threshold event details
user_dataUser-provided data pointer

Enumeration Type Documentation

◆ hf_adc_atten_t

enum class hf_adc_atten_t : uint8_t
strong

ADC attenuation levels for ESP32 These control the input voltage range that can be measured Values must match ESP-IDF adc_atten_t enum.

Enumerator
ATTEN_DB_0 

No attenuation (0dB) - Input range: 0V to ~0.95V.

ATTEN_DB_2_5 

2.5dB attenuation - Input range: 0V to ~1.32V

ATTEN_DB_6 

6dB attenuation - Input range: 0V to ~1.98V

ATTEN_DB_12 

12dB attenuation - Input range: 0V to ~3.3V

◆ hf_adc_bitwidth_t

enum class hf_adc_bitwidth_t : uint8_t
strong

ADC resolution/bit width settings for ESP32 Values must match ESP-IDF adc_bitwidth_t enum.

Enumerator
WIDTH_9BIT 

9-bit resolution (0-511)

WIDTH_10BIT 

10-bit resolution (0-1023)

WIDTH_11BIT 

11-bit resolution (0-2047)

WIDTH_12BIT 

12-bit resolution (0-4095) - Default for ESP32

WIDTH_13BIT 

13-bit resolution (0-8191)

WIDTH_DEFAULT 

Default width (12-bit for ESP32)

◆ hf_adc_filter_coeff_t

enum class hf_adc_filter_coeff_t : uint8_t
strong

ADC filter coefficient enumeration.

Enumerator
COEFF_2 

Coefficient 2.

COEFF_4 

Coefficient 4.

COEFF_8 

Coefficient 8.

COEFF_16 

Coefficient 16.

COEFF_64 

Coefficient 64.

◆ hf_adc_mode_t

enum class hf_adc_mode_t : uint8_t
strong

ADC operating modes supported by ESP32.

Enumerator
ONESHOT 

One-shot mode for single conversions.

CONTINUOUS 

Continuous mode with DMA for high-speed sampling.

◆ hf_adc_monitor_event_type_t

enum class hf_adc_monitor_event_type_t : uint8_t
strong

ADC monitor event type enumeration.

Enumerator
HIGH_THRESH 

High threshold exceeded.

LOW_THRESH 

Below low threshold.

Function Documentation

◆ AdcChannelToGpio()

constexpr hf_pin_num_t AdcChannelToGpio ( hf_channel_id_t channel_id)
inlineconstexprnoexcept

Convert ADC channel to GPIO pin for ESP32.

Note
This is a simplified mapping function. For accurate conversions, use the ESP-IDF adc_continuous_channel_to_io() function at runtime.
Parameters
channel_idADC channel ID
Returns
GPIO pin number or HF_INVALID_PIN if invalid

◆ CalcBufferPoolSize()

constexpr uint32_t CalcBufferPoolSize ( uint32_t samples_per_frame,
uint32_t enabled_channels,
uint32_t max_store_frames )
inlineconstexprnoexcept

Calculate total buffer pool size based on frames and enabled channels.

Parameters
samples_per_frameNumber of samples per frame per enabled channel
enabled_channelsNumber of enabled channels
max_store_framesMaximum frames to store
Returns
Buffer pool size in bytes

◆ CalcFrameSize()

constexpr uint32_t CalcFrameSize ( uint32_t samples_per_frame,
uint32_t enabled_channels )
inlineconstexprnoexcept

Calculate frame size in bytes based on samples per frame and enabled channels.

Parameters
samples_per_frameNumber of samples per frame per enabled channel
enabled_channelsNumber of enabled channels
Returns
Frame size in bytes

◆ GetFrameResultCount()

constexpr uint32_t GetFrameResultCount ( uint32_t frame_size)
inlineconstexprnoexcept

Calculate number of conversion results that fit in a frame.

Parameters
frame_sizeFrame size in bytes
Returns
Number of conversion results

◆ GetMaxInputVoltage()

constexpr uint32_t GetMaxInputVoltage ( hf_adc_atten_t atten)
inlineconstexprnoexcept

Get maximum input voltage for given attenuation.

Parameters
attenAttenuation level
Returns
Maximum input voltage in millivolts

◆ GetMaxRawValue()

constexpr uint32_t GetMaxRawValue ( hf_adc_bitwidth_t bitwidth)
inlineconstexprnoexcept

Get maximum raw value for given bit width.

Parameters
bitwidthADC bit width
Returns
Maximum raw value

◆ GpioToAdcChannel()

constexpr hf_channel_id_t GpioToAdcChannel ( hf_pin_num_t gpio_pin)
inlineconstexprnoexcept

Convert GPIO pin to ADC channel for ESP32.

Note
This is a simplified mapping function. For accurate conversions, use the ESP-IDF adc_continuous_io_to_channel() function at runtime.
Parameters
gpio_pinGPIO pin number
Returns
ADC channel ID or HF_INVALID_CHANNEL if invalid

◆ IsValidContinuousConfig()

constexpr bool IsValidContinuousConfig ( uint32_t samples_per_frame,
uint32_t enabled_channels,
uint32_t max_store_frames )
inlineconstexprnoexcept

Validate continuous mode configuration parameters.

Parameters
samples_per_frameNumber of samples per frame per enabled channel
enabled_channelsNumber of enabled channels
max_store_framesMaximum frames to store
Returns
true if valid, false otherwise

◆ IsValidFrameSize()

constexpr bool IsValidFrameSize ( uint32_t frame_size)
inlineconstexprnoexcept

Validate that frame size is properly aligned.

Parameters
frame_sizeFrame size in bytes
Returns
true if valid, false otherwise

Variable Documentation

◆ HF_ESP32_ADC_DATA_BYTES_PER_CONV

constexpr uint32_t HF_ESP32_ADC_DATA_BYTES_PER_CONV
constexpr
Initial value:
=
SOC_ADC_DIGI_DATA_BYTES_PER_CONV

Example of ISR-safe ADC callback implementation.

// Queue handle (global or class member)
static QueueHandle_t adc_data_queue;
// ISR-safe callback function
bool adc_continuous_callback(const hf_adc_continuous_data_t* data, void* user_data) {
// Create a lightweight message for the queue
AdcDataMessage msg;
msg.timestamp = data->timestamp_us;
msg.conversion_count = data->conversion_count;
msg.size = data->size;
// Try to send to queue (non-blocking)
BaseType_t higher_priority_task_woken = pdFALSE;
if (xQueueSendFromISR(adc_data_queue, &msg, &higher_priority_task_woken) == pdTRUE) {
return higher_priority_task_woken == pdTRUE;
}
return false; // Continue receiving callbacks
}
static QueueHandle_t adc_data_queue
Definition AdcComprehensiveTest.cpp:78
uint64_t timestamp_us
Timestamp in microseconds.
Definition EspTypes_ADC.h:180
uint32_t size
Data size in bytes.
Definition EspTypes_ADC.h:178
uint32_t conversion_count
Number of conversions.
Definition EspTypes_ADC.h:179

ESP32 ADC continuous mode constants Bytes per conversion result from ESP-IDF

◆ HF_ESP32_ADC_DEFAULT_FRAME_SIZE

constexpr uint32_t HF_ESP32_ADC_DEFAULT_FRAME_SIZE = 256
constexpr

Default frame size.

◆ HF_ESP32_ADC_MAX_FRAME_SIZE

constexpr uint32_t HF_ESP32_ADC_MAX_FRAME_SIZE = 1024
constexpr

Maximum frame size.

◆ HF_ESP32_ADC_MIN_FRAME_SIZE

constexpr uint32_t HF_ESP32_ADC_MIN_FRAME_SIZE = 64
constexpr

Minimum frame size.