Page 1 of 1

Using Arduino for continuous ADC--limitations?

Posted: Sun Jun 04, 2023 3:20 pm
by jlhavens
Using Arduino on Platformio (Arduino 2.0.9 and espressif32@6.3.0); taking the example provided in the ESP-IDF for continuous ADC; and by modifying a few function call names running in an Arduino environment.

On an esp32s2 I am sampling ADC1_CHANNELs; two at a time as in the example.

Using ADC_CONV_MODE = ADC_CONV_SINGLE_UNIT_1 I can only access channels ADC1_CHANNEL0 - ADC1_CHANNEL4. Trying to use higher channels seems to just skip over them.

Using ADC_CONV_MODE = ADC_CONV_BOTH_UNIT I can only access channels ADC1_CHANNEL0 - ADC1_CHANNEL7. Trying to use higher channels seems to just skip over them, too.

Even after adjusting values for adc_dma_config .max_store_buf_size and adc_dma_config.conv_num_each_intr I seem to be limited to about 2048 as the max out_length per adc_digi_read_bytes call.

Are these limitations of the esp32s2 and/or the software or am I doing something wrong?

Any comments or advise would be appreciated.


Here is the code

Code: Select all

#include "Arduino.h"

#include <string.h>
#include <stdio.h>
#include "sdkconfig.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "driver/adc.h"

void DoDMA(int gpio_in1, int gpio_in2, int samples, uint32_t * samples_back, uint8_t * data_back);
static bool check_valid_data(const adc_digi_output_data_t *data);


#define ADC_RESULT_BYTE     2
#define ADC_CONV_LIMIT_EN   0

//#define ADC_CONV_MODE       ADC_CONV_SINGLE_UNIT_1 
//#define ADC_OUTPUT_TYPE     ADC_DIGI_OUTPUT_FORMAT_TYPE1 

#define ADC_CONV_MODE       ADC_CONV_BOTH_UNIT
#define ADC_OUTPUT_TYPE     ADC_DIGI_OUTPUT_FORMAT_TYPE2

void setup() {
  Serial.begin(115200);
}

void loop() {
  uint32_t  s_back;
  uint8_t *result = (uint8_t*)malloc(3*2048 * sizeof(uint8_t));

  DoDMA(1, 9, 256, &s_back, result);  //call using GPIO pins for first two arguments, third argument ignored
  
  //Serial.printf("samples taken = %d\n",s_back);
  
  for (int i = 0; i < 40; i += ADC_RESULT_BYTE) {
      adc_digi_output_data_t *p = (adc_digi_output_data_t*)&result[i];
      if (check_valid_data(p))
        // Use for ADC_CONV_SINGLE_UNIT_1
        //Serial.printf("i=%d  Unit: %d, Channel: %d, Value: %d\n",i, 1, p->type1.channel, p->type1.data);
        // Use for ADC_CONV_BOTH_UNIT
        Serial.printf("i=%d  Unit: %d,_Channel: %d, Value: %d\n",i,p->type2.unit+1, p->type2.channel, p->type2.data);
  }
  vTaskDelay(2000);
   free(result);
}



#define TIMES              2048  
#define GET_UNIT(x)        ((x>>3) & 0x1)


uint16_t adc1_chan_mask;
static uint16_t adc2_chan_mask = 0; 
static adc_channel_t channel[2] = {(adc_channel_t)ADC1_CHANNEL_2, (adc_channel_t)ADC1_CHANNEL_3};

static const char *TAG = "--(TAG ADC DMA)--";

static void continuous_adc_init(uint16_t adc1_chan_mask, uint16_t adc2_chan_mask, adc_channel_t *channel, uint8_t channel_num)
{

    adc_digi_init_config_t adc_dma_config = {
        .max_store_buf_size = 6*TIMES,
        .conv_num_each_intr = TIMES,
        .adc1_chan_mask = adc1_chan_mask,
        .adc2_chan_mask = adc2_chan_mask,
    };

    ESP_ERROR_CHECK(adc_digi_initialize(&adc_dma_config));

    adc_digi_configuration_t dig_cfg = {
        .conv_limit_en = ADC_CONV_LIMIT_EN,
        .conv_limit_num = 250,
        .sample_freq_hz = 83333, 
        //.sample_freq_hz =  1000,
        .conv_mode = ADC_CONV_MODE,
        .format = ADC_OUTPUT_TYPE,
    };

    adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0};
    dig_cfg.pattern_num = channel_num;

    for (int i = 0; i < channel_num; i++) {
        uint8_t unit = GET_UNIT(channel[i]);
        uint8_t ch = channel[i] & 0x7;
        adc_pattern[i].atten = ADC_ATTEN_DB_11;
        adc_pattern[i].channel = ch;
        adc_pattern[i].unit = unit;
        adc_pattern[i].bit_width = SOC_ADC_DIGI_MAX_BITWIDTH;   //11 data bits limit   
        

        ESP_LOGI(TAG, "adc_pattern[%d].atten is :%x", i, adc_pattern[i].atten);
        ESP_LOGI(TAG, "adc_pattern[%d].channel is :%x", i, adc_pattern[i].channel);
        ESP_LOGI(TAG, "adc_pattern[%d].unit is :%x", i, adc_pattern[i].unit);
        
    }
    dig_cfg.adc_pattern = adc_pattern;
    ESP_ERROR_CHECK(adc_digi_controller_configure(&dig_cfg));

    
}

#if !CONFIG_IDF_TARGET_ESP32
static bool check_valid_data(const adc_digi_output_data_t *data)
{
    const unsigned int unit = data->type2.unit;
    if (unit > 2) return false;
    if (data->type2.channel >= SOC_ADC_CHANNEL_NUM(unit)) return false;

    return true;
}
#endif

void DoDMA(int gpio_in1, int gpio_in2, int samples, uint32_t * samples_back, uint8_t * data_back)
{
    esp_err_t ret,ret2,ret3;
    esp_err_t ret_stop;
    uint32_t ret_num = 0,ret_num2 = 0,ret_num3=0;


    ulong timer;
  
    adc1_chan_mask = BIT(digitalPinToAnalogChannel(gpio_in1)) | BIT(digitalPinToAnalogChannel(gpio_in2));
    adc2_chan_mask = 0;
    channel[0] = (adc_channel_t)digitalPinToAnalogChannel(gpio_in1);
    channel[1] = (adc_channel_t)digitalPinToAnalogChannel(gpio_in2);

    memset(data_back,0xcc,3*2048);

        continuous_adc_init(adc1_chan_mask, adc2_chan_mask, channel, sizeof(channel) / sizeof(adc_channel_t));
        ret_stop = adc_digi_start();
        timer=micros();

        ret = adc_digi_read_bytes(data_back, TIMES, &ret_num, ADC_MAX_DELAY);
        ret2 = adc_digi_read_bytes(data_back+ret_num, TIMES, &ret_num2, ADC_MAX_DELAY);
        ret3 = adc_digi_read_bytes(data_back+ret_num+ret_num2, TIMES, &ret_num3, ADC_MAX_DELAY);    

        timer=micros()-timer;
        ret_stop = adc_digi_stop();

        adc_digi_deinitialize();
        *samples_back = ret_num+ret_num2+ret_num3;
        
    //assert(ret == ESP_OK);
}