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  1 // Currently H2 has one 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 ~ 2, error = (K0 * X^0) + (K1 * X^1)
24  * @note For atten3, error = (K0 * X^0) + (K1 * X^1) + (K2 * X^2)
25  * @note Above formula is rewritten from the original documentation, please note that the coefficients are re-ordered.
26  */
27 const static uint64_t adc1_error_coef_atten[COEFF_VERSION_NUM][COEFF_GROUP_NUM][TERM_MAX][2] = {
28     /* Coefficients of calibration version 1 */
29     {
30         {{5081991760658888,     1e16}, {7858995318513,    1e19}, {0, 1},                {0, 0}, {0, 0}},    //atten0
31         {{8359230818901277,     1e16}, {9025419089179,    1e19}, {0, 1},                {0, 0}, {0, 0}},    //atten1
32         {{1165668771581976,     1e15}, {8294679249061,    1e19}, {0, 1},                {0, 0}, {0, 0}},    //atten2
33         {{3637329628677273,     1e16}, {19607259738935,   1e18}, {7871689227,   1e16},  {0, 0}, {0, 0}},    //atten3
34     },
35 };
36 
37 /**
38  * Term sign
39  */
40 const static int32_t adc1_error_sign[COEFF_VERSION_NUM][COEFF_GROUP_NUM][TERM_MAX] = {
41     /* Coefficient sign of calibration version 1 */
42     {
43         {-1,  1,  1, 0, 0}, //atten0
44         {-1,  1,  1, 0, 0}, //atten1
45         {-1,  1,  1, 0, 0}, //atten2
46         {-1, -1,  1, 0, 0}, //atten3
47     },
48 };
49 
curve_fitting_get_second_step_coeff(const adc_cali_curve_fitting_config_t * config,cali_chars_second_step_t * ctx)50 void curve_fitting_get_second_step_coeff(const adc_cali_curve_fitting_config_t *config, cali_chars_second_step_t *ctx)
51 {
52     uint32_t adc_calib_ver = esp_efuse_rtc_calib_get_ver();
53     assert((adc_calib_ver >= ESP_EFUSE_ADC_CALIB_VER_MIN) &&
54            (adc_calib_ver <= ESP_EFUSE_ADC_CALIB_VER_MAX));
55 
56     ctx->term_num = 3;
57     ctx->coeff = &adc1_error_coef_atten[VER2IDX(adc_calib_ver)];
58     ctx->sign = &adc1_error_sign[VER2IDX(adc_calib_ver)];
59 }
60