adc_oneshot_hal.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2021 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_hal_conf.h"
  12. #include "hal/adc_ll.h"
  13. #include "hal/assert.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. }
  32. void adc_oneshot_hal_channel_config(adc_oneshot_hal_ctx_t *hal, const adc_oneshot_hal_chan_cfg_t *config, adc_channel_t chan)
  33. {
  34. hal->chan_configs[chan].atten = config->atten;
  35. hal->chan_configs[chan].bitwidth = config->bitwidth;
  36. }
  37. void adc_oneshot_hal_setup(adc_oneshot_hal_ctx_t *hal, adc_channel_t chan)
  38. {
  39. adc_unit_t unit = hal->unit;
  40. #ifdef CONFIG_IDF_TARGET_ESP32
  41. adc_ll_hall_disable(); //Disable other peripherals.
  42. adc_ll_amp_disable(); //Currently the LNA is not open, close it by default.
  43. #endif
  44. #if CONFIG_ADC_DISABLE_DAC_OUTPUT
  45. s_disable_dac(hal, chan);
  46. #endif
  47. #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
  48. adc_ll_digi_clk_sel(0);
  49. #else
  50. adc_ll_set_sar_clk_div(unit, ADC_HAL_SAR_CLK_DIV_DEFAULT(unit));
  51. if (unit == ADC_UNIT_2) {
  52. adc_ll_pwdet_set_cct(ADC_HAL_PWDET_CCT_DEFAULT);
  53. }
  54. #endif
  55. adc_oneshot_ll_output_invert(unit, ADC_HAL_DATA_INVERT_DEFAULT(unit));
  56. adc_oneshot_ll_set_atten(unit, chan, hal->chan_configs[chan].atten);
  57. adc_oneshot_ll_set_output_bits(unit, hal->chan_configs[chan].bitwidth);
  58. adc_oneshot_ll_set_channel(unit, chan);
  59. adc_hal_set_controller(unit, hal->work_mode);
  60. #if SOC_ADC_ARBITER_SUPPORTED
  61. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  62. adc_hal_arbiter_config(&config);
  63. #endif //#if SOC_ADC_ARBITER_SUPPORTED
  64. }
  65. static void adc_hal_onetime_start(adc_unit_t unit)
  66. {
  67. #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
  68. (void)unit;
  69. /**
  70. * 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
  71. * ADC digital controller (when its clock frequency is too slow). A rough estimate for this step should be at least 3 ADC digital controller
  72. * clock cycle.
  73. *
  74. * This limitation will be removed in hardware future versions.
  75. *
  76. */
  77. uint32_t digi_clk = APB_CLK_FREQ / (ADC_LL_CLKM_DIV_NUM_DEFAULT + ADC_LL_CLKM_DIV_A_DEFAULT / ADC_LL_CLKM_DIV_B_DEFAULT + 1);
  78. //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.
  79. uint32_t delay = (1000 * 1000) / digi_clk + 1;
  80. //3 ADC digital controller clock cycle
  81. delay = delay * 3;
  82. //This coefficient (8) is got from test. When digi_clk is not smaller than ``APB_CLK_FREQ/8``, no delay is needed.
  83. if (digi_clk >= APB_CLK_FREQ/8) {
  84. delay = 0;
  85. }
  86. adc_oneshot_ll_start(false);
  87. esp_rom_delay_us(delay);
  88. adc_oneshot_ll_start(true);
  89. //No need to delay here. Becuase if the start signal is not seen, there won't be a done intr.
  90. #else
  91. adc_oneshot_ll_start(unit);
  92. #endif
  93. }
  94. bool adc_oneshot_hal_convert(adc_oneshot_hal_ctx_t *hal, int *out_raw)
  95. {
  96. bool valid = true;
  97. uint32_t event = 0;
  98. if (hal->unit == ADC_UNIT_1) {
  99. event = ADC_LL_EVENT_ADC1_ONESHOT_DONE;
  100. } else {
  101. event = ADC_LL_EVENT_ADC2_ONESHOT_DONE;
  102. }
  103. adc_oneshot_ll_clear_event(event);
  104. adc_oneshot_ll_disable_all_unit();
  105. adc_oneshot_ll_enable(hal->unit);
  106. adc_hal_onetime_start(hal->unit);
  107. while (!adc_oneshot_ll_get_event(event)) {
  108. ;
  109. }
  110. *out_raw = adc_oneshot_ll_get_raw_result(hal->unit);
  111. #if (SOC_ADC_PERIPH_NUM == 2)
  112. if (hal->unit == ADC_UNIT_2) {
  113. valid = adc_oneshot_ll_raw_check_valid(ADC_UNIT_2, *out_raw);
  114. if (!valid) {
  115. *out_raw = -1;
  116. }
  117. }
  118. #endif
  119. adc_oneshot_ll_disable_all_unit();
  120. return valid;
  121. }
  122. /*---------------------------------------------------------------
  123. Workarounds
  124. ---------------------------------------------------------------*/
  125. #if CONFIG_ADC_DISABLE_DAC_OUTPUT
  126. static void s_disable_dac(adc_oneshot_hal_ctx_t *hal, adc_channel_t channel)
  127. {
  128. /**
  129. * Workaround: Disable the synchronization operation function of ADC1 and DAC.
  130. * If enabled(default), ADC RTC controller sampling will cause the DAC channel output voltage.
  131. */
  132. if (hal->unit == ADC_UNIT_1) {
  133. dac_ll_rtc_sync_by_adc(false);
  134. }
  135. #if CONFIG_IDF_TARGET_ESP32
  136. if (hal->unit == ADC_UNIT_2) {
  137. if (channel == ADC_CHANNEL_8) {
  138. dac_ll_power_down(DAC_CHANNEL_1); // the same as DAC channel 1
  139. }
  140. if (channel == ADC_CHANNEL_9) {
  141. dac_ll_power_down(DAC_CHANNEL_2);
  142. }
  143. }
  144. #elif CONFIG_IDF_TARGET_ESP32S2
  145. if (hal->unit == ADC_UNIT_2) {
  146. if (channel == ADC_CHANNEL_6) {
  147. dac_ll_power_down(DAC_CHANNEL_1); // the same as DAC channel 1
  148. }
  149. if (channel == ADC_CHANNEL_7) {
  150. dac_ll_power_down(DAC_CHANNEL_2);
  151. }
  152. }
  153. #else
  154. //Nothing needed (DAC is only supported on ESP32 and ESP32S2), add this if future chips needs
  155. #endif
  156. }
  157. #endif