Post Go back to editing

Inconsistent channel position in a libiio buffer

Category: Software
Product Number: CN0554

Hi Everyone,

While working on an application that continuously acquires data from multiple channels using the AD7124 on a CN0554 board, I noticed that sometimes the order of the channels is mixed up. I thought that was an issue with my code, but then I observed the same behavior when using the iio_readdev example that comes with the libiio distribution. I tried the following test.

  • I applied 1000 mV to channel "voltage0-voltage1", 2000 mV to channel "voltage2-voltage3", 3000 mV to "voltage4-voltage5", and 4000 mV to "channel6-channel7". 
  • Set the sampling rate to 19200 for those channels
  • Run the command:  ./iio_readdev -u ip:localhost ad7124-8 voltage0-voltage1 voltage2-voltage3 voltage4-voltage5 voltage6-voltage7 | ./showout -n 4, where showout is a simple program that gets the binary output dumped by iio_readdev and converts it into readable values (the code of this program is reported below)

At the beginning the output is as expected, e.g.,  

1000.25         1998.33         3006.18   4008.38, 

however after sometime, typically within 30 seconds, the order of the values changes, e.g., 

  1999.74         3007.41         4008.81   1001.40,

i.e. the order of the channels is a circular permutation of the original order. 

The iio_readdev app basically dumps the entire buffer without de-interleaving the data. However, I observed the same behavior even when the code iterates over the samples channel by channel (using the functions iio_buffer_first(),  iio_buffer_step() and iio_buffer_end() ).

Has anybody come across this behavior? It seems that the libiio loses track of the correct slot assigned to a channel. 

Thanks,

Massimo

Here is the code for printing the values dumped by the iio_readdev app.

 

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#define WORD_SIZE 4
#define COUNT2MILLIVOLT 0.000149011
#define scale_res_div 11.3373


int main(int argc, char **argv )
{
int i, flag, c, n = 1, k;
unsigned char buf[4], tmp[4];
unsigned long * p, dc;
double volt;

while ((c = getopt(argc, argv, "n:")) != -1)
{
switch (c)
{
case 'n':
n = atoi(optarg);
break;
case '?':
if (optopt == 'n')
printf("Option -%c requires an argument.\n", optopt);
else
printf("Unknown option -%c", optopt);
return 1;
default:
n = 1;
break;
}
}

flag = 1 ;
k = 0;
while (flag > 0) {
flag = fread(buf, sizeof(char), WORD_SIZE, stdin) ;
if (flag != WORD_SIZE) {
break ;
}
// dc = (long)buf[0] << 24 + (long)buf[1] << 16 + (long)buf[2] << 8 + (long)buf[0];
for(i = 0; i < WORD_SIZE; i++)
{
tmp[i] = buf[WORD_SIZE - 1 - i];
}
dc = *((unsigned long *)tmp);
printf("%9.2f\t", dc * COUNT2MILLIVOLT * scale_res_div);
k++;
if(k == n)
{
k = 0;
printf("\n");
}
}

return 0 ;
}

Parents
  • Given  comments I tried the newly release Kuiper version (2022_r2) to see if it was fixing this issue. Unfortunately it didn't. Running the same example given in the previous post, the only difference is in the voltages applied to the 4 analog inputs (in mV: 1000, 1100, 1200, and 1300, respectively), now the same value is reported on all 4 slots of the buffer. Here is a snippet of the output

       999.56          999.56          999.56          999.56
       999.56          999.56         1100.28         1100.28
      1100.28         1100.28         1100.28         1100.28
      1100.28         1202.51         1202.51         1202.51
      1202.51         1202.51         1202.51         1202.51
      1202.51         1202.51         1202.51         1202.51
      1202.51         1202.51         1202.51         1202.51
      1202.51         1202.51         1202.51         1202.51
      1202.51         1100.28         1100.28         1100.28
      1100.28         1100.28         1100.28         1100.28
      1205.21         1205.21         1205.21         1205.21
      1205.21         1205.21         1205.21         1303.91
      1303.91         1303.91         1303.91         1303.91
      1303.91         1303.91         1000.68         1000.68
      1000.68         1000.68         1000.68         1000.68

    Here is a plot of the data contained in one buffer (128 samples). It should have been 4 straight lines...

    Hopefully, somebody will have a look at this issue.

    Thanks,

    Massimo

Reply
  • Given  comments I tried the newly release Kuiper version (2022_r2) to see if it was fixing this issue. Unfortunately it didn't. Running the same example given in the previous post, the only difference is in the voltages applied to the 4 analog inputs (in mV: 1000, 1100, 1200, and 1300, respectively), now the same value is reported on all 4 slots of the buffer. Here is a snippet of the output

       999.56          999.56          999.56          999.56
       999.56          999.56         1100.28         1100.28
      1100.28         1100.28         1100.28         1100.28
      1100.28         1202.51         1202.51         1202.51
      1202.51         1202.51         1202.51         1202.51
      1202.51         1202.51         1202.51         1202.51
      1202.51         1202.51         1202.51         1202.51
      1202.51         1202.51         1202.51         1202.51
      1202.51         1100.28         1100.28         1100.28
      1100.28         1100.28         1100.28         1100.28
      1205.21         1205.21         1205.21         1205.21
      1205.21         1205.21         1205.21         1303.91
      1303.91         1303.91         1303.91         1303.91
      1303.91         1303.91         1000.68         1000.68
      1000.68         1000.68         1000.68         1000.68

    Here is a plot of the data contained in one buffer (128 samples). It should have been 4 straight lines...

    Hopefully, somebody will have a look at this issue.

    Thanks,

    Massimo

Children