adc_oneshot_hal.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/param.h>
  7. #include "sdkconfig.h"
  8. #include "soc/soc_caps.h"
  9. #include "hal/adc_oneshot_hal.h"
  10. #include "hal/adc_hal_common.h"
  11. #include "hal/adc_ll.h"
  12. #include "hal/assert.h"
  13. #include "hal/log.h"
  14. #if SOC_DAC_SUPPORTED
  15. #include "hal/dac_ll.h"
  16. #endif
  17. #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
  18. /**
  19. * For chips without RTC controller, Digital controller is used to trigger an ADC single read.
  20. */
  21. #include "esp_rom_sys.h"
  22. #endif
  23. #if CONFIG_ADC_DISABLE_DAC_OUTPUT
  24. // To disable DAC, workarounds, see this function body to know more
  25. static void s_disable_dac(adc_oneshot_hal_ctx_t *hal, adc_channel_t channel);
  26. #endif
  27. void adc_oneshot_hal_init(adc_oneshot_hal_ctx_t *hal, const adc_oneshot_hal_cfg_t *config)
  28. {
  29. hal->unit = config->unit;
  30. hal->work_mode = config->work_mode;
  31. hal->clk_src = config->clk_src;
  32. hal->clk_src_freq_hz = config->clk_src_freq_hz;
  33. }
  34. void adc_oneshot_hal_channel_config(adc_oneshot_hal_ctx_t *hal, const adc_oneshot_hal_chan_cfg_t *config, adc_channel_t chan)
  35. {
  36. hal->chan_configs[chan].atten = config->atten;
  37. hal->chan_configs[chan].bitwidth = config->bitwidth;
  38. }
  39. void adc_oneshot_hal_setup(adc_oneshot_hal_ctx_t *hal, adc_channel_t chan)
  40. {
  41. adc_unit_t unit = hal->unit;
  42. #ifdef CONFIG_IDF_TARGET_ESP32
  43. adc_ll_hall_disable(); //Disable other peripherals.
  44. adc_ll_amp_disable(); //Currently the LNA is not open, close it by default.
  45. #endif
  46. #if CONFIG_ADC_DISABLE_DAC_OUTPUT
  47. s_disable_dac(hal, chan);
  48. #endif
  49. #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
  50. adc_ll_digi_clk_sel(hal->clk_src);
  51. adc_ll_digi_controller_clk_div(ADC_LL_CLKM_DIV_NUM_DEFAULT, ADC_LL_CLKM_DIV_A_DEFAULT, ADC_LL_CLKM_DIV_B_DEFAULT);
  52. adc_ll_digi_set_clk_div(ADC_LL_DIGI_SAR_CLK_DIV_DEFAULT);
  53. #else
  54. adc_ll_set_sar_clk_div(unit, ADC_LL_SAR_CLK_DIV_DEFAULT(unit));
  55. if (unit == ADC_UNIT_2) {
  56. adc_ll_pwdet_set_cct(ADC_LL_PWDET_CCT_DEFAULT);
  57. }
  58. #endif
  59. adc_oneshot_ll_output_invert(unit, ADC_LL_DATA_INVERT_DEFAULT(unit));
  60. adc_oneshot_ll_set_atten(unit, chan, hal->chan_configs[chan].atten);
  61. adc_oneshot_ll_set_output_bits(unit, hal->chan_configs[chan].bitwidth);
  62. adc_oneshot_ll_set_channel(unit, chan);
  63. adc_hal_set_controller(unit, hal->work_mode);
  64. #if SOC_ADC_ARBITER_SUPPORTED
  65. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  66. adc_hal_arbiter_config(&config);
  67. #endif //#if SOC_ADC_ARBITER_SUPPORTED
  68. }
  69. static void adc_hal_onetime_start(adc_unit_t unit, uint32_t clk_src_freq_hz, uint32_t *read_delay_us)
  70. {
  71. #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
  72. (void)unit;
  73. /**
  74. * There is a hardware limitation. If the APB clock frequency is high, the step of this reg signal: ``onetime_start`` may not be captured by the
  75. * ADC digital controller (when its clock frequency is too slow). A rough estimate for this step should be at least 3 ADC digital controller
  76. * clock cycle.
  77. */
  78. uint32_t adc_ctrl_clk = clk_src_freq_hz / (ADC_LL_CLKM_DIV_NUM_DEFAULT + ADC_LL_CLKM_DIV_A_DEFAULT / ADC_LL_CLKM_DIV_B_DEFAULT + 1);
  79. //Convert frequency to time (us). Since decimals are removed by this division operation. Add 1 here in case of the fact that delay is not enough.
  80. uint32_t sample_delay_us = ((1000 * 1000) / adc_ctrl_clk + 1) * 3;
  81. HAL_EARLY_LOGD("adc_hal", "clk_src_freq_hz: %"PRIu32", adc_ctrl_clk: %"PRIu32", sample_delay_us: %"PRIu32"", clk_src_freq_hz, adc_ctrl_clk, sample_delay_us);
  82. //This coefficient (8) is got from test, and verified from DT. When digi_clk is not smaller than ``APB_CLK_FREQ/8``, no delay is needed.
  83. if (adc_ctrl_clk >= APB_CLK_FREQ/8) {
  84. sample_delay_us = 0;
  85. }
  86. HAL_EARLY_LOGD("adc_hal", "delay for `onetime_start` signal captured: %"PRIu32"", sample_delay_us);
  87. adc_oneshot_ll_start(false);
  88. esp_rom_delay_us(sample_delay_us);
  89. adc_oneshot_ll_start(true);
  90. #if ADC_LL_DELAY_CYCLE_AFTER_DONE_SIGNAL
  91. /**
  92. * There is a hardware limitation.
  93. * After ADC get DONE signal, it still need a delay to synchronize ADC raw data or it may get zero.
  94. * A rough estimate for this step should be at least ADC_LL_DELAY_CYCLE_AFTER_DONE_SIGNAL ADC sar clock cycle.
  95. */
  96. uint32_t sar_clk = adc_ctrl_clk / ADC_LL_DIGI_SAR_CLK_DIV_DEFAULT;
  97. *read_delay_us = ((1000 * 1000) / sar_clk + 1) * ADC_LL_DELAY_CYCLE_AFTER_DONE_SIGNAL;
  98. HAL_EARLY_LOGD("adc_hal", "clk_src_freq_hz: %"PRIu32", sar_clk: %"PRIu32", read_delay_us: %"PRIu32"", clk_src_freq_hz, sar_clk, read_delay_us);
  99. #endif //ADC_LL_DELAY_CYCLE_AFTER_DONE_SIGNAL
  100. #else
  101. adc_oneshot_ll_start(unit);
  102. #endif // SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
  103. }
  104. bool adc_oneshot_hal_convert(adc_oneshot_hal_ctx_t *hal, int *out_raw)
  105. {
  106. bool valid = true;
  107. uint32_t event = 0;
  108. uint32_t read_delay_us = 0;
  109. if (hal->unit == ADC_UNIT_1) {
  110. event = ADC_LL_EVENT_ADC1_ONESHOT_DONE;
  111. } else {
  112. event = ADC_LL_EVENT_ADC2_ONESHOT_DONE;
  113. }
  114. adc_oneshot_ll_clear_event(event);
  115. adc_oneshot_ll_disable_all_unit();
  116. adc_oneshot_ll_enable(hal->unit);
  117. adc_hal_onetime_start(hal->unit, hal->clk_src_freq_hz, &read_delay_us);
  118. while (!adc_oneshot_ll_get_event(event)) {
  119. ;
  120. }
  121. esp_rom_delay_us(read_delay_us);
  122. *out_raw = adc_oneshot_ll_get_raw_result(hal->unit);
  123. #if (SOC_ADC_PERIPH_NUM == 2)
  124. if (hal->unit == ADC_UNIT_2) {
  125. valid = adc_oneshot_ll_raw_check_valid(ADC_UNIT_2, *out_raw);
  126. if (!valid) {
  127. *out_raw = -1;
  128. }
  129. }
  130. #endif
  131. adc_oneshot_ll_disable_all_unit();
  132. return valid;
  133. }
  134. /*---------------------------------------------------------------
  135. Workarounds
  136. ---------------------------------------------------------------*/
  137. #if CONFIG_ADC_DISABLE_DAC_OUTPUT
  138. static void s_disable_dac(adc_oneshot_hal_ctx_t *hal, adc_channel_t channel)
  139. {
  140. /**
  141. * Workaround: Disable the synchronization operation function of ADC1 and DAC.
  142. * If enabled(default), ADC RTC controller sampling will cause the DAC channel output voltage.
  143. */
  144. if (hal->unit == ADC_UNIT_1) {
  145. dac_ll_rtc_sync_by_adc(false);
  146. }
  147. #if CONFIG_IDF_TARGET_ESP32
  148. if (hal->unit == ADC_UNIT_2) {
  149. if (channel == ADC_CHANNEL_8) {
  150. dac_ll_power_down(DAC_CHAN_0); // the same as DAC channel 0
  151. }
  152. if (channel == ADC_CHANNEL_9) {
  153. dac_ll_power_down(DAC_CHAN_1);
  154. }
  155. }
  156. #elif CONFIG_IDF_TARGET_ESP32S2
  157. if (hal->unit == ADC_UNIT_2) {
  158. if (channel == ADC_CHANNEL_6) {
  159. dac_ll_power_down(DAC_CHAN_0); // the same as DAC channel 0
  160. }
  161. if (channel == ADC_CHANNEL_7) {
  162. dac_ll_power_down(DAC_CHAN_1);
  163. }
  164. }
  165. #else
  166. //Nothing needed (DAC is only supported on ESP32 and ESP32S2), add this if future chips needs
  167. #endif
  168. }
  169. #endif