mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-11 04:57:38 +00:00
esp_adc_cal/Remove lookup table
This commit removes the lookup table mode due to inferior performance when compared to linear mode under attenuation 0, 1 and 2. However small portions of the lookup table are kept for the higher voltages of atten 3 (above ADC reading 2880). That voltage range in atten 3 has non linear characteristics making the LUT performan better than linear mode.
This commit is contained in:
@@ -14,67 +14,79 @@
|
||||
#include "driver/adc.h"
|
||||
#include "esp_adc_cal.h"
|
||||
|
||||
#define PRINT_VAL_SUPPORT(support, type) ({ \
|
||||
if(support == ESP_OK){ \
|
||||
printf("%s: supported\n", (type)); \
|
||||
} else { \
|
||||
printf("%s: not supported\n", (type)); \
|
||||
} \
|
||||
})
|
||||
|
||||
#define PRINT_VAL_TYPE(type, mode) ({ \
|
||||
if(type == ESP_ADC_CAL_VAL_EFUSE_TP){ \
|
||||
printf("%s mode: Two Point Value\n", (mode)); \
|
||||
} else if (type == ESP_ADC_CAL_VAL_EFUSE_VREF){ \
|
||||
printf("%s mode: eFuse Vref\n", (mode)); \
|
||||
} else { \
|
||||
printf("%s mode: Default Vref\n", (mode)); \
|
||||
} \
|
||||
})
|
||||
|
||||
#define DEFAULT_VREF 1100 //Use adc2_vref_to_gpio() to obtain a better estimate
|
||||
#define NO_OF_SAMPLES 64
|
||||
#define NO_OF_SAMPLES 64 //Multisampling
|
||||
|
||||
static esp_adc_cal_characteristics_t *adc_linear_chars;
|
||||
static esp_adc_cal_characteristics_t *adc_lut_chars;
|
||||
static adc1_channel_t channel = ADC1_CHANNEL_6;
|
||||
static esp_adc_cal_characteristics_t *adc_chars;
|
||||
static const adc_channel_t channel = ADC_CHANNEL_6; //GPIO34 if ADC1, GPIO14 if ADC2
|
||||
static const adc_atten_t atten = ADC_ATTEN_DB_0;
|
||||
static const adc_unit_t unit = ADC_UNIT_1;
|
||||
|
||||
void app_main(void)
|
||||
static void check_efuse()
|
||||
{
|
||||
//Check if Two Point or Vref are burned into eFuse
|
||||
esp_err_t efuse_vref_support = esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF);
|
||||
esp_err_t efuse_tp_support = esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP);
|
||||
PRINT_VAL_SUPPORT(efuse_vref_support, "eFuse Vref");
|
||||
PRINT_VAL_SUPPORT(efuse_tp_support, "eFuse Two Point");
|
||||
|
||||
|
||||
//Configure ADC1
|
||||
adc1_config_width(ADC_WIDTH_BIT_12);
|
||||
adc1_config_channel_atten(channel, ADC_ATTEN_DB_0);
|
||||
|
||||
//Characterize ADC1 in both linear and lut mode
|
||||
adc_linear_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
|
||||
adc_lut_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
|
||||
|
||||
esp_adc_cal_value_t lin_val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_0, ESP_ADC_CAL_MODE_LIN, DEFAULT_VREF, adc_linear_chars);
|
||||
esp_adc_cal_value_t lut_val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_0, ESP_ADC_CAL_MODE_LUT, DEFAULT_VREF, adc_lut_chars);
|
||||
PRINT_VAL_TYPE(lin_val_type, "Linear");
|
||||
PRINT_VAL_TYPE(lut_val_type, "LUT");
|
||||
|
||||
//Continuously sample ADC1
|
||||
while(1){
|
||||
uint32_t adc1_raw = 0;
|
||||
//Multisample
|
||||
for(int i = 0; i < NO_OF_SAMPLES; i++){
|
||||
adc1_raw += adc1_get_raw(channel);
|
||||
}
|
||||
adc1_raw /= NO_OF_SAMPLES;
|
||||
|
||||
uint32_t corrected_linear = esp_adc_cal_raw_to_voltage(adc1_raw, ADC_WIDTH_BIT_12, adc_linear_chars);
|
||||
uint32_t corrected_lut = esp_adc_cal_raw_to_voltage(adc1_raw, ADC_WIDTH_BIT_12, adc_lut_chars);
|
||||
printf("Raw: %d\tLinear: %dmV\tLUT: %dmV\n", adc1_raw, corrected_linear, corrected_lut);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
//Check TP is burned into eFuse
|
||||
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
|
||||
printf("eFuse Two Point: Supported\n");
|
||||
} else {
|
||||
printf("eFuse Two Point: NOT supported\n");
|
||||
}
|
||||
|
||||
//Check Vref is burned into eFuse
|
||||
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
|
||||
printf("eFuse Vref: Supported\n");
|
||||
} else {
|
||||
printf("eFuse Vref: NOT supported\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void print_char_val_type(esp_adc_cal_value_t val_type)
|
||||
{
|
||||
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
|
||||
printf("Characterized using Two Point Value\n");
|
||||
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
|
||||
printf("Characterized using eFuse Vref\n");
|
||||
} else {
|
||||
printf("Characterized using Default Vref\n");
|
||||
}
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
//Check if Two Point or Vref are burned into eFuse
|
||||
check_efuse();
|
||||
|
||||
//Configure ADC
|
||||
if (unit == ADC_UNIT_1) {
|
||||
adc1_config_width(ADC_WIDTH_BIT_12);
|
||||
adc1_config_channel_atten(channel, atten);
|
||||
} else {
|
||||
adc2_config_channel_atten((adc2_channel_t)channel, atten);
|
||||
}
|
||||
|
||||
//Characterize ADC
|
||||
adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
|
||||
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
|
||||
print_char_val_type(val_type);
|
||||
|
||||
//Continuously sample ADC1
|
||||
while (1) {
|
||||
uint32_t adc_reading = 0;
|
||||
//Multisampling
|
||||
for (int i = 0; i < NO_OF_SAMPLES; i++) {
|
||||
if (unit == ADC_UNIT_1) {
|
||||
adc_reading += adc1_get_raw((adc1_channel_t)channel);
|
||||
} else {
|
||||
int raw;
|
||||
adc2_get_raw((adc2_channel_t)channel, ADC_WIDTH_BIT_12, &raw);
|
||||
adc_reading += raw;
|
||||
}
|
||||
}
|
||||
adc_reading /= NO_OF_SAMPLES;
|
||||
//Convert adc_reading to voltage in mV
|
||||
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
|
||||
printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user