Skip to contentSkip to conversation

NI 6251 "wrong" samples

Xenomai-help Daniele Nicolodi 2 participants 6 messages Jun 4, 2010 – Jun 21, 2010 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

Hello.

I'm working with a NI PCI 6251 ADC board. I configured it to sample channels 6 and 7, configured as differential analog inputs, at 50 kHz with a conversion time of 2000 ns. I'm using a Xenomai kernel and the Analogy drivers, a port of comedi drivers over the Xenomai real time device driver abstraction layer.

The systems works well (I do not fully understand the properties fo the noise spectrum of the sample data, see my other email to the mailing list, but this is quite mynor) except that now and then I receive a clearly "wrong" sample from the board.

I think I receive a wrong sample because I'm testing the system acquiring a simple sine wave generated from a function generator, and sometimes I obtain a sample that is clearly not where expected and I I hardly believe that the discrepancy is due to physical noise.

I'm unable to identify a pattern of when those "wrong" samples appear in my output, so I do not believe the issue is due to wrong FIFO synchronization.

Has someone ever observed a similar behavior? Does someone has a suggestion on how I can try to investigate it?

Thanks. Cheers,

Signature · 1 line
Daniele
#3 lore raw
In reply to a message not in the archive

Hello. Sorry for the long delay, I had some real work to get done with the acquisition system and I had to implement a work-around for this problem...

I have been investigating the issue further, but its random nature makes this quite difficult.

Quoted 2 lines

The observation that the "wrong" sample occurs on the last sample of the scan sounds useful. Does it occur randomly or on every scan?

I must retire this statement. The "wrong" samples can occur on any channel. Some are just more probable than other. The "wrong" samples do NOT occur at each scan, and they look to be completely random in their occurrence, I'm trying to find a pattern but without luck up to now.

Quoted 6 lines

Also, are you using the TRIG_WAKE_EOS command flag (or equivalently, setting stop_arg to TRIG_COUNT and stop_count to 1)? If so, does the wrong sample still occur when the TRIG_WAKE_EOS flag is not set? (You might need to look harder for the wrong samples when TRIG_WAKE_EOS is not set, as they might occur much less frequently and they might occur at any position in the scan.

No. I'm running a continuous memory mapped acquisition. The only maybe uncommon thing I'm doing is that I'm reading the mmapped buffer in fixed chunks, rather than reading all the samples available each time. The number of samples I read each time is larger than the 4095 samples after which the hardware sends the FIFO half full trigger event.

Suggestions?

Thanks. Cheers,

Signature · 1 line
Daniele
#4 lore raw
Quoted 5 lines · Daniele Nicolodi

No. I'm running a continuous memory mapped acquisition. The only maybe uncommon thing I'm doing is that I'm reading the mmapped buffer in fixed chunks, rather than reading all the samples available each time. The number of samples I read each time is larger than the 4095 samples after which the hardware sends the FIFO half full trigger event.

I modified my code from a loop like:

unsigned int required = <compute required number of bytes>; unsigned int read = 0;
void * buffer = a4l_mmap(dsc, ...);
while (1) {

  while (read < required) {
    read = a4l_pool(...);
    clock_nanosleep(...);

}
process(buffer, ...);
a4l_buff_markrw(dsc, subdevice, required, ...);
}

to something like:

unsigned int required = <compute required number of bytes>; unsigned int read = 0;
void *buffer = malloc(required);
while (1) {

  while (read < required) {
    a4l_sys_read(dsc, buffer + read, required - read);

}
process(buffer, ...);
}

I tested this setup over the weekend and the wrong samples went away!

Therefore i suspect that the problem is not related to FIFO synchronization but to kernel space buffer handling.

I have tried to look at Analogy buffer handling, but the lack of any comment in the source code makes understanding how it is supposed to work a complex task.

Alexis, can you see any case where there may be a synchronization issue like the one I'm seeing in your code? Any suggestion in where to look for the bug?

Thanks. Cheers,

Signature · 1 line
Daniele
#5 lore raw

Hi,

Quoted 33 lines · Daniele Nicolodi
Quoted 5 lines · Daniele Nicolodi

No. I'm running a continuous memory mapped acquisition. The only maybe uncommon thing I'm doing is that I'm reading the mmapped buffer in fixed chunks, rather than reading all the samples available each time. The number of samples I read each time is larger than the 4095 samples after which the hardware sends the FIFO half full trigger event.

I modified my code from a loop like:

unsigned int required = <compute required number of bytes>; unsigned int read = 0;
void * buffer = a4l_mmap(dsc, ...);
while (1) {
 while (read < required) {
   read = a4l_pool(...);
   clock_nanosleep(...);
 }
 process(buffer, ...);
 a4l_buff_markrw(dsc, subdevice, required, ...);
}

to something like:

unsigned int required = <compute required number of bytes>; unsigned int read = 0;
void *buffer = malloc(required);
while (1) {
 while (read < required) {
   a4l_sys_read(dsc, buffer + read, required - read); }
 process(buffer, ...);
}

Quoted 9 lines

I tested this setup over the weekend and the wrong samples went away!

Therefore i suspect that the problem is not related to FIFO synchronization but to kernel space buffer handling.

I have tried to look at Analogy buffer handling, but the lack of any comment in the source code makes understanding how it is supposed to work a complex task.

Please, tell me where comments are lacking, I will fix that.

Quoted 4 lines

Alexis, can you see any case where there may be a synchronization issue like the one I'm seeing in your code? Any suggestion in where to look for the bug?

You told you that the bug was occuring randomly and that there was no pattern in the repetition of the bad samples. But are you sure that the wrong samples do not occur every modulo x * 64KB ?

If the bug is located in Analogy buffer handling, it should not be specific to a driver. So, I will try to reproduce it with the testing drivers. I will compare the mapped and unmapped acquisition results. If I remember well, the "fake" driver outputs some deterministic content. If nothing comes out, I will try to use a NI device.

Many thanks for reporting that.

Quoted 4 lines

Thanks. Cheers,
--
Daniele

Alexis.

#6 lore raw
Quoted 5 lines · Alexis Berlemont
Quoted 4 lines

I have tried to look at Analogy buffer handling, but the lack of any comment in the source code makes understanding how it is supposed to work a complex task.

Please, tell me where comments are lacking, I will fix that.

I find buffer.c and buffer.h to be under documented to be easily understood. For example it is not clear what the *_count members of the a4l_buf_t struct are for without a quite tedious inspection of the code.

Quoted 8 lines
Quoted 4 lines

Alexis, can you see any case where there may be a synchronization issue like the one I'm seeing in your code? Any suggestion in where to look for the bug?

You told you that the bug was occuring randomly and that there was no pattern in the repetition of the bad samples. But are you sure that the wrong samples do not occur every modulo x * 64KB ?

I convinced myself that this is not the case. For sure the error is not there at any ring buffer wrap around. I increased the buffer size to 128KB and there hasn't been any noticeable difference. Am I right supposing that those 64KB you are referring to are the buffer size?

Quoted 5 lines

If the bug is located in Analogy buffer handling, it should not be specific to a driver. So, I will try to reproduce it with the testing drivers. I will compare the mapped and unmapped acquisition results. If I remember well, the "fake" driver outputs some deterministic content. If nothing comes out, I will try to use a NI device.

To detect the problem you need to feed a deterministic signal to your device. It looks like every now and then an old sample is returned (I say so because the wrong value is always in the signal range). If you feed a constant value you would be unable to distinguish the wrong sample from the good ones. If you are missing a function generator the DAC channels of the NI ADC board work just fine, as long as you provide proper low pass filters.

My test case is a phase-meter implementation. I'm testing the phase noise of the phase-meter and thus I acquire a zero mean sine wave. Every now and then the average of a fixed number of samples, containing an integer number of sine waves, is far from zero. When I detect this situation I dump the content of the current buffer, and in each dump there is always a single wrong sample.

My code is embedded into a data acquisition framework I'm developing, but I can provide the code, however I can take a little bit to have it to compile stand alone.

The bug shows usually after some minutes to half an hour of data taking at 10 to 50 kHz. It would be unpractical to save all that data to disk for inspection.

Thanks. Cheers,

Signature · 1 line
Daniele
#7 lore raw

Hi,

Quoted 16 lines · Daniele Nicolodi
Quoted 5 lines · Alexis Berlemont
Quoted 4 lines

I have tried to look at Analogy buffer handling, but the lack of any comment in the source code makes understanding how it is supposed to work a complex task.

Please, tell me where comments are lacking, I will fix that.

I find buffer.c and buffer.h to be under documented to be easily understood. For example it is not clear what the *_count members of the a4l_buf_t struct are for without a quite tedious inspection of the code.

Quoted 5 lines
Quoted 4 lines

Alexis, can you see any case where there may be a synchronization issue like the one I'm seeing in your code? Any suggestion in where to look for the bug?

Quoted 9 lines
Quoted 3 lines

You told you that the bug was occuring randomly and that there was no pattern in the repetition of the bad samples. But are you sure that the wrong samples do not occur every modulo x * 64KB ?

I convinced myself that this is not the case. For sure the error is not there at any ring buffer wrap around. I increased the buffer size to 128KB and there hasn't been any noticeable difference. Am I right supposing that those 64KB you are referring to are the buffer size?

Yes.

Quoted 6 lines
Quoted 5 lines

If the bug is located in Analogy buffer handling, it should not be specific to a driver. So, I will try to reproduce it with the testing drivers. I will compare the mapped and unmapped acquisition results. If I remember well, the "fake" driver outputs some deterministic content. If nothing comes out, I will try to use a NI device.

Quoted 8 lines

To detect the problem you need to feed a deterministic signal to your device. It looks like every now and then an old sample is returned (I say so because the wrong value is always in the signal range). If you feed a constant value you would be unable to distinguish the wrong sample from the good ones. If you are missing a function generator the DAC channels of the NI ADC board work just fine, as long as you provide proper low pass filters.

I was just starting to define how to debug it with the testing driver "fake" but your additional comment made me doubt: especially the fact that I will not notice it with a constant value as the wrong acquired sample is an "old one".

Regarding that, would you consider possible that the user application could read the data too early? Maybe there is a synchronization issue between the interrupt and the DMA transfer. That would explain why unoptimized accesses (unmapped buffer and read syscalls) make the bug vanish.

I may have messed something up with the mite transfer counters. I will double-check the code in this location (mite.c).

If you re-read a faulting value after a while, does it get updated ? You might find this question silly but what strikes me is that an unmapped acquisition gives you no wrong sample; that means that the data are properly copied by the DMA controller into the kernel memory buffer, the very same buffer you map in user space in the optimized mode.

Quoted 18 lines

My test case is a phase-meter implementation. I'm testing the phase noise of the phase-meter and thus I acquire a zero mean sine wave. Every now and then the average of a fixed number of samples, containing an integer number of sine waves, is far from zero. When I detect this situation I dump the content of the current buffer, and in each dump there is always a single wrong sample.

My code is embedded into a data acquisition framework I'm developing, but I can provide the code, however I can take a little bit to have it to compile stand alone.

The bug shows usually after some minutes to half an hour of data taking at 10 to 50 kHz. It would be unpractical to save all that data to disk for inspection.

Thanks. Cheers,

Signature · 1 line
Daniele
Signature · 1 line
Alexis.