Arduino DUE SPI

I am new to DUE . I am interfacing a sensor and DUE through SPI. But I get 0 0 0 . . . as response from the sensor. So I just wanted to confirm if my program is proper. My aim is to write a data and read it back from a particular memory . I read through many forums ,but I am still not sure of my problem.

#include <SPI.h>
void setup() {

  Serial.begin(9600);

  SPI.begin(4);
// opcodes here are samples oly
 write_opcode = 0x99;
 address= 0x98;
 value=0xAA;
 read_opcode=0x97;
}

void loop() {
  // put your main code here, to run repeatedly:
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE1));
SPI.transfer(4, write_opcode, SPI_CONTINUE);
SPI.transfer(4, address, SPI_CONTINUE);
SPI.transfer(4, value, SPI_CONTINUE);
SPI.transfer(4, read_opcode, SPI_CONTINUE);
SPI.transfer(4, address, SPI_CONTINUE);
byte rd_value = SPI.transfer(4, 0x00);
Serial.print(rd_value);
SPI.endTransaction();
}

This is written in Arduino DUE. Is my program correct or am I missing something ?

The hardware wiring , I followed from here
http://forum.arduino.cc/index.php?topic=132130.0

TurboSpi works fine. See this thread, from #reply35:

https://forum.arduino.cc/index.php?topic=437243.30

The turboSPI seems to be tough for me to use it. As it uses a buffer to send values, but I just send 2 bytes to sensor and read it back. My requirement is simple . Write a data in a particular memory and read it back. I would like to know what is the possiblity of mistake! All the values I use are in HEX(opcode,register address,values,etc)

void loop() {
  // put your main code here, to run repeatedly:
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE1)); //begin 
SPI.transfer(4, write_opcode, SPI_CONTINUE); //opcode for writing
SPI.transfer(4, address, SPI_CONTINUE); //adress of the register
SPI.transfer(4, value, SPI_CONTINUE); // val to be written
SPI.transfer(4, read_opcode, SPI_CONTINUE); // opcode for reading back
SPI.transfer(4, address, SPI_CONTINUE); // adress of the reg  (same as above)
byte rd_value = SPI.transfer(4, 0x00);  // storing the value by sending dummy data
Serial.print(rd_value); 
SPI.endTransaction();
}

I also tried this way of code

#define WRITE 0x99 // writeopcode
#define ADD 0x98 //adress
#define DATA 0xED // data
#define READ 0x97 // read opcode

#include <SPI.h>
#define SS 10
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
SPI.begin(10);
}



void loop() {
  // put your main code here, to run repeatedly:
 SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE1));  // gain control of SPI bus
  digitalWrite(10, LOW);         // assert chip select
  SPI.transfer(10,WRITE);            // send  command
  SPI.transfer(10,ADD);
   SPI.transfer(10,DATA);
   delay(100);
   SPI.transfer(10,READ);
   SPI.transfer(10,ADD);
  byte b1 = SPI.transfer(10,ADD);     // read  data

  digitalWrite(10, HIGH);        // deassert chip select
  Serial.println(b1,HEX);
  delay(500);
  SPI.endTransaction();          // release the SPI bus
}

When i tried with 2 DUE's 'I am able to send data and receive. So I am not able to understand where problem lies.

See a successful example of SPI.h usage in this document:

http://schianorobotics.altervista.org/Arduino_DUE___DAC_MCP4922.pdf

Hello,

the link does not show anything :frowning:
I have attached a picture of what I saw on my screen. The requested file is downloadable here. Whenever I pressed 'here' , the tab just duplicates.

Kindly help.

Hello all,

When I looked into the library SPI.h , the spi.transfer function is

Write to the SPI bus (MOSI pin) and also receive (MISO pin)
  inline static uint8_t transfer(uint8_t data) {
    SPDR = data;
    /*
     * The following NOP introduces a small delay that can prevent the wait
     * loop form iterating when running at the maximum speed. This gives
     * about 10% more speed, even if it seems counter-intuitive. At lower
     * speeds it is unnoticed.
     */
    asm volatile("nop");
    while (!(SPSR & _BV(SPIF))) ; // wait
    return SPDR;
  }

as the SPI.transfer receives values in uint8_t, but i send hex values. Could this be a problem ?