Go Down

aleshonsa

Dec 16, 2012, 01:05 am
Hello,

I want to build USB osciloscope and I need time resolution minimally few us. So I'm interested if I'm able to build it based on Arduino Due.

Thanks

Best Regards
Ales

#1

stimmer

#2
Dec 16, 2012, 03:26 pm
I'm working on a similar project at the moment. If analogRead is too slow for you it is possible to put the ADC in 'free-running' mode and read the registers directly - doing this I am getting conversions in 1uS (although I have not tested for accuracy yet). This is the code I was using to test speed:

Code: [Select]

void setup() {
Serial.begin(9600);

}

void loop() {
int t=micros();
int q=0;
int a0;
for(int i=0;i<1000000;i++){
q+=a0;
}
t=micros()-t;
Serial.print("1 million conversions in ");Serial.print(t);Serial.println(" micros");
}

Grumpy_Mike

#3
Dec 16, 2012, 07:26 pm
You might want to read this:-
http://www.atmel.com/Images/doc11106.pdf
The fast mode is what is known as a tracking mode. This is useful if the input signal is changing by less than the least significant bit amount between samples. Things like bandwidth limited audio waveforms are good for this.

fat16lib

#4
Dec 16, 2012, 09:28 pm
When measuring a signal, time jitter is as important as amplitude accuracy.

See this http://www.analog.com/library/analogDialogue/archives/42-02/clock_optimization.html

Here is an example for 100,000 samples per second with 10-bits of accuracy and a clock jitter of 5 ns.

Quote

SRN of an ideal n-bit ADC:

SNR(dB) = 6.02*n + 1.76

Quote

SNR due clock jitter:

SNR(dB) = -20*log(6.28*f*t)

f is the measurement frequency

t is the time jitter in seconds

For an ideal 10-bit ADC the max SNR is about 62 dB.

The SNR limit for clock jitter is

SNR(jitter) = -20*log(6.28*100000*5e-9) = 50.06

So jitter is the limiting factor.  What is the point of fast measurements if you don't know the time to sufficient accuracy?

PakARD

#5
Feb 28, 2013, 01:49 am

I'm working on a similar project at the moment. If analogRead is too slow for you it is possible to put the ADC in 'free-running' mode and read the registers directly - doing this I am getting conversions in 1uS (although I have not tested for accuracy yet). This is the code I was using to test speed:

Code: [Select]

void setup() {
Serial.begin(9600);

}

void loop() {
int t=micros();
int q=0;
int a0;
for(int i=0;i<1000000;i++){
q+=a0;
}
t=micros()-t;
Serial.print("1 million conversions in ");Serial.print(t);Serial.println(" micros");
}

This piece of code is great!! I'm sampling at 1MHz (one channel) in my Due.
Would the registers setup change for two channels?

Thank you

stimmer

#6
Feb 28, 2013, 02:35 amLast Edit: Feb 28, 2013, 10:36 pm by stimmer Reason: 1
It's similar. Basically in free running mode the SAM3X cycles through all enabled channels. So we just enable 2 channels and wait for 2 conversions each time:
Code: [Select]
void setup() {
Serial.begin(9600);

}

void loop() {
int t=micros();
int q0=0,q1=0;
int a0,a1;
for(int i=0;i<500000;i++){
q0+=a0;
q1+=a1;
}
t=micros()-t;
Serial.print("500000 pairs of conversions in ");Serial.print(t);Serial.println(" micros");
Serial.print("A0 total:");Serial.println(q0);
Serial.print("A1 total:");Serial.println(q1);
}

Obviously there's only one ADC which is shared by the 2 channels so the effective sample rate halves. This means that you will lose some accuracy, because internally the chip is having to switch between two different input signals 1 million times per second, and that won't be as good as it tracking the same input all the time like in the 1-channel example.

PakARD

#7
Feb 28, 2013, 08:55 pm

It's similar. Basically in free running mode the SAM3X cycles through all enabled channels. So we just enable 2 channels and wait for 2 conversions each time:
Code: [Select]
void setup() {
Serial.begin(9600);

}

void loop() {
int t=micros();
int q0=0,q1=0;
int a0,a1;
for(int i=0;i<500000;i++){
q0+=a0;
q1+=a1;
}
t=micros()-t;
Serial.print("500000 pairs of conversions in ");Serial.print(t);Serial.println(" micros");
Serial.print("A0 total:");Serial.println(q0);
Serial.print("A1 total:");Serial.println(q1);
}

Obviously there's only one ADC which is shared by the 2 channels so the effective sample rate halves. This means that you will lose some accuracy, because internally the chip is having to switch between two different input signals 1 million times per second, and that won't be as good as it tracking the same input all the time like in the 1-channel example.

This is also working.
But I have some doubts about it:

- Why on the
Code: [Select]

line, you use the same pin of the ADC to compare?

Wouldn't it be:
Code: [Select]

or better:
Code: [Select]

??

On the other hand, to reduce Jitter and free more time for "processing" in the loop function, could it enabling ADC interrupts be a solution?
Do you know how to accomplish this? I have seen ADC_IER register in the datasheet, but I'm pretty new with Arduino Due (just a couple of days) and don't know how to start.

Thank you again

stimmer

#8
Feb 28, 2013, 10:31 pmLast Edit: Feb 28, 2013, 10:35 pm by stimmer Reason: 1
Quote

Code: [Select]

Well spotted - what it should actually be is this:
Code: [Select]

I'll go back and change my code in case anyone else copy/pastes it.

For reducing missed samples and allowing more processing time in the main loop, the biggest win comes through using peripheral DMA. This example uses DMA and interrupts - although if you are new to the Due, don't expect to understand it all at once

Code: [Select]
#undef HID_ENABLED

// by stimmer
// Output: Raw stream of uint16_t in range 0-4095 on Native USB Serial/ACM

// on linux, to stop the OS cooking your data:
// stty -F /dev/ttyACM0 raw -iexten -echo -echoe -echok -echoctl -echoke -onlcr

volatile int bufn,obufn;
uint16_t buf[4][256];   // 4 buffers of 256 readings

void ADC_Handler(){     // move DMA pointers to next buffer
if (f&(1<<27)){
bufn=(bufn+1)&3;
}
}

void setup(){
SerialUSB.begin(0);
while(!SerialUSB);

bufn=obufn=1;
}

void loop(){
while(obufn==bufn); // wait for buffer to be full
SerialUSB.write((uint8_t *)buf[obufn],512); // send it - 512 bytes = 256 uint16_t
obufn=(obufn+1)&3;
}

It reads ADC data at 1 million samples/sec and outputs the data to SerialUSB. (I've only tested it on Linux and most of the time it works, sometimes it is unreliable, I don't know why). Using GNU Radio I was then able to analyse the data stream and receive a long-wave radio signal, with an LC tuned circuit into A0 as an aerial.

PakARD

#9
Mar 01, 2013, 06:22 pmLast Edit: Mar 01, 2013, 06:25 pm by PakARD Reason: 1

Quote

Code: [Select]

Well spotted - what it should actually be is this:
Code: [Select]

I'll go back and change my code in case anyone else copy/pastes it.

For reducing missed samples and allowing more processing time in the main loop, the biggest win comes through using peripheral DMA. This example uses DMA and interrupts - although if you are new to the Due, don't expect to understand it all at once

Code: [Select]
#undef HID_ENABLED

// by stimmer
// Output: Raw stream of uint16_t in range 0-4095 on Native USB Serial/ACM

// on linux, to stop the OS cooking your data:
// stty -F /dev/ttyACM0 raw -iexten -echo -echoe -echok -echoctl -echoke -onlcr

volatile int bufn,obufn;
uint16_t buf[4][256];   // 4 buffers of 256 readings

void ADC_Handler(){     // move DMA pointers to next buffer
if (f&(1<<27)){
bufn=(bufn+1)&3;
}
}

void setup(){
SerialUSB.begin(0);
while(!SerialUSB);

bufn=obufn=1;
}

void loop(){
while(obufn==bufn); // wait for buffer to be full
SerialUSB.write((uint8_t *)buf[obufn],512); // send it - 512 bytes = 256 uint16_t
obufn=(obufn+1)&3;
}

It reads ADC data at 1 million samples/sec and outputs the data to SerialUSB. (I've only tested it on Linux and most of the time it works, sometimes it is unreliable, I don't know why). Using GNU Radio I was then able to analyse the data stream and receive a long-wave radio signal, with an LC tuned circuit into A0 as an aerial.

This code is running smoother and with less jitter.
I am measuring jitter, by setting HIGH and LOW one Digital pin with an oscilloscope and the inline function:
Code: [Select]
inline void digitalWriteDirect(int pin, boolean val){
if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
else    g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}

BTW, I am controlling the sample rate by using the PRESCAL register in:
Code: [Select]

ADC->ADC_MR |= 0x2680; // these lines set free running mode on adc 7 (pin A0)  [PRESCAL at  50kHz]

Is there any other (better) way to  do it?

Actually, depending on if I am using one channel or two an the ADC, I need to setup a different PRESCAL, since there is only one ADC for all input pins:
Code: [Select]

if (channels_2)
{
}
else
{
ADC->ADC_MR |= 0x2680; // these lines set free running mode on adc 7 (pin A0) PRESCAL at 50kHz
}

On the other hand, what would the changes for two channels and 4 buffers? Is it possible to get a buffer for each channel?

Finally, I'm trying to measure  performance (the same way as jitter) and for  50kHz sample rate and 512 points, the time in the interrupt is 1.8us and the free time in the loop is 10.23ms (99.98% free!!).
For 1MHz, it is still around 1.8us against 256us (it is still 99.29% free), however more jitter at the beggining of the loop is appreciated.

I think it is because of the
Code: [Select]
while(obufn==bufn); instruction

It is interesting the
Code: [Select]

SerialUSB.begin(0);
while(!SerialUSB);

Does the computer (or other device) set the speed of the port?

Your spectrogram app looks great, keep up the good work!! Congratulations.

stimmer

#10
Mar 01, 2013, 10:26 pm
The jitter you are measuring is not the same thing as the clock jitter that was mentioned earlier in the thread and will not affect the accuracy of the ADC readings. Clock jitter is caused by the accuracy of the timing and waveform of the master clock oscillator and there is nothing we can do about that.

It isn't possible to get a buffer per channel as far as I know. There is a tagged mode where the highest 4 bits are written with the channel the sample came from, that is useful when you enable more than one channel.

SerialUSB works as fast as it can, the baud rate is ignored. Since it's USB2.0 the port is faster than the SAM3X can throw data down it. I've had it running at over 50Mbit/sec.

PakARD

#11
Mar 01, 2013, 11:39 pm

The jitter you are measuring is not the same thing as the clock jitter that was mentioned earlier in the thread and will not affect the accuracy of the ADC readings. Clock jitter is caused by the accuracy of the timing and waveform of the master clock oscillator and there is nothing we can do about that.

It isn't possible to get a buffer per channel as far as I know. There is a tagged mode where the highest 4 bits are written with the channel the sample came from, that is useful when you enable more than one channel.

SerialUSB works as fast as it can, the baud rate is ignored. Since it's USB2.0 the port is faster than the SAM3X can throw data down it. I've had it running at over 50Mbit/sec.

I'm sorry about the jitter misunderstanding, after thinking it I knew it was a different issue.

So, about the ADC sampling in both channels, then I suppose that the samples are alternated in the buffers, right?  Sorry to insist,but I am learning about the system...so, how could I set up a second channel?

stimmer

#12
Mar 02, 2013, 02:05 am
I think that to use a second channel, all you have to do is enable it. You are right, the samples end up alternated in the buffers.

I don't know of any proper documentation for the adc_init function. I just searched through the Arduino directory for files containing adc_init, then looked at the source files.

Gericom

#13
Mar 02, 2013, 10:10 amLast Edit: Mar 02, 2013, 01:00 pm by Gericom Reason: 1

The jitter you are measuring is not the same thing as the clock jitter that was mentioned earlier in the thread and will not affect the accuracy of the ADC readings. Clock jitter is caused by the accuracy of the timing and waveform of the master clock oscillator and there is nothing we can do about that.

It isn't possible to get a buffer per channel as far as I know. There is a tagged mode where the highest 4 bits are written with the channel the sample came from, that is useful when you enable more than one channel.

SerialUSB works as fast as it can, the baud rate is ignored. Since it's USB2.0 the port is faster than the SAM3X can throw data down it. I've had it running at over 50Mbit/sec.

I'm sorry about the jitter misunderstanding, after thinking it I knew it was a different issue.

So, about the ADC sampling in both channels, then I suppose that the samples are alternated in the buffers, right?  Sorry to insist,but I am learning about the system...so, how could I set up a second channel?