2021-11-04 08:01:19 -04:00
|
|
|
/*
|
2022-01-20 07:34:37 -05:00
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
2021-11-04 08:01:19 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-08-07 09:33:01 -04:00
|
|
|
|
2017-09-01 00:07:37 -04:00
|
|
|
#ifndef __ESP_ADC_CAL_H__
|
|
|
|
#define __ESP_ADC_CAL_H__
|
|
|
|
|
2017-08-07 09:33:01 -04:00
|
|
|
#include <stdint.h>
|
2018-01-24 04:22:13 -05:00
|
|
|
#include "esp_err.h"
|
2017-08-07 09:33:01 -04:00
|
|
|
#include "driver/adc.h"
|
|
|
|
|
2021-02-28 18:21:44 -05:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-01-24 04:22:13 -05:00
|
|
|
/**
|
|
|
|
* @brief Type of calibration value used in characterization
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
ESP_ADC_CAL_VAL_EFUSE_VREF = 0, /**< Characterization based on reference voltage stored in eFuse*/
|
|
|
|
ESP_ADC_CAL_VAL_EFUSE_TP = 1, /**< Characterization based on Two Point values stored in eFuse*/
|
|
|
|
ESP_ADC_CAL_VAL_DEFAULT_VREF = 2, /**< Characterization based on default reference voltage*/
|
2021-09-06 23:21:35 -04:00
|
|
|
ESP_ADC_CAL_VAL_EFUSE_TP_FIT = 3, /**< Characterization based on Two Point values and fitting curve coefficients stored in eFuse */
|
2020-12-22 23:29:57 -05:00
|
|
|
ESP_ADC_CAL_VAL_MAX,
|
|
|
|
ESP_ADC_CAL_VAL_NOT_SUPPORTED = ESP_ADC_CAL_VAL_MAX,
|
2018-01-24 04:22:13 -05:00
|
|
|
} esp_adc_cal_value_t;
|
2017-08-07 09:33:01 -04:00
|
|
|
|
|
|
|
/**
|
2018-01-24 04:22:13 -05:00
|
|
|
* @brief Structure storing characteristics of an ADC
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-02-13 07:47:18 -05:00
|
|
|
* @note Call esp_adc_cal_characterize() to initialize the structure
|
2017-08-07 09:33:01 -04:00
|
|
|
*/
|
|
|
|
typedef struct {
|
2018-02-13 07:47:18 -05:00
|
|
|
adc_unit_t adc_num; /**< ADC number*/
|
|
|
|
adc_atten_t atten; /**< ADC attenuation*/
|
|
|
|
adc_bits_width_t bit_width; /**< ADC bit width */
|
|
|
|
uint32_t coeff_a; /**< Gradient of ADC-Voltage curve*/
|
|
|
|
uint32_t coeff_b; /**< Offset of ADC-Voltage curve*/
|
|
|
|
uint32_t vref; /**< Vref used by lookup table*/
|
|
|
|
const uint32_t *low_curve; /**< Pointer to low Vref curve of lookup table (NULL if unused)*/
|
|
|
|
const uint32_t *high_curve; /**< Pointer to high Vref curve of lookup table (NULL if unused)*/
|
2021-09-06 23:21:35 -04:00
|
|
|
uint8_t version; /**< ADC Calibration */
|
2017-08-07 09:33:01 -04:00
|
|
|
} esp_adc_cal_characteristics_t;
|
|
|
|
|
|
|
|
/**
|
2018-02-13 07:47:18 -05:00
|
|
|
* @brief Checks if ADC calibration values are burned into eFuse
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-02-13 07:47:18 -05:00
|
|
|
* This function checks if ADC reference voltage or Two Point values have been
|
|
|
|
* burned to the eFuse of the current ESP32
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-01-24 04:22:13 -05:00
|
|
|
* @param value_type Type of calibration value (ESP_ADC_CAL_VAL_EFUSE_VREF or ESP_ADC_CAL_VAL_EFUSE_TP)
|
2020-12-03 07:08:59 -05:00
|
|
|
* @note in ESP32S2, only ESP_ADC_CAL_VAL_EFUSE_TP is supported. Some old ESP32S2s do not support this, either.
|
|
|
|
* In which case you have to calibrate it manually, possibly by performing your own two-point calibration on the chip.
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-01-24 04:22:13 -05:00
|
|
|
* @return
|
|
|
|
* - ESP_OK: The calibration mode is supported in eFuse
|
|
|
|
* - ESP_ERR_NOT_SUPPORTED: Error, eFuse values are not burned
|
|
|
|
* - ESP_ERR_INVALID_ARG: Error, invalid argument (ESP_ADC_CAL_VAL_DEFAULT_VREF)
|
|
|
|
*/
|
|
|
|
esp_err_t esp_adc_cal_check_efuse(esp_adc_cal_value_t value_type);
|
|
|
|
|
|
|
|
/**
|
2018-02-13 07:47:18 -05:00
|
|
|
* @brief Characterize an ADC at a particular attenuation
|
2018-01-24 04:22:13 -05:00
|
|
|
*
|
2018-02-13 07:47:18 -05:00
|
|
|
* This function will characterize the ADC at a particular attenuation and generate
|
|
|
|
* the ADC-Voltage curve in the form of [y = coeff_a * x + coeff_b].
|
|
|
|
* Characterization can be based on Two Point values, eFuse Vref, or default Vref
|
|
|
|
* and the calibration values will be prioritized in that order.
|
2018-01-24 04:22:13 -05:00
|
|
|
*
|
2020-11-10 02:40:01 -05:00
|
|
|
* @note
|
2020-07-09 08:55:52 -04:00
|
|
|
* For ESP32, Two Point values and eFuse Vref calibration can be enabled/disabled using menuconfig.
|
|
|
|
* For ESP32s2, only Two Point values calibration and only ADC_WIDTH_BIT_13 is supported. The parameter default_vref is unused.
|
2020-11-10 02:40:01 -05:00
|
|
|
*
|
2018-01-24 04:22:13 -05:00
|
|
|
*
|
|
|
|
* @param[in] adc_num ADC to characterize (ADC_UNIT_1 or ADC_UNIT_2)
|
|
|
|
* @param[in] atten Attenuation to characterize
|
2018-02-13 07:47:18 -05:00
|
|
|
* @param[in] bit_width Bit width configuration of ADC
|
2020-07-09 08:55:52 -04:00
|
|
|
* @param[in] default_vref Default ADC reference voltage in mV (Only in ESP32, used if eFuse values is not available)
|
2018-01-24 04:22:13 -05:00
|
|
|
* @param[out] chars Pointer to empty structure used to store ADC characteristics
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_ADC_CAL_VAL_EFUSE_VREF: eFuse Vref used for characterization
|
|
|
|
* - ESP_ADC_CAL_VAL_EFUSE_TP: Two Point value used for characterization (only in Linear Mode)
|
|
|
|
* - ESP_ADC_CAL_VAL_DEFAULT_VREF: Default Vref used for characterization
|
2017-08-07 09:33:01 -04:00
|
|
|
*/
|
2018-01-24 04:22:13 -05:00
|
|
|
esp_adc_cal_value_t esp_adc_cal_characterize(adc_unit_t adc_num,
|
|
|
|
adc_atten_t atten,
|
2018-02-13 07:47:18 -05:00
|
|
|
adc_bits_width_t bit_width,
|
|
|
|
uint32_t default_vref,
|
2018-01-24 04:22:13 -05:00
|
|
|
esp_adc_cal_characteristics_t *chars);
|
|
|
|
|
2017-08-07 09:33:01 -04:00
|
|
|
/**
|
2018-01-24 04:22:13 -05:00
|
|
|
* @brief Convert an ADC reading to voltage in mV
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-02-13 07:47:18 -05:00
|
|
|
* This function converts an ADC reading to a voltage in mV based on the ADC's
|
|
|
|
* characteristics.
|
|
|
|
*
|
|
|
|
* @note Characteristics structure must be initialized before this function
|
|
|
|
* is called (call esp_adc_cal_characterize())
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-01-24 04:22:13 -05:00
|
|
|
* @param[in] adc_reading ADC reading
|
2018-02-13 07:47:18 -05:00
|
|
|
* @param[in] chars Pointer to initialized structure containing ADC characteristics
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-01-24 04:22:13 -05:00
|
|
|
* @return Voltage in mV
|
2017-08-07 09:33:01 -04:00
|
|
|
*/
|
2018-02-13 07:47:18 -05:00
|
|
|
uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_characteristics_t *chars);
|
2017-08-07 09:33:01 -04:00
|
|
|
|
|
|
|
/**
|
2018-02-13 07:47:18 -05:00
|
|
|
* @brief Reads an ADC and converts the reading to a voltage in mV
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-01-24 04:22:13 -05:00
|
|
|
* This function reads an ADC then converts the raw reading to a voltage in mV
|
2018-02-13 07:47:18 -05:00
|
|
|
* based on the characteristics provided. The ADC that is read is also
|
2018-01-24 04:22:13 -05:00
|
|
|
* determined by the characteristics.
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-02-13 07:47:18 -05:00
|
|
|
* @note The Characteristics structure must be initialized before this
|
|
|
|
* function is called (call esp_adc_cal_characterize())
|
|
|
|
*
|
2018-01-24 04:22:13 -05:00
|
|
|
* @param[in] channel ADC Channel to read
|
|
|
|
* @param[in] chars Pointer to initialized ADC characteristics structure
|
|
|
|
* @param[out] voltage Pointer to store converted voltage
|
2017-08-07 09:33:01 -04:00
|
|
|
*
|
2018-01-24 04:22:13 -05:00
|
|
|
* @return
|
2018-02-13 07:47:18 -05:00
|
|
|
* - ESP_OK: ADC read and converted to mV
|
2018-01-24 04:22:13 -05:00
|
|
|
* - ESP_ERR_INVALID_ARG: Error due to invalid arguments
|
2021-09-06 23:21:35 -04:00
|
|
|
* - ESP_ERR_INVALID_STATE: Reading result is invalid. Try to read again.
|
2018-01-24 04:22:13 -05:00
|
|
|
*/
|
2018-02-13 07:47:18 -05:00
|
|
|
esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, const esp_adc_cal_characteristics_t *chars, uint32_t *voltage);
|
2018-01-24 04:22:13 -05:00
|
|
|
|
2017-09-01 00:07:37 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __ESP_ADC_CAL_H__ */
|