简体   繁体   中英

SPI communication between Arduino Uno and Raspberry Pi using wiringPi

I have been trying to implement with no success a SPI communication between a Raspberry Pi 2 B+ (master) to a Arduino Uno Rev3 (slave) to no success.

I used the tutorial: http://mitchtech.net/raspberry-pi-arduino-spi/

However, the codes offered there are not working properly. I have already search throughout the internet for a solution, but I do could not find one. I use the same code for arduino on the site, but I use the following code for raspberry pi:

/**
 *  Hello, SPI!
 */

 #include <stdio.h> // printf()
 #include <signal.h> // signal()
 #include <errno.h> // strerro
 #include <string.h>

 #include <wiringPi.h> // GPIO
 #include <wiringPiSPI.h> // SPI

 int volatile interrupt = 0;
 #define len_max 100
 int volatile len = 0;
 unsigned char buffer[len_max];
 static const int speed = 500000;

 int const CE0 = 0;

 void sig_handler(int signo)
 {
   if(signo == SIGINT)
   {
     interrupt = 1;
   }
 }

 void setup(void)
 {
   signal(SIGINT, sig_handler);

   wiringPiSetupGpio () ;

   if(wiringPiSPISetup(CE0, speed) < 0)
   {
     printf("SPI setup failed: %s\n", strerror(errno));
     interrupt = 1;
   }

   printf("System ready.\n");
 }

 void loop(void)
 {
   memcpy(buffer, "Hello world!\n", sizeof buffer);
   len = 12;
   if( wiringPiSPIDataRW (CE0, buffer, len) < 0)
     printf("Error while recieving mesage\n");
   printf("Received mesage: %s \n", buffer);
   delay(1000);
 }

 void close(void)
 {
   printf("Ending activities.\n");
 }

 int main(void)
 {
   setup();
   while(!interrupt) loop();
   close();
   return 0;
 }

This code only returns me garbage. I have no idea of what to do.

If you were to post the output you get when you execute your code, that would be helpful in figuring out whats going wrong. Also, it looks like the tutorial you refer to uses schematic setup with a pi model B. I believe the b+ has 15 extra gpio pins, and thus a different pinout, so you might want to double check that all of your physical connections are properly made.

The following C code for Arduino which is also inspired by same code that your. Do implement an SPI slave for Arduino. I using the USB .NET compatible device NUSBIO to be the MASTER SPI and talk to the Arduino.

The code does work up to 10Kbyte/s. For now at higher speed, I am loosing data.

`

 include <Adafruit_NeoPixel.h>
 include <SPI.h>
 include <fArduino.h>

#define API_SIZE 128
char buf[API_SIZE];

volatile int bufCheckSum = 0;
volatile int pos = 0;
volatile boolean process_it = false;

void spiSetup(void)
{
    // Move from SPI_CLOCK_DIV4 to SPI_CLOCK_DIV2
    //SPI.setClockDivider(SPI_CLOCK_DIV2);

    SPCR |= bit(SPE); // turn on SPI in slave mode
    pinMode(MISO, OUTPUT); // have to send on master in, *slave out*
    pos = 0;
    process_it = false;
    SPI.attachInterrupt(); // now turn on interrupts
}

// SPI interrupt routine
ISR(SPI_STC_vect)
{
    byte c = SPDR;        // Grab byte from SPI Data Register

    if (pos < sizeof buf) // Add to buffer if room
    {
        if (c == '\r') { // Performance test

            bufCheckSum = 0;
            for (int i = 0; i < pos; i++) {
                bufCheckSum += buf[i];
            }
            pos = 0; // Reset buffer
            SPDR = 1;
            process_it = true;
        }
        else {
            buf[pos++] = c; // Store c in buffer
            SPDR = c;
        }
    }
    else {
        pos = 0;
    }
}

void setup() {

    Board.Delay(1000 * 2); // Wait 1.5 second before initializing the serial com, so  I can start the ArduinoWindowsConsole on the Windows machine
    Board.InitializeComputerCommunication(9600, "Arduino Spi Slave +");
    Board.SetPinMode(10, INPUT);
    Board.SetPinMode(2, INPUT); // JUST FOR TESTING
    spiSetup();
}

void loop() {

    if (process_it) {

        process_it = false;
        if (bufCheckSum != 8255) {
            Serial.println("ko ");
        }
        bufCheckSum = -1;
    }
}

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM