adc_arch.h File Reference

Architecture specific ADC definitions and functions for ESP32. More...

Detailed Description

Architecture specific ADC definitions and functions for ESP32.

All ESP32x SoCs have two SAR ADC units each. However, these have functionalities as well as specific properties that vary between the ESP32x SoC and therefore require different handling for each ESP32x SoC. This is already taken into account in the high-level API of the ESP-IDF. To avoid having to reimplement these specifics and the different handling, the high-level API of the ESP-IDF is used directly for the ADC peripherals.

Author
Gunar Schorcht gunar.nosp@m.@sch.nosp@m.orcht.nosp@m..net

Definition in file adc_arch.h.

#include "periph/adc.h"
#include "periph/gpio.h"
#include "driver/adc.h"
#include "driver/adc_types_legacy.h"
+ Include dependency graph for adc_arch.h:

Go to the source code of this file.

Macros

#define ADC_ATTENUATION_11_DB   ADC_ATTENUATION_12_DB
 Attenuation of 11 dB is depcricated and has to be mapped. More...
 

Enumerations

enum  adc_attenuation_t { ADC_ATTENUATION_0_DB = ADC_ATTEN_DB_0 , ADC_ATTENUATION_3_DB = ADC_ATTEN_DB_2_5 , ADC_ATTENUATION_6_DB = ADC_ATTEN_DB_6 , ADC_ATTENUATION_12_DB = ADC_ATTEN_DB_12 }
 Attenuations that can be set for ADC lines. More...
 

Functions

int adc_set_attenuation (adc_t line, adc_atten_t atten)
 Set the attenuation for the ADC line. More...
 
int adc_raw_to_voltage (adc_t line, int sample, int *voltage)
 Get the voltage for a given sample value. More...
 

Macro Definition Documentation

◆ ADC_ATTENUATION_11_DB

#define ADC_ATTENUATION_11_DB   ADC_ATTENUATION_12_DB

Attenuation of 11 dB is depcricated and has to be mapped.

The define ensures the compatibility with older versions.

Deprecated:
ADC_ATTENUATION_11_DB is deprecated, use ADC_ATTENUATION_12_DB instead.

Definition at line 61 of file adc_arch.h.

Enumeration Type Documentation

◆ adc_attenuation_t

Attenuations that can be set for ADC lines.

Event though ESP-IDF type adc_atten_t and ADC_ATTEN_DB_* are used now, the adc_attenuation_t type with constants ADC_ATTENUATION_*_DB are kept for compatibility.

Enumerator
ADC_ATTENUATION_0_DB 

full-range is about 1.1 V (Vref)

ADC_ATTENUATION_3_DB 

full-range is about 1.5 V

ADC_ATTENUATION_6_DB 

full-range is about 2.2 V

ADC_ATTENUATION_12_DB 

full-range is about 3.3 V

Definition at line 46 of file adc_arch.h.

Function Documentation

◆ adc_raw_to_voltage()

int adc_raw_to_voltage ( adc_t  line,
int  sample,
int *  voltage 
)

Get the voltage for a given sample value.

The function converts the given sample value as read from the channel according to the attenuation set with adc_set_attenuation and the resolution used to read the sample with adc_sample. It uses a predefined calibration scheme and the calibration parameters that have been burned into the eFuses of the ESP32x SoC. If the calibration parameters have not been burned into the eFuses and the initialization of the calibration fails, a linear conversion according to the predefined voltage ranges is used as a fallback.

Note
In the case that the initialization of the calibration fails, the function returns -EINVAL and the value in parameter voltage is expected to be inaccurate.
For ESP32, the valid voltages start at around 140 mV.
Parameters
[in]lineADC line
[in]samplesample sample as read by adc_read
[out]voltagevoltage in mV
Return values
0on success
-EINVALif the initialization of the calibration failed

◆ adc_set_attenuation()

int adc_set_attenuation ( adc_t  line,
adc_atten_t  atten 
)

Set the attenuation for the ADC line.

Default attenuation is 12 dB.

For each ADC line, an attenuation of the input signal can be defined separately. This results in different full ranges of the measurable voltage at the input. The attenuation can be set to 0 dB, 3 dB, 6 dB and 12 dB, with 12 dB being the standard attenuation. Since an ADC input is measured against a reference voltage Vref of 1.1 V, approximately the following measurement ranges are given when using a corresponding attenuation:

Attenuation Voltage Range Symbol
0 dB 0 ... 1.1V (Vref) ADC_ATTEN_DB_0
2.5 dB 0 ... 1.5V ADC_ATTEN_DB_2_5
6 dB 0 ... 2.2V ADC_ATTEN_DB_6
12 dB (default) 0 ... 3.3V ADC_ATTEN_DB_12
Precondition
adc_init must have been executed for the line before.
Note
The function has to be executed before adc_sample if required. The configured attenuation is then used for all subsequent samples.
Parameters
[in]lineADC line for which the attenuation is set
[in]attenAttenuation, see type definition of adc_attenuation_t
Return values
0on success
-1on error