|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <stdio.h> |
| 7 | +#include <string.h> |
| 8 | +#include "esp_log.h" |
| 9 | +#include "esp_check.h" |
| 10 | +#include "adc_tp_calibration.h" |
| 11 | + |
| 12 | +static const char* TAG = "adc_tp_cal"; |
| 13 | + |
| 14 | +#if CONFIG_IDF_TARGET_ESP32 |
| 15 | +#define TP_LOW_VOLTAGE 150 |
| 16 | +#define TP_HIGH_VOLTAGE 850 |
| 17 | +#define LIN_COEFF_A_SCALE 65536 |
| 18 | +#define LIN_COEFF_A_ROUND (LIN_COEFF_A_SCALE/2) |
| 19 | +static const uint32_t adc1_tp_atten_scale[4] = {65504, 86975, 120389, 224310}; |
| 20 | +static const uint32_t adc2_tp_atten_scale[4] = {65467, 86861, 120416, 224708}; |
| 21 | +static const uint32_t adc1_tp_atten_offset[4] = {0, 1, 27, 54}; |
| 22 | +static const uint32_t adc2_tp_atten_offset[4] = {0, 9, 26, 66}; |
| 23 | +#elif CONFIG_IDF_TARGET_ESP32S2 |
| 24 | +static const int coeff_a_scaling = 65536; |
| 25 | +static const int coeff_b_scaling = 1024; |
| 26 | +static const uint32_t v_high[] = {600, 800, 1000, 2000}; |
| 27 | +#endif |
| 28 | + |
| 29 | +typedef struct { |
| 30 | + adc_atten_t atten; |
| 31 | + adc_tp_cali_config_t config; |
| 32 | + uint32_t coeff_a; |
| 33 | + uint32_t coeff_b; |
| 34 | +#if CONFIG_IDF_TARGET_ESP32 |
| 35 | + const uint32_t *atten_scales; |
| 36 | + const uint32_t *atten_offsets; |
| 37 | +#elif CONFIG_IDF_TARGET_ESP32S2 |
| 38 | + uint32_t high[4]; |
| 39 | +#endif |
| 40 | +} adc_tp_cali_t; |
| 41 | + |
| 42 | +adc_tp_cali_handle_t adc_tp_cali_create(adc_tp_cali_config_t *config, adc_atten_t atten) |
| 43 | +{ |
| 44 | + ESP_RETURN_ON_FALSE(config, NULL, TAG, "config is NULL"); |
| 45 | + ESP_RETURN_ON_FALSE(config->adc_unit == ADC_UNIT_1 || config->adc_unit == ADC_UNIT_2, NULL, TAG, "invalid ADC unit"); |
| 46 | + |
| 47 | + adc_tp_cali_t *adc_tp_cal = (adc_tp_cali_t *)calloc(1, sizeof(adc_tp_cali_t)); |
| 48 | + ESP_RETURN_ON_FALSE(adc_tp_cal, NULL, TAG, "memory allocation for device handler failed"); |
| 49 | + |
| 50 | + adc_tp_cal->atten = atten; |
| 51 | + adc_tp_cal->config = *config; |
| 52 | + |
| 53 | +#if CONFIG_IDF_TARGET_ESP32 |
| 54 | + if (adc_tp_cal->config.adc_unit == ADC_UNIT_1) { |
| 55 | + adc_tp_cal->atten_scales = adc1_tp_atten_scale; |
| 56 | + adc_tp_cal->atten_offsets = adc1_tp_atten_offset; |
| 57 | + } else { |
| 58 | + adc_tp_cal->atten_scales = adc2_tp_atten_scale; |
| 59 | + adc_tp_cal->atten_offsets = adc2_tp_atten_offset; |
| 60 | + } |
| 61 | + |
| 62 | + uint32_t delta_x = config->adc_raw_value_850mv_atten0 - config->adc_raw_value_150mv_atten0; |
| 63 | + uint32_t delta_v = TP_HIGH_VOLTAGE - TP_LOW_VOLTAGE; |
| 64 | + adc_tp_cal->coeff_a = (delta_v * adc_tp_cal->atten_scales[atten] + (delta_x / 2)) / delta_x; |
| 65 | + adc_tp_cal->coeff_b = TP_HIGH_VOLTAGE - ((delta_v * config->adc_raw_value_850mv_atten0 + (delta_x / 2)) / delta_x) + adc_tp_cal->atten_offsets[atten]; |
| 66 | +#elif CONFIG_IDF_TARGET_ESP32S2 |
| 67 | + const uint32_t high_values[] = { |
| 68 | + config->adc_raw_value_600mv_atten0, |
| 69 | + config->adc_raw_value_800mv_atten2_5, |
| 70 | + config->adc_raw_value_1000mv_atten6, |
| 71 | + config->adc_raw_value_2000mv_atten12 |
| 72 | + }; |
| 73 | + memcpy(adc_tp_cal->high, high_values, sizeof(high_values)); |
| 74 | + |
| 75 | + adc_tp_cal->coeff_a = coeff_a_scaling * v_high[atten] / adc_tp_cal->high[atten]; |
| 76 | + adc_tp_cal->coeff_b = 0; |
| 77 | +#endif |
| 78 | + return (adc_tp_cali_handle_t)adc_tp_cal; |
| 79 | +} |
| 80 | + |
| 81 | +esp_err_t adc_tp_cali_delete(adc_tp_cali_handle_t *adc_tp_cali_handle) |
| 82 | +{ |
| 83 | + if (*adc_tp_cali_handle == NULL) { |
| 84 | + return ESP_OK; |
| 85 | + } |
| 86 | + |
| 87 | + adc_tp_cali_t *adc_tp_cal = (adc_tp_cali_t *)(*adc_tp_cali_handle); |
| 88 | + free(adc_tp_cal); |
| 89 | + *adc_tp_cali_handle = NULL; |
| 90 | + return ESP_OK; |
| 91 | +} |
| 92 | + |
| 93 | +esp_err_t adc_tp_cali_raw_to_voltage(adc_tp_cali_handle_t adc_tp_cali_handle, int raw_value, int *voltage) |
| 94 | +{ |
| 95 | + ESP_RETURN_ON_FALSE(adc_tp_cali_handle && voltage, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer"); |
| 96 | + |
| 97 | + adc_tp_cali_t *adc_tp_cal = (adc_tp_cali_t *)adc_tp_cali_handle; |
| 98 | +#if CONFIG_IDF_TARGET_ESP32 |
| 99 | + *voltage = (((adc_tp_cal->coeff_a * raw_value) + LIN_COEFF_A_ROUND) / LIN_COEFF_A_SCALE) + adc_tp_cal->coeff_b; |
| 100 | +#elif CONFIG_IDF_TARGET_ESP32S2 |
| 101 | + *voltage = (raw_value * adc_tp_cal->coeff_a / (coeff_a_scaling / coeff_b_scaling) + adc_tp_cal->coeff_b) / coeff_b_scaling; |
| 102 | +#endif |
| 103 | + return ESP_OK; |
| 104 | +} |
0 commit comments