adc_share_hw_ctrl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * ADC is shared by multiple components, including:
  8. * - esp_phy
  9. * - esp_wifi
  10. * - driver
  11. *
  12. * However, usages of above components are different.
  13. * Therefore, we put the common used parts into `esp_hw_support`, including:
  14. * - adc power maintainance
  15. * - adc hw calibration settings
  16. * - adc locks, to prevent concurrently using adc hw
  17. */
  18. #include <esp_types.h>
  19. #include "sdkconfig.h"
  20. #include "sys/lock.h"
  21. #include "esp_log.h"
  22. #include "esp_check.h"
  23. #include "freertos/FreeRTOS.h"
  24. #include "hal/adc_types.h"
  25. #include "hal/adc_hal_common.h"
  26. #include "hal/adc_ll.h"
  27. #include "esp_private/adc_share_hw_ctrl.h"
  28. #include "esp_private/sar_periph_ctrl.h"
  29. //For calibration
  30. #if CONFIG_IDF_TARGET_ESP32S2
  31. #include "esp_efuse_rtc_table.h"
  32. #elif SOC_ADC_CALIBRATION_V1_SUPPORTED
  33. #include "esp_efuse_rtc_calib.h"
  34. #endif
  35. static const char *TAG = "adc_share_hw_ctrl";
  36. extern portMUX_TYPE rtc_spinlock;
  37. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  38. /*---------------------------------------------------------------
  39. ADC Hardware Calibration
  40. ---------------------------------------------------------------*/
  41. #if CONFIG_IDF_TARGET_ESP32S2
  42. #define esp_efuse_rtc_calib_get_ver() esp_efuse_rtc_table_read_calib_version()
  43. static inline uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten)
  44. {
  45. int tag = esp_efuse_rtc_table_get_tag(version, adc_unit, atten, RTCCALIB_V2_PARAM_VINIT);
  46. return esp_efuse_rtc_table_get_parsed_efuse_value(tag, false);
  47. }
  48. #endif
  49. static uint32_t s_adc_cali_param[SOC_ADC_PERIPH_NUM][SOC_ADC_ATTEN_NUM] = {};
  50. void adc_calc_hw_calibration_code(adc_unit_t adc_n, adc_atten_t atten)
  51. {
  52. if (s_adc_cali_param[adc_n][atten]) {
  53. ESP_EARLY_LOGV(TAG, "Use calibrated val ADC%d atten=%d: %04X", adc_n + 1, atten, s_adc_cali_param[adc_n][atten]);
  54. return ;
  55. }
  56. // check if we can fetch the values from eFuse.
  57. int version = esp_efuse_rtc_calib_get_ver();
  58. uint32_t init_code = 0;
  59. if ((version >= ESP_EFUSE_ADC_CALIB_VER_MIN) &&
  60. (version <= ESP_EFUSE_ADC_CALIB_VER_MAX)) {
  61. // Guarantee the calibration version before calling efuse function
  62. init_code = esp_efuse_rtc_calib_get_init_code(version, adc_n, atten);
  63. }
  64. #if SOC_ADC_SELF_HW_CALI_SUPPORTED
  65. else {
  66. ESP_EARLY_LOGD(TAG, "Calibration eFuse is not configured, use self-calibration for ICode");
  67. sar_periph_ctrl_adc_oneshot_power_acquire();
  68. portENTER_CRITICAL(&rtc_spinlock);
  69. adc_ll_pwdet_set_cct(ADC_LL_PWDET_CCT_DEFAULT);
  70. const bool internal_gnd = true;
  71. init_code = adc_hal_self_calibration(adc_n, atten, internal_gnd);
  72. portEXIT_CRITICAL(&rtc_spinlock);
  73. sar_periph_ctrl_adc_oneshot_power_release();
  74. }
  75. #else
  76. else {
  77. ESP_EARLY_LOGD(TAG, "ICode self-calibration isn't supported");
  78. }
  79. #endif //SOC_ADC_SELF_HW_CALI_SUPPORTED
  80. s_adc_cali_param[adc_n][atten] = init_code;
  81. ESP_EARLY_LOGV(TAG, "Calib(V%d) ADC%d atten=%d: %04X", version, adc_n + 1, atten, init_code);
  82. }
  83. void IRAM_ATTR adc_set_hw_calibration_code(adc_unit_t adc_n, adc_atten_t atten)
  84. {
  85. adc_hal_set_calibration_param(adc_n, s_adc_cali_param[adc_n][atten]);
  86. }
  87. #if SOC_ADC_CALIB_CHAN_COMPENS_SUPPORTED
  88. static int s_adc_cali_chan_compens[SOC_ADC_MAX_CHANNEL_NUM][SOC_ADC_ATTEN_NUM] = {};
  89. void adc_load_hw_calibration_chan_compens(adc_unit_t adc_n, adc_channel_t chan, adc_atten_t atten)
  90. {
  91. int version = esp_efuse_rtc_calib_get_ver();
  92. if ((version >= ESP_EFUSE_ADC_CALIB_VER_MIN) &&
  93. (version <= ESP_EFUSE_ADC_CALIB_VER_MAX)) {
  94. // Guarantee the calibration version before calling efuse function
  95. s_adc_cali_chan_compens[chan][atten] = esp_efuse_rtc_calib_get_chan_compens(version, adc_n, chan, atten);
  96. }
  97. // No warning when version doesn't match because should has warned in adc_calc_hw_calibration_code
  98. }
  99. int IRAM_ATTR adc_get_hw_calibration_chan_compens(adc_unit_t adc_n, adc_channel_t chan, adc_atten_t atten)
  100. {
  101. return s_adc_cali_chan_compens[chan][atten];
  102. }
  103. #endif // SOC_ADC_CALIB_CHAN_COMPENS_SUPPORTED
  104. #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED
  105. /*---------------------------------------------------------------
  106. ADC Hardware Locks
  107. ---------------------------------------------------------------*/
  108. static _lock_t adc1_lock;
  109. static _lock_t adc2_lock;
  110. esp_err_t adc_lock_acquire(adc_unit_t adc_unit)
  111. {
  112. if (adc_unit == ADC_UNIT_1) {
  113. _lock_acquire(&adc1_lock);
  114. }
  115. if (adc_unit == ADC_UNIT_2) {
  116. _lock_acquire(&adc2_lock);
  117. }
  118. return ESP_OK;
  119. }
  120. esp_err_t adc_lock_release(adc_unit_t adc_unit)
  121. {
  122. if (adc_unit == ADC_UNIT_2) {
  123. ESP_RETURN_ON_FALSE(((uint32_t *)adc2_lock != NULL), ESP_ERR_INVALID_STATE, TAG, "adc2 lock release without acquiring");
  124. _lock_release(&adc2_lock);
  125. }
  126. if (adc_unit == ADC_UNIT_1) {
  127. ESP_RETURN_ON_FALSE(((uint32_t *)adc1_lock != NULL), ESP_ERR_INVALID_STATE, TAG, "adc1 lock release without acquiring");
  128. _lock_release(&adc1_lock);
  129. }
  130. return ESP_OK;
  131. }
  132. esp_err_t adc_lock_try_acquire(adc_unit_t adc_unit)
  133. {
  134. if (adc_unit == ADC_UNIT_1) {
  135. if (_lock_try_acquire(&adc1_lock) == -1) {
  136. return ESP_ERR_TIMEOUT;
  137. }
  138. }
  139. if (adc_unit == ADC_UNIT_2) {
  140. if (_lock_try_acquire(&adc2_lock) == -1) {
  141. return ESP_ERR_TIMEOUT;
  142. }
  143. }
  144. return ESP_OK;
  145. }
  146. esp_err_t adc2_wifi_acquire(void)
  147. {
  148. #if CONFIG_IDF_TARGET_ESP32
  149. /* Wi-Fi module will use adc2. Use locks to avoid conflicts. */
  150. adc_lock_acquire(ADC_UNIT_2);
  151. ESP_LOGD(TAG, "Wi-Fi takes adc2 lock.");
  152. #endif
  153. return ESP_OK;
  154. }
  155. esp_err_t adc2_wifi_release(void)
  156. {
  157. #if CONFIG_IDF_TARGET_ESP32
  158. return adc_lock_release(ADC_UNIT_2);
  159. #endif
  160. return ESP_OK;
  161. }