Latency when reading ADC in Apalis iMX6

Hi all, I tried to read the ADC from an Apalis iMX6. It is working with a ixora carrier board. I put the reading process in a for-loop.

However, even when I set the loop length to be 1000, the actual time cost is ~8sec. i.e., The time interval between two data points is ~4ms (i.e., the sampling rate is about 250S/s). It is kind of slower than I expected, as in iMX6’s data sheet, the sampling rate of the ADC is supposed to be ~800kS/s, which should be ~100kS/s for each channel (assuming 8 channels in total). I would like to know if it is due to the setting of the ADC’s sampling rate? Or it is due to the time lag of the C code? Is there a way I can change the ADC’s sampling rate, or make the reading process faster?

fd = open("/sys/bus/iio/devices/iio:device0/in_voltage4_raw", O_RDONLY);
for(i=0;i<1000;i++)
{    
        read(fd, &value[i], 1);
        gettimeofday(&timestamp[i],NULL);
        lseek(fd,0,SEEK_SET);
}
close(fd);

Have you tried using libiio for benchmarking? It handles the transfers using kernel ring buffers and hardware DMA, so copying with read() won’t bottleneck the conversion.

The Apalis iMX6 uses an external ADC converter (STMPE811). Since this chip is attached externally via I2C, a rather slow sampling rate is expected.

For higher frequency application I recommend using an external ADC converter on the base board, ideally SPI attached.

This works well and makes sense for ADC drivers which do support continuous sampling modes. We do have support for this mode for Colibri VFxx modules, which has an internal ADC converter. See also ADC (Linux) Continuous Sampling.

Where did you found the number ~800kS/s exactly?