Page 1 of 1

ESP32S2和S3 ADC采样速率的问题

Posted: Tue Mar 01, 2022 11:04 am
by xinhaojie
咨询一下,ESP32S2和S3中的RTC ADC和DIG ADC的最大采样速率和 ESP32的一样吗,是RTC 200KHz, DIG 2MHz吗?,我看手册上S2和S3对最大速率均未描述。

Re: ESP32S2和S3 ADC采样速率的问题

Posted: Wed Mar 16, 2022 9:09 am
by Kevin_WWW
我开了一个新Task计数,DMA ADC每秒采6400次
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include "sdkconfig.h"
  4. #include "esp_log.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "freertos/semphr.h"
  8. #include "driver/adc.h"
  9.  
  10. #define TIMES 256
  11. #define GET_UNIT(x) ((x >> 3) & 0x1)
  12.  
  13. #define ADC_RESULT_BYTE 4
  14. #define ADC_CONV_LIMIT_EN 0
  15. #define ADC_CONV_MODE ADC_CONV_ALTER_UNIT
  16. #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
  17.  
  18. static uint16_t adc1_chan_mask = BIT(2) | BIT(3) | BIT(4);
  19. static uint16_t adc2_chan_mask = BIT(0);
  20. static adc_channel_t channel[4] = {ADC1_CHANNEL_2, ADC1_CHANNEL_3, ADC1_CHANNEL_4, (ADC2_CHANNEL_0 | 1 << 3)};
  21.  
  22. static const char *TAG = "ADC DMA";
  23. int value_channel_2[1100] = {0};
  24. int value_channel_3[1100] = {0};
  25. int value_channel_4[1100] = {0};
  26. int index_channel_2 = 0;
  27. int index_channel_3 = 0;
  28. int index_channel_4 = 0;
  29. int sum_temp = 0;
  30.  
  31. int countNum = 0;
  32.  
  33. static void
  34. continuous_adc_init(uint16_t adc1_chan_mask, uint16_t adc2_chan_mask, adc_channel_t *channel, uint8_t channel_num)
  35. {
  36.     adc_digi_init_config_t adc_dma_config = {
  37.         .max_store_buf_size = 1024,
  38.         .conv_num_each_intr = TIMES,
  39.         .adc1_chan_mask = adc1_chan_mask,
  40.         .adc2_chan_mask = adc2_chan_mask,
  41.     };
  42.     ESP_ERROR_CHECK(adc_digi_initialize(&adc_dma_config));
  43.  
  44.     adc_digi_configuration_t dig_cfg = {
  45.         .conv_limit_en = ADC_CONV_LIMIT_EN,
  46.         .conv_limit_num = 250,
  47.         .sample_freq_hz = 10 * 1000,
  48.         .conv_mode = ADC_CONV_MODE,
  49.         .format = ADC_OUTPUT_TYPE,
  50.     };
  51.  
  52.     adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0};
  53.     dig_cfg.pattern_num = channel_num;
  54.     for (int i = 0; i < channel_num; i++)
  55.     {
  56.         uint8_t unit = GET_UNIT(channel[i]);
  57.         uint8_t ch = channel[i] & 0x7;
  58.         adc_pattern[i].atten = ADC_ATTEN_DB_11;
  59.         adc_pattern[i].channel = ch;
  60.         adc_pattern[i].unit = unit;
  61.         adc_pattern[i].bit_width = SOC_ADC_DIGI_MAX_BITWIDTH;
  62.  
  63.         ESP_LOGI(TAG, "adc_pattern[%d].atten is :%x", i, adc_pattern[i].atten);
  64.         ESP_LOGI(TAG, "adc_pattern[%d].channel is :%x", i, adc_pattern[i].channel);
  65.         ESP_LOGI(TAG, "adc_pattern[%d].unit is :%x", i, adc_pattern[i].unit);
  66.     }
  67.     dig_cfg.adc_pattern = adc_pattern;
  68.     ESP_ERROR_CHECK(adc_digi_controller_configure(&dig_cfg));
  69. }
  70.  
  71. #if !CONFIG_IDF_TARGET_ESP32
  72.  
  73. static bool check_valid_data(const adc_digi_output_data_t *data)
  74. {
  75.     const unsigned int unit = data->type2.unit;
  76.     if (unit > 2)
  77.         return false;
  78.     if (data->type2.channel >= SOC_ADC_CHANNEL_NUM(unit))
  79.         return false;
  80.  
  81.     return true;
  82. }
  83.  
  84. #endif
  85.  
  86. void LOG_ADV_VALUE(int index_channel, const int *value_channel, char * LOG_TAG)
  87. {
  88.     // for (int i = 0; i < index_channel; ++i)
  89.     // { sum_temp += value_channel[i]; }
  90.     // ESP_LOGI(LOG_TAG, "%d", (int)(sum_temp / index_channel));
  91.     ESP_LOGI(LOG_TAG, "%d", value_channel[1]);
  92.     sum_temp = 0;
  93. }
  94.  
  95. void clearTask(void *param)
  96. {
  97.     while (1)
  98.     {
  99.         vTaskDelay(250 / portTICK_PERIOD_MS);
  100.         LOG_ADV_VALUE(index_channel_2, value_channel_2,"channel 2 adv value");
  101.         LOG_ADV_VALUE(index_channel_3, value_channel_3,"channel 3 adv value");
  102.         LOG_ADV_VALUE(index_channel_4, value_channel_4,"channel 4 adv value");
  103.  
  104.         index_channel_2 = 0;
  105.         index_channel_3 = 0;
  106.         index_channel_4 = 0;
  107.     }
  108. }
  109.  
  110. void countTask(void *param)
  111. {
  112.     while (1)
  113.     {
  114.         vTaskDelay(1000 / portTICK_PERIOD_MS);
  115.         printf("count=%d\n", countNum);
  116.         countNum = 0;
  117.     }
  118. }
  119.  
  120. void app_main(void)
  121. {
  122.     xTaskCreate(clearTask, "clearTask", 1024 * 5, NULL, 1, NULL);
  123.     xTaskCreate(countTask, "countTask", 1024 * 5, NULL, 2, NULL);
  124.     continuous_adc_init(adc1_chan_mask, adc2_chan_mask, channel, sizeof(channel) / sizeof(adc_channel_t));
  125.     adc_digi_start();
  126.     uint32_t ret_num = 0;
  127.     uint8_t result[TIMES] = {0};
  128.     memset(result, 0xcc, TIMES);
  129.     while (1)
  130.     {
  131.         adc_digi_read_bytes(result, TIMES, &ret_num, ADC_MAX_DELAY);
  132.         for (int i = 0; i < ret_num; i += ADC_RESULT_BYTE)
  133.         {
  134.             adc_digi_output_data_t *p = (void *)&result[i];
  135.             if (check_valid_data(p))
  136.             {
  137.                 if (((p->type2.unit + 1) == 1) || (p->type2.channel >= 2) || (p->type2.channel <= 4))
  138.                 {
  139.                     // ESP_LOGI(TAG, "Unit: %d,_Channel: %d, Value: %d", p->type2.unit + 1, p->type2.channel,
  140.                     //          p->type2.data);
  141.                     countNum++;
  142.                     switch (p->type2.channel)
  143.                     {
  144.                     case 2:
  145.                         value_channel_2[index_channel_2] = p->type2.data;
  146.                         index_channel_2++;
  147.                         break;
  148.                     case 3:
  149.                         value_channel_3[index_channel_3] = p->type2.data;
  150.                         index_channel_3++;
  151.                         break;
  152.                     case 4:
  153.                         value_channel_4[index_channel_4] = p->type2.data;
  154.                         index_channel_4++;
  155.                         break;
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.         vTaskDelay(1);
  161.     }
  162. }
  163.  
  164.