curve_fitting_coefficients.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include "../curve_fitting_coefficients.h"
  8. #define COEFF_GROUP_NUM 4
  9. #define TERM_MAX 5
  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. * @note ADC1 and ADC2 use same coefficients
  22. */
  23. const static uint64_t adc1_error_coef_atten[COEFF_GROUP_NUM][TERM_MAX][2] = {
  24. {{225966470500043, 1e15}, {7265418501948, 1e16}, {109410402681, 1e16}, {0, 0}, {0, 0}}, //atten0
  25. {{4229623392600516, 1e16}, {731527490903, 1e16}, {88166562521, 1e16}, {0, 0}, {0, 0}}, //atten1
  26. {{1017859239236435, 1e15}, {97159265299153, 1e16}, {149794028038, 1e16}, {0, 0}, {0, 0}}, //atten2
  27. {{14912262772850453, 1e16}, {228549975564099, 1e16}, {356391935717, 1e16}, {179964582, 1e16}, {42046, 1e16}} //atten3
  28. };
  29. /**
  30. * Term sign
  31. */
  32. const static int32_t adc1_error_sign[COEFF_GROUP_NUM][TERM_MAX] = {
  33. {-1, -1, 1, 0, 0}, //atten0
  34. { 1, -1, 1, 0, 0}, //atten1
  35. {-1, -1, 1, 0, 0}, //atten2
  36. {-1, -1, 1, -1, 1} //atten3
  37. };
  38. void curve_fitting_get_second_step_coeff(const adc_cali_curve_fitting_config_t *config, cali_chars_second_step_t *ctx)
  39. {
  40. ctx->term_num = (config->atten == 3) ? 5 : 3;
  41. // On esp32c3, ADC1 and ADC2 share the second step coefficients
  42. // And if the target only has 1 ADC peripheral, just use the ADC1 directly
  43. ctx->coeff = adc1_error_coef_atten[config->atten];
  44. ctx->sign = adc1_error_sign[config->atten];
  45. }