Go Down

Topic: Arduino as SPI Master and Slave Demo Code (Read 5 times) previous topic - next topic

LinhartR

Aug 26, 2013, 02:21 am Last Edit: Aug 26, 2013, 02:46 am by LinhartR Reason: 1
What a fun project this turned out to be! I feel like I just finished a good video game or something. I hope I'm not the only one who found the documentation for Arduino SPI challenging. For example the SPI library does not support SPI slave mode. This doesn't present too much of a problem but does require you to do a bit more of the "dirty work" yourself when working on the SPI slave sketch. I've attached my sketches for an Arduino SPI Master and Slave. The code is heavily documented and loaded with Serial.println output for debugging. Don't forget to make a Gnd connection between each Arduino. I'm hoping to have more Arduinos to work with and would enjoy testing a SPI master with two or more SPI slaves. My demo video can be seen on YouTube at http://youtu.be/A9oSX_dwJKQ

If you found this fun or interesting please make a small donation to my PayPal account at https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GTBD7T7BXPGQY. I have many more Arduino projects in mind and appreciate your support.

Nick Gammon

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

On that page:

http://www.gammon.com.au/forum/?id=10892&reply=1#reply1  ("How to make an SPI slave")
http://www.gammon.com.au/electronics

LinhartR

Hey Nick! Great reference! Most of what I did was actually based on this article. You are even credited in the sketches I uploaded. I appreciated but chose not to use an Interrupt Service Routine in my solution. I must have been viewing your article from a mirror site since I didn't see any of the follow up posts. Looks like I've got some more reading to do.
One thing I feel that I feel is significant in my example is controlling the MISO pin. Many of the examples I found ignored this important requirement when dealing with multiple slaves. You've inspired me to try implementing the SS as an interrupt. The biggest challenge I found was ensuring the slave had written it's data to the DR before the master begins the SPI transfer. I thought about using SS to signal each transfer and this would be much easier as an ISR.
I'm looking forward to purchasing a sensor board and SPI based sensor modules to learn more about the communication protocols used by these devices.

Nick Gammon

Quote

The biggest challenge I found was ensuring the slave had written it's data to the DR before the master begins the SPI transfer. I thought about using SS to signal each transfer and this would be much easier as an ISR.


I don't understand that bit. The master controls the timing (it sends the clock pulses). Once an SPI.transfer call completes the data is transferred. There is no waiting.
http://www.gammon.com.au/electronics

LinhartR


Quote

The biggest challenge I found was ensuring the slave had written it's data to the DR before the master begins the SPI transfer. I thought about using SS to signal each transfer and this would be much easier as an ISR.


I don't understand that bit. The master controls the timing (it sends the clock pulses). Once an SPI.transfer call completes the data is transferred. There is no waiting.


If the master expects a reply from the slave, the slave has no way of knowing when the master is going start cycling the clock. It has to write each bit to the DR before the master tries to clock it out.

Nick Gammon

OK, I see what you mean. From page 171 of the datasheet:



Basically you set up (get the data ready) on the opposite edge to the sample one. An interesting question is: "when does the first bit get set up?". I presume the answer would be:


  • After SS is asserted; or

  • As soon as possible after a response is requested


http://www.gammon.com.au/electronics

mr_pacheko

Hy Guys,

First of all, thanks for all the info, very good work!

I'm trying to do the same, but with a Master Arduino Due, and Slave Arduino Uno.

Colud be a problem with the SS pin to set the logical state if Due works in 3.3v and Uno in 5v?

Regards

Go Up