esp_adc_cal/Add eFuse functionality

This commit updates the esp_adc_cal ocmponent to support new calibration methods
which utilize calibratoin values stored in eFuse. This commit includes LUT mode
This commit is contained in:
Darian Leung
2018-01-24 17:22:13 +08:00
parent ca3faa6186
commit 73cdfbfe79
12 changed files with 1018 additions and 339 deletions

View File

@@ -10,42 +10,71 @@
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#include "esp_system.h"
#include "esp_adc_cal.h"
/*Note: Different ESP32 modules may have different reference voltages varying from
* 1000mV to 1200mV. Use #define GET_VREF to route v_ref to a GPIO
*/
#define V_REF 1100
#define ADC1_TEST_CHANNEL (ADC1_CHANNEL_6) //GPIO 34
//#define V_REF_TO_GPIO //Remove comment on define to route v_ref to GPIO
#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
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;
void app_main(void)
{
#ifndef V_REF_TO_GPIO
//Init ADC and Characteristics
esp_adc_cal_characteristics_t characteristics;
//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(ADC1_TEST_CHANNEL, ADC_ATTEN_DB_0);
esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_0, ADC_WIDTH_BIT_12, &characteristics);
uint32_t voltage;
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){
voltage = adc1_to_voltage(ADC1_TEST_CHANNEL, &characteristics);
printf("%d mV\n",voltage);
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));
}
#else
//Get v_ref
esp_err_t status;
status = adc2_vref_to_gpio(GPIO_NUM_25);
if (status == ESP_OK){
printf("v_ref routed to GPIO\n");
}else{
printf("failed to route v_ref\n");
}
fflush(stdout);
#endif
}