adc_calibration.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. Analog to Digital Converter (ADC) Calibration Driver
  2. ====================================================
  3. Introduction
  4. ------------
  5. Based on series of comparisons with the reference voltage, {IDF_TARGET_NAME} ADC determines each bit of the output digital result. Per design the {IDF_TARGET_NAME} ADC reference voltage is 1100 mV, however the true reference voltage can range from 1000 mV to 1200 mV among different chips. This guide will introduce an ADC calibration driver to minimize this difference.
  6. Functional Overview
  7. -------------------
  8. The following sections of this document cover the typical steps to install and use the ADC calibration driver:
  9. - `Calibration Scheme Creation <#calibration-scheme-creation>`__ - covers how to create a calibration scheme handle and delete the calibration scheme handle.
  10. - `Result Conversion <#result-conversion>`__ - convers how to convert ADC raw result to calibrated result.
  11. - `Thread Safety <#thread-safety>`__ - lists which APIs are guaranteed to be thread safe by the driver.
  12. - `Minimize Noise <#minimize-noise>`__ - describes a general way to minimize the noise.
  13. .. only:: esp32
  14. - `Kconfig Options <#kconfig-options>`__ - lists the supported Kconfig options that can be used to make a different effect on driver behavior.
  15. Calibration Scheme Creation
  16. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  17. The ADC calibration driver provides ADC calibration scheme(s). From calibration driver's point of view, an ADC calibration scheme is created to an ADC calibration handle :cpp:type:`adc_cali_handle_t`.
  18. :cpp:func:`adc_cali_check_scheme` can be used to know which calibration scheme is supported on the chip. For those users who are already aware of the supported scheme, this step can be skipped. Just call the corresponding function to create the scheme handle.
  19. For those users who use their custom ADC calibration schemes, you could either modify this function :cpp:func:`adc_cali_check_scheme`, or just skip this step and call your custom creation function.
  20. .. only:: esp32 or esp32s2 or esp32c2
  21. ADC Calibration Line Fitting Scheme
  22. ```````````````````````````````````
  23. {IDF_TARGET_NAME} supports :c:macro:`ADC_CALI_SCHEME_VER_LINE_FITTING` scheme. To create this scheme, set up :cpp:type:`adc_cali_line_fitting_config_t` first.
  24. - :cpp:member:`adc_cali_line_fitting_config_t::unit_id`, the ADC that your ADC raw results are from.
  25. - :cpp:member:`adc_cali_line_fitting_config_t::atten`, ADC attenuation that your ADC raw results use.
  26. - :cpp:member:`adc_cali_line_fitting_config_t::bitwidth`, the ADC raw result bitwidth.
  27. .. only:: esp32
  28. There is also a configuration :cpp:member:`adc_cali_line_fitting_config_t::default_vref`. Normally this can be simply set to 0. Line Fitting scheme doesn't rely on this value. However, if the Line Fitting scheme required eFuse bits are not burnt on your board, driver will rely on this value to do the calibration.
  29. You can use :cpp:func:`adc_cali_scheme_line_fitting_check_efuse` to check the eFuse bits. Normally the Line Fitting scheme eFuse value will be :c:macro:`ADC_CALI_LINE_FITTING_EFUSE_VAL_EFUSE_TP` or :c:macro:`ADC_CALI_LINE_FITTING_EFUSE_VAL_EFUSE_VREF`. This means Line Fitting scheme will use calibration parameters burnt in the eFuse to do the calibration.
  30. When the Line Fitting scheme eFuse value is :c:macro:`ADC_CALI_LINE_FITTING_EFUSE_VAL_DEFAULT_VREF`, you need to set the :cpp:member:`esp_adc_cali_line_fitting_init::default_vref`. Default vref is an estimate of the ADC reference voltage provided by the users as a parameter during calibration.
  31. After setting up the configuration structure, call :cpp:func:`adc_cali_create_scheme_line_fitting` to create a Line Fitting calibration scheme handle.
  32. .. only:: esp32s2
  33. This function may fail due to reasons such as :c:macro:`ESP_ERR_INVALID_ARG` or :c:macro:`ESP_ERR_NO_MEM`. Especially, when the function return :c:macro:`ESP_ERR_NOT_SUPPORTED`, this means the calibration scheme required eFuse bits are not burnt on your board.
  34. .. code:: c
  35. ESP_LOGI(TAG, "calibration scheme version is %s", "Line Fitting");
  36. adc_cali_line_fitting_config_t cali_config = {
  37. .unit_id = unit,
  38. .atten = atten,
  39. .bitwidth = ADC_BITWIDTH_DEFAULT,
  40. };
  41. ESP_ERROR_CHECK(adc_cali_create_scheme_line_fitting(&cali_config, &handle));
  42. When the ADC calibration is no longer used, please delete the calibration scheme handle by calling :cpp:func:`adc_cali_delete_scheme_line_fitting`.
  43. Delete Line Fitting Scheme
  44. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. .. code:: c
  46. ESP_LOGI(TAG, "delete %s calibration scheme", "Line Fitting");
  47. ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle));
  48. .. only:: esp32c3 or esp32s3 or esp32c6
  49. ADC Calibration Curve Fitting Scheme
  50. ````````````````````````````````````
  51. {IDF_TARGET_NAME} supports :c:macro:`ADC_CALI_SCHEME_VER_CURVE_FITTING` scheme. To create this scheme, set up :cpp:type:`adc_cali_curve_fitting_config_t` first.
  52. .. only:: not esp32c6
  53. - :cpp:member:`adc_cali_curve_fitting_config_t::unit_id`, the ADC that your ADC raw results are from.
  54. - :cpp:member:`adc_cali_curve_fitting_config_t::chan`, this member is kept here for extensibility. The calibration scheme only differs by attenuation, there's no difference among different channels.
  55. - :cpp:member:`adc_cali_curve_fitting_config_t::atten`, ADC attenuation that your ADC raw results use.
  56. - :cpp:member:`adc_cali_curve_fitting_config_t::bitwidth`, the ADC raw result bitwidth.
  57. .. only:: esp32c6
  58. - :cpp:member:`adc_cali_curve_fitting_config_t::unit_id`, the ADC that your ADC raw results are from.
  59. - :cpp:member:`adc_cali_curve_fitting_config_t::chan`, the ADC channel that your ADC raw results are from. The calibration scheme not only differs by attenuation but also related to the channels.
  60. - :cpp:member:`adc_cali_curve_fitting_config_t::atten`, ADC attenuation that your ADC raw results use.
  61. - :cpp:member:`adc_cali_curve_fitting_config_t::bitwidth`, the ADC raw result bitwidth.
  62. After setting up the configuration structure, call :cpp:func:`adc_cali_create_scheme_curve_fitting` to create a Curve Fitting calibration scheme handle. This function may fail due to reasons such as :c:macro:`ESP_ERR_INVALID_ARG` or :c:macro:`ESP_ERR_NO_MEM`. Especially, when the function return :c:macro:`ESP_ERR_NOT_SUPPORTED`, this means the calibration scheme required eFuse bits are not burnt on your board.
  63. Create Curve Fitting Scheme
  64. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. .. code:: c
  66. ESP_LOGI(TAG, "calibration scheme version is %s", "Curve Fitting");
  67. adc_cali_curve_fitting_config_t cali_config = {
  68. .unit_id = unit,
  69. .atten = atten,
  70. .bitwidth = ADC_BITWIDTH_DEFAULT,
  71. };
  72. ESP_ERROR_CHECK(adc_cali_create_scheme_curve_fitting(&cali_config, &handle));
  73. When the ADC calibration is no longer used, please delete the calibration scheme driver from the calibration handle by calling :cpp:func:`adc_cali_delete_scheme_curve_fitting`.
  74. Delete Curve Fitting Scheme
  75. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. .. code:: c
  77. ESP_LOGI(TAG, "delete %s calibration scheme", "Curve Fitting");
  78. ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
  79. .. only:: esp32h2
  80. There is no supported calibration scheme yet.
  81. .. note::
  82. For users who want to use their custom calibration schemes, you could provide a creation function to create your calibration scheme handle. Check the function table `adc_cali_scheme_t` in `components/esp_adc/interface/adc_cali_interface.h` to know the ESP ADC calibration interface.
  83. Result Conversion
  84. ^^^^^^^^^^^^^^^^^
  85. After setting up the calibration characteristics, you can call :cpp:func:`adc_cali_raw_to_voltage` to convert the ADC raw result into calibrated result. The calibrated result is in the unit of mV. This function may fail due to invalid argument. Especailly, if this function returns :c:macro:`ESP_ERR_INVALID_STATE`, this means the calibration scheme isn't created. You need to create a calibration scheme handle, use :cpp:func:`adc_cali_check_scheme` to know the supported calibration scheme. On the other hand, you could also provide a custom calibration scheme and create the handle.
  86. .. only:: esp32c2
  87. .. note::
  88. ADC calibration is only supported under :c:macro:`ADC_ATTEN_DB_0` and :c:macro:`ADC_ATTEN_DB_11`. Under :c:macro:`ADC_ATTEN_DB_0`, input voltage higher than 950 mV is not supported. Under :c:macro:`ADC_ATTEN_DB_11`, input voltage higher than 2800 mV is not supported.
  89. Get Voltage
  90. ~~~~~~~~~~~
  91. .. code:: c
  92. ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc_cali_handle, adc_raw[0][0], &voltage[0][0]));
  93. ESP_LOGI(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", ADC_UNIT_1 + 1, EXAMPLE_ADC1_CHAN0, voltage[0][0]);
  94. Thread Safety
  95. ^^^^^^^^^^^^^
  96. The factory function :cpp:func:`esp_adc_cali_new_scheme` is guaranteed to be thread safe by the driver. Therefore, you can call them from different RTOS tasks without protection by extra locks.
  97. Other functions that take the :cpp:type:`adc_cali_handle_t` as the first positional parameter are not thread safe, you should avoid calling them from multiple tasks.
  98. .. only:: esp32
  99. Kconfig Options
  100. ^^^^^^^^^^^^^^^
  101. - :ref:`CONFIG_ADC_CAL_EFUSE_TP_ENABLE`, disable this to decrease the code size, if you are aware of the calibration eFuse value :cpp:type:`ADC_CALI_LINE_FITTING_EFUSE_VAL_EFUSE_TP` isn't this one.
  102. - :ref:`CONFIG_ADC_CAL_EFUSE_VREF_ENABLE`, disable this to decrease the code size, if you are aware of the calibration eFuse value :cpp:type:`ADC_CALI_LINE_FITTING_EFUSE_VAL_EFUSE_VREF` isn't this one.
  103. - :ref:`CONFIG_ADC_CAL_LUT_ENABLE`, disable this to decrease the code size, if you don't calibrate the ADC raw results under :c:macro:`ADC_ATTEN_DB_11`.
  104. Minimize Noise
  105. ^^^^^^^^^^^^^^
  106. The {IDF_TARGET_NAME} ADC can be sensitive to noise leading to large discrepancies in ADC readings. Depending on the usage scenario, you may need to connect a bypass capacitor (e.g. a 100 nF ceramic capacitor) to the ADC input pad in use, to minimize noise. Besides, multisampling may also be used to further mitigate the effects of noise.
  107. .. only:: esp32
  108. .. figure:: ../../../_static/diagrams/adc/adc-noise-graph.jpg
  109. :align: center
  110. :alt: ADC noise mitigation
  111. Graph illustrating noise mitigation using capacitor and multisampling of 64 samples.
  112. API Reference
  113. -------------
  114. .. include-build-file:: inc/adc_cali.inc
  115. .. include-build-file:: inc/adc_cali_scheme.inc