Having hard time achieving 1 Msps with external 12bit ADC(LTC2365) using Arduino

I have an LTC2365/1 Msps 12bit-ADC with SPI pins. Somehow I only could achieve a maximum of 200 Ksps with this ADC rather than something close to 1 Msps using an Arduino Due. I wonder if anyone could help me with this issue. I tried many ways but couldn’t figure it out.

Datasheet for the ADC: http://cds.linear.com/docs/en/datasheet/23656fb.pdf

Here is the code I use for Arduino:

#include <SPI.h>
const int spi_ss = 10;      // set SPI SS Pin
uint8_t byte_0, byte_1;    // First and second bytes read
uint16_t spi_bytes;       // final 12 bit shited value
long int starttime,endtime;
long int count=0;

void setup() {
  SerialUSB.begin(2000000);          // begin serial and set speed
  pinMode(spi_ss, OUTPUT);     // Set SPI slave select pin as output
  digitalWriteDirect(spi_ss, HIGH);  // Make sure spi_ss is held high
  SPI.begin();
  SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE0));
  loop2();
  SPI.endTransaction();
}

void loop2() {
   starttime=millis();
   endtime=starttime;
   while((endtime-starttime)<=1000) {
      // write the LTC CS pin low to initiate ADC sample and data transmit
      digitalWriteDirect(spi_ss, LOW);
      byte_0 = spi_read(0x00); // read firt 8 bits
      byte_1 = spi_read(0x00); // read second 8 bits
      digitalWriteDirect(spi_ss, HIGH);
      // wite LTC CS pin high to stop LTC from transmitting zeros.
      spi_bytes = ( ( (byte_0 <<6) ) + (byte_1 >>2) );
      SerialUSB.println(spi_bytes);
      count=count+1;
      endtime=millis(); 
   }
   //samples per second
   SerialUSB.println(count);  
}

static inline uint8_t spi_read(uint8_t b) {
  return SPI.transfer(b);
}

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;
}

I was able to read 351 ksps with library called TurboSPI. It seems like SPI.transfer() taking a lot of time. Anybetter Suggestions to make it faster?

Library is at: TurboSPI

#include <TurboSPI.h>
TurboSPI  g_SPI;
//DigitalPin  spi_ss, g_PinRS;
const int spi_ss=10;
uint8_t   g_Buffer[256];  // some data buffer to transfer
uint8_t   g_Divisor = 2;  // transfer speed set to MCU's clock divide by 2
uint8_t byte_0, byte_1;    // First and second bytes read
uint16_t spi_bytes;       // final 12 bit shited value
long int starttime, endtime;
long int count = 0;
void setup() {
  // put your setup code here, to run once:

SerialUSB.begin(2000000);          // begin serial and set speed
//spi_ss.Begin(10);
//spi_ss.PinMode(OUTPUT);
pinMode(spi_ss,OUTPUT);
g_SPI.Init(g_Divisor);
g_SPI.Begin();
loop2();
}
void loop2() {
  for (int i = 0; i < 30; i++) {
    starttime = millis();
    endtime = starttime;
    count = 0;
    while ((endtime - starttime) <= 1000) {
      // write the LTC CS pin low to initiate ADC sample and data transmit
      //spi_ss.Low();
      digitalWriteDirect(spi_ss,LOW);
      byte_0 = g_SPI.Receive(); // read firt 8 bits
      byte_1 = g_SPI.Receive(); // read second 8 bits
      digitalWriteDirect(spi_ss,HIGH);
      //spi_ss.High();
      // wite LTC CS pin high to stop LTC from transmitting zeros.
      spi_bytes = ( ( (byte_0 << 6) ) + (byte_1 >> 2) );
      //SerialUSB.println(spi_bytes);
      count = count + 1;
      endtime = millis();
    }
    //samples per second
    SerialUSB.println(count);
  }
}
void loop() {
  // put your main code here, to run repeatedly:

}
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;
 }

Hi guys, I have found an other library similar to turbo spi. now i am getting exactly 1Msps @ 16MHZ. i have not check it with function generator yet. It may also help others since i couldn’t find a lot information about this.

This is the full code with library

duespidiif.ino (9.69 KB)