0

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;

}
  • 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. – Majenko Aug 2 '17 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. – Nick Gammon Aug 2 '17 at 10:37
  • I added the Arduino code. – Thesaurus Rex Aug 2 '17 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? – Majenko Aug 2 '17 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. – Thesaurus Rex Aug 2 '17 at 11:32
1

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

  • Thank you for your answer ! Do you have any source to confirm this ? – Thesaurus Rex Aug 2 '17 at 11:33
  • 1
    You can find this in ATmega328 datasheet. The ATmega328 is the chip on Arduino Uno. – bouaaah Aug 3 '17 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.