1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include "sdkconfig.h"
11 #include "esp_err.h"
12 #include "hal/adc_types.h"
13 #include "driver/adc_types_legacy.h"
14 #include "esp_adc_cal_types_legacy.h"
15 
16 #if !CONFIG_ADC_SUPPRESS_DEPRECATE_WARN
17 #warning "legacy adc calibration driver is deprecated, please migrate to use esp_adc/adc_cali.h and esp_adc/adc_cali_scheme.h"
18 #endif
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
25 /**
26  * @brief Checks if ADC calibration values are burned into eFuse
27  *
28  * This function checks if ADC reference voltage or Two Point values have been
29  * burned to the eFuse of the current ESP32
30  *
31  * @param   value_type  Type of calibration value (ESP_ADC_CAL_VAL_EFUSE_VREF or ESP_ADC_CAL_VAL_EFUSE_TP)
32  * @note in ESP32S2, only ESP_ADC_CAL_VAL_EFUSE_TP is supported. Some old ESP32S2s do not support this, either.
33  * In which case you have to calibrate it manually, possibly by performing your own two-point calibration on the chip.
34  *
35  * @return
36  *      - ESP_OK: The calibration mode is supported in eFuse
37  *      - ESP_ERR_NOT_SUPPORTED: Error, eFuse values are not burned
38  *      - ESP_ERR_INVALID_ARG: Error, invalid argument (ESP_ADC_CAL_VAL_DEFAULT_VREF)
39  */
40 esp_err_t esp_adc_cal_check_efuse(esp_adc_cal_value_t value_type);
41 
42 /**
43  * @brief Characterize an ADC at a particular attenuation
44  *
45  * This function will characterize the ADC at a particular attenuation and generate
46  * the ADC-Voltage curve in the form of [y = coeff_a * x + coeff_b].
47  * Characterization can be based on Two Point values, eFuse Vref, or default Vref
48  * and the calibration values will be prioritized in that order.
49  *
50  * @note
51  * For ESP32, Two Point values and eFuse Vref calibration can be enabled/disabled using menuconfig.
52  * For ESP32s2, only Two Point values calibration and only ADC_WIDTH_BIT_13 is supported. The parameter default_vref is unused.
53  *
54  *
55  * @param[in]   adc_num         ADC to characterize (ADC_UNIT_1 or ADC_UNIT_2)
56  * @param[in]   atten           Attenuation to characterize
57  * @param[in]   bit_width       Bit width configuration of ADC
58  * @param[in]   default_vref    Default ADC reference voltage in mV (Only in ESP32, used if eFuse values is not available)
59  * @param[out]  chars           Pointer to empty structure used to store ADC characteristics
60  *
61  * @return
62  *      - ESP_ADC_CAL_VAL_EFUSE_VREF: eFuse Vref used for characterization
63  *      - ESP_ADC_CAL_VAL_EFUSE_TP: Two Point value used for characterization (only in Linear Mode)
64  *      - ESP_ADC_CAL_VAL_DEFAULT_VREF: Default Vref used for characterization
65  */
66 esp_adc_cal_value_t esp_adc_cal_characterize(adc_unit_t adc_num,
67                                              adc_atten_t atten,
68                                              adc_bits_width_t bit_width,
69                                              uint32_t default_vref,
70                                              esp_adc_cal_characteristics_t *chars);
71 
72 /**
73  * @brief   Convert an ADC reading to voltage in mV
74  *
75  * This function converts an ADC reading to a voltage in mV based on the ADC's
76  * characteristics.
77  *
78  * @note    Characteristics structure must be initialized before this function
79  *          is called (call esp_adc_cal_characterize())
80  *
81  * @param[in]   adc_reading     ADC reading
82  * @param[in]   chars           Pointer to initialized structure containing ADC characteristics
83  *
84  * @return      Voltage in mV
85  */
86 uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_characteristics_t *chars);
87 
88 /**
89  * @brief   Reads an ADC and converts the reading to a voltage in mV
90  *
91  * This function reads an ADC then converts the raw reading to a voltage in mV
92  * based on the characteristics provided. The ADC that is read is also
93  * determined by the characteristics.
94  *
95  * @note    The Characteristics structure must be initialized before this
96  *          function is called (call esp_adc_cal_characterize())
97  *
98  * @param[in]   channel     ADC Channel to read
99  * @param[in]   chars       Pointer to initialized ADC characteristics structure
100  * @param[out]  voltage     Pointer to store converted voltage
101  *
102  * @return
103  *      - ESP_OK: ADC read and converted to mV
104  *      - ESP_ERR_INVALID_ARG: Error due to invalid arguments
105  *      - ESP_ERR_INVALID_STATE: Reading result is invalid. Try to read again.
106  */
107 esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, const esp_adc_cal_characteristics_t *chars, uint32_t *voltage);
108 
109 #endif  //#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
110 
111 #ifdef __cplusplus
112 }
113 #endif
114