1 /*
2  * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <stdint.h>
9 #include "esp_efuse_rtc_calib.h"
10 #include "../curve_fitting_coefficients.h"
11 
12 #define COEFF_VERSION_NUM  2 // Currently C6 has two versions of curve calibration schemes
13 
14 /**
15  * @note Error Calculation
16  * Coefficients for calculating the reading voltage error.
17  * Four sets of coefficients for atten0 ~ atten3 respectively.
18  *
19  * For each item, first element is the Coefficient, second element is the Multiple. (Coefficient / Multiple) is the real coefficient.
20  *
21  * @note {0,0} stands for unused item
22  * @note In case of the overflow, these coefficients are recorded as Absolute Value
23  * @note For atten0 ~ 3, error = (K0 * X^0) + (K1 * X^1) + (K2 * X^2)
24  * @note Above formula is rewritten from the original documentation, please note that the coefficients are re-ordered.
25  */
26 const static uint64_t adc1_error_coef_atten[COEFF_VERSION_NUM][COEFF_GROUP_NUM][TERM_MAX][2] = {
27     /* Coefficients of calibration version 1 */
28     {
29         {{487166399931449,   1e15}, {6436483033201,   1e16}, {30410131806, 1e16}, {0, 0}, {0, 0}},   //atten0
30         {{8665498165817785,  1e16}, {15239070452946,  1e16}, {13818878844, 1e16}, {0, 0}, {0, 0}},   //atten1
31         {{12277821756674387, 1e16}, {22275554717885,  1e16}, {5924302667,  1e16}, {0, 0}, {0, 0}},   //atten2
32         {{3801417550380255,  1e16}, {6020352420772,   1e16}, {12442478488, 1e16}, {0, 0}, {0, 0}},   //atten3
33     },
34     /* Coefficients of calibration version 2 */
35     {
36         {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},                                                    //atten0
37         {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}},                                                    //atten1
38         {{12217864764388775, 1e16}, {1954123107752,   1e16}, {6409679727,  1e16}, {0, 0}, {0, 0}},   //atten2
39         {{3915910437042445 , 1e16}, {31536470857564,  1e16}, {12493873014, 1e16}, {0, 0}, {0, 0}},   //atten3
40     },
41 };
42 
43 /**
44  * Term sign
45  */
46 const static int32_t adc1_error_sign[COEFF_VERSION_NUM][COEFF_GROUP_NUM][TERM_MAX] = {
47     /* Coefficient sign of calibration version 1 */
48     {
49         {-1,  1,  1, 0, 0}, //atten0
50         {-1,  1,  1, 0, 0}, //atten1
51         {-1,  1,  1, 0, 0}, //atten2
52         {-1, -1,  1, 0, 0}, //atten3
53     },
54     /* Coefficient sign of calibration version 2 */
55     {
56         { 0,  0,  0, 0, 0}, //atten0
57         { 0,  0,  0, 0, 0}, //atten1
58         {-1, -1,  1, 0, 0}, //atten2
59         {-1, -1,  1, 0, 0}, //atten3
60     },
61 };
62 
63 
curve_fitting_get_second_step_coeff(const adc_cali_curve_fitting_config_t * config,cali_chars_second_step_t * ctx)64 void curve_fitting_get_second_step_coeff(const adc_cali_curve_fitting_config_t *config, cali_chars_second_step_t *ctx)
65 {
66     uint32_t adc_calib_ver = esp_efuse_rtc_calib_get_ver();
67     assert((adc_calib_ver >= ESP_EFUSE_ADC_CALIB_VER_MIN) &&
68            (adc_calib_ver <= ESP_EFUSE_ADC_CALIB_VER_MAX));
69     ctx->term_num = 3;
70     ctx->coeff = &adc1_error_coef_atten[VER2IDX(adc_calib_ver)];
71     ctx->sign = &adc1_error_sign[VER2IDX(adc_calib_ver)];
72 }
73