Using SPI for two devices

Hello,

I'm working on a project now and I want to transmit data from EEPROM to SD card. Both EEPROM and SD card use SPI. And I use SPIEEP library to write and read data to EEPROM. However, I have some problems switching SPI between two devices. The code is below:

#include <SPIEEP.h>
#include <SPI.h>
#include <SD.h>

//SdFat sd;
File myFile;

SPIEEP eep(16, 128, 65536); // Microchip 25LC512
#define EEPROM_CSPIN 6  // Chip Select attached to pin 10
#define SDcard_CSPIN 5
void setup() {
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  //digitalWrite(EEPROM_CSPIN,LOW);
  byte buf[500];
  int i;
  byte buf1[500];

  for (i=0; i < 500; i++) {
    buf[i] = 'A' + (i%26);
  }
   pinMode (SDcard_CSPIN,OUTPUT);
   digitalWrite(SDcard_CSPIN,HIGH);
   
    eep.begin_spi(EEPROM_CSPIN);// start eeprom. sets EEPROM_CSPIN as output and high
   if (!eep.writen(0x2F00, buf, 500)) {
    Serial.println("Error writing 500 bytes to address=0x2F00");
  }
    delay(500);
  if (!eep.readn(0x2F00, buf1, 500)) {
    Serial.println("Error reading 500 bytes from address=0x2F00");
   }
   delay(500);

    
    Serial.println("debug1");
    
    Serial.println((char *)buf1);
    
    //Terminate eep. To restart run eep.begin_spi(EEPROM_CSPIN) again
  
    pinMode (EEPROM_CSPIN,OUTPUT);
    digitalWrite(EEPROM_CSPIN,HIGH); // it looks to me as if the eeprom needs to be ended before having anything to do with the SD card
    

   delay(100);

  
    if (!SD.begin(SDcard_CSPIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("debug2");
  Serial.println("initialization done."); 
  
}

void loop() {
  
    while(1);
    
}

The only thing I got in serial monitor is "debug1". I wrote and read to arbitrary address of EEPROM and it does work. I'm confused about what's the problem. Also, I tried SPI.end(); and eep.end(); after EEPROM data reading, but still not work.

Thank you for any advice.

Regards,
Cody

If you are using an Uno, these two arrays may be using too much SRAM to allow the SD card to initialize. Try reducing the size to 200 at first and see if it works.

  byte buf[500];
  int i;
  byte buf1[500];

Can you tell which SPIEEP library you are using ?
You don't have set EEPROM_CSPIN high again, if the library is okay, it is done in the library at the end of every function that you call.

After reducing the buffer size, remove those extra things. It should work in a normal way.

SurferTim:
If you are using an Uno, these two arrays may be using too much SRAM to allow the SD card to initialize. Try reducing the size to 200 at first and see if it works.

  byte buf[500];

int i;
  byte buf1[500];

Peter_n:
Can you tell which SPIEEP library you are using ?
You don't have set EEPROM_CSPIN high again, if the library is okay, it is done in the library at the end of every function that you call.

After reducing the buffer size, remove those extra things. It should work in a normal way.

Hi SuferTim and Peter_n,

I decreased the size of buffer to 200 and delete the line which sets EEPROM_CSPIN HIGH, but the result is the same.

I was wondering whether the SPIEEP library does not shut off SPI after reading from EEPROM so SPI cannot be shared?

Thank you for your replies.

Try this test sketch. It disables the EEPROM SPI, then starts the SD. This presumes the EEPROM slave select is D6 and the SD card slave select is D5. Does this start the SD card ok?

#include <SPI.h>
#include <SD.h>

//SdFat sd;
File myFile;

#define EEPROM_CSPIN 6
#define SDcard_CSPIN 5

void setup() {
  Serial.begin(9600);

  // disable EEPROM SPI
  pinMode (EEPROM_CSPIN,OUTPUT);
  digitalWrite(EEPROM_CSPIN,HIGH);

  if (!SD.begin(SDcard_CSPIN)) {
    Serial.println("initialization failed!");
    return;
  }
 
}

void loop() {
}
void setup() {
  Serial.begin(9600);
 pinMode (SDcard_CSPIN,OUTPUT);
   digitalWrite(SDcard_CSPIN,HIGH);
  eep.begin_spi(EEPROM_CSPIN);
   
  SD.begin(SDcard_CSPIN);
  
}

Your example really helps.
I tried this for the setup and it works! Thank you very much!