1 /*
2  * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include "../curve_fitting_coefficients.h"
9 
10 /**
11  * @note Error Calculation
12  * Coefficients for calculating the reading voltage error.
13  * Four sets of coefficients for atten0 ~ atten3 respectively.
14  *
15  * For each item, first element is the Coefficient, second element is the Multiple. (Coefficient / Multiple) is the real coefficient.
16  *
17  * @note {0,0} stands for unused item
18  * @note In case of the overflow, these coefficients are recorded as Absolute Value
19  * @note For atten0 ~ 2, error = (K0 * X^0) + (K1 * X^1) + (K2 * X^2); For atten3, error = (K0 * X^0) + (K1 * X^1)  + (K2 * X^2) + (K3 * X^3) + (K4 * X^4);
20  * @note Above formula is rewritten from the original documentation, please note that the coefficients are re-ordered.
21  */
22 const static uint64_t adc1_error_coef_atten[COEFF_GROUP_NUM][TERM_MAX][2] = {
23                                                     {{27856531419538344, 1e16}, {50871540569528, 1e16}, {9798249589, 1e15}, {0, 0}, {0, 0}},                       //ADC1 atten0
24                                                     {{29831022915028695, 1e16}, {49393185868806, 1e16}, {101379430548, 1e16}, {0, 0}, {0, 0}},                     //ADC1 atten1
25                                                     {{23285545746296417, 1e16}, {147640181047414, 1e16}, {208385525314, 1e16}, {0, 0}, {0, 0}},                    //ADC1 atten2
26                                                     {{644403418269478, 1e15}, {644334888647536, 1e16}, {1297891447611, 1e16}, {70769718, 1e15}, {13515, 1e15}}     //ADC1 atten3
27                                                 };
28 const static uint64_t adc2_error_coef_atten[COEFF_GROUP_NUM][TERM_MAX][2] = {
29                                                     {{25668651654328927, 1e16}, {1353548869615, 1e16}, {36615265189, 1e16}, {0, 0}, {0, 0}},                       //ADC2 atten0
30                                                     {{23690184690298404, 1e16}, {66319894226185, 1e16}, {118964995959, 1e16}, {0, 0}, {0, 0}},                     //ADC2 atten1
31                                                     {{9452499397020617, 1e16}, {200996773954387, 1e16}, {259011467956, 1e16}, {0, 0}, {0, 0}},                     //ADC2 atten2
32                                                     {{12247719764336924,1e16}, {755717904943462, 1e16}, {1478791187119, 1e16}, {79672528, 1e15}, {15038, 1e15}}    //ADC2 atten3
33                                                 };
34 /**
35  * Term sign
36  */
37 const static int32_t adc1_error_sign[COEFF_GROUP_NUM][TERM_MAX] = {
38                                         {-1, -1, 1, 0,  0},  //ADC1 atten0
39                                         {-1, -1, 1, 0,  0},  //ADC1 atten1
40                                         {-1, -1, 1, 0,  0},  //ADC1 atten2
41                                         {-1, -1, 1, -1, 1}   //ADC1 atten3
42                                     };
43 const static int32_t adc2_error_sign[COEFF_GROUP_NUM][TERM_MAX] = {
44                                         {-1,  1, 1,  0, 0},  //ADC2 atten0
45                                         {-1, -1, 1,  0, 0},  //ADC2 atten1
46                                         {-1, -1, 1,  0, 0},  //ADC2 atten2
47                                         { 1, -1, 1, -1, 1}   //ADC2 atten3
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     ctx->term_num = (config->atten == 3) ? 5 : 3;
53     ctx->coeff = (config->unit_id == ADC_UNIT_1) ? &adc1_error_coef_atten : &adc2_error_coef_atten;
54     ctx->sign = (config->unit_id == ADC_UNIT_1) ? &adc1_error_sign : &adc2_error_sign;
55 }
56