Is SPI working for the Zero?
#include <SPI.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
These are the first couple of errors:
In file included from sketch_Zero_jun24b.ino:1:0:
\dc-01\userhome\dave\My Documents\Arduino\libraries\SPI/SPI.h: In static member function 'static byte SPIClass::transfer(byte)':
\dc-01\userhome\dave\My Documents\Arduino\libraries\SPI/SPI.h:56:3: error: 'SPDR' was not declared in this scope
SPDR = _data;
^
And there are many more errors.
You have a copy of the SPI library in your sketchbook folder:
\\dc-01\userhome\dave\My Documents\Arduino\libraries\SPI/SPI.h
I don't know if you really need it, but moving it somewhere else should solve your problem.
[edit:]
The IDE pick the SPI lib in your sketchbook folder instead of the one available in the Zero's core.
You are right. Thank you. I must have done that 2 years ago and kept copy and pasting the library folder every reinstall.
Hi
I'm experiencing same issue of "spdr was not declared in this scope" error during compilation of the basic SPI tutorial to inteface EEPROM to my SAMD21 sparkfun mini breakout board (same as Zero)
I have added in the sketch #include <SPI.h>
I have copied the SPI libary in my \My Documents\Arduino\libraries\ folder
but still getting the error !
any further idea? something to check ?
thanks
Davide
You are trying to configure the Atmega328p SPI register on the Arduino Zero, which can't work...
Arduino provide a SPI library that is doing all of the register's stuff. Try this modified code :
#include <SPI.h>
#define SLAVESELECT 4
//opcodes
#define WREN 6
#define WRDI 4
#define RDSR 5
#define WRSR 1
#define READ 3
#define WRITE 2
byte eeprom_output_data;
byte eeprom_input_data=0;
byte clr;
int address=0;
//data buffer
char buffer [128];
void fill_buffer()
{
for (int I=0;I<128;I++)
{
buffer[I]=I;
}
}
void setup()
{
Serial.begin(9600);
SPI.begin();
digitalWrite(SLAVESELECT, HIGH);
fill_buffer();
//fill eeprom w/ buffer
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(WREN); //write enable
digitalWrite(SLAVESELECT,HIGH);
delay(10);
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(WRITE); //write instruction
address=0;
SPI.transfer((char)(address>>8)); //send MSByte address first
SPI.transfer((char)(address)); //send LSByte address
//write 128 bytes
for (int I=0;I<128;I++)
{
SPI.transfer(buffer[I]); //write data byte
}
digitalWrite(SLAVESELECT,HIGH); //release chip
//wait for eeprom to finish writing
delay(3000);
Serial.println("hi");
delay(1000);
}
byte read_eeprom(int EEPROM_address)
{
//READ EEPROM
int data;
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ); //transmit read opcode
SPI.transfer((char)(EEPROM_address>>8)); //send MSByte address first
SPI.transfer((char)(EEPROM_address)); //send LSByte address
data = SPI.transfer(0xFF); //get data byte
digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
return data;
}
void loop()
{
eeprom_output_data = read_eeprom(address);
Serial.println(eeprom_output_data,DEC);
address++;
if (address == 128)
address = 0;
delay(500); //pause for readability
}
Thank You for the support
I have modified the Slave Select pin since in my SAMD21 sparkfun breakout board is connected to D10
#define SLAVESELECT 10
now I can compile and upload the sketch but when I start the serial monitor I got as output only "0" decimal value
the buffer written/read from EEPROM shoud contain value from 0-128 isn't it ?
never get the output "hi"
can you help to debug?
Davide
just to provide more details the connection schema is the following

the EEPROM is SPANSION S25FL208K0RMFI011 MEMORIA, FLASH, 8MB, 3V, SPI, 8SOIC
and from data sheet the command code seems ok

getting crazy to make it working
Davide
Is the issue due to the fact that the Arduino Zero's standard SPI library uses the Zero's 6 pin header, which unlike the Uno isn't connected to pins D11-D13?
Perhaps Sparkfun provide their own SPI library to work on these pins? Alternatively, Dirk67 provided a link to an excellent Adafruit article which details how to set-up an SPI port on those pins (and loads of other sercom configuration stuff), just follow the links: Using ATSAMD21 SERCOM for more SPI, I2C and Serial ports - Arduino Zero - Arduino Forum
AloyseTech:
You are trying to configure the Atmega328p SPI register on the Arduino Zero, which can't work...
Arduino provide a SPI library that is doing all of the register's stuff. Try this modified code :
#include <SPI.h>
#define SLAVESELECT 4
//opcodes
#define WREN 6
#define WRDI 4
#define RDSR 5
#define WRSR 1
#define READ 3
#define WRITE 2
byte eeprom_output_data;
byte eeprom_input_data=0;
byte clr;
int address=0;
//data buffer
char buffer [128];
void fill_buffer()
{
for (int I=0;I<128;I++)
{
buffer[I]=I;
}
}
void setup()
{
Serial.begin(9600);
SPI.begin();
digitalWrite(SLAVESELECT, HIGH);
fill_buffer();
//fill eeprom w/ buffer
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(WREN); //write enable
digitalWrite(SLAVESELECT,HIGH);
delay(10);
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(WRITE); //write instruction
address=0;
SPI.transfer((char)(address>>8)); //send MSByte address first
SPI.transfer((char)(address)); //send LSByte address
//write 128 bytes
for (int I=0;I<128;I++)
{
SPI.transfer(buffer[I]); //write data byte
}
digitalWrite(SLAVESELECT,HIGH); //release chip
//wait for eeprom to finish writing
delay(3000);
Serial.println("hi");
delay(1000);
}
byte read_eeprom(int EEPROM_address)
{
//READ EEPROM
int data;
digitalWrite(SLAVESELECT,LOW);
SPI.transfer(READ); //transmit read opcode
SPI.transfer((char)(EEPROM_address>>8)); //send MSByte address first
SPI.transfer((char)(EEPROM_address)); //send LSByte address
data = SPI.transfer(0xFF); //get data byte
digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
return data;
}
void loop()
{
eeprom_output_data = read_eeprom(address);
Serial.println(eeprom_output_data,DEC);
address++;
if (address == 128)
address = 0;
delay(500); //pause for readability
}
It is my first post here,so excuse me for any mistakes. I was wondering in this piece of code that so wonderfully works,if I can set the clock frequency.And if yes how,because I modifed this code and used it for the ESP32 from Adafruit to input data as a MISO.
Hi cantonioupao,
To change the frequency using the SPI library, just use beginTransmission() and endTransmission() functions.
Here's an example of a function that writes to a barometer using the SPI bus, setting the SCK frequency to 10MHz:
void writeBaroByte(uint8_t subAddress, uint8_t data)
{
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
REG_PORT_OUTCLR1 = PORT_PB03; //digitalWrite(BARO_CS_PIN, LOW);
SPI.transfer(subAddress);
SPI.transfer(data);
REG_PORT_OUTSET1 = PORT_PB03; //digitalWrite(BARO_CS_PIN, HIGH);
SPI.endTransaction();
}
The SPISettings are detailed here: SPI - Arduino Reference.