Arduino Due - SPCR, SPSR, and SPDR not declared in SPI library

Hello,

Im using Arduino DUE to read and write blocks, byte and word to a MX25L6406E EEPROM. I followed a sample code online and it didn't recognize SPCR, SPSR and SPDR registers. Here is part of my code:

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

void setup()
{
  Serial.begin(9600);
  SPI.begin(10);
  pinMode(PowerControl, OUTPUT);
    digitalWrite(PowerControl, LOW);                           //Set it LOW to activate 3V based on Shield Schematic
  pinMode(RelayCtrl, OUTPUT);                                 //Enable Relay Control
    digitalWrite(RelayCtrl, HIGH);                               //Set HIGH to activate 
  
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device
  //SPCR = 01010000
  //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
  //sample on leading edge of clk,system clock/4 rate (fastest)
  SPCR = (1<<SPE)|(1<<MSTR);            //  SPCR Control Register
  clr = SPSR;                           //  SPSR Status Register
  clr = SPDR;                           //  SPDR Data Register

The error is below:

Writing_Reading5:47:3: error: 'SPDR' was not declared in this scope
SPDR = data; // Start the transmission
^
Writing_Reading5:48:12: error: 'SPSR' was not declared in this scope
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
^
Writing_Reading5:48:23: error: 'SPIF' was not declared in this scope
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission

I did declare the SPI library in the beginning "#include <SPI.h>" What should I do?

Those are the registers used in the 8 bit avr architecture like the AT328.

The Arduino Due is based on the Atmel SAM3X8E ARM Cortex-M3 CPU which is a 32-bit ARM core microcontroller. There is different nomenclature and values for the spi registers on the device.

Apples and Oranges.

You should look for a library that uses the chip-independent SPI library, instead of manipulating the SPI peripheral directly. Perhaps spieeprom/spieeprom.cpp at master · kbondarev/spieeprom · GitHub ? Or perhaps GitHub - adafruit/Adafruit_SPIFlash: Arduino library for external (Q)SPI flash device if you want to implement a filesystem.

Please give me an example of writing bytes to MX25L6406E EEPROM using arduino DUE. I doesn't need to be a full code but just this part.... I cant make it work.

Did you try the previously referenced and linked kbondarev library?
There is is a read write example. Did you run it?
What were the results?

I changed CS and SPI settings so the code looks like this:

#include <SPI.h>
#include <spieeprom.h>
#include "Adafruit_LiquidCrystal.h"
#define PowerControl                          11           //Shield power 3VDC power control pin
#define RelayCtrl                             12           //Relay Control SPI pin MOSI & SCK. 
#define CS                                    10           //Chip Select Pin
#define ModuleDetectPin                       55           //Module insertion detect pin

#define DataOut                               75          //MOSI
#define DataIn                                74          //MISO
#define SPIclock                              76          //SCK                   

#define WRITE 2
#define READ  3
#define WREN  6    //Write Enable
#define WRDI  4    //Write Disable
#define RSDR  5    //Read Status Register
#define WRSR  1    //Write Status Register

byte buffer[256];
long address;


SPIEEPROM disk1(EEPROM_TYPE_16BIT, CS); // parameter is type
                    // type=0: 16-bits address
                    // type=1: 24-bits address
                    // type>1: defaults to type 0

void fill_buffer()
{
  for (int i=0;i<256;i++)
  {
    buffer[i]=i;
  }
}

void setup() {
  pinMode(PowerControl, OUTPUT);
  digitalWrite(PowerControl, LOW);                           //Set it LOW to activate 3V based on Shield Schematic
   
  pinMode(RelayCtrl, OUTPUT);                                 //Enable Relay Control
  digitalWrite(RelayCtrl, HIGH);                              //Set HIGH to activate 
  
  // set up to match device datasheet

  SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE0));    // SPI_MODE0:    CPOL = 0 | CPHA = 0 | Output Edge = Falling | Data Capture = Rising
  SPI.begin(10); // sets up pin modes etc.
  Serial.begin(9600);
  
  disk1.setup(); // setup disk1
  
  fill_buffer();
  address = 0;
  
  Serial.print("Starting to write on EEPROM:");
  Serial.print(millis());
  Serial.println();
  
  disk1.write(address, buffer, (sizeof(buffer)/sizeof(byte)));
  
  Serial.print("Finish to write:");
  Serial.print(millis());
  Serial.println();
  
  delay(1000);
}

void loop() {
  Serial.print("Address:|");
  Serial.print(address);
  Serial.print("| - Value:|");
  Serial.print(disk1.readByte(address), DEC);
  Serial.println("|");
  address++;
  if (address == 256)
    address = 0;
  delay(500);
}

then, the error are the following:

> C:\Users\105261\OneDrive - Fr\Documents\Test Kit\BIOS_JUR\Writing_Reading6/Writing_Reading6.ino:59: undefined reference to `SPIEEPROM::write(long, unsigned char*, int)'
collect2.exe: error: ld returned 1 exit status
exit status 1

Still not working..... I just need an example that explain how to use SPI,transfer when trying the commands for MX25L6406E EEPROM using arduino DUE

#include <spieeprom.h>

I do not believe that you have properly installed the library. Can you see the library example in the ide?

Your sketch compiles without error for me, but I do not have an SPI eeprom to test with.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.