esp_adc_cal.h 4.7 KB

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