SPI across two Arduino DUE's

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.

/**********************************************
  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);
}
/**********************************************
  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.

Hi, the SPI library only supports master mode:

[...]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.

Bump. Can anyone help with this topic?

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 ...

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!

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:-

Error received: "error: 'SPCR' was not declared in this scope"

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

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

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.

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.

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

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.

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

From the datasheet:

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

Here is a simple DUE spislave.ino sketch, proof of concept,

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.

mantoui:
Here is a simple DUE spislave.ino sketch, proof of concept,
DUEZoo/spislave.ino at master · manitou48/DUEZoo · GitHub
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.

Thanks for the spislave.ino! I tried it but it failed within few byte communication. Then, the reply from due were wrong byte. I only changed this line: "in = transfer(SS, 0x88);",then observe at master device "raspi" and "logic analyzer ". After received few correct 0x88 from due, then raspi received random wrong replies. I also tried to used "in = SPI.transfer(SS, 0x83) ;"
, and got the same situation. Can you help me with this or tell me where to find the solution. THX!

Does your application involve devices mounted on the same board or are this communication between two systems? I ask because I think that SPI communications are typically between a master and slave in the same system, if not on the same PCB then on a daughter card or similar.

For communication between systems using a UART or USART might be more useful. And in that case there is no concept of master and slave so symmetric libraries can be used on both sides.

If you must use the SPI bus I suggest digging into the initialization code so you can see how the master is set up and that should give you some hints on how to configure the slave. You will also likely have to study the data sheet to understand how the SPI port is programmed and write your own slave driver. It maybe that some of the code from the library can be copied and modified as necessary to make a slave driver. (no pun intended!)

It's been just over 5 years since the last post here. I don't suppose anyone found a way to make the DUE a slave?