adc_hal.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2015-2019 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // The HAL layer for ADC (ESP32-S2 specific part)
  15. #include "sdkconfig.h"
  16. #include "hal/adc_hal.h"
  17. #include "hal/adc_types.h"
  18. #include "hal/adc_hal_conf.h"
  19. #include "esp_log.h"
  20. /*---------------------------------------------------------------
  21. Digital controller setting
  22. ---------------------------------------------------------------*/
  23. void adc_hal_digi_deinit(void)
  24. {
  25. adc_ll_digi_trigger_disable(); // boss
  26. adc_ll_digi_dma_disable();
  27. adc_ll_digi_clear_pattern_table(ADC_NUM_1);
  28. adc_ll_digi_clear_pattern_table(ADC_NUM_2);
  29. adc_ll_digi_filter_reset(ADC_NUM_1);
  30. adc_ll_digi_filter_reset(ADC_NUM_2);
  31. adc_ll_digi_reset();
  32. adc_ll_digi_controller_clk_disable();
  33. }
  34. void adc_hal_digi_controller_config(const adc_digi_config_t *cfg)
  35. {
  36. /* Single channel mode or multi channel mode. */
  37. adc_ll_digi_set_convert_mode(cfg->conv_mode);
  38. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  39. if (cfg->adc1_pattern_len) {
  40. adc_ll_digi_clear_pattern_table(ADC_NUM_1);
  41. adc_ll_digi_set_pattern_table_len(ADC_NUM_1, cfg->adc1_pattern_len);
  42. for (uint32_t i = 0; i < cfg->adc1_pattern_len; i++) {
  43. adc_ll_digi_set_pattern_table(ADC_NUM_1, i, cfg->adc1_pattern[i]);
  44. }
  45. }
  46. }
  47. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  48. if (cfg->adc2_pattern_len) {
  49. adc_ll_digi_clear_pattern_table(ADC_NUM_2);
  50. adc_ll_digi_set_pattern_table_len(ADC_NUM_2, cfg->adc2_pattern_len);
  51. for (uint32_t i = 0; i < cfg->adc2_pattern_len; i++) {
  52. adc_ll_digi_set_pattern_table(ADC_NUM_2, i, cfg->adc2_pattern[i]);
  53. }
  54. }
  55. }
  56. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  57. adc_ll_set_controller(ADC_NUM_1, ADC_CTRL_DIG);
  58. }
  59. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  60. adc_ll_set_controller(ADC_NUM_2, ADC_CTRL_DIG);
  61. }
  62. adc_ll_digi_set_output_format(cfg->format);
  63. if (cfg->conv_limit_en) {
  64. adc_ll_digi_set_convert_limit_num(cfg->conv_limit_num);
  65. adc_ll_digi_convert_limit_enable();
  66. } else {
  67. adc_ll_digi_convert_limit_disable();
  68. }
  69. adc_ll_digi_set_trigger_interval(cfg->interval);
  70. adc_hal_digi_clk_config(&cfg->dig_clk);
  71. adc_ll_digi_dma_set_eof_num(cfg->dma_eof_num);
  72. }
  73. /**
  74. * Set ADC digital controller clock division factor. The clock divided from `APLL` or `APB` clock.
  75. * Enable clock and select clock source for ADC digital controller.
  76. * Expression: controller_clk = (`APLL` or `APB`) / (div_num + div_a / div_b + 1).
  77. *
  78. * @note ADC and DAC digital controller share the same frequency divider.
  79. * Please set a reasonable frequency division factor to meet the sampling frequency of the ADC and the output frequency of the DAC.
  80. *
  81. * @param clk Refer to ``adc_digi_clk_t``.
  82. */
  83. void adc_hal_digi_clk_config(const adc_digi_clk_t *clk)
  84. {
  85. adc_ll_digi_controller_clk_div(clk->div_num, clk->div_b, clk->div_a);
  86. adc_ll_digi_controller_clk_enable(clk->use_apll);
  87. }
  88. /**
  89. * Enable digital controller to trigger the measurement.
  90. */
  91. void adc_hal_digi_enable(void)
  92. {
  93. adc_ll_digi_dma_enable();
  94. adc_ll_digi_trigger_enable();
  95. }
  96. /**
  97. * Disable digital controller to trigger the measurement.
  98. */
  99. void adc_hal_digi_disable(void)
  100. {
  101. adc_ll_digi_trigger_disable();
  102. adc_ll_digi_dma_disable();
  103. }
  104. /**
  105. * Config monitor of adc digital controller.
  106. *
  107. * @note The monitor will monitor all the enabled channel data of the each ADC unit at the same time.
  108. * @param adc_n ADC unit.
  109. * @param config Refer to ``adc_digi_monitor_t``.
  110. */
  111. void adc_hal_digi_monitor_config(adc_ll_num_t adc_n, adc_digi_monitor_t *config)
  112. {
  113. adc_ll_digi_monitor_set_mode(adc_n, config->mode);
  114. adc_ll_digi_monitor_set_thres(adc_n, config->threshold);
  115. }
  116. /*---------------------------------------------------------------
  117. Common setting
  118. ---------------------------------------------------------------*/
  119. /**
  120. * Config ADC2 module arbiter.
  121. * The arbiter is to improve the use efficiency of ADC2. After the control right is robbed by the high priority,
  122. * the low priority controller will read the invalid ADC2 data, and the validity of the data can be judged by the flag bit in the data.
  123. *
  124. * @note Only ADC2 support arbiter.
  125. * @note The arbiter's working clock is APB_CLK. When the APB_CLK clock drops below 8 MHz, the arbiter must be in shield mode.
  126. * @note Default priority: Wi-Fi > RTC > Digital;
  127. *
  128. * @param config Refer to ``adc_arbiter_t``.
  129. */
  130. void adc_hal_arbiter_config(adc_arbiter_t *config)
  131. {
  132. adc_ll_set_arbiter_work_mode(config->mode);
  133. adc_ll_set_arbiter_priority(config->rtc_pri, config->dig_pri, config->pwdet_pri);
  134. }