esp_adc_cal.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_MAX
  29. } esp_adc_cal_value_t;
  30. /**
  31. * @brief Structure storing characteristics of an ADC
  32. *
  33. * @note Call esp_adc_cal_characterize() to initialize the structure
  34. */
  35. typedef struct {
  36. adc_unit_t adc_num; /**< ADC number*/
  37. adc_atten_t atten; /**< ADC attenuation*/
  38. adc_bits_width_t bit_width; /**< ADC bit width */
  39. uint32_t coeff_a; /**< Gradient of ADC-Voltage curve*/
  40. uint32_t coeff_b; /**< Offset of ADC-Voltage curve*/
  41. uint32_t vref; /**< Vref used by lookup table*/
  42. const uint32_t *low_curve; /**< Pointer to low Vref curve of lookup table (NULL if unused)*/
  43. const uint32_t *high_curve; /**< Pointer to high Vref curve of lookup table (NULL if unused)*/
  44. } esp_adc_cal_characteristics_t;
  45. /**
  46. * @brief Checks if ADC calibration values are burned into eFuse
  47. *
  48. * This function checks if ADC reference voltage or Two Point values have been
  49. * burned to the eFuse of the current ESP32
  50. *
  51. * @param value_type Type of calibration value (ESP_ADC_CAL_VAL_EFUSE_VREF or ESP_ADC_CAL_VAL_EFUSE_TP)
  52. *
  53. * @return
  54. * - ESP_OK: The calibration mode is supported in eFuse
  55. * - ESP_ERR_NOT_SUPPORTED: Error, eFuse values are not burned
  56. * - ESP_ERR_INVALID_ARG: Error, invalid argument (ESP_ADC_CAL_VAL_DEFAULT_VREF)
  57. */
  58. esp_err_t esp_adc_cal_check_efuse(esp_adc_cal_value_t value_type);
  59. /**
  60. * @brief Characterize an ADC at a particular attenuation
  61. *
  62. * This function will characterize the ADC at a particular attenuation and generate
  63. * the ADC-Voltage curve in the form of [y = coeff_a * x + coeff_b].
  64. * Characterization can be based on Two Point values, eFuse Vref, or default Vref
  65. * and the calibration values will be prioritized in that order.
  66. *
  67. * @note
  68. * For ESP32, Two Point values and eFuse Vref calibration can be enabled/disabled using menuconfig.
  69. * For ESP32s2, only Two Point values calibration and only ADC_WIDTH_BIT_13 is supported. The parameter default_vref is unused.
  70. *
  71. *
  72. * @param[in] adc_num ADC to characterize (ADC_UNIT_1 or ADC_UNIT_2)
  73. * @param[in] atten Attenuation to characterize
  74. * @param[in] bit_width Bit width configuration of ADC
  75. * @param[in] default_vref Default ADC reference voltage in mV (Only in ESP32, used if eFuse values is not available)
  76. * @param[out] chars Pointer to empty structure used to store ADC characteristics
  77. *
  78. * @return
  79. * - ESP_ADC_CAL_VAL_EFUSE_VREF: eFuse Vref used for characterization
  80. * - ESP_ADC_CAL_VAL_EFUSE_TP: Two Point value used for characterization (only in Linear Mode)
  81. * - ESP_ADC_CAL_VAL_DEFAULT_VREF: Default Vref used for characterization
  82. */
  83. esp_adc_cal_value_t esp_adc_cal_characterize(adc_unit_t adc_num,
  84. adc_atten_t atten,
  85. adc_bits_width_t bit_width,
  86. uint32_t default_vref,
  87. esp_adc_cal_characteristics_t *chars);
  88. /**
  89. * @brief Convert an ADC reading to voltage in mV
  90. *
  91. * This function converts an ADC reading to a voltage in mV based on the ADC's
  92. * characteristics.
  93. *
  94. * @note Characteristics structure must be initialized before this function
  95. * is called (call esp_adc_cal_characterize())
  96. *
  97. * @param[in] adc_reading ADC reading
  98. * @param[in] chars Pointer to initialized structure containing ADC characteristics
  99. *
  100. * @return Voltage in mV
  101. */
  102. uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_characteristics_t *chars);
  103. /**
  104. * @brief Reads an ADC and converts the reading to a voltage in mV
  105. *
  106. * This function reads an ADC then converts the raw reading to a voltage in mV
  107. * based on the characteristics provided. The ADC that is read is also
  108. * determined by the characteristics.
  109. *
  110. * @note The Characteristics structure must be initialized before this
  111. * function is called (call esp_adc_cal_characterize())
  112. *
  113. * @param[in] channel ADC Channel to read
  114. * @param[in] chars Pointer to initialized ADC characteristics structure
  115. * @param[out] voltage Pointer to store converted voltage
  116. *
  117. * @return
  118. * - ESP_OK: ADC read and converted to mV
  119. * - ESP_ERR_TIMEOUT: Error, timed out attempting to read ADC
  120. * - ESP_ERR_INVALID_ARG: Error due to invalid arguments
  121. */
  122. esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, const esp_adc_cal_characteristics_t *chars, uint32_t *voltage);
  123. #ifdef __cplusplus
  124. }
  125. #endif
  126. #endif /* __ESP_ADC_CAL_H__ */