1

I have a Rapsberry Pi master sending SPI requests to an Uno Chinese clone slave : I send 16 characters to the Arduino, which answers 16 others characters. What I send is not important, it's the Arduino answer that matters.

When I set the speed on the Raspberry Pi at 1 Mbit/s with my C program, the received answer coming from the Arduino is mostly garbage (random characters), but this issue disappears with lower speeds.

So that leads me to this question : what is the maximum SPI speed for a slave Uno/clone, in order to have no errors ?

Edit : the Arduino code :

#include <SPI.h>
char receivingBuffer[5];
char sendingBuffer[5] = {'T', 'e', 's', 't', '!'};
volatile char bytes_received = 0;
void setup()
{
 SPCR |= bit(SPE);
 pinMode(MISO, OUTPUT);
 SPI.attachInterrupt();
 SPDR = sendingBuffer[bytes_received];
}
ISR(SPI_STC_vect)
{
 if (++bytes_received == 5)
 {
 bytes_received = 0;
 }
 SPDR = sendingBuffer[bytes_received];
}
void loop()
{
 //
}

Also, the C code on the RPi :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPiSPI.h>
#define SPI_CHANNEL 0
#define SPI_SPEED 1000000 // Hz.
int main(void)
{
 wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED);
 while (1)
 {
 char buf[5] = {'T', 'e', 's', 't', '?'};
 printf("Sending : %s\n", buf);
 wiringPiSPIDataRW(SPI_CHANNEL, buf, 5);
 printf("Receiving : %s\n\n", buf);
 sleep(1);
 }
 return 1;
}
asked Aug 2, 2017 at 8:44
7
  • 1
    The theoretical maximum is 8Mbps, which is F_CPU/2. What matters though is that you react fast enough to the requests for each byte and fill the buffer fast enough. That's down to your program to determine. Commented Aug 2, 2017 at 9:48
  • Please show your code. What you are doing in order to answer is highly relevant. If you are sending at 1 Mbits/s then the slave doesn't have many clock cycles to formulate a response. Commented Aug 2, 2017 at 10:37
  • I added the Arduino code. Commented Aug 2, 2017 at 11:22
  • What happens if you set a transfer speed of >1Mbps and put a delay in between each byte (at the Pi side) - does it start working then? Commented Aug 2, 2017 at 11:29
  • With 1.5 or 2 Mbps, I receive the first character of the expected answer, and the other characters are the sent message's ones. Commented Aug 2, 2017 at 11:32

1 Answer 1

2

The maximum speed for Arduino as a slave is F_CPU/4, so it's 4Mbps. When the Arduino is the master it can work at F_CPU/2, so it's 8Mbps

answered Aug 2, 2017 at 9:56
2
  • Thank you for your answer ! Do you have any source to confirm this ? Commented Aug 2, 2017 at 11:33
  • 1
    You can find this in ATmega328 datasheet. The ATmega328 is the chip on Arduino Uno. Commented Aug 3, 2017 at 7:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.