mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'feature/adc_calibration_c2_v5.0' into 'release/v5.0'
esp_adc: support adc calibration on esp32c2 (v5.0) See merge request espressif/esp-idf!22382
This commit is contained in:
commit
f58f8116de
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
@ -47,11 +47,11 @@
|
||||
#define ADC_TEST_HIGH_THRESH 0
|
||||
|
||||
#elif CONFIG_IDF_TARGET_ESP32C2
|
||||
#define ADC_TEST_LOW_VAL 2147
|
||||
#define ADC_TEST_LOW_THRESH 100
|
||||
#define ADC_TEST_LOW_VAL 0
|
||||
#define ADC_TEST_LOW_THRESH 15
|
||||
|
||||
#define ADC_TEST_HIGH_VAL 4095
|
||||
#define ADC_TEST_HIGH_THRESH 0
|
||||
#define ADC_TEST_HIGH_VAL 3400
|
||||
#define ADC_TEST_HIGH_THRESH 200
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -5,14 +5,103 @@
|
||||
*/
|
||||
|
||||
#include <esp_bit_defs.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_efuse.h"
|
||||
#include "esp_efuse_table.h"
|
||||
#include "esp_efuse_rtc_calib.h"
|
||||
#include "hal/adc_types.h"
|
||||
#include "inttypes.h"
|
||||
|
||||
int esp_efuse_rtc_calib_get_ver(void)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
esp_efuse_read_field_blob(ESP_EFUSE_BLK_VERSION_MINOR, &result, ESP_EFUSE_BLK_VERSION_MINOR[0]->bit_count); // IDF-5366
|
||||
return result;
|
||||
uint32_t blk_ver_major = 0;
|
||||
esp_efuse_read_field_blob(ESP_EFUSE_BLK_VERSION_MAJOR, &blk_ver_major, ESP_EFUSE_BLK_VERSION_MAJOR[0]->bit_count); // IDF-5366
|
||||
|
||||
uint32_t cali_version = (blk_ver_major == 0) ? ESP_EFUSE_ADC_CALIB_VER : 0;
|
||||
if (!cali_version) {
|
||||
ESP_LOGW("eFuse", "calibration efuse version does not match, set default version: %d", 0);
|
||||
}
|
||||
|
||||
return cali_version;
|
||||
}
|
||||
|
||||
uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten)
|
||||
{
|
||||
assert(version == ESP_EFUSE_ADC_CALIB_VER);
|
||||
assert(atten <= ADC_ATTEN_DB_11);
|
||||
(void) adc_unit;
|
||||
|
||||
if (atten == ADC_ATTEN_DB_2_5 || atten == ADC_ATTEN_DB_6) {
|
||||
/**
|
||||
* - ESP32C2 only supports HW calibration on ADC_ATTEN_DB_0 and ADC_ATTEN_DB_11
|
||||
* - For other attenuation, we just return default value, which is 0.
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t adc_icode_diff_atten0 = 0;
|
||||
int32_t adc_icode_diff_atten3 = 0;
|
||||
int efuse_icode_bits = 0;
|
||||
|
||||
efuse_icode_bits = esp_efuse_get_field_size(ESP_EFUSE_ADC1_INIT_CODE_ATTEN0);
|
||||
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_ADC1_INIT_CODE_ATTEN0, &adc_icode_diff_atten0, efuse_icode_bits));
|
||||
adc_icode_diff_atten0 = ((adc_icode_diff_atten0 & BIT(7)) != 0) ? -(adc_icode_diff_atten0 & 0x7f): adc_icode_diff_atten0;
|
||||
|
||||
efuse_icode_bits = esp_efuse_get_field_size(ESP_EFUSE_ADC1_INIT_CODE_ATTEN3);
|
||||
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_ADC1_INIT_CODE_ATTEN3, &adc_icode_diff_atten3, efuse_icode_bits));
|
||||
|
||||
ESP_EARLY_LOGV("eFuse", "adc_icode_diff_atten0: 0d%"PRId32", adc_icode_diff_atten3: 0d%"PRId32, adc_icode_diff_atten0, adc_icode_diff_atten3);
|
||||
|
||||
uint32_t init_code = 0;
|
||||
if (atten == ADC_ATTEN_DB_0) {
|
||||
init_code = adc_icode_diff_atten0 + 2160;
|
||||
} else {
|
||||
//ADC_ATTEN_DB_11
|
||||
init_code = adc_icode_diff_atten3 + adc_icode_diff_atten0 + 2160;
|
||||
}
|
||||
|
||||
return init_code;
|
||||
}
|
||||
|
||||
esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, int atten, uint32_t *out_digi, uint32_t *out_vol_mv)
|
||||
{
|
||||
assert(version == ESP_EFUSE_ADC_CALIB_VER);
|
||||
assert(atten <= ADC_ATTEN_DB_11);
|
||||
(void) adc_unit;
|
||||
|
||||
if (atten == ADC_ATTEN_DB_2_5 || atten == ADC_ATTEN_DB_6) {
|
||||
/**
|
||||
* - ESP32C2 only supports SW calibration on ADC_ATTEN_DB_0 and ADC_ATTEN_DB_11
|
||||
* - For other attenuation, we need to return an error, informing upper layer SW calibration driver
|
||||
* to deal with the error.
|
||||
*/
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
int32_t adc_vol_diff_atten0 = 0;
|
||||
int32_t adc_vol_diff_atten3 = 0;
|
||||
int efuse_vol_bits = 0;
|
||||
|
||||
efuse_vol_bits = esp_efuse_get_field_size(ESP_EFUSE_ADC1_CAL_VOL_ATTEN0);
|
||||
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_ADC1_CAL_VOL_ATTEN0, &adc_vol_diff_atten0, efuse_vol_bits));
|
||||
adc_vol_diff_atten0 = ((adc_vol_diff_atten0 & BIT(7)) != 0) ? -(adc_vol_diff_atten0 & 0x7f): adc_vol_diff_atten0;
|
||||
|
||||
efuse_vol_bits = esp_efuse_get_field_size(ESP_EFUSE_ADC1_CAL_VOL_ATTEN3);
|
||||
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_ADC1_CAL_VOL_ATTEN3, &adc_vol_diff_atten3, efuse_vol_bits));
|
||||
adc_vol_diff_atten3 = ((adc_vol_diff_atten3 & BIT(5)) != 0) ? -(adc_vol_diff_atten3 & 0x1f): adc_vol_diff_atten3;
|
||||
|
||||
ESP_EARLY_LOGV("eFuse", "adc_vol_diff_atten0: 0d%"PRId32", adc_vol_diff_atten3: 0d%"PRId32, adc_vol_diff_atten0, adc_vol_diff_atten3);
|
||||
|
||||
if (atten == ADC_ATTEN_DB_0) {
|
||||
*out_digi = adc_vol_diff_atten0 + 1540;
|
||||
*out_vol_mv = 400;
|
||||
} else {
|
||||
//ADC_ATTEN_DB_11
|
||||
*out_digi = adc_vol_diff_atten0 + 1540 - adc_vol_diff_atten3 - 123;
|
||||
*out_vol_mv = 1370;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_efuse_rtc_calib_get_tsens_val(float* tsens_cal)
|
||||
|
@ -9,7 +9,7 @@
|
||||
#include <assert.h>
|
||||
#include "esp_efuse_table.h"
|
||||
|
||||
// md5_digest_table 4d0ed19c755bd49610cefdd83f798536
|
||||
// md5_digest_table ceedae45d1a885ced865a05eeca7d7ee
|
||||
// This file was generated from the file esp_efuse_table.csv. DO NOT CHANGE THIS FILE MANUALLY.
|
||||
// If you want to change some fields, you need to change esp_efuse_table.csv file
|
||||
// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.
|
||||
@ -176,10 +176,6 @@ static const esp_efuse_desc_t BLK_VERSION_MAJOR[] = {
|
||||
{EFUSE_BLK2, 60, 2}, // BLK_VERSION_MAJOR,
|
||||
};
|
||||
|
||||
static const esp_efuse_desc_t PVT_LOW[] = {
|
||||
{EFUSE_BLK2, 91, 5}, // EFUSE_PVT_LOW,
|
||||
};
|
||||
|
||||
static const esp_efuse_desc_t KEY0[] = {
|
||||
{EFUSE_BLK3, 0, 256}, // [256bit FE key] or [128bit FE key and 128key SB key] or [user data],
|
||||
};
|
||||
@ -200,6 +196,26 @@ static const esp_efuse_desc_t OCODE[] = {
|
||||
{EFUSE_BLK2, 62, 7}, // OCode,
|
||||
};
|
||||
|
||||
static const esp_efuse_desc_t TEMP_CALIB[] = {
|
||||
{EFUSE_BLK2, 69, 9}, // Temperature calibration data,
|
||||
};
|
||||
|
||||
static const esp_efuse_desc_t ADC1_INIT_CODE_ATTEN0[] = {
|
||||
{EFUSE_BLK2, 78, 8}, // ADC1 init code at atten0,
|
||||
};
|
||||
|
||||
static const esp_efuse_desc_t ADC1_INIT_CODE_ATTEN3[] = {
|
||||
{EFUSE_BLK2, 86, 5}, // ADC1 init code at atten3,
|
||||
};
|
||||
|
||||
static const esp_efuse_desc_t ADC1_CAL_VOL_ATTEN0[] = {
|
||||
{EFUSE_BLK2, 91, 8}, // ADC1 calibration voltage at atten0,
|
||||
};
|
||||
|
||||
static const esp_efuse_desc_t ADC1_CAL_VOL_ATTEN3[] = {
|
||||
{EFUSE_BLK2, 99, 6}, // ADC1 calibration voltage at atten3,
|
||||
};
|
||||
|
||||
static const esp_efuse_desc_t DIG_DBIAS_HVT[] = {
|
||||
{EFUSE_BLK2, 105, 5}, // BLOCK2 digital dbias when hvt,
|
||||
};
|
||||
@ -444,11 +460,6 @@ const esp_efuse_desc_t* ESP_EFUSE_BLK_VERSION_MAJOR[] = {
|
||||
NULL
|
||||
};
|
||||
|
||||
const esp_efuse_desc_t* ESP_EFUSE_PVT_LOW[] = {
|
||||
&PVT_LOW[0], // EFUSE_PVT_LOW
|
||||
NULL
|
||||
};
|
||||
|
||||
const esp_efuse_desc_t* ESP_EFUSE_KEY0[] = {
|
||||
&KEY0[0], // [256bit FE key] or [128bit FE key and 128key SB key] or [user data]
|
||||
NULL
|
||||
@ -474,6 +485,31 @@ const esp_efuse_desc_t* ESP_EFUSE_OCODE[] = {
|
||||
NULL
|
||||
};
|
||||
|
||||
const esp_efuse_desc_t* ESP_EFUSE_TEMP_CALIB[] = {
|
||||
&TEMP_CALIB[0], // Temperature calibration data
|
||||
NULL
|
||||
};
|
||||
|
||||
const esp_efuse_desc_t* ESP_EFUSE_ADC1_INIT_CODE_ATTEN0[] = {
|
||||
&ADC1_INIT_CODE_ATTEN0[0], // ADC1 init code at atten0
|
||||
NULL
|
||||
};
|
||||
|
||||
const esp_efuse_desc_t* ESP_EFUSE_ADC1_INIT_CODE_ATTEN3[] = {
|
||||
&ADC1_INIT_CODE_ATTEN3[0], // ADC1 init code at atten3
|
||||
NULL
|
||||
};
|
||||
|
||||
const esp_efuse_desc_t* ESP_EFUSE_ADC1_CAL_VOL_ATTEN0[] = {
|
||||
&ADC1_CAL_VOL_ATTEN0[0], // ADC1 calibration voltage at atten0
|
||||
NULL
|
||||
};
|
||||
|
||||
const esp_efuse_desc_t* ESP_EFUSE_ADC1_CAL_VOL_ATTEN3[] = {
|
||||
&ADC1_CAL_VOL_ATTEN3[0], // ADC1 calibration voltage at atten3
|
||||
NULL
|
||||
};
|
||||
|
||||
const esp_efuse_desc_t* ESP_EFUSE_DIG_DBIAS_HVT[] = {
|
||||
&DIG_DBIAS_HVT[0], // BLOCK2 digital dbias when hvt
|
||||
NULL
|
||||
|
@ -71,7 +71,6 @@
|
||||
BLK_VERSION_MAJOR, EFUSE_BLK2, 60, 2, BLK_VERSION_MAJOR
|
||||
|
||||
# EFUSE_RD_BLK2_DATA2_REG
|
||||
PVT_LOW, EFUSE_BLK2, 91, 5, EFUSE_PVT_LOW
|
||||
|
||||
|
||||
################
|
||||
@ -83,6 +82,11 @@ KEY0.SB_128BIT, EFUSE_BLK3, 128, 128, [128bit SB
|
||||
# AUTO CONFIG DIG&RTC DBIAS#
|
||||
################
|
||||
OCODE, EFUSE_BLK2, 62, 7, OCode
|
||||
TEMP_CALIB, EFUSE_BLK2, 69, 9, Temperature calibration data
|
||||
ADC1_INIT_CODE_ATTEN0, EFUSE_BLK2, 78, 8, ADC1 init code at atten0
|
||||
ADC1_INIT_CODE_ATTEN3, EFUSE_BLK2, 86, 5, ADC1 init code at atten3
|
||||
ADC1_CAL_VOL_ATTEN0, EFUSE_BLK2, 91, 8, ADC1 calibration voltage at atten0
|
||||
ADC1_CAL_VOL_ATTEN3, EFUSE_BLK2, 99, 6, ADC1 calibration voltage at atten3
|
||||
DIG_DBIAS_HVT, EFUSE_BLK2, 105, 5, BLOCK2 digital dbias when hvt
|
||||
DIG_LDO_SLP_DBIAS2, EFUSE_BLK2, 110, 7, BLOCK2 DIG_LDO_DBG0_DBIAS2
|
||||
DIG_LDO_SLP_DBIAS26, EFUSE_BLK2, 117, 8, BLOCK2 DIG_LDO_DBG0_DBIAS26
|
||||
|
Can't render this file because it contains an unexpected character in line 7 and column 53.
|
@ -11,6 +11,40 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//This is the ADC calibration value version burnt in efuse
|
||||
#define ESP_EFUSE_ADC_CALIB_VER 1
|
||||
|
||||
/**
|
||||
* @brief Get the RTC calibration efuse version
|
||||
*
|
||||
* @return Version of the stored efuse
|
||||
*/
|
||||
int esp_efuse_rtc_calib_get_ver(void);
|
||||
|
||||
/**
|
||||
* @brief Get the init code in the efuse, for the corresponding attenuation.
|
||||
*
|
||||
* @param version Version of the stored efuse
|
||||
* @param adc_unit ADC unit
|
||||
* @param atten Attenuation of the init code
|
||||
* @return The init code stored in efuse
|
||||
*/
|
||||
uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten);
|
||||
|
||||
/**
|
||||
* @brief Get the calibration digits stored in the efuse, and the corresponding voltage.
|
||||
*
|
||||
* @param version Version of the stored efuse
|
||||
* @param adc_unit ADC unit
|
||||
* @param atten Attenuation to use
|
||||
* @param out_digi Output buffer of the digits
|
||||
* @param out_vol_mv Output of the voltage, in mV
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG: If `ADC_ATTEN_DB_2_5` or `ADC_ATTEN_DB_6` is used
|
||||
* - ESP_OK: if success
|
||||
*/
|
||||
esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, int atten, uint32_t *out_digi, uint32_t *out_vol_mv);
|
||||
|
||||
/**
|
||||
* @brief Get the temperature sensor calibration number delta_T stored in the efuse.
|
||||
*
|
||||
|
@ -10,7 +10,7 @@ extern "C" {
|
||||
|
||||
#include "esp_efuse.h"
|
||||
|
||||
// md5_digest_table 4d0ed19c755bd49610cefdd83f798536
|
||||
// md5_digest_table ceedae45d1a885ced865a05eeca7d7ee
|
||||
// This file was generated from the file esp_efuse_table.csv. DO NOT CHANGE THIS FILE MANUALLY.
|
||||
// If you want to change some fields, you need to change esp_efuse_table.csv file
|
||||
// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.
|
||||
@ -56,12 +56,16 @@ extern const esp_efuse_desc_t* ESP_EFUSE_WAFER_VERSION_MAJOR[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_PKG_VERSION[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_BLK_VERSION_MINOR[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_BLK_VERSION_MAJOR[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_PVT_LOW[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_KEY0[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_KEY0_FE_256BIT[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_KEY0_FE_128BIT[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_KEY0_SB_128BIT[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_OCODE[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_TEMP_CALIB[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_ADC1_INIT_CODE_ATTEN0[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_ADC1_INIT_CODE_ATTEN3[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_ADC1_CAL_VOL_ATTEN0[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_ADC1_CAL_VOL_ATTEN3[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_DIG_DBIAS_HVT[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_DIG_LDO_SLP_DBIAS2[];
|
||||
extern const esp_efuse_desc_t* ESP_EFUSE_DIG_LDO_SLP_DBIAS26[];
|
||||
|
@ -7,17 +7,24 @@
|
||||
#include <esp_bit_defs.h>
|
||||
#include "esp_efuse.h"
|
||||
#include "esp_efuse_table.h"
|
||||
#include "esp_efuse_rtc_calib.h"
|
||||
|
||||
int esp_efuse_rtc_calib_get_ver(void)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
esp_efuse_read_field_blob(ESP_EFUSE_BLK_VERSION_MAJOR, &result, ESP_EFUSE_BLK_VERSION_MAJOR[0]->bit_count); // IDF-5366
|
||||
return result;
|
||||
uint32_t blk_ver_major = 0;
|
||||
esp_efuse_read_field_blob(ESP_EFUSE_BLK_VERSION_MAJOR, &blk_ver_major, ESP_EFUSE_BLK_VERSION_MAJOR[0]->bit_count); // IDF-5366
|
||||
|
||||
uint32_t cali_version = (blk_ver_major == 1) ? ESP_EFUSE_ADC_CALIB_VER : 0;
|
||||
if (!cali_version) {
|
||||
ESP_LOGW("eFuse", "calibration efuse version does not match, set default version: %d", 0);
|
||||
}
|
||||
|
||||
return cali_version;
|
||||
}
|
||||
|
||||
uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten)
|
||||
{
|
||||
assert(version == 1);
|
||||
assert(version == ESP_EFUSE_ADC_CALIB_VER);
|
||||
(void) adc_unit;
|
||||
const esp_efuse_desc_t** init_code_efuse;
|
||||
assert(atten < 4);
|
||||
@ -44,7 +51,7 @@ esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, in
|
||||
(void)adc_unit; //On esp32c3, V1 we don't have calibration data for ADC2, using the efuse data of ADC1
|
||||
const esp_efuse_desc_t** cal_vol_efuse;
|
||||
uint32_t calib_vol_expected_mv;
|
||||
if (version != 1) {
|
||||
if (version != ESP_EFUSE_ADC_CALIB_VER) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (atten >= 4) {
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_efuse.h"
|
||||
#include "esp_efuse_table.h"
|
||||
#include "esp_efuse_rtc_calib.h"
|
||||
#include "hal/adc_types.h"
|
||||
|
||||
int esp_efuse_rtc_calib_get_ver(void)
|
||||
@ -16,17 +17,17 @@ int esp_efuse_rtc_calib_get_ver(void)
|
||||
uint32_t blk_ver_major = 0;
|
||||
ESP_ERROR_CHECK(esp_efuse_read_field_blob(ESP_EFUSE_BLK_VERSION_MAJOR, &blk_ver_major, ESP_EFUSE_BLK_VERSION_MAJOR[0]->bit_count)); // IDF-5366
|
||||
|
||||
uint32_t cali_version_v1 = (blk_ver_major == 1) ? 1 : 0;
|
||||
if (!cali_version_v1) {
|
||||
uint32_t cali_version = (blk_ver_major == 1) ? ESP_EFUSE_ADC_CALIB_VER : 0;
|
||||
if (!cali_version) {
|
||||
ESP_LOGW("eFuse", "calibration efuse version does not match, set default version: %d", 0);
|
||||
}
|
||||
|
||||
return cali_version_v1;
|
||||
return cali_version;
|
||||
}
|
||||
|
||||
uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten)
|
||||
{
|
||||
assert(version == 1);
|
||||
assert(version == ESP_EFUSE_ADC_CALIB_VER);
|
||||
assert(atten < 4);
|
||||
assert(adc_unit <= ADC_UNIT_2);
|
||||
|
||||
@ -61,7 +62,7 @@ uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int a
|
||||
|
||||
esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, int atten, uint32_t *out_digi, uint32_t *out_vol_mv)
|
||||
{
|
||||
assert(version == 1);
|
||||
assert(version == ESP_EFUSE_ADC_CALIB_VER);
|
||||
assert(atten < 4);
|
||||
assert(adc_unit <= ADC_UNIT_2);
|
||||
|
||||
|
@ -84,9 +84,9 @@ esp_err_t adc_cali_create_scheme_curve_fitting(const adc_cali_curve_fitting_conf
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
// current version only accepts encoding ver 1.
|
||||
// current version only accepts encoding version: `ESP_EFUSE_ADC_CALIB_VER`.
|
||||
uint8_t adc_encoding_version = esp_efuse_rtc_calib_get_ver();
|
||||
ESP_RETURN_ON_FALSE(adc_encoding_version == 1, ESP_ERR_NOT_SUPPORTED, TAG, "Calibration required eFuse bits not burnt");
|
||||
ESP_RETURN_ON_FALSE(adc_encoding_version == ESP_EFUSE_ADC_CALIB_VER, ESP_ERR_NOT_SUPPORTED, TAG, "Calibration required eFuse bits not burnt");
|
||||
|
||||
adc_cali_scheme_t *scheme = (adc_cali_scheme_t *)heap_caps_calloc(1, sizeof(adc_cali_scheme_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
ESP_RETURN_ON_FALSE(scheme, ESP_ERR_NO_MEM, TAG, "no mem for adc calibration scheme");
|
||||
@ -151,7 +151,7 @@ static esp_err_t cali_raw_to_voltage(void *arg, int raw, int *voltage)
|
||||
//To get the reference point (Dout, Vin)
|
||||
static void get_first_step_reference_point(int version_num, adc_unit_t unit_id, adc_atten_t atten, adc_calib_info_t *calib_info)
|
||||
{
|
||||
assert(version_num == 1);
|
||||
assert(version_num == ESP_EFUSE_ADC_CALIB_VER);
|
||||
esp_err_t ret;
|
||||
|
||||
calib_info->version_num = version_num;
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
134
components/esp_adc/esp32c2/adc_cali_line_fitting.c
Normal file
134
components/esp_adc/esp32c2/adc_cali_line_fitting.c
Normal file
@ -0,0 +1,134 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "esp_types.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_efuse_rtc_calib.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
#include "adc_cali_interface.h"
|
||||
#include "inttypes.h"
|
||||
|
||||
|
||||
/**
|
||||
* This file contains Line Fitting Calibration Scheme for ESP32C2.
|
||||
*
|
||||
* If ESP EFuse Line Fitting Calibration Scheme on future chips are similar to the scheme in this file, we can:
|
||||
*
|
||||
* 1. Rename this file to `adc_cali_line_fitting_v2.c`, as the Line Fitting Scheme on ESP32 and ESP32S2 are different to this.
|
||||
* 2. Move this file to common directory
|
||||
* 3. Still support `ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED`
|
||||
* 4. Add a new internal maccro `ADC_CALI_SCHEME_LINE_FITTING_V2_SUPPORTED`
|
||||
* 5. Only build this file, when `ADC_CALI_SCHEME_LINE_FITTING_V2_SUPPORTED == true`
|
||||
*/
|
||||
|
||||
|
||||
// coeff_a is actually a float number
|
||||
// it is scaled to put them into uint32_t so that the headers do not have to be changed
|
||||
static const int coeff_a_scaling = 65536;
|
||||
const static char *TAG = "adc_cali";
|
||||
|
||||
typedef struct {
|
||||
adc_unit_t unit_id;
|
||||
adc_atten_t atten;
|
||||
uint32_t coeff_a; ///< Gradient of ADC-Voltage characteristics
|
||||
uint32_t coeff_b; ///< Offset of ADC-Voltage characteristics
|
||||
} cali_chars_line_fitting_t;
|
||||
|
||||
|
||||
/* ------------------------ Interface Functions --------------------------- */
|
||||
static esp_err_t cali_raw_to_voltage(void *arg, int raw, int *voltage);
|
||||
static esp_err_t check_valid(const adc_cali_line_fitting_config_t *config);
|
||||
|
||||
/* ------------------------- Public API ------------------------------------- */
|
||||
esp_err_t adc_cali_create_scheme_line_fitting(const adc_cali_line_fitting_config_t *config, adc_cali_handle_t *ret_handle)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
ESP_RETURN_ON_FALSE(config && ret_handle, ESP_ERR_INVALID_ARG, TAG, "invalid arg: null pointer");
|
||||
ret = check_valid(config);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
//current version only accepts encoding version: `ESP_EFUSE_ADC_CALIB_VER`.
|
||||
uint8_t adc_cali_version = esp_efuse_rtc_calib_get_ver();
|
||||
ESP_RETURN_ON_FALSE(adc_cali_version == ESP_EFUSE_ADC_CALIB_VER, ESP_ERR_NOT_SUPPORTED, TAG, "Calibration required eFuse bits not burnt");
|
||||
|
||||
adc_cali_scheme_t *scheme = (adc_cali_scheme_t *)heap_caps_calloc(1, sizeof(adc_cali_scheme_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
ESP_RETURN_ON_FALSE(scheme, ESP_ERR_NO_MEM, TAG, "no mem for adc calibration scheme");
|
||||
|
||||
cali_chars_line_fitting_t *chars = (cali_chars_line_fitting_t *)heap_caps_calloc(1, sizeof(cali_chars_line_fitting_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
ESP_GOTO_ON_FALSE(chars, ESP_ERR_NO_MEM, err, TAG, "no memory for the calibration characteristics");
|
||||
|
||||
scheme->raw_to_voltage = cali_raw_to_voltage;
|
||||
scheme->ctx = chars;
|
||||
|
||||
chars->unit_id = config->unit_id;
|
||||
chars->atten = config->atten;
|
||||
|
||||
uint32_t voltage_mv = 0;
|
||||
uint32_t digi_val = 0;
|
||||
esp_efuse_rtc_calib_get_cal_voltage(adc_cali_version, chars->unit_id, chars->atten, &digi_val, &voltage_mv);
|
||||
assert(ret == ESP_OK);
|
||||
chars->coeff_a = coeff_a_scaling * voltage_mv / digi_val;
|
||||
chars->coeff_b = 0;
|
||||
ESP_LOGV(TAG, "Calib V1, Cal Voltage = %"PRId32", Digi out = %"PRId32", Coef_a = %"PRId32"\n", voltage_mv, digi_val, chars->coeff_a);
|
||||
|
||||
*ret_handle = scheme;
|
||||
|
||||
return ESP_OK;
|
||||
|
||||
err:
|
||||
if (scheme) {
|
||||
free(scheme);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t adc_cali_delete_scheme_line_fitting(adc_cali_handle_t handle)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
|
||||
|
||||
free(handle->ctx);
|
||||
handle->ctx = NULL;
|
||||
|
||||
free(handle);
|
||||
handle = NULL;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------ Interface Functions --------------------------- */
|
||||
static esp_err_t cali_raw_to_voltage(void *arg, int raw, int *voltage)
|
||||
{
|
||||
//pointers are checked in the upper layer
|
||||
|
||||
cali_chars_line_fitting_t *ctx = arg;
|
||||
*voltage = raw * ctx->coeff_a / coeff_a_scaling + ctx->coeff_b;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t check_valid(const adc_cali_line_fitting_config_t *config)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(config->unit_id < SOC_ADC_PERIPH_NUM, ESP_ERR_INVALID_ARG, TAG, "invalid ADC unit");
|
||||
ESP_RETURN_ON_FALSE((config->atten == ADC_ATTEN_DB_0 || config->atten == ADC_ATTEN_DB_11), ESP_ERR_NOT_SUPPORTED, TAG, "only ADC_ATTEN_DB_0 and ADC_ATTEN_DB_11 are supported");
|
||||
|
||||
bool available_oneshot_bitwidth = (config->bitwidth >= SOC_ADC_RTC_MIN_BITWIDTH && config->bitwidth <= SOC_ADC_RTC_MAX_BITWIDTH);
|
||||
bool available_dma_bitwidth = (config->bitwidth >= SOC_ADC_DIGI_MIN_BITWIDTH && config->bitwidth <= SOC_ADC_DIGI_MAX_BITWIDTH);
|
||||
bool default_bitwidth_mark = (config->bitwidth == ADC_BITWIDTH_DEFAULT);
|
||||
bool available_bitwidth = (available_oneshot_bitwidth || available_dma_bitwidth || default_bitwidth_mark);
|
||||
ESP_RETURN_ON_FALSE(available_bitwidth, ESP_ERR_INVALID_ARG, TAG, "invalid bitwidth");
|
||||
|
||||
return ESP_OK;
|
||||
}
|
@ -12,4 +12,4 @@
|
||||
* @brief Supported calibration schemes
|
||||
*/
|
||||
|
||||
//No scheme supported
|
||||
#define ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED 1
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@ -132,7 +132,6 @@ TEST_CASE("ADC oneshot high/low test", "[adc_oneshot]")
|
||||
|
||||
static void s_adc_oneshot_with_sleep(adc_unit_t unit_id, adc_channel_t channel)
|
||||
{
|
||||
adc_atten_t atten[SOC_ADC_ATTEN_NUM] = {ADC_ATTEN_DB_0, ADC_ATTEN_DB_2_5, ADC_ATTEN_DB_6, ADC_ATTEN_DB_11};
|
||||
//-------------ADC Init---------------//
|
||||
adc_oneshot_unit_handle_t adc_handle;
|
||||
adc_oneshot_unit_init_cfg_t init_config = {
|
||||
@ -148,20 +147,20 @@ static void s_adc_oneshot_with_sleep(adc_unit_t unit_id, adc_channel_t channel)
|
||||
|
||||
//-------------ADC Calibration Init---------------//
|
||||
bool do_calibration = false;
|
||||
adc_cali_handle_t cali_handle[SOC_ADC_ATTEN_NUM] = {};
|
||||
for (int i = 0; i < SOC_ADC_ATTEN_NUM; i++) {
|
||||
do_calibration = test_adc_calibration_init(unit_id, i, SOC_ADC_RTC_MAX_BITWIDTH, &cali_handle[i]);
|
||||
adc_cali_handle_t cali_handle[TEST_ATTEN_NUMS] = {};
|
||||
for (int i = 0; i < TEST_ATTEN_NUMS; i++) {
|
||||
do_calibration = test_adc_calibration_init(unit_id, g_test_atten[i], SOC_ADC_RTC_MAX_BITWIDTH, &cali_handle[i]);
|
||||
}
|
||||
if (!do_calibration) {
|
||||
ESP_LOGW(TAG, "No efuse bits burnt, only test the regi2c analog register values");
|
||||
}
|
||||
|
||||
for (int i = 0; i < SOC_ADC_ATTEN_NUM; i++) {
|
||||
for (int i = 0; i < TEST_ATTEN_NUMS; i++) {
|
||||
|
||||
//-------------ADC Channel Config---------------//
|
||||
config.atten = atten[i];
|
||||
config.atten = g_test_atten[i];
|
||||
TEST_ESP_OK(adc_oneshot_config_channel(adc_handle, channel, &config));
|
||||
printf("Test with atten: %d\n", atten[i]);
|
||||
printf("Test with atten: %d\n", g_test_atten[i]);
|
||||
|
||||
//---------------------------------Before Sleep-----------------------------------//
|
||||
printf("Before Light Sleep\n");
|
||||
@ -234,7 +233,7 @@ static void s_adc_oneshot_with_sleep(adc_unit_t unit_id, adc_channel_t channel)
|
||||
|
||||
}
|
||||
TEST_ESP_OK(adc_oneshot_del_unit(adc_handle));
|
||||
for (int i = 0; i < SOC_ADC_ATTEN_NUM; i++) {
|
||||
for (int i = 0; i < TEST_ATTEN_NUMS; i++) {
|
||||
if (cali_handle[i]) {
|
||||
test_adc_calibration_deinit(cali_handle[i]);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@ -125,7 +125,6 @@ static void s_print_summary(bool figure)
|
||||
|
||||
TEST_CASE("ADC1 oneshot raw average / std_deviation", "[adc_oneshot][ignore][manual]")
|
||||
{
|
||||
adc_atten_t atten[SOC_ADC_ATTEN_NUM] = {ADC_ATTEN_DB_0, ADC_ATTEN_DB_2_5, ADC_ATTEN_DB_6, ADC_ATTEN_DB_11};
|
||||
adc_channel_t channel = TEST_STD_ADC1_CHANNEL0;
|
||||
int raw = 0;
|
||||
bool print_figure = false;
|
||||
@ -145,20 +144,20 @@ TEST_CASE("ADC1 oneshot raw average / std_deviation", "[adc_oneshot][ignore][man
|
||||
|
||||
//-------------ADC Calibration Init---------------//
|
||||
bool do_calibration = false;
|
||||
adc_cali_handle_t cali_handle[SOC_ADC_ATTEN_NUM] = {};
|
||||
for (int i = 0; i < SOC_ADC_ATTEN_NUM; i++) {
|
||||
do_calibration = test_adc_calibration_init(ADC_UNIT_1, i, ADC_BITWIDTH_DEFAULT, &cali_handle[i]);
|
||||
adc_cali_handle_t cali_handle[TEST_ATTEN_NUMS] = {};
|
||||
for (int i = 0; i < TEST_ATTEN_NUMS; i++) {
|
||||
do_calibration = test_adc_calibration_init(ADC_UNIT_1, g_test_atten[i], ADC_BITWIDTH_DEFAULT, &cali_handle[i]);
|
||||
}
|
||||
if (!do_calibration) {
|
||||
ESP_LOGW(TAG, "calibration fail, jump calibration\n");
|
||||
}
|
||||
|
||||
for (int i = 0; i < SOC_ADC_ATTEN_NUM; i++) {
|
||||
for (int i = 0; i < TEST_ATTEN_NUMS; i++) {
|
||||
|
||||
//-------------ADC1 Channel Config---------------//
|
||||
config.atten = atten[i];
|
||||
config.atten = g_test_atten[i];
|
||||
TEST_ESP_OK(adc_oneshot_config_channel(adc1_handle, channel, &config));
|
||||
ESP_LOGI("TEST_ADC", "Test with atten: %d", atten[i]);
|
||||
ESP_LOGI("TEST_ADC", "Test with atten: %d", g_test_atten[i]);
|
||||
|
||||
while (1) {
|
||||
|
||||
@ -181,7 +180,7 @@ TEST_CASE("ADC1 oneshot raw average / std_deviation", "[adc_oneshot][ignore][man
|
||||
}
|
||||
|
||||
TEST_ESP_OK(adc_oneshot_del_unit(adc1_handle));
|
||||
for (int i = 0; i < SOC_ADC_ATTEN_NUM; i++) {
|
||||
for (int i = 0; i < TEST_ATTEN_NUMS; i++) {
|
||||
if (cali_handle[i]) {
|
||||
test_adc_calibration_deinit(cali_handle[i]);
|
||||
}
|
||||
@ -192,15 +191,7 @@ TEST_CASE("ADC1 oneshot raw average / std_deviation", "[adc_oneshot][ignore][man
|
||||
/*---------------------------------------------------------------
|
||||
ADC Calibration Speed
|
||||
---------------------------------------------------------------*/
|
||||
#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
|
||||
#define CPU_FREQ_MHZ CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ
|
||||
|
||||
#define RECORD_TIME_PREPARE() uint32_t __t1, __t2
|
||||
#define RECORD_TIME_START() do {__t1 = esp_cpu_get_cycle_count();}while(0)
|
||||
@ -236,9 +227,9 @@ static void s_adc_cali_speed(adc_unit_t unit_id, adc_channel_t channel)
|
||||
{
|
||||
//-------------ADC Calibration Init---------------//
|
||||
bool do_calibration = false;
|
||||
adc_cali_handle_t cali_handle[SOC_ADC_ATTEN_NUM] = {};
|
||||
for (int i = 0; i < SOC_ADC_ATTEN_NUM; i++) {
|
||||
do_calibration = test_adc_calibration_init(unit_id, i, SOC_ADC_RTC_MAX_BITWIDTH, &cali_handle[i]);
|
||||
adc_cali_handle_t cali_handle[TEST_ATTEN_NUMS] = {};
|
||||
for (int i = 0; i < TEST_ATTEN_NUMS; i++) {
|
||||
do_calibration = test_adc_calibration_init(unit_id, g_test_atten[i], SOC_ADC_RTC_MAX_BITWIDTH, &cali_handle[i]);
|
||||
}
|
||||
|
||||
if (!do_calibration) {
|
||||
@ -246,7 +237,6 @@ static void s_adc_cali_speed(adc_unit_t unit_id, adc_channel_t channel)
|
||||
} else {
|
||||
|
||||
ESP_LOGI(TAG, "CPU FREQ is %dMHz", CPU_FREQ_MHZ);
|
||||
adc_atten_t atten[SOC_ADC_ATTEN_NUM] = {ADC_ATTEN_DB_0, ADC_ATTEN_DB_2_5, ADC_ATTEN_DB_6, ADC_ATTEN_DB_11};
|
||||
uint32_t adc_time_record[4][TIMES_PER_ATTEN] = {};
|
||||
int adc_raw = 0;
|
||||
|
||||
@ -264,12 +254,12 @@ static void s_adc_cali_speed(adc_unit_t unit_id, adc_channel_t channel)
|
||||
};
|
||||
|
||||
//atten0 ~ atten3
|
||||
for (int i = 0; i < SOC_ADC_ATTEN_NUM; i++) {
|
||||
for (int i = 0; i < TEST_ATTEN_NUMS; i++) {
|
||||
|
||||
//-------------ADC Channel Config---------------//
|
||||
config.atten = atten[i];
|
||||
config.atten = g_test_atten[i];
|
||||
TEST_ESP_OK(adc_oneshot_config_channel(adc_handle, channel, &config));
|
||||
ESP_LOGI("TEST_ADC", "Test with atten: %d", atten[i]);
|
||||
ESP_LOGI("TEST_ADC", "Test with atten: %d", g_test_atten[i]);
|
||||
|
||||
for (int j = 0; j < TIMES_PER_ATTEN; j++) {
|
||||
TEST_ESP_OK(adc_oneshot_read(adc_handle, channel, &adc_raw));
|
||||
@ -279,7 +269,7 @@ static void s_adc_cali_speed(adc_unit_t unit_id, adc_channel_t channel)
|
||||
}
|
||||
|
||||
TEST_ESP_OK(adc_oneshot_del_unit(adc_handle));
|
||||
for (int i = 0; i < SOC_ADC_ATTEN_NUM; i++) {
|
||||
for (int i = 0; i < TEST_ATTEN_NUMS; i++) {
|
||||
if (cali_handle[i]) {
|
||||
test_adc_calibration_deinit(cali_handle[i]);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
@ -14,6 +14,16 @@
|
||||
|
||||
__attribute__((unused)) static const char *TAG = "TEST_ADC";
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
ADC Attenuation
|
||||
---------------------------------------------------------------*/
|
||||
#if CONFIG_IDF_TARGET_ESP32C2
|
||||
adc_atten_t g_test_atten[TEST_ATTEN_NUMS] = {ADC_ATTEN_DB_0, ADC_ATTEN_DB_11};
|
||||
#else
|
||||
adc_atten_t g_test_atten[TEST_ATTEN_NUMS] = {ADC_ATTEN_DB_0, ADC_ATTEN_DB_2_5, ADC_ATTEN_DB_6, ADC_ATTEN_DB_11};
|
||||
#endif
|
||||
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
ADC Calibration
|
||||
---------------------------------------------------------------*/
|
||||
|
@ -26,29 +26,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
ADC Calibration
|
||||
---------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Initialise ADC Calibration
|
||||
*
|
||||
* @param[out] out_handle ADC calibration handle
|
||||
*
|
||||
* @return
|
||||
* - True Calibration success
|
||||
* - False Calibration fail
|
||||
*/
|
||||
bool test_adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_bitwidth_t bitwidth, adc_cali_handle_t *out_handle);
|
||||
|
||||
/**
|
||||
* @brief De-initialise ADC Calibration
|
||||
*
|
||||
* @param[in] handle ADC calibration handle
|
||||
*/
|
||||
void test_adc_calibration_deinit(adc_cali_handle_t handle);
|
||||
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
ADC GPIO
|
||||
ADC Level
|
||||
---------------------------------------------------------------*/
|
||||
/**
|
||||
* We use weak pulldown, `ADC_TEST_LOW_THRESH` may vary.
|
||||
@ -87,13 +65,51 @@ void test_adc_calibration_deinit(adc_cali_handle_t handle);
|
||||
#define ADC_TEST_HIGH_THRESH 0
|
||||
|
||||
#elif CONFIG_IDF_TARGET_ESP32C2
|
||||
#define ADC_TEST_LOW_VAL 2147
|
||||
#define ADC_TEST_LOW_THRESH 100
|
||||
#define ADC_TEST_LOW_VAL 0
|
||||
#define ADC_TEST_LOW_THRESH 15
|
||||
|
||||
#define ADC_TEST_HIGH_VAL 4095
|
||||
#define ADC_TEST_HIGH_THRESH 0
|
||||
#define ADC_TEST_HIGH_VAL 3400
|
||||
#define ADC_TEST_HIGH_THRESH 200
|
||||
#endif
|
||||
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
ADC Attenuation
|
||||
---------------------------------------------------------------*/
|
||||
#if CONFIG_IDF_TARGET_ESP32C2
|
||||
#define TEST_ATTEN_NUMS 2
|
||||
extern adc_atten_t g_test_atten[TEST_ATTEN_NUMS];
|
||||
#else
|
||||
#define TEST_ATTEN_NUMS 4
|
||||
extern adc_atten_t g_test_atten[TEST_ATTEN_NUMS];
|
||||
#endif
|
||||
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
ADC Calibration
|
||||
---------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Initialise ADC Calibration
|
||||
*
|
||||
* @param[out] out_handle ADC calibration handle
|
||||
*
|
||||
* @return
|
||||
* - True Calibration success
|
||||
* - False Calibration fail
|
||||
*/
|
||||
bool test_adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_bitwidth_t bitwidth, adc_cali_handle_t *out_handle);
|
||||
|
||||
/**
|
||||
* @brief De-initialise ADC Calibration
|
||||
*
|
||||
* @param[in] handle ADC calibration handle
|
||||
*/
|
||||
void test_adc_calibration_deinit(adc_cali_handle_t handle);
|
||||
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
ADC GPIO
|
||||
---------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Set ADC IO level
|
||||
*
|
||||
|
@ -28,7 +28,7 @@ static const char *TAG = "rtc_clk";
|
||||
// Current PLL frequency, in 480MHZ. Zero if PLL is not enabled.
|
||||
static int s_cur_pll_freq;
|
||||
|
||||
static void rtc_clk_cpu_freq_to_xtal(int freq, int div);
|
||||
void rtc_clk_cpu_freq_to_xtal(int freq, int div);
|
||||
static void rtc_clk_cpu_freq_to_8m(void);
|
||||
|
||||
void rtc_clk_32k_enable_external(void)
|
||||
@ -279,7 +279,7 @@ void rtc_clk_cpu_freq_set_xtal(void)
|
||||
/**
|
||||
* Switch to XTAL frequency. Does not disable the PLL.
|
||||
*/
|
||||
static void rtc_clk_cpu_freq_to_xtal(int freq, int div)
|
||||
void rtc_clk_cpu_freq_to_xtal(int freq, int div)
|
||||
{
|
||||
ets_update_cpu_frequency(freq);
|
||||
/* Set divider from XTAL to APB clock. Need to set divider to 1 (reg. value 0) first. */
|
||||
|
@ -92,7 +92,9 @@ void adc_hal_calibration_init(adc_unit_t adc_n)
|
||||
adc_ll_calibration_init(adc_n);
|
||||
}
|
||||
|
||||
static uint32_t s_previous_init_code[SOC_ADC_PERIPH_NUM] = {-1, -1};
|
||||
static uint32_t s_previous_init_code[SOC_ADC_PERIPH_NUM] = {
|
||||
[0 ... (SOC_ADC_PERIPH_NUM - 1)] = -1,
|
||||
};
|
||||
|
||||
void adc_hal_set_calibration_param(adc_unit_t adc_n, uint32_t param)
|
||||
{
|
||||
@ -145,10 +147,12 @@ static uint32_t read_cal_channel(adc_unit_t adc_n)
|
||||
|
||||
uint32_t adc_hal_self_calibration(adc_unit_t adc_n, adc_atten_t atten, bool internal_gnd)
|
||||
{
|
||||
#if SOC_ADC_ARBITER_SUPPORTED
|
||||
if (adc_n == ADC_UNIT_2) {
|
||||
adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
|
||||
adc_hal_arbiter_config(&config);
|
||||
}
|
||||
#endif // #if SOC_ADC_ARBITER_SUPPORTED
|
||||
|
||||
cal_setup(adc_n, atten);
|
||||
|
||||
|
@ -133,7 +133,6 @@ static inline void adc_ll_digi_output_invert(adc_unit_t adc_n, bool inv_en)
|
||||
*/
|
||||
static inline void adc_ll_digi_controller_clk_div(uint32_t div_num, uint32_t div_b, uint32_t div_a)
|
||||
{
|
||||
abort(); //TODO IDF-3908
|
||||
HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.saradc_apb_adc_clkm_conf, saradc_reg_clkm_div_num, div_num);
|
||||
APB_SARADC.saradc_apb_adc_clkm_conf.saradc_reg_clkm_div_b = div_b;
|
||||
APB_SARADC.saradc_apb_adc_clkm_conf.saradc_reg_clkm_div_a = div_a;
|
||||
@ -316,12 +315,8 @@ static inline void adc_ll_set_controller(adc_unit_t adc_n, adc_ll_controller_t c
|
||||
__attribute__((always_inline))
|
||||
static inline void adc_ll_calibration_init(adc_unit_t adc_n)
|
||||
{
|
||||
abort(); //TODO IDF-3908
|
||||
// if (adc_n == ADC_UNIT_1) {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_DREF_ADDR, 1);
|
||||
// } else {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_DREF_ADDR, 1);
|
||||
// }
|
||||
(void)adc_n;
|
||||
REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_DREF_ADDR, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -330,27 +325,18 @@ static inline void adc_ll_calibration_init(adc_unit_t adc_n)
|
||||
* @note Different ADC units and different attenuation options use different calibration data (initial data).
|
||||
*
|
||||
* @param adc_n ADC index number.
|
||||
* @param channel adc channel number.
|
||||
* @param internal_gnd true: Disconnect from the IO port and use the internal GND as the calibration voltage.
|
||||
* false: Use IO external voltage as calibration voltage.
|
||||
*/
|
||||
static inline void adc_ll_calibration_prepare(adc_unit_t adc_n, adc_channel_t channel, bool internal_gnd)
|
||||
static inline void adc_ll_calibration_prepare(adc_unit_t adc_n, bool internal_gnd)
|
||||
{
|
||||
abort(); //TODO IDF-3908
|
||||
// /* Enable/disable internal connect GND (for calibration). */
|
||||
// if (adc_n == ADC_UNIT_1) {
|
||||
// if (internal_gnd) {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 1);
|
||||
// } else {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
|
||||
// }
|
||||
// } else {
|
||||
// if (internal_gnd) {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 1);
|
||||
// } else {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 0);
|
||||
// }
|
||||
// }
|
||||
(void)adc_n;
|
||||
/* Enable/disable internal connect GND (for calibration). */
|
||||
if (internal_gnd) {
|
||||
REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 1);
|
||||
} else {
|
||||
REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -360,12 +346,8 @@ static inline void adc_ll_calibration_prepare(adc_unit_t adc_n, adc_channel_t ch
|
||||
*/
|
||||
static inline void adc_ll_calibration_finish(adc_unit_t adc_n)
|
||||
{
|
||||
abort(); //TODO IDF-3908
|
||||
// if (adc_n == ADC_UNIT_1) {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
|
||||
// } else {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 0);
|
||||
// }
|
||||
(void)adc_n;
|
||||
REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -378,16 +360,11 @@ static inline void adc_ll_calibration_finish(adc_unit_t adc_n)
|
||||
__attribute__((always_inline))
|
||||
static inline void adc_ll_set_calibration_param(adc_unit_t adc_n, uint32_t param)
|
||||
{
|
||||
abort(); //TODO IDF-3908
|
||||
// uint8_t msb = param >> 8;
|
||||
// uint8_t lsb = param & 0xFF;
|
||||
// if (adc_n == ADC_UNIT_1) {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_HIGH_ADDR, msb);
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_LOW_ADDR, lsb);
|
||||
// } else {
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_HIGH_ADDR, msb);
|
||||
// REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_LOW_ADDR, lsb);
|
||||
// }
|
||||
(void)adc_n;
|
||||
uint8_t msb = param >> 8;
|
||||
uint8_t lsb = param & 0xFF;
|
||||
REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_HIGH_ADDR, msb);
|
||||
REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_LOW_ADDR, lsb);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------
|
||||
|
@ -139,6 +139,10 @@ config SOC_RTC_SLOW_CLOCK_SUPPORT_8MD256
|
||||
bool
|
||||
default y
|
||||
|
||||
config SOC_ADC_CALIBRATION_V1_SUPPORTED
|
||||
bool
|
||||
default y
|
||||
|
||||
config SOC_BROWNOUT_RESET_SUPPORTED
|
||||
bool
|
||||
default y
|
||||
|
@ -72,6 +72,9 @@
|
||||
#define SOC_ADC_RTC_MAX_BITWIDTH (12)
|
||||
#define SOC_RTC_SLOW_CLOCK_SUPPORT_8MD256 (1)
|
||||
|
||||
/*!< Calibration */
|
||||
#define SOC_ADC_CALIBRATION_V1_SUPPORTED (1) /*!< support HW offset calibration version 1*/
|
||||
|
||||
/*-------------------------- BROWNOUT CAPS -----------------------------------*/
|
||||
#define SOC_BROWNOUT_RESET_SUPPORTED 1
|
||||
|
||||
|
@ -203,7 +203,7 @@ To do further calbration to convert the ADC raw result to voltage in mV, please
|
||||
Hardware Limitations
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
- A specific ADC unit can only work under one operating mode at any one time, either Continuous Mode or Oneshot Mode. :cpp:func:`adc_continuous_start` has provided the protection.
|
||||
- A specific ADC unit can only work under one operating mode at any one time, either continuous mode or oneshot mode. :cpp:func:`adc_continuous_start` has provided the protection.
|
||||
|
||||
- Random Number Generator uses ADC as an input source. When ADC continuous mode driver works, the random number generated from RNG will be less random.
|
||||
|
||||
|
@ -154,11 +154,11 @@ Hardware Limitations
|
||||
|
||||
.. only:: SOC_ADC_DMA_SUPPORTED
|
||||
|
||||
- A specific ADC unit can only work under one operating mode at any one time, either Continuous Mode or Oneshot Mode. :cpp:func:`adc_oneshot_read` has provided the protection.
|
||||
- A specific ADC unit can only work under one operating mode at any one time, either continuous mode or oneshot mode. :cpp:func:`adc_oneshot_read` has provided the protection.
|
||||
|
||||
.. only:: esp32 or esp32s2 or esp32s3
|
||||
|
||||
- ADC2 is also used by the Wi-Fi. :cpp:func:`adc_oneshot_read` has provided the protection between Wi-Fi driver and ADC continuous mode driver.
|
||||
- ADC2 is also used by the Wi-Fi. :cpp:func:`adc_oneshot_read` has provided the protection between Wi-Fi driver and ADC oneshot mode driver.
|
||||
|
||||
.. only:: esp32c3
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@ -45,6 +45,8 @@ const static char *TAG = "EXAMPLE";
|
||||
#endif
|
||||
#endif //#if EXAMPLE_USE_ADC2
|
||||
|
||||
#define EXAMPLE_ADC_ATTEN ADC_ATTEN_DB_11
|
||||
|
||||
static int adc_raw[2][10];
|
||||
static int voltage[2][10];
|
||||
static bool example_adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle);
|
||||
@ -63,14 +65,14 @@ void app_main(void)
|
||||
//-------------ADC1 Config---------------//
|
||||
adc_oneshot_chan_cfg_t config = {
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
.atten = ADC_ATTEN_DB_11,
|
||||
.atten = EXAMPLE_ADC_ATTEN,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, EXAMPLE_ADC1_CHAN0, &config));
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, EXAMPLE_ADC1_CHAN1, &config));
|
||||
|
||||
//-------------ADC1 Calibration Init---------------//
|
||||
adc_cali_handle_t adc1_cali_handle = NULL;
|
||||
bool do_calibration1 = example_adc_calibration_init(ADC_UNIT_1, ADC_ATTEN_DB_11, &adc1_cali_handle);
|
||||
bool do_calibration1 = example_adc_calibration_init(ADC_UNIT_1, EXAMPLE_ADC_ATTEN, &adc1_cali_handle);
|
||||
|
||||
|
||||
#if EXAMPLE_USE_ADC2
|
||||
@ -84,7 +86,7 @@ void app_main(void)
|
||||
|
||||
//-------------ADC2 Calibration Init---------------//
|
||||
adc_cali_handle_t adc2_cali_handle = NULL;
|
||||
bool do_calibration2 = example_adc_calibration_init(ADC_UNIT_2, ADC_ATTEN_DB_11, &adc2_cali_handle);
|
||||
bool do_calibration2 = example_adc_calibration_init(ADC_UNIT_2, EXAMPLE_ADC_ATTEN, &adc2_cali_handle);
|
||||
|
||||
//-------------ADC2 Config---------------//
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc2_handle, EXAMPLE_ADC2_CHAN0, &config));
|
||||
|
Loading…
Reference in New Issue
Block a user