1 /*
2  * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <esp_bit_defs.h>
8 #include "esp_err.h"
9 #include "esp_log.h"
10 #include "esp_efuse.h"
11 #include "esp_efuse_table.h"
12 
13 //Don't introduce new dependency of ADC, keep these macro same as ADC related definations
14 #define ADC_ATTEN_MAX    4
15 #define ADC_NUM_MAX      2
16 #define ADC_NUM_1        0
17 #define ADC_NUM_2        1
18 
19 
esp_efuse_rtc_calib_get_ver(void)20 int esp_efuse_rtc_calib_get_ver(void)
21 {
22     uint32_t blk_ver_major = 0;
23     esp_efuse_read_field_blob(ESP_EFUSE_BLK_VERSION_MAJOR, &blk_ver_major, ESP_EFUSE_BLK_VERSION_MAJOR[0]->bit_count); // IDF-5366
24 
25     uint32_t cali_version_v1 = (blk_ver_major == 1) ? 1 : 0;
26     if (!cali_version_v1) {
27         ESP_LOGW("eFuse", "calibration efuse version does not match, set default version: %d", 0);
28     }
29 
30     return cali_version_v1;
31 }
32 
esp_efuse_rtc_calib_get_init_code(int version,uint32_t adc_unit,int atten)33 uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten)
34 {
35     assert(version == 1);
36     assert(atten < 4);
37     assert(adc_unit < ADC_NUM_MAX);
38 
39     const esp_efuse_desc_t **desc[8] = {ESP_EFUSE_ADC1_INIT_CODE_ATTEN0, ESP_EFUSE_ADC1_INIT_CODE_ATTEN1, ESP_EFUSE_ADC1_INIT_CODE_ATTEN2, ESP_EFUSE_ADC1_INIT_CODE_ATTEN3,
40                                         ESP_EFUSE_ADC2_INIT_CODE_ATTEN0, ESP_EFUSE_ADC2_INIT_CODE_ATTEN1, ESP_EFUSE_ADC2_INIT_CODE_ATTEN2, ESP_EFUSE_ADC2_INIT_CODE_ATTEN3};
41     int efuse_icode_bits = 0;
42     uint32_t adc_icode[4] = {};
43     uint32_t adc_icode_diff[4] = {};
44     uint8_t desc_index = (adc_unit == ADC_NUM_1) ? 0 : 4;
45 
46     for (int diff_index = 0; diff_index < 4; diff_index++) {
47         efuse_icode_bits = esp_efuse_get_field_size(desc[desc_index]);
48         esp_efuse_read_field_blob(desc[desc_index], &adc_icode_diff[diff_index], efuse_icode_bits);
49         desc_index++;
50     }
51 
52     //Version 1 logic for calculating ADC ICode based on EFUSE burnt value
53     if (adc_unit == ADC_NUM_1) {
54         adc_icode[0] = adc_icode_diff[0] + 1850;
55         adc_icode[1] = adc_icode_diff[1] + adc_icode[0] + 90;
56         adc_icode[2] = adc_icode_diff[2] + adc_icode[1];
57         adc_icode[3] = adc_icode_diff[3] + adc_icode[2] + 70;
58     } else {
59         adc_icode[0] = adc_icode_diff[0] + 2020;
60         adc_icode[1] = adc_icode_diff[1] + adc_icode[0];
61         adc_icode[2] = adc_icode_diff[2] + adc_icode[1];
62         adc_icode[3] = adc_icode_diff[3] + adc_icode[2];
63     }
64 
65     return adc_icode[atten];
66 }
67 
esp_efuse_rtc_calib_get_cal_voltage(int version,uint32_t adc_unit,int atten,uint32_t * out_digi,uint32_t * out_vol_mv)68 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)
69 {
70     assert(version == 1);
71     assert(atten < 4);
72     assert(adc_unit < ADC_NUM_MAX);
73 
74     int efuse_vol_bits = 0;
75     uint32_t adc_vol_diff[8] = {};
76     uint32_t adc1_vol[4] = {};
77     uint32_t adc2_vol[4] = {};
78     const esp_efuse_desc_t **desc[8] = {ESP_EFUSE_ADC1_CAL_VOL_ATTEN0, ESP_EFUSE_ADC1_CAL_VOL_ATTEN1, ESP_EFUSE_ADC1_CAL_VOL_ATTEN2, ESP_EFUSE_ADC1_CAL_VOL_ATTEN3,
79                                         ESP_EFUSE_ADC2_CAL_VOL_ATTEN0, ESP_EFUSE_ADC2_CAL_VOL_ATTEN1, ESP_EFUSE_ADC2_CAL_VOL_ATTEN2, ESP_EFUSE_ADC2_CAL_VOL_ATTEN3};
80     for (int i = 0; i < 8; i++) {
81         efuse_vol_bits = esp_efuse_get_field_size(desc[i]);
82         esp_efuse_read_field_blob(desc[i], &adc_vol_diff[i], efuse_vol_bits);
83     }
84 
85     adc1_vol[3] = adc_vol_diff[3] + 900;
86     adc1_vol[2] = adc_vol_diff[2] + adc1_vol[3] + 800;
87     adc1_vol[1] = adc_vol_diff[1] + adc1_vol[2] + 700;
88     adc1_vol[0] = adc_vol_diff[0] + adc1_vol[1] + 800;
89     adc2_vol[3] = adc1_vol[3] - adc_vol_diff[7] + 15;
90     adc2_vol[2] = adc1_vol[2] - adc_vol_diff[6] + 20;
91     adc2_vol[1] = adc1_vol[1] - adc_vol_diff[5] + 10;
92     adc2_vol[0] = adc1_vol[0] - adc_vol_diff[4] + 40;
93 
94     *out_digi = (adc_unit == ADC_NUM_1) ? adc1_vol[atten] : adc2_vol[atten];
95     *out_vol_mv = 850;
96 
97     return ESP_OK;
98 }
99 
esp_efuse_rtc_calib_get_cal_temp(int version)100 float esp_efuse_rtc_calib_get_cal_temp(int version)
101 {
102     assert(version == 1);
103     const esp_efuse_desc_t** cal_temp_efuse;
104     cal_temp_efuse = ESP_EFUSE_TEMP_CALIB;
105     int cal_temp_size = esp_efuse_get_field_size(cal_temp_efuse);
106     assert(cal_temp_size == 9);
107 
108     uint32_t cal_temp = 0;
109     esp_err_t err = esp_efuse_read_field_blob(cal_temp_efuse, &cal_temp, cal_temp_size);
110     assert(err == ESP_OK);
111     (void)err;
112     // BIT(8) stands for sign: 1: negtive, 0: positive
113     return ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
114 }
115