简体   繁体   English

Arduino Uno和Raspberry Pi之间的SPI通信使用connectioningPi

[英]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. 我一直在尝试将Raspberry Pi 2 B +(主机)与Arduino Uno Rev3(从机)之间的SPI通信实现不成功。

I used the tutorial: http://mitchtech.net/raspberry-pi-arduino-spi/ 我使用了该教程: 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: 我在网站上对arduino使用相同的代码,但对树莓派使用以下代码:

/**
 *  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. 另外,看起来您所参考的教程使用的是pi模型B的原理图设置。我相信b +具有15个额外的gpio引脚,因此引脚分配也不同,因此您可能需要仔细检查所有物理连接是否正确制作。

The following C code for Arduino which is also inspired by same code that your. 以下Arduino的C代码也受到您相同代码的启发。 Do implement an SPI slave for Arduino. 一定要为Arduino实现SPI从设备。 I using the USB .NET compatible device NUSBIO to be the MASTER SPI and talk to the Arduino. 我将USB .NET兼容设备NUSBIO用作MASTER SPI,并与Arduino进行了交谈。

The code does work up to 10Kbyte/s. 该代码的确以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;
    }
}

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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