every 256th sample is missing when I read ADC via I2S with DMA

joges1987
Posts: 2
Joined: Mon Dec 14, 2020 4:27 pm

every 256th sample is missing when I read ADC via I2S with DMA

Postby joges1987 » Mon Dec 14, 2020 5:09 pm

Hello guys,

I want to use the ESP32s internal DAC and ADC with a very high sampling rate (200kHz), so I use the I2S with DMA.
When I connected the DAC to the ADC and I sent a 20 kHz sine wave (10 samples per period) to the DAC, I got an ALMOST correct result. I can see in my ADC data a period is repeated every 10 steps.
However, it happens that some samples are omitted. This happens every 255 sample.

Example Data:
  1. Samp;  AdVolt;
  2. 87;      1.83;
  3. 88;      2.06;
  4. 89;      2.27;
  5. 90;      2.27;
  6. 91;      2.06;
  7. 92;      1.82;
  8. 93;      1.55;
  9. 94;      1.39;
  10. 95;      1.39;
  11. 96;      1.55;
  12. 97;      1.82;
  13. 98;      2.06;
  14. 99;      2.27;
  15. 100;     2.06; //  ERROR: here should be the value 2.27 instead
  16. 101;     1.82;
  17. 102;     1.55;
  18. 103;     1.39;
  19. 104;     1.39;
  20. 105;     1.55;
  21. 106;     1.82;
  22. 107;     2.06;
  23. 108;     2.27;
  24. 109;     2.27;
  25. 110;     2.06;
And this error ocurres 255 samples later again:
  1. 337;     2.06;
  2. 338;     2.27;
  3. 339;     2.27;//period start
  4. 340;     2.06;
  5. 341;     1.82;
  6. 342;     1.55;
  7. 343;     1.39;
  8. 344;     1.38;
  9. 345;     1.55;
  10. 346;     1.82;
  11. 347;     2.06;
  12. 348;     2.27;//end  (10 samples, correct period)
  13. 349;     2.27; //period starts
  14. 350;     2.06;
  15. 351;     1.82;
  16. 352;     1.55;
  17. 353;     1.39;
  18. 354;     1.55;// ERROR: here should be a value arround 1.38 instead
  19. 355;     1.82;
  20. 356;     2.06;
  21. 357;     2.27; //period has only 9 samples, 1 was omitted
  22. 358;     2.27;
  23. 359;     2.06;
The DAC seems not to be the problem because I deactivated it and used a DebugPin instead and still the same error ocurred.
I also changed the buffer size and the sampling rate, but without any success.
Anybody has an explanation for this problem or knows how to solve it?

Here the code of my configuration:
  1. #include <Arduino.h>
  2. #include <driver/i2s.h>
  3. #include "prj_FastDmaAdc.h"//module header
  4.  
  5. #define ADC_CHANNEL   ADC1_CHANNEL_0     // GPIO36
  6. #define NUM_SAMPLES   1000              // number of samples in  buffer
  7.  
  8. void InitI2S (){
  9.     i2s_config_t i2s_config = {
  10.         //I2S with ADC and DAC
  11.         .mode = (i2s_mode_t) (I2S_MODE_MASTER
  12.                             | I2S_MODE_RX
  13.                             | I2S_MODE_TX
  14.                             | I2S_MODE_DAC_BUILT_IN
  15.                             | I2S_MODE_ADC_BUILT_IN),
  16.         .sample_rate            = (200000),                                                      
  17.         .bits_per_sample        = I2S_BITS_PER_SAMPLE_16BIT,                            
  18.         .channel_format         = I2S_CHANNEL_FMT_RIGHT_LEFT,                                
  19.         .communication_format   = I2S_COMM_FORMAT_I2S_MSB,                            
  20.         .intr_alloc_flags       = 0,                                                    
  21.         .dma_buf_count          = 4,                                                          
  22.         .dma_buf_len            = NUM_SAMPLES,                                                  
  23.         .use_apll               = false,                                                        
  24.     };
  25.  
  26.     adc1_config_channel_atten (ADC_CHANNEL, ADC_ATTEN_11db);
  27.  
  28.     adc1_config_width (ADC_WIDTH_12Bit);
  29.  
  30.     i2s_driver_install (I2S_NUM_0, &i2s_config, 0, NULL);
  31.  
  32.     i2s_set_dac_mode (I2S_DAC_CHANNEL_BOTH_EN);
  33.  
  34.     i2s_set_adc_mode (ADC_UNIT_1, ADC_CHANNEL);
  35.  
  36.     i2s_adc_enable (I2S_NUM_0);
  37.  
  38. }
  39.  
  40. int GetAdcData(float* po_AdcValues)
  41. {
  42.     uint16_t i2s_read_buff[NUM_SAMPLES*2];
  43.     size_t num_bytes_read =0;
  44.  
  45.      i2s_read( I2S_NUM_0, (void*) i2s_read_buff,  NUM_SAMPLES * 2* sizeof (uint16_t), &num_bytes_read, portMAX_DELAY);
  46.  
  47.     int NumSamps =  num_bytes_read/(2*sizeof (uint16_t));
  48.  
  49.     for (int i=0; i< NumSamps; i++)
  50.     {   //convert 12 bits to voltage
  51.         po_AdcValues[i] = 3.3* ( (float) (i2s_read_buff[i*2]& 0x0FFF)) /0x0FFF;
  52.     }
  53.     return NumSamps; //return  number of the read samples
  54. }
Thank your very much for helping me!
Joseph

Who is online

Users browsing this forum: No registered users and 108 guests