1 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <esp_bit_defs.h>
16 #include "esp_efuse.h"
17 #include "esp_efuse_table.h"
18 
esp_efuse_rtc_calib_get_ver(void)19 int esp_efuse_rtc_calib_get_ver(void)
20 {
21     uint32_t result = 0;
22     esp_efuse_read_field_blob(ESP_EFUSE_BLOCK2_VERSION, &result, 3);
23     return result;
24 }
25 
esp_efuse_rtc_calib_get_init_code(int version,int atten)26 uint16_t esp_efuse_rtc_calib_get_init_code(int version, int atten)
27 {
28     assert(version == 1);
29     const esp_efuse_desc_t** init_code_efuse;
30     assert(atten < 4);
31     if (atten == 0) {
32         init_code_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN0;
33     } else if (atten == 1) {
34         init_code_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN1;
35     } else if (atten == 2) {
36         init_code_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN2;
37     } else {
38         init_code_efuse = ESP_EFUSE_ADC1_INIT_CODE_ATTEN3;
39     }
40 
41     int init_code_size = esp_efuse_get_field_size(init_code_efuse);
42     assert(init_code_size == 10);
43 
44     uint32_t init_code = 0;
45     esp_err_t err = esp_efuse_read_field_blob(init_code_efuse, &init_code, init_code_size);
46     assert(err == ESP_OK);
47     return init_code + 1000;    // version 1 logic
48 }
49 
esp_efuse_rtc_calib_get_cal_voltage(int version,int atten,uint32_t * out_digi,uint32_t * out_vol_mv)50 esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, int atten, uint32_t* out_digi, uint32_t* out_vol_mv)
51 {
52     const esp_efuse_desc_t** cal_vol_efuse;
53     uint32_t calib_vol_expected_mv;
54     if (version != 1) {
55         return ESP_ERR_INVALID_ARG;
56     }
57     if (atten >= 4) {
58         return ESP_ERR_INVALID_ARG;
59     }
60     if (atten == 0) {
61         cal_vol_efuse = ESP_EFUSE_ADC1_CAL_VOL_ATTEN0;
62         calib_vol_expected_mv = 400;
63     } else if (atten == 1) {
64         cal_vol_efuse = ESP_EFUSE_ADC1_CAL_VOL_ATTEN1;
65         calib_vol_expected_mv = 550;
66     } else if (atten == 2) {
67         cal_vol_efuse = ESP_EFUSE_ADC1_CAL_VOL_ATTEN2;
68         calib_vol_expected_mv = 750;
69     } else {
70         cal_vol_efuse = ESP_EFUSE_ADC1_CAL_VOL_ATTEN3;
71         calib_vol_expected_mv = 1370;
72     }
73 
74     int cal_vol_size = esp_efuse_get_field_size(cal_vol_efuse);
75     assert(cal_vol_size == 10);
76 
77     uint32_t cal_vol = 0;
78     esp_err_t err = esp_efuse_read_field_blob(cal_vol_efuse, &cal_vol, cal_vol_size) & 0x3FF;
79     assert(err == ESP_OK);
80 
81     *out_digi = 2000 + ((cal_vol & BIT(9))? -(cal_vol & ~BIT9): cal_vol);
82     *out_vol_mv = calib_vol_expected_mv;
83     return ESP_OK;
84 }
85 
esp_efuse_rtc_calib_get_cal_temp(int version)86 float esp_efuse_rtc_calib_get_cal_temp(int version)
87 {
88     assert(version == 1);
89     const esp_efuse_desc_t** cal_temp_efuse;
90     cal_temp_efuse = ESP_EFUSE_TEMP_CALIB;
91     int cal_temp_size = esp_efuse_get_field_size(cal_temp_efuse);
92     assert(cal_temp_size == 9);
93 
94     uint32_t cal_temp = 0;
95     esp_err_t err = esp_efuse_read_field_blob(cal_temp_efuse, &cal_temp, cal_temp_size);
96     assert(err == ESP_OK);
97     // BIT(8) stands for sign: 1: negtive, 0: positive
98     return ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp;
99 }
100