Arduino-SPI
Serial Peripheral Interface (SPI) library for Arduino
Benchmark.ino
Go to the documentation of this file.
1 #include "GPIO.h"
2 #include "SPI.h"
3 #include "benchmark.h"
4 
5 // Configuration: SPI/BITORDER
6 // #define USE_SOFTWARE_SPI
7 // #define BITORDER LSBFIRST
8 #define BITORDER MSBFIRST
9 
10 #if defined(USE_SOFTWARE_SPI)
11 #include "Software/SPI.h"
12 #if defined(ARDUINO_attiny)
13 #include "Software/Serial.h"
14 Software::Serial<BOARD::D0> Serial;
15 GPIO<BOARD::D1> ss;
18 #else
19 GPIO<BOARD::D10> ss;
22 #endif
23 
24 #else
25 #include "Hardware/SPI.h"
26 GPIO<BOARD::D10> ss;
29 #endif
30 
31 void setup()
32 {
33  Serial.begin(57600);
34  while (!Serial);
35  BENCHMARK_BASELINE(1000);
36 }
37 
38 void loop()
39 {
40  // Measure SPI bus manager performance with and without slave select
41  // and device setting
42 
43  const uint8_t PRESCALE = spi.prescale(SPI::MAX_FREQ);
44  uint8_t src[100];
45  uint8_t dest[100];
46 
47  ss.toggle();
48  spi.acquire(0, BITORDER, PRESCALE);
49  BENCHMARK("1a. SPI bus manager data transfer only (1 byte)", 1000) {
50  spi.transfer(src[0]);
51  }
52 
53  BENCHMARK("1b. - transfer (10 byte)", 1000) {
54  spi.transfer(dest, src, 10);
55  }
56 
57  BENCHMARK("1c. - transfer (100 byte)", 1000) {
58  spi.transfer(dest, src, 100);
59  }
60 
61  BENCHMARK("1d. - read (100 byte)", 1000) {
62  spi.read(dest, 100);
63  }
64 
65  BENCHMARK("1e. - write only (100 byte)", 1000) {
66  spi.write(src, 100);
67  }
68  spi.release();
69  ss.toggle();
70 
71  BENCHMARK("2a. SPI bus manager data transfer (1 byte)", 1000) {
72  ss.toggle();
73  spi.acquire(0, BITORDER, PRESCALE);
74  spi.transfer(src[0]);
75  spi.release();
76  ss.toggle();
77  }
78 
79  BENCHMARK("2b. - transfer (10 byte)", 1000) {
80  ss.toggle();
81  spi.acquire(0, BITORDER, PRESCALE);
82  spi.transfer(dest, src, 10);
83  spi.release();
84  ss.toggle();
85  }
86 
87  BENCHMARK("2c. - transfer (100 byte)", 1000) {
88  ss.toggle();
89  spi.acquire(0, BITORDER, PRESCALE);
90  spi.transfer(dest, src, 100);
91  spi.release();
92  ss.toggle();
93  }
94 
95  BENCHMARK("2d. - read (100 byte)", 1000) {
96  ss.toggle();
97  spi.acquire(0, BITORDER, PRESCALE);
98  spi.read(dest, 100);
99  spi.release();
100  ss.toggle();
101  }
102 
103  BENCHMARK("2e. - write (100 byte)", 1000) {
104  ss.toggle();
105  spi.acquire(0, BITORDER, PRESCALE);
106  spi.write(dest, 100);
107  spi.release();
108  ss.toggle();
109  }
110 
111  dev.acquire();
112  BENCHMARK("3a. SPI device driver data transfer only (1 byte)", 1000) {
113  dev.transfer(src[0]);
114  }
115 
116  BENCHMARK("3b. - transfer (10 bytes)", 1000) {
117  dev.transfer(dest, src, 10);
118  }
119 
120  BENCHMARK("3c. - transfer (100 bytes)", 1000) {
121  dev.transfer(dest, src, 100);
122  }
123 
124  BENCHMARK("3d. - read (100 bytes)", 1000) {
125  dev.read(dest, 100);
126  }
127 
128  BENCHMARK("3e. - write (100 bytes)", 1000) {
129  dev.write(src, 100);
130  }
131  dev.release();
132 
133  BENCHMARK("4a. SPI device driver data transfer (1 byte)", 1000) {
134  dev.acquire();
135  dev.transfer(src[0]);
136  dev.release();
137  }
138 
139  BENCHMARK("4b. - transfer (10 bytes)", 1000) {
140  dev.acquire();
141  dev.transfer(dest, src, 10);
142  dev.release();
143  }
144 
145  BENCHMARK("4c. - transfer (100 bytes)", 1000) {
146  dev.acquire();
147  dev.transfer(dest, src, 100);
148  dev.release();
149  }
150 
151  BENCHMARK("4d. - read (100 bytes)", 1000) {
152  dev.acquire();
153  dev.read(dest, 100);
154  dev.release();
155  }
156 
157  BENCHMARK("4e. - write (100 bytes)", 1000) {
158  dev.acquire();
159  dev.write(src, 100);
160  dev.release();
161  }
162 
163  Serial.println();
164  delay(2000);
165 }
virtual void acquire(uint8_t mode, uint8_t bitorder, uint8_t prescale)
Definition: SPI.h:70
virtual uint8_t transfer(uint8_t value)
Definition: SPI.h:90
static const uint32_t MAX_FREQ
Definition: SPI.h:30
virtual uint8_t prescale(uint32_t frequency)
Definition: SPI.h:51
void loop()
Definition: Benchmark.ino:38
GPIO< BOARD::D10 > ss
Definition: Benchmark.ino:26
virtual void write(const void *buf, size_t count)
Definition: SPI.h:154
Hardware::SPI spi
Definition: Benchmark.ino:27
#define BITORDER
Definition: Benchmark.ino:8
virtual void read(void *buf, size_t count)
Definition: SPI.h:132
void setup()
Definition: Benchmark.ino:31
virtual void release()
Definition: SPI.h:67
SPI::Device< 0, BITORDER, SPI::MAX_FREQ, BOARD::D10 > dev(spi)