esp_adc_cal.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_ADC_CAL_H__
  14. #define __ESP_ADC_CAL_H__
  15. #include <stdint.h>
  16. #include "esp_err.h"
  17. #include "driver/adc.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /**
  22. * @brief Type of calibration value used in characterization
  23. */
  24. typedef enum {
  25. ESP_ADC_CAL_VAL_EFUSE_VREF = 0, /**< Characterization based on reference voltage stored in eFuse*/
  26. ESP_ADC_CAL_VAL_EFUSE_TP = 1, /**< Characterization based on Two Point values stored in eFuse*/
  27. ESP_ADC_CAL_VAL_DEFAULT_VREF = 2, /**< Characterization based on default reference voltage*/
  28. ESP_ADC_CAL_VAL_EFUSE_TP_FIT = 3, /**< Characterization based on Two Point values and fitting curve coefficients stored in eFuse */
  29. ESP_ADC_CAL_VAL_MAX,
  30. ESP_ADC_CAL_VAL_NOT_SUPPORTED = ESP_ADC_CAL_VAL_MAX,
  31. } esp_adc_cal_value_t;
  32. /**
  33. * @brief Structure storing characteristics of an ADC
  34. *
  35. * @note Call esp_adc_cal_characterize() to initialize the structure
  36. */
  37. typedef struct {
  38. adc_unit_t adc_num; /**< ADC number*/
  39. adc_atten_t atten; /**< ADC attenuation*/
  40. adc_bits_width_t bit_width; /**< ADC bit width */
  41. uint32_t coeff_a; /**< Gradient of ADC-Voltage curve*/
  42. uint32_t coeff_b; /**< Offset of ADC-Voltage curve*/
  43. uint32_t vref; /**< Vref used by lookup table*/
  44. const uint32_t *low_curve; /**< Pointer to low Vref curve of lookup table (NULL if unused)*/
  45. const uint32_t *high_curve; /**< Pointer to high Vref curve of lookup table (NULL if unused)*/
  46. uint8_t version; /**< ADC Calibration */
  47. } esp_adc_cal_characteristics_t;
  48. /**
  49. * @brief Checks if ADC calibration values are burned into eFuse
  50. *
  51. * This function checks if ADC reference voltage or Two Point values have been
  52. * burned to the eFuse of the current ESP32
  53. *
  54. * @param value_type Type of calibration value (ESP_ADC_CAL_VAL_EFUSE_VREF or ESP_ADC_CAL_VAL_EFUSE_TP)
  55. * @note in ESP32S2, only ESP_ADC_CAL_VAL_EFUSE_TP is supported. Some old ESP32S2s do not support this, either.
  56. * In which case you have to calibrate it manually, possibly by performing your own two-point calibration on the chip.
  57. *
  58. * @return
  59. * - ESP_OK: The calibration mode is supported in eFuse
  60. * - ESP_ERR_NOT_SUPPORTED: Error, eFuse values are not burned
  61. * - ESP_ERR_INVALID_ARG: Error, invalid argument (ESP_ADC_CAL_VAL_DEFAULT_VREF)
  62. */
  63. esp_err_t esp_adc_cal_check_efuse(esp_adc_cal_value_t value_type);
  64. /**
  65. * @brief Characterize an ADC at a particular attenuation
  66. *
  67. * This function will characterize the ADC at a particular attenuation and generate
  68. * the ADC-Voltage curve in the form of [y = coeff_a * x + coeff_b].
  69. * Characterization can be based on Two Point values, eFuse Vref, or default Vref
  70. * and the calibration values will be prioritized in that order.
  71. *
  72. * @note
  73. * For ESP32, Two Point values and eFuse Vref calibration can be enabled/disabled using menuconfig.
  74. * For ESP32s2, only Two Point values calibration and only ADC_WIDTH_BIT_13 is supported. The parameter default_vref is unused.
  75. *
  76. *
  77. * @param[in] adc_num ADC to characterize (ADC_UNIT_1 or ADC_UNIT_2)
  78. * @param[in] atten Attenuation to characterize
  79. * @param[in] bit_width Bit width configuration of ADC
  80. * @param[in] default_vref Default ADC reference voltage in mV (Only in ESP32, used if eFuse values is not available)
  81. * @param[out] chars Pointer to empty structure used to store ADC characteristics
  82. *
  83. * @return
  84. * - ESP_ADC_CAL_VAL_EFUSE_VREF: eFuse Vref used for characterization
  85. * - ESP_ADC_CAL_VAL_EFUSE_TP: Two Point value used for characterization (only in Linear Mode)
  86. * - ESP_ADC_CAL_VAL_DEFAULT_VREF: Default Vref used for characterization
  87. */
  88. esp_adc_cal_value_t esp_adc_cal_characterize(adc_unit_t adc_num,
  89. adc_atten_t atten,
  90. adc_bits_width_t bit_width,
  91. uint32_t default_vref,
  92. esp_adc_cal_characteristics_t *chars);
  93. /**
  94. * @brief Convert an ADC reading to voltage in mV
  95. *
  96. * This function converts an ADC reading to a voltage in mV based on the ADC's
  97. * characteristics.
  98. *
  99. * @note Characteristics structure must be initialized before this function
  100. * is called (call esp_adc_cal_characterize())
  101. *
  102. * @param[in] adc_reading ADC reading
  103. * @param[in] chars Pointer to initialized structure containing ADC characteristics
  104. *
  105. * @return Voltage in mV
  106. */
  107. uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_characteristics_t *chars);
  108. /**
  109. * @brief Reads an ADC and converts the reading to a voltage in mV
  110. *
  111. * This function reads an ADC then converts the raw reading to a voltage in mV
  112. * based on the characteristics provided. The ADC that is read is also
  113. * determined by the characteristics.
  114. *
  115. * @note The Characteristics structure must be initialized before this
  116. * function is called (call esp_adc_cal_characterize())
  117. *
  118. * @param[in] channel ADC Channel to read
  119. * @param[in] chars Pointer to initialized ADC characteristics structure
  120. * @param[out] voltage Pointer to store converted voltage
  121. *
  122. * @return
  123. * - ESP_OK: ADC read and converted to mV
  124. * - ESP_ERR_INVALID_ARG: Error due to invalid arguments
  125. * - ESP_ERR_INVALID_STATE: Reading result is invalid. Try to read again.
  126. */
  127. esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, const esp_adc_cal_characteristics_t *chars, uint32_t *voltage);
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif /* __ESP_ADC_CAL_H__ */