adc: add a ut for calibration speed

pull/7680/head
Armando 2021-09-07 11:22:09 +08:00 zatwierdzone przez Armando (Dou Yiwen)
rodzic c45c6f52f1
commit 9aae92fd16
1 zmienionych plików z 122 dodań i 1 usunięć

Wyświetl plik

@ -4,15 +4,20 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#include "sdkconfig.h"
#include <sys/param.h> #include <sys/param.h>
#include <string.h> #include <string.h>
#include "esp_log.h"
#include "test_utils.h" #include "test_utils.h"
#include "esp_adc_cal.h"
#include "driver/adc_common.h"
static const char *TAG = "ADC";
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32, ESP32S2, ESP32S3) #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32, ESP32S2, ESP32S3)
//API only supported for C3 now. //API only supported for C3 now.
#include "driver/adc.h" #include "driver/adc.h"
#include "esp_adc_cal.h"
#include "esp_log.h" #include "esp_log.h"
#define TEST_COUNT 4096 #define TEST_COUNT 4096
@ -274,4 +279,120 @@ TEST_CASE("test_adc_single", "[adc][ignore][manual]")
} }
} }
#endif //#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32, ESP32S2, ESP32S3)
/********************************************************************************
* ADC Speed Related Tests
********************************************************************************/
#ifdef CONFIG_IDF_TARGET_ESP32
#define CPU_FREQ_MHZ CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ
#elif CONFIG_IDF_TARGET_ESP32S2
#define CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ
#elif CONFIG_IDF_TARGET_ESP32S3
#define CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
#elif CONFIG_IDF_TARGET_ESP32C3
#define CPU_FREQ_MHZ CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ
#endif #endif
#define RECORD_TIME_PREPARE() uint32_t __t1, __t2
#define RECORD_TIME_START() do {__t1 = esp_cpu_get_ccount();}while(0)
#define RECORD_TIME_END(p_time) do{__t2 = esp_cpu_get_ccount(); *p_time = (__t2-__t1);}while(0)
#define GET_US_BY_CCOUNT(t) ((double)t/CPU_FREQ_MHZ)
//ADC Channels
#if CONFIG_IDF_TARGET_ESP32
#define ADC1_CALI_TEST_CHAN0 ADC1_CHANNEL_6
#define ADC2_CALI_TEST_CHAN0 ADC2_CHANNEL_0
#else
#define ADC1_CALI_TEST_CHAN0 ADC1_CHANNEL_2
#define ADC2_CALI_TEST_CHAN0 ADC2_CHANNEL_0
#endif
//ADC Calibration
#if CONFIG_IDF_TARGET_ESP32
#define ADC_TEST_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_VREF
#elif CONFIG_IDF_TARGET_ESP32S2
#define ADC_TEST_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP
#elif CONFIG_IDF_TARGET_ESP32C3
#define ADC_TEST_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP
#elif CONFIG_IDF_TARGET_ESP32S3
#define ADC_TEST_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP_FIT
#endif
#define TIMES_PER_ATTEN 10
static esp_adc_cal_characteristics_t adc1_chars;
static esp_adc_cal_characteristics_t adc2_chars;
static void adc_single_cali_init(adc_unit_t adc_n, adc_channel_t chan, uint32_t atten)
{
esp_err_t ret;
esp_adc_cal_value_t ret_val = ESP_ADC_CAL_VAL_NOT_SUPPORTED;
ret = esp_adc_cal_check_efuse(ADC_TEST_CALI_SCHEME);
if (ret == ESP_ERR_NOT_SUPPORTED) {
ESP_LOGE(TAG, "Cali scheme not supported!");
TEST_ASSERT(ret != ESP_ERR_NOT_SUPPORTED);
} else if (ret != ESP_OK) {
ESP_LOGW(TAG, "No cali eFuse, but will run the test");
}
if (adc_n == ADC_UNIT_1) {
ret_val = esp_adc_cal_characterize(adc_n, atten, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars);
TEST_ESP_OK(adc1_config_width(ADC_WIDTH_BIT_DEFAULT));
TEST_ESP_OK(adc1_config_channel_atten((adc1_channel_t)chan, atten));
} else if (adc_n == ADC_UNIT_2) {
TEST_ESP_OK(adc2_config_channel_atten((adc2_channel_t)chan, atten));
ret_val = esp_adc_cal_characterize(adc_n, atten, ADC_WIDTH_BIT_DEFAULT, 0, &adc2_chars);
}
if (ret_val == ESP_ADC_CAL_VAL_NOT_SUPPORTED) {
ESP_LOGW(TAG, "No cali eFuse, or invalid arg, but will run the test");
}
ESP_LOGI(TAG, "ADC%d, channel%d, atten%d", adc_n, chan, atten);
}
static IRAM_ATTR NOINLINE_ATTR uint32_t get_cali_time_in_ccount(uint32_t adc_raw, esp_adc_cal_characteristics_t *chars)
{
uint32_t time;
RECORD_TIME_PREPARE();
RECORD_TIME_START();
esp_adc_cal_raw_to_voltage(adc_raw, chars);
RECORD_TIME_END(&time);
return time;
}
TEST_CASE("test_adc_single_cali_time", "[adc][ignore][manual]")
{
ESP_LOGI(TAG, "CPU FREQ is %dMHz", CPU_FREQ_MHZ);
uint32_t adc1_time_record[4][TIMES_PER_ATTEN] = {};
uint32_t adc2_time_record[4][TIMES_PER_ATTEN] = {};
int adc1_raw = 0;
int adc2_raw = 0;
//atten0 ~ atten3
for (int i = 0; i < 4; i++) {
ESP_LOGI(TAG, "----------------atten%d----------------", i);
adc_single_cali_init(ADC_UNIT_1, ADC1_CALI_TEST_CHAN0, i);
adc_single_cali_init(ADC_UNIT_2, ADC2_CALI_TEST_CHAN0, i);
for (int j = 0; j < TIMES_PER_ATTEN; j++) {
adc1_raw = adc1_get_raw(ADC1_CALI_TEST_CHAN0);
TEST_ESP_OK(adc2_get_raw(ADC2_CALI_TEST_CHAN0, ADC_WIDTH_BIT_DEFAULT, &adc2_raw));
adc1_time_record[i][j] = get_cali_time_in_ccount(adc1_raw, &adc1_chars);
adc2_time_record[i][j] = get_cali_time_in_ccount(adc2_raw, &adc2_chars);
IDF_LOG_PERFORMANCE("ADC1 Cali time", "%d us", (int)GET_US_BY_CCOUNT(adc1_time_record[i][j]));
IDF_LOG_PERFORMANCE("ADC2 Cali time", "%d us", (int)GET_US_BY_CCOUNT(adc2_time_record[i][j]));
}
}
}