Reading ADC Linux iMX7

Hello,

I’m using the following code to read from an ADC in iMX7D.

int ADC_read()
{
  int fadc;
        int value;
        char buffer[5];
    
    
    	fadc = open("/sys/bus/iio/devices/iio:device0/in_voltage0_raw", O_RDONLY);
    
    	if (fadc == -1)
    		return(-1);
    
        if (read(fadc, buffer, 4) == -1)
            return(-1);
    
    
        close(fadc);
    
        buffer[4] = '\0';
        value = atoi(buffer);
    
        return(value);
}

By measuring the time needed to complete this read function, it takes about 115 µs, (which means the sampling rate would be about 9 khz). In the documentation of Colibri iMX7 it is stated that the sampling rate can be up to 1Mhz.
How can I reduce the current delay? is there a better ready function from Toradex to read from ADCs?

Best regards,
Majd

Hi @majd.m

In the documentation of Colibri iMX7 it is stated that the sampling rate can be up to 1Mhz.

This is what the hardware can do. The implementation on software side is your responsibility.

is there a better ready function from Toradex to read from ADCs?

No, this is standard Input/Output interface provided by Linux.

How can I reduce the current delay?

There are lot of possibilities:

  1. Optimize the code of your function
  2. Write a kernel driver
  3. Use M4 with DMA to readout the ADC

I am not sure what you are trying to achieve, but you should also take care that if you just optimize your function, then you will also have lot of (time) jitter when reading out the ADC.

Best regards,
Jaski