adc_hal.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2020 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-C3 specific part)
  15. #include <string.h>
  16. #include "soc/soc_caps.h"
  17. #include "hal/adc_hal.h"
  18. #include "hal/adc_types.h"
  19. #include "soc/soc.h"
  20. //Currently we don't have context for the ADC HAL. So HAL variables are temporarily put here. But
  21. //please don't follow this code. Create a context for your own HAL!
  22. static bool s_filter_enabled[SOC_ADC_DIGI_FILTER_NUM] = {};
  23. static adc_digi_filter_t s_filter[SOC_ADC_DIGI_FILTER_NUM] = {};
  24. static bool s_monitor_enabled[SOC_ADC_DIGI_MONITOR_NUM] = {};
  25. static adc_digi_monitor_t s_monitor_config[SOC_ADC_DIGI_MONITOR_NUM] = {};
  26. /*---------------------------------------------------------------
  27. Digital controller setting
  28. ---------------------------------------------------------------*/
  29. void adc_hal_digi_deinit(void)
  30. {
  31. adc_ll_digi_trigger_disable(); // boss
  32. adc_ll_digi_dma_disable();
  33. adc_ll_digi_clear_pattern_table(ADC_NUM_1);
  34. adc_ll_digi_clear_pattern_table(ADC_NUM_2);
  35. adc_ll_digi_filter_reset(ADC_NUM_1);
  36. adc_ll_digi_filter_reset(ADC_NUM_2);
  37. adc_ll_digi_reset();
  38. adc_ll_digi_controller_clk_disable();
  39. }
  40. /**
  41. * - Set ADC digital controller clock division factor. The clock is divided from `APLL` or `APB` clock.
  42. * Expression: controller_clk = APLL/APB * (div_num + div_a / div_b + 1).
  43. * - Enable clock and select clock source for ADC digital controller.
  44. */
  45. static void adc_hal_digi_clk_config(void)
  46. {
  47. //Here we set the clock divider factor to make the digital clock to 5M Hz
  48. adc_ll_digi_controller_clk_div(ADC_LL_CLKM_DIV_NUM_DEFAULT, ADC_LL_CLKM_DIV_B_DEFAULT, ADC_LL_CLKM_DIV_A_DEFAULT);
  49. adc_ll_digi_controller_clk_enable(0);
  50. }
  51. void adc_hal_digi_controller_config(const adc_digi_config_t *cfg)
  52. {
  53. //only one pattern table is supported on C3, but LL still needs one argument.
  54. const int pattern_both = 0;
  55. if (cfg->adc_pattern_len) {
  56. adc_ll_digi_clear_pattern_table(pattern_both);
  57. adc_ll_digi_set_pattern_table_len(pattern_both, cfg->adc_pattern_len);
  58. for (uint32_t i = 0; i < cfg->adc_pattern_len; i++) {
  59. adc_ll_digi_set_pattern_table(pattern_both, i, cfg->adc_pattern[i]);
  60. }
  61. }
  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. //clock
  69. uint32_t interval = APB_CLK_FREQ / (ADC_LL_CLKM_DIV_NUM_DEFAULT + ADC_LL_CLKM_DIV_A_DEFAULT / ADC_LL_CLKM_DIV_B_DEFAULT + 1) / 2 / cfg->sample_freq_hz;
  70. adc_ll_digi_set_trigger_interval(interval);
  71. adc_hal_digi_clk_config();
  72. }
  73. static void filter_update(adc_digi_filter_idx_t idx)
  74. {
  75. //ESP32-C3 has no enable bit, the filter will be enabled when the filter channel is configured
  76. if (s_filter_enabled[idx]) {
  77. adc_ll_digi_filter_set_factor(idx, &s_filter[idx]);
  78. } else {
  79. adc_ll_digi_filter_disable(idx);
  80. }
  81. }
  82. /**
  83. * Set adc digital controller filter factor.
  84. *
  85. * @param idx ADC filter unit.
  86. * @param filter Filter config. Expression: filter_data = (k-1)/k * last_data + new_data / k. Set values: (2, 4, 8, 16, 64).
  87. */
  88. void adc_hal_digi_filter_set_factor(adc_digi_filter_idx_t idx, adc_digi_filter_t *filter)
  89. {
  90. s_filter[idx] = *filter;
  91. filter_update(idx);
  92. }
  93. /**
  94. * Get adc digital controller filter factor.
  95. *
  96. * @param adc_n ADC unit.
  97. * @param factor Expression: filter_data = (k-1)/k * last_data + new_data / k. Set values: (2, 4, 8, 16, 64).
  98. */
  99. void adc_hal_digi_filter_get_factor(adc_digi_filter_idx_t idx, adc_digi_filter_t *filter)
  100. {
  101. *filter = s_filter[idx];
  102. }
  103. void adc_hal_digi_filter_enable(adc_digi_filter_idx_t filter_idx, bool enable)
  104. {
  105. s_filter_enabled[filter_idx] = enable;
  106. filter_update(filter_idx);
  107. }
  108. static void update_monitor(adc_digi_monitor_idx_t idx)
  109. {
  110. //ESP32-C3 has no enable bit, the monitor will be enabled when the monitor channel is configured
  111. if (s_monitor_enabled[idx]) {
  112. adc_ll_digi_monitor_set_mode(idx, &s_monitor_config[idx]);
  113. } else {
  114. adc_ll_digi_monitor_disable(idx);
  115. }
  116. }
  117. void adc_hal_digi_monitor_config(adc_digi_monitor_idx_t idx, adc_digi_monitor_t *config)
  118. {
  119. s_monitor_config[idx] = *config;
  120. update_monitor(idx);
  121. }
  122. void adc_hal_digi_monitor_enable(adc_digi_monitor_idx_t mon_idx, bool enable)
  123. {
  124. s_monitor_enabled[mon_idx] = enable;
  125. update_monitor(mon_idx);
  126. }