Bidirectional SPI (Arduino - Raspberry PI)

Hi Guys

I'm having trouble starting off with developing on the Arduino. I find the Documentation on how to actually communicate over SPI kind of non existing. So i'm asking for guidance.

Raspberry Pi + Arduino MEGA 2560

The setup i seek is as followed:

Pi controls Arduino by sending commands. (This works and is the topic of many samples). After the Arduino reveived a command, the Pi waits for a Response.

The whole communication should be as asynchronous as possible. Meaning the Pi can receive data anytime but only reacts when its expecting something. And also the Pi should be able to send data anytime it wants.

So where do i start? How to find out which of the Two should be Master or Slave? How do i wait on the Pi (with c/c++) for SPI input?

You see i'm a beginner, asking beginner questions. Any directions, hints or possibly code would be much appreciated.

One more Question: I have the two spi devices /dev/spidev0.0 and /dev/spidev0.1. Is one of them the MOSI and the other one the MISO device? Because the spidev_test.c file uses spidev0.0 and uses both, a read and a transmit buffer which wouldn't make any sense in that case.

Regards, nothinghere

SPI is not really geared towards bi-directional asynchronous communication like this. You would be much better off working with normal RS232.

With SPI one device, and one device only, is the master. That one generates all the clock signals, and dictates the timing to everything else.

SPI is full duplex. While the master is transmitting, it is also receiving data from the slave. If the slave has data to send, it should send then - if not, it typically sends 0.

For the master to "wait for a response" from the slave it is a case of the master polling the slave by sending a constant stream of 0 (or some other null token), so that the slave is able to send data if it has any (remember, the slave can only send data to the master at the same time that the master is sending data to the slave). This effectively bogs down the master with pointless work.

Another option would be to have a separate signalling channel - could be a single GPIO connection - that the slave uses to indicate to the master that it has finished its work and has data available, then the master starts its polling for data from the slave.

After the Arduino reveived a command, the Pi waits for a Response.

The whole communication should be as asynchronous as possible.

That's not my definition of asynchronous.

PaulS: That's not my definition of asynchronous.

Thanks for quoting that out. Meaning: the pi should wait for responses while still being able to send commands.

Majenko was pretty clear about how SPI works. I will probably end up with a constant stream of data, causing interrupts on the arduino.

So. Now i'm thinking about using a serial connection over USB. Not asynchronous but both can send data when the channel is free. This is probably still fast enough. No time to look into it until next week. But if anybody knows a tutorial or something, feel free to tell me.

Thanks

The Pi has an RS232 interface built into the GPIO header that you can wire direct into the TX and RX pins of the Arduino - no messing with USB.

And my latest library might be of help (though you'd have to implement the Pi side of it yourself, but the packet format is simple and described in the wiki): https://sf.net/projects/arduino-icsc

http://mitchtech.net/raspberry-pi-arduino-spi

Arduino SPI: http://www.gammon.com.au/spi

You might find I2C better for multiple masters.

Is one of them the MOSI and the other one the MISO device? Because the spidev_test.c file uses spidev0.0 and uses both, a read and a transmit buffer which wouldn't make any sense in that case.

That's covered in the link, but basically SPI usually has both MOSI and MISO simultaneously (ie. the same device).

why arduino uno(slave) return back byte received from rpi if no byte to send?

Not sure what your project is or what you have the Arduino doing, but if it is fairly simple
things, you might want to take look at firmata.
This will allow you to control the Arduino pins from the Pi.

— bill

venksbond: why arduino uno(slave) return back byte received from rpi if no byte to send?

It doesn't. Post your code please.