I am working with an ADC that has max sampling rate of 500kHz but using Teensy 3.5 the maximum I have been able to achieve is 300kHz. There is a ~1us delay between the data ready signal going low (blue trace) and the clock to read out the data starting (Yellow trace). Also another ~.75us delay after the clock has finished and the interrupt returns to the main loop to start the next conversion. I have attached screen capture from scope and code below. I assume there is some overhead in the beginTransaction() and endTransaction() commands, is there anyway to reduce these or is entering and exiting the interrupt likely accounting for more of the delay? I commented out the SPI.usingInterrupt() and the frequency increased by approx 20kHz.
Any help on how to reduce these delays would be much appreciated.
Any help on how to reduce these delays would be much appreciated.
Code:
#include <SPI.h>
const int frameSize = 30000;
const byte cnv = 10;
const byte BUSY = 7;
volatile int adcBytes[frameSize];
volatile int count = 0;
volatile boolean stringComplete = false;
volatile String inputString = "";
volatile String Mode;
SPISettings settingsA(50000000, MSBFIRST, SPI_MODE0);
void setup()
{
Serial.begin(250000);
pinMode(cnv, OUTPUT);
digitalWrite(cnv, LOW);
SPI.begin();
//SPI.usingInterrupt(digitalPinToInterrupt(BUSY));
attachInterrupt(digitalPinToInterrupt(BUSY), convers, FALLING);
}
FASTRUN void loop()
{
if (stringComplete)
{
HandleData();
}
while (Mode == "s")
{
digitalWrite(cnv, HIGH);
if (count == frameSize)
{
count = 0;
for (int i=0;i<frameSize;i++)
{
Serial.write((adcBytes[i] >> 8) & 0xFF); // Send the upper byte first
Serial.write(adcBytes[i] & 0xFF);
}
Serial.println();
Mode = "END";
}
}
}
FASTRUN void convers()
{
SPI.beginTransaction(settingsA);
adcBytes[count] = SPI.transfer16(0);
SPI.endTransaction();
digitalWrite(cnv, LOW);
count = count+1;
}
void serialEvent()
{
while (Serial.available())
{
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n')
{
stringComplete = true;
}
}
}
void HandleData()
{
int commaIndex = inputString.indexOf(',');
Mode = inputString.substring(0, commaIndex);
inputString = "";
stringComplete = false;
}