adc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include "sdkconfig.h"
  9. #include "esp_types.h"
  10. #include "esp_log.h"
  11. #include "sys/lock.h"
  12. #include "freertos/FreeRTOS.h"
  13. #include "hal/adc_types.h"
  14. #include "hal/adc_ll.h"
  15. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  16. #define ADC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  17. #define ADC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  18. /**
  19. * Config monitor of adc digital controller.
  20. *
  21. * @note The monitor will monitor all the enabled channel data of the each ADC unit at the same time.
  22. * @param adc_n ADC unit.
  23. * @param config Refer to ``adc_digi_monitor_t``.
  24. */
  25. static void adc_digi_monitor_config(adc_ll_num_t adc_n, adc_digi_monitor_t *config)
  26. {
  27. adc_ll_digi_monitor_set_mode(adc_n, config->mode);
  28. adc_ll_digi_monitor_set_thres(adc_n, config->threshold);
  29. }
  30. /*************************************/
  31. /* Digital controller filter setting */
  32. /*************************************/
  33. esp_err_t adc_digi_filter_reset(adc_digi_filter_idx_t idx)
  34. {
  35. ADC_ENTER_CRITICAL();
  36. if (idx == ADC_DIGI_FILTER_IDX0) {
  37. adc_ll_digi_filter_reset(ADC_NUM_1);
  38. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  39. adc_ll_digi_filter_reset(ADC_NUM_2);
  40. }
  41. ADC_EXIT_CRITICAL();
  42. return ESP_OK;
  43. }
  44. esp_err_t adc_digi_filter_set_config(adc_digi_filter_idx_t idx, adc_digi_filter_t *config)
  45. {
  46. ADC_ENTER_CRITICAL();
  47. if (idx == ADC_DIGI_FILTER_IDX0) {
  48. adc_ll_digi_filter_set_factor(ADC_NUM_1, config->mode);
  49. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  50. adc_ll_digi_filter_set_factor(ADC_NUM_2, config->mode);
  51. }
  52. ADC_EXIT_CRITICAL();
  53. return ESP_OK;
  54. }
  55. esp_err_t adc_digi_filter_get_config(adc_digi_filter_idx_t idx, adc_digi_filter_t *config)
  56. {
  57. ADC_ENTER_CRITICAL();
  58. if (idx == ADC_DIGI_FILTER_IDX0) {
  59. config->adc_unit = ADC_UNIT_1;
  60. config->channel = ADC_CHANNEL_MAX;
  61. adc_ll_digi_filter_get_factor(ADC_NUM_1, &config->mode);
  62. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  63. config->adc_unit = ADC_UNIT_2;
  64. config->channel = ADC_CHANNEL_MAX;
  65. adc_ll_digi_filter_get_factor(ADC_NUM_2, &config->mode);
  66. }
  67. ADC_EXIT_CRITICAL();
  68. return ESP_OK;
  69. }
  70. esp_err_t adc_digi_filter_enable(adc_digi_filter_idx_t idx, bool enable)
  71. {
  72. ADC_ENTER_CRITICAL();
  73. if (idx == ADC_DIGI_FILTER_IDX0) {
  74. adc_ll_digi_filter_enable(ADC_NUM_1, enable);
  75. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  76. adc_ll_digi_filter_enable(ADC_NUM_2, enable);
  77. }
  78. ADC_EXIT_CRITICAL();
  79. return ESP_OK;
  80. }
  81. /**
  82. * @brief Get the filtered data of adc digital controller filter. For debug.
  83. * The data after each measurement and filtering is updated to the DMA by the digital controller. But it can also be obtained manually through this API.
  84. *
  85. * @note For ESP32S2, The filter will filter all the enabled channel data of the each ADC unit at the same time.
  86. * @param idx Filter index.
  87. * @return Filtered data. if <0, the read data invalid.
  88. */
  89. int adc_digi_filter_read_data(adc_digi_filter_idx_t idx)
  90. {
  91. if (idx == ADC_DIGI_FILTER_IDX0) {
  92. return adc_ll_digi_filter_read_data(ADC_NUM_1);
  93. } else if (idx == ADC_DIGI_FILTER_IDX1) {
  94. return adc_ll_digi_filter_read_data(ADC_NUM_2);
  95. } else {
  96. return -1;
  97. }
  98. }
  99. /**************************************/
  100. /* Digital controller monitor setting */
  101. /**************************************/
  102. esp_err_t adc_digi_monitor_set_config(adc_digi_monitor_idx_t idx, adc_digi_monitor_t *config)
  103. {
  104. ADC_ENTER_CRITICAL();
  105. if (idx == ADC_DIGI_MONITOR_IDX0) {
  106. adc_digi_monitor_config(ADC_NUM_1, config);
  107. } else if (idx == ADC_DIGI_MONITOR_IDX1) {
  108. adc_digi_monitor_config(ADC_NUM_2, config);
  109. }
  110. ADC_EXIT_CRITICAL();
  111. return ESP_OK;
  112. }
  113. esp_err_t adc_digi_monitor_enable(adc_digi_monitor_idx_t idx, bool enable)
  114. {
  115. ADC_ENTER_CRITICAL();
  116. if (idx == ADC_DIGI_MONITOR_IDX0) {
  117. adc_ll_digi_monitor_enable(ADC_NUM_1, enable);
  118. } else if (idx == ADC_DIGI_MONITOR_IDX1) {
  119. adc_ll_digi_monitor_enable(ADC_NUM_2, enable);
  120. }
  121. ADC_EXIT_CRITICAL();
  122. return ESP_OK;
  123. }