transmit data from EEPROM to SD card

Hello,

I’m a senior and working on my senior project now. I’m new to Arduino and not very familiar with SPI. Our design need to collect some data store at EEPROM and then transmit to SD card (cannot store at SD card for data collecting speed is much faster than writing to SD card). Both transmission is using SPI. Below is the code, but only the first loop could work, from the second loop, it shows “initialization failed!” in serial monitor. The main part are from

http://arduino.cc/en/Tutorial/SPIEEPROM

I really appreciate any response. Thank you!

#include <SD.h>
File myFile;

#define DATAOUT 11//MOSI
#define DATAIN  12//MISO 
#define SPICLOCK  13//sck
#define SLAVESELECT 9//ss for EEPROM
#define SDSLAVE 10//ss for SD
//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;
  }
}

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);

  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  pinMode(SDSLAVE,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);
  clr=SPSR;
  clr=SPDR;
  delay(10);
  //fill buffer with data
  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.print('h');
  Serial.print('i');
  Serial.print('\n');//debug
  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.print(eeprom_output_data,DEC);
  Serial.print('\n');
  address++;
  if (address == 128)
    address = 0;
  delay(500); //pause for readability

      digitalWrite(SDSLAVE,LOW);
      delay(100);
   
   if (!SD.begin(SDSLAVE)) { //Debug!!
    Serial.println("initialization failed!");
    return;
                            }
  Serial.println("initialization done.");
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println(eeprom_output_data,DEC);
    myFile.println('\n');
	// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
    digitalWrite(SDSLAVE,HIGH);
   delay(100);
//digitalWrite(SDSLAVE,HIGH); 

}

What data rate (kbyte per second) do you want to store ?
The SD library of the Arduino is okay, but slow.
The sdfat library is improving and getting faster: http://github.com/greiman

When you use the SD library, and call SD.begin(), the registers are set for the SD SPI. I doubt is that will work together with all the reading and writing to the SPI control registers.

Before calling SD.begin(), you do : digitalWrite(SDSLAVE,LOW);
But the SD library takes care of enabling the chip select, you also don’t have to set it high again.
Have a look at this example : http://arduino.cc/en/Tutorial/Datalogger
That example can be used with other SPI devices.

At the start of setup(), you set SLAVESELECT HIGH to disable that chip-select. Please make also SDSLAVE HIGH so every SPI device is disabled.

The function SD.begin() is normally called once in setup(). Can you try that ? For example at the end of setup() after all the SPI direct register handling.
You can open and close the file as many times as you like of course.

Which SPI EEPROM are you using ?
Perhaps there is a library for it, that will work together with other SPI devices.