Skip to content

Commit 51dcd9c

Browse files
committed
[avr] Improved SPI speed on 16bit transfer.
From #2376 (comment) Quoting Andrew Kroll: [..this commit..] introduces a small delay that can prevent the wait loop form iterating when running at the maximum speed. This gives you a little more speed, even if it seems counter-intuitive. At lower speeds, it is unnoticed. Watch the output on an oscilloscope when running full SPI speed, and you should see closer back-to-back writes. Quoting Paul Stoffregen: I did quite a bit of experimenting with the NOP addition. The one that's in my copy gives about a 10% speedup on AVR.
1 parent c1cca1f commit 51dcd9c

File tree

1 file changed

+4
-0
lines changed
  • hardware/arduino/avr/libraries/SPI

1 file changed

+4
-0
lines changed

hardware/arduino/avr/libraries/SPI/SPI.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,20 @@ class SPIClass {
193193
in.val = data;
194194
if (!(SPCR & _BV(DORD))) {
195195
SPDR = in.msb;
196+
asm volatile("nop");
196197
while (!(SPSR & _BV(SPIF))) ;
197198
out.msb = SPDR;
198199
SPDR = in.lsb;
200+
asm volatile("nop");
199201
while (!(SPSR & _BV(SPIF))) ;
200202
out.lsb = SPDR;
201203
} else {
202204
SPDR = in.lsb;
205+
asm volatile("nop");
203206
while (!(SPSR & _BV(SPIF))) ;
204207
out.lsb = SPDR;
205208
SPDR = in.msb;
209+
asm volatile("nop");
206210
while (!(SPSR & _BV(SPIF))) ;
207211
out.msb = SPDR;
208212
}

0 commit comments

Comments
 (0)