SPI problems with ethernet shield (Mega and Uno)

Hello,

I am trying to control a set of Unos in the following manner: I send a char string from a remote location (Labview) via an Ethernet connection to a Mega2560 w/Ethernet shield, which then parses the received data and sends it out to a number of Uno slaves via SPI. Each slave is being used to run PID control on a switching mode power supply, and the info from the Mega master updates the tuning coefficients and time step. It also changes whether the setpoint is measured from a local analogRead() pin (which is reading the output of a signal generator) or set remotely to a DC value. Finally, it changes the Input pin, allowing me to set whether the power supply is voltage controlled, current controlled, etc, as I have measurement devices (LEMs) following these values.

The main problem I'm having is that the slave is failing to correctly read in all 20 bytes of SPI communication. It sometimes works, but sometimes only reads in 18 or 19 bytes. I've tried giving the slave more time between bytes, since the communication speed is a low priority and updates to the PID coefficients will happen only when the power supplies are off. I've also tried turning off the Ethernet shield SPI while the master broadcasts to the slave(s) - this actually fixed a previous problem, but the byte-dropping remains. Right now my best guess is that there is some interference with the SPI bus read-in, maybe due to the PID algorithm? I know some digital pins are tied together via Timing stuff, but I don't know how that would mess anything up.

The code for the Master and Slave are attached.

As you can see, the Slave code lets me see how many bytes have been received after an interrupt. I see one of two things. Either the desired behavior, where all 20 bytes are properly read:

pos= 20
pos= 20
buf= 5 0 FA 0 0 0 80 3F 0 0 0 0 0 0 80 3F 0 0 80 3F 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

or the undesired behavior, where less than 20 bytes are read. Note that the Master sent two 20-byte packets to generate this response; everything including and after "control type error" followed the second packet send.

pos= 19
pos= 19
pos= 19
pos= 19
pos= 19
pos= 19
pos= 19
pos= 19
pos= 19
pos= 19
pos= 19
control type error
pos= 39
buf= 40 3E 80 0 0 20 F C0 0 0 0 0 0 20 F C0 0 20 F 5 0 FA 0 0 0 80 3F 0 0 0 0 0 0 80 3F 0 0 80 3F 0 0 0 0 0 0 0 0 0 0 0

In both of the above cases, pos is reset to 0 after a correct read-in, and so either can happen multiple time in a row. It really seems random to me.

The main SPI code is from http://www.gammon.com.au/forum/?id=10892. I apologize for all the messiness in the code - I'm just getting started with Arduinos!

Master_4_15_2013.txt (8.17 KB)

Slave_4_15_2013.txt (7.85 KB)