1 /*
2  * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <esp_bit_defs.h>
8 #include "esp_efuse.h"
9 #include "esp_efuse_table.h"
10 #include "esp_efuse_rtc_calib.h"
11 #include "hal/efuse_hal.h"
12 
13 /**
14  * @brief Get the signed value by the raw data that read from eFuse
15  * @param data  The raw data that read from eFuse
16  * @param sign_bit  The index of the sign bit, start from 0
17  */
18 #define RTC_CALIB_GET_SIGNED_VAL(data, sign_bit)    ((data & BIT##sign_bit) ? -(int)(data & ~BIT##sign_bit) : (int)data)
19 
esp_efuse_rtc_calib_get_ver(void)20 int esp_efuse_rtc_calib_get_ver(void)
21 {
22     uint32_t cali_version = 0;
23     uint32_t blk_ver = efuse_hal_blk_version();
24     if (blk_ver == 1) {
25         cali_version = ESP_EFUSE_ADC_CALIB_VER1;
26     } else if (blk_ver >= 2) {
27         cali_version = ESP_EFUSE_ADC_CALIB_VER2;
28     } else {
29         ESP_LOGW("eFuse", "calibration efuse version does not match, set default version to 0");
30     }
31 
32     return cali_version;
33 }
34 
esp_efuse_rtc_calib_get_init_code(int version,uint32_t adc_unit,int atten)35 uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten)
36 {
37     /* Version validation should be guaranteed in the caller */
38     assert(atten >=0 && atten < 4);
39     (void) adc_unit;
40 
41     const esp_efuse_desc_t** init_code_efuse;
42     if (atten == 0) {
43         init_code_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN0;
44     } else if (atten == 1) {
45         init_code_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN1;
46     } else if (atten == 2) {
47         init_code_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN2;
48     } else {
49         init_code_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN3;
50     }
51 
52     int init_code_size = esp_efuse_get_field_size(init_code_efuse);
53     assert(init_code_size == 10);
54 
55     uint32_t init_code = 0;
56     ESP_ERROR_CHECK(esp_efuse_read_field_blob(init_code_efuse, &init_code, init_code_size));
57     return init_code + 1600;    // version 1 logic
58 }
59 
esp_efuse_rtc_calib_get_chan_compens(int version,uint32_t adc_unit,uint32_t adc_channel,int atten)60 int esp_efuse_rtc_calib_get_chan_compens(int version, uint32_t adc_unit, uint32_t adc_channel, int atten)
61 {
62     /* Version validation should be guaranteed in the caller */
63     assert(atten < 4);
64     assert(adc_channel < SOC_ADC_CHANNEL_NUM(adc_unit));
65 
66     const esp_efuse_desc_t** chan_diff_efuse = NULL;
67     switch (adc_channel) {
68         case 0:
69             chan_diff_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN0_CH0;
70             break;
71         case 1:
72             chan_diff_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN0_CH1;
73             break;
74         case 2:
75             chan_diff_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN0_CH2;
76             break;
77         case 3:
78             chan_diff_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN0_CH3;
79             break;
80         case 4:
81             chan_diff_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN0_CH4;
82             break;
83         case 5:
84             chan_diff_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN0_CH5;
85             break;
86         default:
87             chan_diff_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN0_CH6;
88             break;
89     }
90 
91     int chan_diff_size = esp_efuse_get_field_size(chan_diff_efuse);
92     assert(chan_diff_size == 4);
93     uint32_t chan_diff = 0;
94     ESP_ERROR_CHECK(esp_efuse_read_field_blob(chan_diff_efuse, &chan_diff, chan_diff_size));
95     return RTC_CALIB_GET_SIGNED_VAL(chan_diff, 3) * (4 - atten);
96 }
97 
esp_efuse_rtc_calib_get_cal_voltage(int version,uint32_t adc_unit,int atten,uint32_t * out_digi,uint32_t * out_vol_mv)98 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)
99 {
100     (void) adc_unit;
101     const esp_efuse_desc_t** cal_vol_efuse[4] = {
102         ESP_EFUSE_ADC1_CAL_VOL_ATTEN0,
103         ESP_EFUSE_ADC1_CAL_VOL_ATTEN1,
104         ESP_EFUSE_ADC1_CAL_VOL_ATTEN2,
105         ESP_EFUSE_ADC1_CAL_VOL_ATTEN3,
106     };
107     const uint32_t input_vout_mv[2][4] = {
108         {400,  550,  750, 1370}, // Calibration V1 coefficients
109         {750, 1000, 1500, 2800}, // Calibration V2 coefficients
110     };
111 
112     if ((version < ESP_EFUSE_ADC_CALIB_VER_MIN) ||
113         (version > ESP_EFUSE_ADC_CALIB_VER_MAX)) {
114         return ESP_ERR_INVALID_ARG;
115     }
116     if (atten >= 4 || atten < 0) {
117         return ESP_ERR_INVALID_ARG;
118     }
119 
120     assert(cal_vol_efuse[atten][0]->bit_count == 10);
121 
122     uint32_t cal_vol = 0;
123     esp_err_t ret = esp_efuse_read_field_blob(cal_vol_efuse[atten], &cal_vol, cal_vol_efuse[atten][0]->bit_count);
124     if (ret != ESP_OK) {
125         return ret;
126     }
127     uint32_t chk_offset = (version == ESP_EFUSE_ADC_CALIB_VER1) ? 1500 : (atten == 2) ? 2900 : 2850;
128     *out_digi = chk_offset + RTC_CALIB_GET_SIGNED_VAL(cal_vol, 9);
129     *out_vol_mv = input_vout_mv[VER2IDX(version)][atten];
130     return ESP_OK;
131 }
132 
esp_efuse_rtc_calib_get_tsens_val(float * tsens_cal)133 esp_err_t esp_efuse_rtc_calib_get_tsens_val(float* tsens_cal)
134 {
135     // Currently calibration is not supported on ESP32-C6, IDF-5236
136     *tsens_cal = 0;
137     return ESP_OK;
138 }
139