I have got an issue I am fighting for two days now... I set up SPI communication between Raspberry Pi and Arduino Mega 2560. (it works). The lines are very short (total length about 5 cm) through pin headers; there is 3.3 V to 5.0 V converter inbetween.
On the raspberry side, Pi sends blocks of about 500 bytes 2-3 times a second. On the arduino side, arduino stores the data in a buffer and sends back data from a different buffer.
The raspberry-side data is shorter than the data I need to receive from arduino, so that after sending meaningful data, filler bytes (containing "X") are sent to complete 500 bytes record.
Now the problem: from time to time in the data received from arduino one byte is replaced, and the value of this byte corresponds to the filler byte, i.e. the byte sent from Pi to arduino is "mirrored" back from arduino to pi, replacing the correct byte which should be sent.
The location of the incorrectly returned byte in the record varies and seems to be random. The frequency of error also varies, but is between 2% and 20 %. (i.e. 2% to 20 % of the blocks of 500 bytes contain one byte swapped.)
I tried different speeds, from 4.000.000 down to 40000. There is no clear pattern, but it seems that the error occurs much more frequently at LOWER speeds. At 4000000 I had sequencies of up to 50 transmissions without a glitch... But then they start and several can appear in a row...
I am not sure if it is a hardware issue or some problem with interrupts... Considering that lower bitrate seems to cause more problems I am inclined to think that it has to do with the program. I have several interrupts in use, but from my understanding, if there would be a delay in reading the byte, the byte would be lost, both for receiving and for sending. But this does not happen, byte is not lost, it is replaced..
Any help would be really appreciated, because I am stuck...