Skip to content

Commit 04ad0f7

Browse files
committed
Merge branch 'adc_calibration_update' into 'master'
ADC Calibraiton - Support new new calibration methods and eFuse functionality See merge request idf/esp-idf!1846
2 parents c8ba6cf + 0c9e2c0 commit 04ad0f7

File tree

11 files changed

+751
-338
lines changed

11 files changed

+751
-338
lines changed

components/esp_adc_cal/Kconfig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
menu "ADC-Calibration"
2+
3+
config ADC_CAL_EFUSE_TP_ENABLE
4+
bool "Use Two Point Values"
5+
default "y"
6+
help
7+
Some ESP32s have Two Point calibration values burned into eFuse BLOCK3.
8+
This option will allow the ADC calibration component to characterize the
9+
ADC-Voltage curve using Two Point values if they are available.
10+
11+
config ADC_CAL_EFUSE_VREF_ENABLE
12+
bool "Use eFuse Vref"
13+
default "y"
14+
help
15+
Some ESP32s have Vref burned into eFuse BLOCK0. This option will allow
16+
the ADC calibration component to characterize the ADC-Voltage curve using
17+
eFuse Vref if it is available.
18+
19+
config ADC_CAL_LUT_ENABLE
20+
bool "Use Lookup Tables"
21+
default "y"
22+
help
23+
This option will allow the ADC calibration component to use Lookup Tables
24+
to correct for non-linear behavior in 11db attenuation. Other attenuations
25+
do not exhibit non-linear behavior hence will not be affected by this option.
26+
27+
endmenu # ADC-Calibration

components/esp_adc_cal/esp_adc_cal.c

Lines changed: 387 additions & 76 deletions
Large diffs are not rendered by default.

components/esp_adc_cal/esp_adc_cal_lookup_tables.c

Lines changed: 0 additions & 96 deletions
This file was deleted.

components/esp_adc_cal/include/esp_adc_cal.h

Lines changed: 90 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,131 +20,130 @@ extern "C" {
2020
#endif
2121

2222
#include <stdint.h>
23+
#include "esp_err.h"
2324
#include "driver/adc.h"
2425

25-
/** @cond */
26-
#define ADC_CAL_GAIN_SCALE 16
27-
#define ADC_CAL_OFFSET_SCALE 10
28-
29-
#define ADC_CAL_IDEAL_V_REF 1100 //In mV
30-
#define ADC_CAL_LOW_V_REF 1000
31-
#define ADC_CAL_HIGH_V_REF 1200
32-
#define ADC_CAL_MIN 0
33-
#define ADC_CAL_MAX 4095
34-
/** @endcond */
26+
/**
27+
* @brief Type of calibration value used in characterization
28+
*/
29+
typedef enum {
30+
ESP_ADC_CAL_VAL_EFUSE_VREF = 0, /**< Characterization based on reference voltage stored in eFuse*/
31+
ESP_ADC_CAL_VAL_EFUSE_TP = 1, /**< Characterization based on Two Point values stored in eFuse*/
32+
ESP_ADC_CAL_VAL_DEFAULT_VREF = 2, /**< Characterization based on default reference voltage*/
33+
} esp_adc_cal_value_t;
3534

3635
/**
37-
* @brief Structure storing Lookup Table
38-
*
39-
* The Lookup Tables (LUT) of a given attenuation contains 33 equally spaced
40-
* points. The Gain and Offset curves are used to find the appopriate gain and
41-
* offset factor given a reference voltage v_ref.
36+
* @brief Structure storing characteristics of an ADC
4237
*
43-
* @note A seperate LUT is provided for each attenuation and are defined in
44-
* esp_adc_cal_lookup_tables.c
38+
* @note Call esp_adc_cal_characterize() to initialize the structure
4539
*/
4640
typedef struct {
47-
uint32_t gain_m; /**<Gradient of Gain Curve */
48-
uint32_t gain_c; /**<Offset of Gain Curve */
49-
uint32_t offset_m; /**<Gradient of Offset Curve */
50-
uint32_t offset_c; /**<Offset of Offset Curve */
51-
uint32_t bit_shift; /**<Bit shift used find corresponding LUT points
52-
given an ADC reading*/
53-
uint32_t voltage[]; /**<Array of voltages in mV representing the
54-
ADC-Voltage curve */
55-
} esp_adc_cal_lookup_table_t;
41+
adc_unit_t adc_num; /**< ADC number*/
42+
adc_atten_t atten; /**< ADC attenuation*/
43+
adc_bits_width_t bit_width; /**< ADC bit width */
44+
uint32_t coeff_a; /**< Gradient of ADC-Voltage curve*/
45+
uint32_t coeff_b; /**< Offset of ADC-Voltage curve*/
46+
uint32_t vref; /**< Vref used by lookup table*/
47+
const uint32_t *low_curve; /**< Pointer to low Vref curve of lookup table (NULL if unused)*/
48+
const uint32_t *high_curve; /**< Pointer to high Vref curve of lookup table (NULL if unused)*/
49+
} esp_adc_cal_characteristics_t;
5650

5751
/**
58-
* @brief Structure storing ADC characteristics of given v_ref
52+
* @brief Checks if ADC calibration values are burned into eFuse
5953
*
60-
* The ADC Characteristics structure stores the gain and offset factors of an
61-
* ESP32 module's ADC. These factors are calculated using the reference voltage,
62-
* and the Gain and Offset curves provided in the lookup tables.
54+
* This function checks if ADC reference voltage or Two Point values have been
55+
* burned to the eFuse of the current ESP32
6356
*
64-
* @note Call esp_adc_cal_get_characteristics() to initialize the structure
57+
* @param value_type Type of calibration value (ESP_ADC_CAL_VAL_EFUSE_VREF or ESP_ADC_CAL_VAL_EFUSE_TP)
6558
*
59+
* @return
60+
* - ESP_OK: The calibration mode is supported in eFuse
61+
* - ESP_ERR_NOT_SUPPORTED: Error, eFuse values are not burned
62+
* - ESP_ERR_INVALID_ARG: Error, invalid argument (ESP_ADC_CAL_VAL_DEFAULT_VREF)
6663
*/
67-
typedef struct {
68-
uint32_t v_ref; /**<Reference Voltage of current ESP32 Module in mV*/
69-
uint32_t gain; /**<Scaling factor used to correct LUT voltages to
70-
current v_ref. Bit shifted by << ADC_CAL_GAIN_SCALE
71-
for uint32 arithmetic */
72-
uint32_t offset; /**<Offset in mV used to correct LUT Voltages to current v_ref */
73-
uint32_t ideal_offset; /**<Offset in mV at the ideal reference voltage */
74-
adc_bits_width_t bit_width; /**<Bit width of ADC e.g. ADC_WIDTH_BIT_12 */
75-
const esp_adc_cal_lookup_table_t *table; /**<Pointer to LUT */
76-
} esp_adc_cal_characteristics_t;
77-
78-
extern const esp_adc_cal_lookup_table_t esp_adc_cal_table_atten_0; /**<LUT for atten0 */
79-
extern const esp_adc_cal_lookup_table_t esp_adc_cal_table_atten_1; /**<LUT for atten1 */
80-
extern const esp_adc_cal_lookup_table_t esp_adc_cal_table_atten_2; /**<LUT for atten2 */
81-
extern const esp_adc_cal_lookup_table_t esp_adc_cal_table_atten_3; /**<LUT for atten3 */
64+
esp_err_t esp_adc_cal_check_efuse(esp_adc_cal_value_t value_type);
8265

8366
/**
84-
* @brief Calculate characteristics of ADC
67+
* @brief Characterize an ADC at a particular attenuation
8568
*
86-
* This function will calculate the gain and offset factors based on the
87-
* reference voltage parameter and the Gain and Offset curve provided in the LUT.
69+
* This function will characterize the ADC at a particular attenuation and generate
70+
* the ADC-Voltage curve in the form of [y = coeff_a * x + coeff_b].
71+
* Characterization can be based on Two Point values, eFuse Vref, or default Vref
72+
* and the calibration values will be prioritized in that order.
8873
*
89-
* @note reference voltage of the ADCs can be routed to GPIO using
90-
* adc2_vref_to_gpio() from the ADC driver
74+
* @note Two Point values and eFuse Vref can be enabled/disabled using menuconfig.
9175
*
92-
* @note The LUT members have been bit shifted by ADC_CAL_GAIN_SCALE or
93-
* ADC_CAL_OFFSET_SCALE to make them uint32_t compatible. This bit shifting will
94-
* accounted for in this function
76+
* @param[in] adc_num ADC to characterize (ADC_UNIT_1 or ADC_UNIT_2)
77+
* @param[in] atten Attenuation to characterize
78+
* @param[in] bit_width Bit width configuration of ADC
79+
* @param[in] default_vref Default ADC reference voltage in mV (used if eFuse values is not available)
80+
* @param[out] chars Pointer to empty structure used to store ADC characteristics
9581
*
96-
* @param[in] v_ref true reference voltage of the ADC in mV (1000 to 1200mV). Nominal
97-
* value for reference voltage is 1100mV.
98-
* @param[in] atten attenuation setting used to select the corresponding lookup table
99-
* @param[in] bit_width bit width of ADC
100-
* @param[out] chars pointer to structure used to store ADC characteristics of module
82+
* @return
83+
* - ESP_ADC_CAL_VAL_EFUSE_VREF: eFuse Vref used for characterization
84+
* - ESP_ADC_CAL_VAL_EFUSE_TP: Two Point value used for characterization (only in Linear Mode)
85+
* - ESP_ADC_CAL_VAL_DEFAULT_VREF: Default Vref used for characterization
10186
*/
102-
void esp_adc_cal_get_characteristics(uint32_t v_ref,
103-
adc_atten_t atten,
104-
adc_bits_width_t bit_width,
105-
esp_adc_cal_characteristics_t *chars);
87+
esp_adc_cal_value_t esp_adc_cal_characterize(adc_unit_t adc_num,
88+
adc_atten_t atten,
89+
adc_bits_width_t bit_width,
90+
uint32_t default_vref,
91+
esp_adc_cal_characteristics_t *chars);
10692

10793
/**
108-
* @brief Convert raw ADC reading to voltage in mV
94+
* @brief Convert an ADC reading to voltage in mV
10995
*
110-
* This function converts a raw ADC reading to a voltage in mV. This conversion
111-
* is based on the ADC's characteristics. The raw ADC reading is referenced
112-
* against the LUT (pointed to inside characteristics struct) to obtain a voltage.
113-
* Gain and offset factors are then applied to the voltage in order to obtain
114-
* the final result.
96+
* This function converts an ADC reading to a voltage in mV based on the ADC's
97+
* characteristics.
11598
*
116-
* @param[in] adc ADC reading (different bit widths will be handled)
117-
* @param[in] chars pointer to structure containing ADC characteristics of
118-
* the module. Structure also contains pointer to the
119-
* corresponding LUT
99+
* @note Characteristics structure must be initialized before this function
100+
* is called (call esp_adc_cal_characterize())
120101
*
121-
* @return Calculated voltage in mV
102+
* @param[in] adc_reading ADC reading
103+
* @param[in] chars Pointer to initialized structure containing ADC characteristics
122104
*
123-
* @note characteristics structure must be initialized using
124-
* esp_adc_cal_get_characteristics() before this function is used
105+
* @return Voltage in mV
125106
*/
126-
uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc,
127-
const esp_adc_cal_characteristics_t *chars);
107+
uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_characteristics_t *chars);
128108

129109
/**
130-
* @brief Reads ADC1 and returns voltage in mV
110+
* @brief Reads an ADC and converts the reading to a voltage in mV
131111
*
132-
* This function reads the ADC1 using adc1_get_raw() to obtain a raw ADC
133-
* reading. The reading is then converted into a voltage value using
134-
* esp_adc_cal_raw_to_voltage().
112+
* This function reads an ADC then converts the raw reading to a voltage in mV
113+
* based on the characteristics provided. The ADC that is read is also
114+
* determined by the characteristics.
135115
*
136-
* @param[in] channel Channel of ADC1 to measure
137-
* @param[in] chars Pointer to ADC characteristics struct
116+
* @note The Characteristics structure must be initialized before this
117+
* function is called (call esp_adc_cal_characterize())
138118
*
139-
* @return voltage Calculated voltage in mV
119+
* @param[in] channel ADC Channel to read
120+
* @param[in] chars Pointer to initialized ADC characteristics structure
121+
* @param[out] voltage Pointer to store converted voltage
140122
*
141-
* @note ADC must be initialized using adc1_config_width() and
142-
* adc1_config_channel_atten() before this function is used
143-
*
144-
* @note characteristics structure must be initialized using
145-
* esp_adc_cal_get_characteristics() before this function is used
123+
* @return
124+
* - ESP_OK: ADC read and converted to mV
125+
* - ESP_ERR_TIMEOUT: Error, timed out attempting to read ADC
126+
* - ESP_ERR_INVALID_ARG: Error due to invalid arguments
127+
*/
128+
esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, const esp_adc_cal_characteristics_t *chars, uint32_t *voltage);
129+
130+
/* -------------------------- Deprecated API ------------------------------- */
131+
132+
/** @cond */ //Doxygen command to hide deprecated function from API Reference
133+
/**
134+
* @deprecated ADC1 characterization function. Deprecated in order to accommodate
135+
* ADC2 and eFuse functionality. Use esp_adc_cal_characterize() instead
146136
*/
147-
uint32_t adc1_to_voltage(adc1_channel_t channel, const esp_adc_cal_characteristics_t *chars);
137+
void esp_adc_cal_get_characteristics(uint32_t vref, adc_atten_t atten, adc_bits_width_t bit_width, esp_adc_cal_characteristics_t *chars) __attribute__((deprecated));
138+
139+
/*
140+
* @deprecated This function reads ADC1 and returns the corrected voltage. This
141+
* has been deprecated in order to accommodate ADC2 support. Use the
142+
* new function esp_adc_cal_get_voltage() instead.
143+
*/
144+
uint32_t adc1_to_voltage(adc1_channel_t channel, const esp_adc_cal_characteristics_t *chars) __attribute__((deprecated));
145+
146+
/** @endcond */
148147

149148
#ifdef __cplusplus
150149
}

0 commit comments

Comments
 (0)