mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-10 04:43:33 +00:00
Merge branch 'feature/esp32s2_adc_calib_V1' into 'master'
driver/adc: support for esp32s2 adc calibration scheme (RTC High Priority) Closes IDFGH-3500 See merge request espressif/esp-idf!10004
This commit is contained in:
@@ -12,15 +12,13 @@
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/adc.h"
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
#include "esp_adc_cal.h"
|
||||
#endif
|
||||
|
||||
#define DEFAULT_VREF 1100 //Use adc2_vref_to_gpio() to obtain a better estimate
|
||||
#define NO_OF_SAMPLES 64 //Multisampling
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
static esp_adc_cal_characteristics_t *adc_chars;
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
static const adc_channel_t channel = ADC_CHANNEL_6; //GPIO34 if ADC1, GPIO14 if ADC2
|
||||
static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
@@ -30,24 +28,34 @@ static const adc_bits_width_t width = ADC_WIDTH_BIT_13;
|
||||
static const adc_atten_t atten = ADC_ATTEN_DB_0;
|
||||
static const adc_unit_t unit = ADC_UNIT_1;
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
|
||||
static void check_efuse(void)
|
||||
{
|
||||
//Check TP is burned into eFuse
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
//Check if 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");
|
||||
}
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
|
||||
printf("eFuse Two Point: Supported\n");
|
||||
} else {
|
||||
printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n");
|
||||
}
|
||||
#else
|
||||
#error "This example is configured for ESP32/ESP32S2."
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
static void print_char_val_type(esp_adc_cal_value_t val_type)
|
||||
{
|
||||
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
|
||||
@@ -58,14 +66,12 @@ static void print_char_val_type(esp_adc_cal_value_t val_type)
|
||||
printf("Characterized using Default Vref\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
//Check if Two Point or Vref are burned into eFuse
|
||||
check_efuse();
|
||||
#endif
|
||||
|
||||
//Configure ADC
|
||||
if (unit == ADC_UNIT_1) {
|
||||
@@ -75,12 +81,10 @@ void app_main(void)
|
||||
adc2_config_channel_atten((adc2_channel_t)channel, atten);
|
||||
}
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
//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, width, DEFAULT_VREF, adc_chars);
|
||||
print_char_val_type(val_type);
|
||||
#endif
|
||||
|
||||
//Continuously sample ADC1
|
||||
while (1) {
|
||||
@@ -96,13 +100,9 @@ void app_main(void)
|
||||
}
|
||||
}
|
||||
adc_reading /= NO_OF_SAMPLES;
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
//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);
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
printf("ADC%d CH%d Raw: %d\t\n", unit, channel, adc_reading);
|
||||
#endif
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
}
|
||||
|
@@ -45,7 +45,7 @@ void app_main(void)
|
||||
|
||||
//be sure to do the init before using adc2.
|
||||
printf("adc2_init...\n");
|
||||
adc2_config_channel_atten( ADC2_EXAMPLE_CHANNEL, ADC_ATTEN_0db );
|
||||
adc2_config_channel_atten( ADC2_EXAMPLE_CHANNEL, ADC_ATTEN_11db );
|
||||
|
||||
vTaskDelay(2 * portTICK_PERIOD_MS);
|
||||
|
||||
|
Reference in New Issue
Block a user