esp_adc: replace hardcoded calibration version to a macro

On esp32c3 and esp32s3, we support calibration version 1. This commit
replace it with ESP_EFUSE_ADC_CALIB_VER to avoid hardcode.
This commit is contained in:
Armando 2022-09-08 19:18:33 +08:00
parent fe0643ca40
commit a03ca90d4a
3 changed files with 21 additions and 13 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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;