Pages: [1]   Go Down
Author Topic: SPI across two Arduino DUE's  (Read 4149 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have been searching for examples of how to set the Arduino DUE as a slave in SPI communication with no luck. I think this should be an easy task, but seem to be missing something important for everything to work. Here is what I am try to accomplish and it should be easy for this community.

Master Arduino DUE send a character 'E' to the slave and the slave just receive and prints 'E' over and over again.

The second task is to have the Master Arduino DUE send 2 characters 'E' and 'M' and the slave prints both out and respond with 1 character 'Y'

If I can get these two task and understand SPI, I will be able to implement similar functionality to a project I am working on.
Here are some code I testes and incorrect results.

Code:
/**********************************************
  MASTER
**********************************************/

#include <SPI.h>

#define SS 52

void setup()
{
   SPI.begin();
   pinMode(SS,OUTPUT);
   digitalWrite(SS,HIGH);
}

void loop()
{
   SPI.transfer(SS,(byte)'E', SPI_CONTINUE);
   //SPI.transfer(SS,(byte)'M', SPI_CONTINUE);
   delay(10);
}

Code:
/**********************************************
  SLAVE
**********************************************/

#include <SPI.h>

#define SS 52

void setup()
{
   SPI.begin();
   pinMode(SS,INPUT);
   Serial.begin(115200);
}

void loop()
{
   char c = SPI.transfer(SS,0x45,SPI_CONTINUE);
   Serial.println(c);
   delay(1000);
}

All I get on the output for every println is 255 as a character. I commented out the second transfer because I was not sure how SPI do the transfer.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 80
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, the SPI library only supports master mode:
Quote
[...]Since this library supports only master mode[...]
(quote from the library page)
So the Slave due is acting as a master too. Unfortunately I don't know how the spi is implemented in the due, so I can't help more.
Logged

L.C.

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bump. Can anyone help with this topic?
Logged

Ottawa,Canada
Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

any luck on slave mode ?

i'm looking to connect a DUE (slave)to a RaspberryPi(master) .. 
and spi has only been implemented on the Pi as master so i figured the Due would be a easier modification ....
 sigh .. time to dig into some data sheets & libs ...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
Errare Humanum Est
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I'm trying to operate a Due as slave too. But with no success.
I'm wondering if anyone has made any progress on that matter.
Is it possible to have a Mega as spi Master and an adafruit data logging shield as slave1 and a Due as slave2 ? I want the due acting as a sensor node.

I would  appreciate any help!
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I also need to make the “Arduino due” run in SPI slave mode as opposed to SPI master mode, (The defaults is SPI Master mode). To set the other Arduinos (Uno / Mega) to run in SPI Slave mode you do the following:-
// turn on SPI in slave mode
SPCR |= _BV(SPE);
Alas SPCR is not available on the “Arduino Due” which means I do not know how to set the DUE to run in SPI slave mode.
This is a popular need so any help will be very much appreciated.

Additional information
IDE used: Arduino V1.5.4 (Windows)
Board used: Arduino Due (Programming port)
Source code: Full version of the Arduino code that I am trying to run can be found on this link:-
http://mitchtech.net/raspberry-pi-arduino-spi/
Error received: "error: 'SPCR' was not declared in this scope"

« Last Edit: September 27, 2013, 01:56:09 pm by Stephan_Duino » Logged

Canada
Offline Offline
Sr. Member
****
Karma: 14
Posts: 450
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the slave setup that I use...

Code:
//SPI setup
 SPI.begin(10);
 SPI.setDataMode(10, SPI_MODE0);
 REG_SPI0_CR |= 0x1;          // SPI enable (write only)
 REG_SPI0_WPMR = 0x53504900;  // Write Protection disable
 REG_SPI0_MR = 0x2;           // DLYBCS=0, PCS=0, PS=1, MSTR=0
 REG_SPI0_CSR = 0xA;          // DLYBCT=0, DLYBS=0, SCBR=0, 8 bit transfer, Clock Phase = 1 for SPI mode 0
« Last Edit: September 28, 2013, 08:52:47 am by dlloyd » Logged

Disclaimer: I sometimes implement the infinite monkey theorem when composing a reply.

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can anyone help? We need an “Arduino Due SPI Slave mode” example? I have been trying to crack this problem with the intent of publishing the example here and using it myself but no luck so far. What I can share are all the good clues and good help that I have received / found, but alas no working example for the Due yet. I guess I need an enlightened soul to get us to the point of a working “Arduino DUE SPI slave mode” example. Please see below all the clues and help that I have received or found thus far:-
A most excellent source for SPI UNO-UNO / MEGA-MEGA is available here
http://gammon.com.au/spi
Many thanks to Nick Gammon for such a detailed write up.

Also a nice example of Arduino UNO/MEGA SPI slave mode to Raspberry PI as master.
http://mitchtech.net/raspberry-pi-arduino-spi/ 
Many thanks to Michael Mitchell for his detailed write up.
The UNO/MEGA examples above give us a good idea of how to implement the Arduino Due SPI slave solution but you cannot use these examples “as-is” on the Due. The Due uses different registers.

The following is Help from dlloyd (Many thanks)
*****
This is the slave setup that I use...
//SPI setup
 SPI.begin(10);
 SPI.setDataMode(10, SPI_MODE0);
 REG_SPI0_CR |= 0x1;          // SPI enable (write only)
 REG_SPI0_WPMR = 0x53504900;  // Write Protection disable
 REG_SPI0_MR = 0x2;           // DLYBCS=0, PCS=0, PS=1, MSTR=0
 REG_SPI0_CSR = 0xA;          // DLYBCT=0, DLYBS=0, SCBR=0, 8 bit transfer, Clock Phase = 1 for SPI mode 0
The most important reference material I used is page 695 of the SAM3X datasheet http://www.atmel.com/Images/doc11057.pdf

In slave mode, the SCLK frequency is set by the external SPI master, and can be anything up to 42 MHz. It’s good to start at a much lower frequency and use a scope on the SPI signals to troubleshoot. On the Due, your just reading and writing to a shift register that can be set anywhere from 8 to 16 bits in length. Just before each read or write, check the status register REG_SPI0_SR. and it will give you all the status flags shown on page 695.

I read from REG_SPI0_RDR when bit RDRF is 1, I write to REG_SPI0_TDR when bit TXEMPTY is 1.
*****
Once again, many thanks to dlloyd for assistance provided.

OK by now I should have enough clues to get this all working on the Due but still no luck getting an SPI slave mode example working on the Due.
If anyone can help produce a working “Arduino Due SPI Slave mode” example it will be very much appreciated.
« Last Edit: October 11, 2013, 08:52:25 am by Stephan_Duino » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everybody

As the topic is a few months old I am hoping somebody managed it to put the Arduino due in Slave mode. If so I would appreciate an example code.
In detail I would like to communicate with a RaspberryPi over SPI.

THX
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


It's a pity that the Arduino DUE will not suppport SPI  as slave mode.

From what I read it can be done, but there is no example code. I have read and understood the clues above, but I dont know how to do it.

I think the key part is:

"I read from REG_SPI0_RDR when bit RDRF is 1, I write to REG_SPI0_TDR when bit TXEMPTY is 1."

If anyone comes up with working source code, it will be a great contribution to arduino community if it is published.

Best Regards,
C.B.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bump again.  Any one of the previous posters manage to get a DUE working in slave mode?
Logged

0
Offline Offline
Shannon Member
****
Karma: 221
Posts: 12710
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From the datasheet:

Quote
The Serial Peripheral Interface (SPI) circuit is a synchronous serial data link that provides communication
with external devices in Master or Slave Mode.

Rather than complain about this, have a go at implementing it?  Most stuff developed
for Arduino is done gratis by enthusiasts, and demand driven...

Start at datasheet section 33.7.1
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Jr. Member
**
Karma: 9
Posts: 80
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a simple DUE spislave.ino sketch, proof of concept,
   https://github.com/manitou48/DUEZoo/blob/master/spislave.ino
I've tested it with a maple (3.3v) master at various data rates, sending various number of bytes between SS LOW/HIGH.
sketch utilizes SPI library (SPI.begin(SS)) to get pins and such set up, then resets the SPI and enables it in slave mode.
Logged

Pages: [1]   Go Up
Jump to: