Fast ADC access on BBB causes buffer underflow error (seg fault)

I discovered a problem when accessing the Analog pins on the BBB.

Whenever I would read the value of the pins, I needed to read a second time to get the correct value. This is because the first file read initiates the conversion, and the second gets the actual value. I found that if I did the read back to back, with no delay in between, the second read would fail by way of a buffer underflow (reading the file using the ifstream class in C++). I am assuming this happens because the conversion is not complete, and as such there is no data yet.

My question is this: Is there a way to check if the conversion is done before attempting the read? I need these reads to happen pretty quick, so I cant afford to burn 50ms before the second read, and if I try to read them too much quicker than that, I get an occasional seg fault from the underflow.

See http://beagleboard-gsoc13.blogspot.com/2013/07/sampling-analogue-signals-using-adc-on.html for some more info and code on doing “continuous” ADC samples.

The important bit I think would be that you can’t just read from the file, you need to use some version of poll on the file descriptor so as to wait for the data to be ready, then you can go ahead and read from it. Also since I think poll() will return when there is any change to the file descriptor[ie file permissions change, file data changes, file owner change, etc] to be safe you need to doublecheck that there actually IS data after poll() returns and loop if there is not.