Last active
August 29, 2015 14:00
-
-
Save cmaglie/11366715 to your computer and use it in GitHub Desktop.
SPITest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
void setup() { | |
SPI.begin(); | |
Serial.begin(9600); | |
SPI.setClockDivider(SPI_CLOCK_DIV2); | |
while (!Serial) | |
; | |
delay(5000); | |
Serial.print(" Plain SPI: "); | |
test_no_transaction(); | |
Serial.println(); | |
#if defined(SPI_HAS_TRANSACTION) | |
Serial.print("Transaction API (interrupts not used): (4) "); | |
test_transaction_4x(); | |
Serial.println(); | |
Serial.print("Transaction API (interrupts not used): (2) "); | |
test_transaction_2x(); | |
Serial.println(); | |
// Uncomment to check interrupt-based API | |
SPI.usingInterrupt(0); | |
// Uncomment to check event-based API | |
//SPI.onBeginEvent(beginEvent); | |
//SPI.onEndEvent(endEvent); | |
Serial.print("Transaction API (using interrupts): (4) "); | |
test_transaction_4x(); | |
Serial.println(); | |
Serial.print("Transaction API (using interrupts): (2) "); | |
test_transaction_2x(); | |
Serial.println(); | |
#endif | |
} | |
boolean interruptSave; | |
void beginEvent() { | |
interruptSave = interruptsStatus(); | |
noInterrupts(); | |
} | |
void endEvent() { | |
if (interruptSave) interrupts(); | |
} | |
void loop() { | |
} | |
void test_transaction_4x() { | |
uint32_t start = millis(); | |
for (int i = 0; i < 30000; i++) { | |
SPI.beginTransaction(SPI_CLOCK_8000000, MSBFIRST, SPI_MODE0); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.endTransaction(); | |
SPI.beginTransaction(SPI_CLOCK_8000000, MSBFIRST, SPI_MODE0); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.endTransaction(); | |
SPI.beginTransaction(SPI_CLOCK_8000000, MSBFIRST, SPI_MODE0); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.endTransaction(); | |
SPI.beginTransaction(SPI_CLOCK_8000000, MSBFIRST, SPI_MODE0); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.endTransaction(); | |
} | |
uint32_t delta = millis() - start; | |
float loopsPerSec = 1000.0 / delta * 30000.0; | |
float throughput = loopsPerSec * 8 * 8; | |
Serial.print(throughput); | |
} | |
void test_transaction_2x() { | |
uint32_t start = millis(); | |
for (int i = 0; i < 30000; i++) { | |
SPI.beginTransaction(SPI_CLOCK_8000000, MSBFIRST, SPI_MODE0); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.endTransaction(); | |
SPI.beginTransaction(SPI_CLOCK_8000000, MSBFIRST, SPI_MODE0); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.endTransaction(); | |
} | |
uint32_t delta = millis() - start; | |
float loopsPerSec = 1000.0 / delta * 30000.0; | |
float throughput = loopsPerSec * 8 * 8; | |
Serial.print(throughput); | |
} | |
void test_no_transaction() { | |
uint32_t start = millis(); | |
for (int i = 0; i < 30000; i++) { | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
SPI.transfer(0xff); | |
} | |
uint32_t delta = millis() - start; | |
float loopsPerSec = 1000.0 / delta * 30000.0; | |
float throughput = loopsPerSec * 8 * 8; | |
Serial.print(throughput); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment