1 /*
2  * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <sys/cdefs.h>
9 #include "esp_types.h"
10 #include "sdkconfig.h"
11 #include "esp_err.h"
12 #include "esp_log.h"
13 #include "esp_check.h"
14 #include "esp_heap_caps.h"
15 #include "hal/adc_types.h"
16 #include "esp_adc/adc_cali.h"
17 #include "adc_cali_interface.h"
18 
19 const __attribute__((unused)) static char *TAG = "adc_cali";
20 
21 
adc_cali_check_scheme(adc_cali_scheme_ver_t * scheme_mask)22 esp_err_t adc_cali_check_scheme(adc_cali_scheme_ver_t *scheme_mask)
23 {
24     ESP_RETURN_ON_FALSE(scheme_mask, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
25     *scheme_mask = 0;
26 #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
27     *scheme_mask |= ADC_CALI_SCHEME_VER_LINE_FITTING;
28 #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
29     *scheme_mask |= ADC_CALI_SCHEME_VER_CURVE_FITTING;
30 #endif
31 
32     //Add your custom ADC calibration scheme here
33 
34     ESP_RETURN_ON_FALSE((*scheme_mask) != 0, ESP_ERR_NOT_SUPPORTED, TAG, "no supported calibration scheme yet");
35 
36     return ESP_OK;
37 }
38 
adc_cali_raw_to_voltage(adc_cali_handle_t handle,int raw,int * voltage)39 esp_err_t adc_cali_raw_to_voltage(adc_cali_handle_t handle, int raw, int *voltage)
40 {
41     ESP_RETURN_ON_FALSE(handle && voltage, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
42     ESP_RETURN_ON_FALSE(handle->ctx, ESP_ERR_INVALID_STATE, TAG, "no calibration scheme, create a scheme first");
43 
44     return handle->raw_to_voltage(handle->ctx, raw, voltage);
45 }
46