I found my old code, it has a lot of error checking and preloading of the SPDR because it was a two-way transaction. It also detected the master and slave getting out of sync, this can be a real problem with SPI as there is no maximum period for the clock so once you get out of sync you can easily read say 100 bytes from end of one packet and the 156 from the start of the next one 2 seconds later. (This is not a problem if SS is used to sync the two).
I've removed all that code to leave a bare bones function.
#define MAX_BYTES 256; // or 249 or whatever
byte spi_data[MAX_BYTES];
volatile byte data_ready = false;
ISR (SPI_STC_vect) {
/////////////////////////////////////////////////////
// This interrupt is caused by the first 8 clock pulses from the
// master having shifted a byte into the SPDR
//
// Now we're here though we don't use interrupts but poll the
// SPIF flag. This gives much faster response to the server and
// reduces the delays the server has to apply between bytes.
int count = 0;
byte * ptr = data;
*ptr++ = SPDR; // get first byte ASAP
for (int i = 0; i < MAX_BYTES - 1; i++) {
while ((SPSR & (1 << SPIF)) == 0) ;
*ptr++ = SPDR;
}
data_ready = true;
}
void loop() {
if (data_ready) {
data_ready = false;
// process the data
}
}
Note that I do the whole job in an ISR, I'll get a caning for that but sometimes it's OK to break the rules I think, and after all what else are you going to do, you don't have time for any other code and you really don't want other interrupts during this either. You could however just set the data_ready flag and do the data acquisition elsewhere as we normally recommend, but this would add a large and non-deterministic latency and expose you to who-knows-what interrupts.
Note also this comment
This gives much faster response to the server and reduces the delays the server has to apply between bytes.
You may find that you have to add a small delay between bytes at the master end, if you do then it's probably better to just drop the bit rate, either way you've reached the end of the performance rope.
Adding a small delay after the first byte however may always be required to give the slave a bit more time to get ready.
It should go without saying that the data stream cannot be constant, you are doing a lot of printing so there has to be a large delay between bursts of data.
Totally untested but does compile.
EDIT: Small change made to the code.
______
Rob