I'm trying to use the sdfat library with the arduino spi library. I'm using the arduino uno and trying to use both as slaves. They both work seperatley, but when i try to combine them it doesn't work. Neither one of the spi slaves interacts and the whole thing just goes wrong, it doesn't even write anything to the slave.
heres the code:
#include <SPI.h>
#include <SdFat.h>
int CS_pin = 8;
int DCS_pin = 9;
int DREQ_pin = 7;
int XRST_pin = 6;
int MP3_CS_pin = 10;
SdFat sd;
SdFile myFile;
byte dat[10000];
int index = 0;
void setup()
{
Serial.begin(9600);
pinMode(MP3_CS_pin, OUTPUT);
pinMode(CS_pin, OUTPUT);
pinMode(DREQ_pin, INPUT);
pinMode(DCS_pin, OUTPUT);
pinMode(XRST_pin, OUTPUT);
}
void playSong(){
for(int i = 0; i < index; i++){
SPI.transfer(dat[i]);
}
}
void loop()
{
digitalWrite(MP3_CS_pin, HIGH);
digitalWrite(CS_pin, HIGH);
digitalWrite(DCS_pin, HIGH);
delay(100);
if (!sd.init(SPI_HALF_SPEED, MP3_CS_pin)) sd.initErrorHalt();
// re-open the file for reading:
if (!myFile.open("song1.mp3", O_READ)) {
sd.errorHalt("error opening song1.mp3 for read");
}
Serial.println("song1.mp3:");
int data;
while ((data = myFile.read()) >= 0){
Serial.write(data);
dat[index++] = data;
}
myFile.close();
digitalWrite(MP3_CS_pin, HIGH);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);
//max SDI clock freq = CLKI/7 and (datasheet) CLKI = 36.864, hence max clock = 5MHz
//SPI clock arduino = 16MHz. 16/ 4 = 4MHz -- ok!
SPI.setClockDivider(SPI_CLOCK_DIV4);
digitalWrite(XRST_pin, LOW);
delay(100);
digitalWrite(CS_pin, HIGH);
digitalWrite(DCS_pin, HIGH);
digitalWrite(XRST_pin, HIGH);
delay(5);
// CHIP Write
// sets sci_mode register, SM_SDINEW, SM_TESTS, SM_STREAM. pg 25, 26
byte aux;
digitalWrite(CS_pin, LOW);
delay(1);
SPI.transfer(0x02); //write command
SPI.transfer(0x00); //SDI_MODE register
//extract and send higher byte of data
//0x0400
aux = 0x08A0 >> 8;
SPI.transfer(aux);
//extract and send lower byte of data
aux = 0x08A0 & 0b11111111;
SPI.transfer(aux);
//wait for the chip to finish executing command
while (!DREQ_pin){};
digitalWrite(CS_pin, HIGH);
delay(1);
// Start SINE test
digitalWrite(CS_pin, HIGH);
digitalWrite(DCS_pin, LOW);
delay(1);
playSong();
delay(200);
digitalWrite(DCS_pin, HIGH);
}
i've been at this for like 2 weeks now and im super frustrated >.<
please help.