adc_hal.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /*---------------------------------------------------------------
  20. Digital controller setting
  21. ---------------------------------------------------------------*/
  22. void adc_hal_digi_deinit(void)
  23. {
  24. adc_ll_digi_trigger_disable(); // boss
  25. adc_ll_digi_dma_disable();
  26. adc_ll_digi_clear_pattern_table(ADC_NUM_1);
  27. adc_ll_digi_clear_pattern_table(ADC_NUM_2);
  28. adc_ll_digi_filter_reset(ADC_NUM_1);
  29. adc_ll_digi_filter_reset(ADC_NUM_2);
  30. adc_ll_digi_reset();
  31. adc_ll_digi_controller_clk_disable();
  32. }
  33. void adc_hal_digi_controller_config(const adc_digi_config_t *cfg)
  34. {
  35. /* Single channel mode or multi channel mode. */
  36. adc_ll_digi_set_convert_mode(cfg->conv_mode);
  37. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  38. if (cfg->adc1_pattern_len) {
  39. adc_ll_digi_clear_pattern_table(ADC_NUM_1);
  40. adc_ll_digi_set_pattern_table_len(ADC_NUM_1, cfg->adc1_pattern_len);
  41. for (uint32_t i = 0; i < cfg->adc1_pattern_len; i++) {
  42. adc_ll_digi_set_pattern_table(ADC_NUM_1, i, cfg->adc1_pattern[i]);
  43. }
  44. }
  45. }
  46. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  47. if (cfg->adc2_pattern_len) {
  48. adc_ll_digi_clear_pattern_table(ADC_NUM_2);
  49. adc_ll_digi_set_pattern_table_len(ADC_NUM_2, cfg->adc2_pattern_len);
  50. for (uint32_t i = 0; i < cfg->adc2_pattern_len; i++) {
  51. adc_ll_digi_set_pattern_table(ADC_NUM_2, i, cfg->adc2_pattern[i]);
  52. }
  53. }
  54. }
  55. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  56. adc_ll_set_controller(ADC_NUM_1, ADC_CTRL_DIG);
  57. }
  58. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  59. adc_ll_set_controller(ADC_NUM_2, ADC_CTRL_DIG);
  60. }
  61. adc_ll_digi_set_output_format(cfg->format);
  62. if (cfg->conv_limit_en) {
  63. adc_ll_digi_set_convert_limit_num(cfg->conv_limit_num);
  64. adc_ll_digi_convert_limit_enable();
  65. } else {
  66. adc_ll_digi_convert_limit_disable();
  67. }
  68. adc_ll_digi_set_trigger_interval(cfg->interval);
  69. adc_hal_digi_clk_config(&cfg->dig_clk);
  70. adc_ll_digi_dma_set_eof_num(cfg->dma_eof_num);
  71. }
  72. /**
  73. * Set ADC digital controller clock division factor. The clock divided from `APLL` or `APB` clock.
  74. * Enable clock and select clock source for ADC digital controller.
  75. * Expression: controller_clk = (`APLL` or `APB`) / (div_num + div_a / div_b + 1).
  76. *
  77. * @note ADC and DAC digital controller share the same frequency divider.
  78. * Please set a reasonable frequency division factor to meet the sampling frequency of the ADC and the output frequency of the DAC.
  79. *
  80. * @param clk Refer to ``adc_digi_clk_t``.
  81. */
  82. void adc_hal_digi_clk_config(const adc_digi_clk_t *clk)
  83. {
  84. adc_ll_digi_controller_clk_div(clk->div_num, clk->div_b, clk->div_a);
  85. adc_ll_digi_controller_clk_enable(clk->use_apll);
  86. }
  87. /**
  88. * Enable digital controller to trigger the measurement.
  89. */
  90. void adc_hal_digi_enable(void)
  91. {
  92. adc_ll_digi_dma_enable();
  93. adc_ll_digi_trigger_enable();
  94. }
  95. /**
  96. * Disable digital controller to trigger the measurement.
  97. */
  98. void adc_hal_digi_disable(void)
  99. {
  100. adc_ll_digi_trigger_disable();
  101. adc_ll_digi_dma_disable();
  102. }
  103. /**
  104. * Config monitor of adc digital controller.
  105. *
  106. * @note The monitor will monitor all the enabled channel data of the each ADC unit at the same time.
  107. * @param adc_n ADC unit.
  108. * @param config Refer to ``adc_digi_monitor_t``.
  109. */
  110. void adc_hal_digi_monitor_config(adc_ll_num_t adc_n, adc_digi_monitor_t *config)
  111. {
  112. adc_ll_digi_monitor_set_mode(adc_n, config->mode);
  113. adc_ll_digi_monitor_set_thres(adc_n, config->threshold);
  114. }