SPI Vs TurboSPI speed

Hello In order to optimize SPI speed for a data communication application, I made some comparative tests with ARDUINO standard library SPI and TurboSPI downloaded from: https://github.com/anydream/TurboSPI I used an ARDUINO MEGA with unconnected SPI, with 8 Mbits clock I used byte by byte transfers and buffer transfers, no DMA. I used a sketch base on the example provided with TurboSPI library.

The results were: Standard SPI TurboSPI

10000 byte transfers 16.3 ms 17.6 ms

100 transfers of 100 byte buffer 14.5 ms 15.1 ms

Either I made a stupid mistake in my sketch (which I revised seceral times) or TurboSPI is slower than Standard SPI for non DMA transfers.

Thanks for any comments.

Here is the code for the previous subject:

//For Standard SPI:

#include <SPI.h>
#define CS 10
unsigned long t=0;
byte Buf[100];
void setup() {

pinMode(CS, OUTPUT); digitalWrite (CS,HIGH);
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
SPI.begin();
Serial.begin(115200);//Serial.println(“Setup”);
}

void loop() {

digitalWrite (CS,LOW);
t=micros();
for (int i=0; i<10000;i++) {
SPI.transfer(0xAB); //Bytes transfer
}
Serial.print(micros()-t); Serial.print(" ");
digitalWrite (CS,HIGH);

digitalWrite (CS,LOW);
t=micros();
for (int i=0; i<100;i++) {
SPI.transfer(Buf,sizeof(Buf)); //Buffer Transfer
}
Serial.println(micros()-t);
delay(3000);
}

//For TurboSPI:

#include <TurboSPI.h>

TurboSPI g_SPI;
DigitalPin g_PinCS, g_PinRS;
byte g_Buffer[100]; // some data buffer to transfer
byte g_Divisor = 2; // transfer speed set to MCU’s clock divide by 2
unsigned long t=0;
void setup() {

// setup pins
g_PinCS.Begin(45);
g_PinRS.Begin(47);

g_PinCS.PinMode(OUTPUT);
g_PinRS.PinMode(OUTPUT);

// setup SPI
g_PinCS.High();
Serial.begin(115200);
g_SPI.Begin();
}

void loop() {
// setup speed and select slave
g_SPI.Init(g_Divisor);
g_PinCS.Low();
// set some pins
g_PinRS.High(); // Select slave
t=micros();
for (int i=0; i<10000;i++) {
g_SPI.Send(0xAB); //Bytes transfer
}
Serial.print(micros()-t); Serial.print(" ");
g_PinCS.High(); // Unselect slave

g_PinCS.Low();
t=micros();
for (int i=0; i<100;i++) {
g_SPI.Send(g_Buffer, sizeof(g_Buffer)); //Buffer Transfer
}
Serial.println(micros()-t);
g_PinCS.High();
delay(3000);

}

Please use [ code ] tags. The </> icon on the full editor.

The hardware SPI works at the speed you tell it to. The default speed is fast, but not the fastest. You can change it with the standard Arduino library.

I posted the code separately. With the MEGA board, I used the maximum speed (8 MBits/sec) for both sketches.