Go Down

Topic: Sd and SPI library? (Read 1 time) previous topic - next topic

Leonidas

Hie everyone, i am trying to make an mp3 player through an sd card and a vs1003 breakout board. i got the sine test to work on the board and i also am able to read the mp3 file from the sd card. using the sd library and the spi library. But on separate programs. as soon as i try to combine them to play the song neither one works. I'm not sure that it's how the sd library is set up or how it works.

anyways heres the code:
Code: [Select]
#include <SPI.h>
#include <SD.h>

File myFile;

int CS_pin = 9;
int DCS_pin = 8;
int DREQ_pin = 7;
int XRST_pin = 6;
byte received;

void setup()
{
pinMode(CS_pin, OUTPUT);
pinMode(DREQ_pin, INPUT);
pinMode(DCS_pin, OUTPUT);
pinMode(XRST_pin, OUTPUT);
pinMode(10, OUTPUT);

Serial.begin(9600);
  Serial.print("Initializing SD card...");
 
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  myFile = SD.open("song1.mp3");
  if (myFile) {
    Serial.println("song1.mp3 succesfully opened!");
  } else {
  // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

SPI.begin();
SPI.setBitOrder(MSBFIRST);
//CPOL = 0, CPHA = 1
//see en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_Numbers
//and decoder chip datasheet
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);

}

void loop()
{
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.  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 = 0x0820 >> 8;
SPI.transfer(aux);
//extract and send lower byte of data
aux = 0x0820 & 0b11111111;
SPI.transfer(aux);
//wait for the chip to finish executing command
while (!DREQ_pin){};
digitalWrite(CS_pin, HIGH);
delay(1);

digitalWrite(DCS_pin, LOW);
     
// read from the file until there's nothing else in it:
while (myFile.available()) {
    Serial.write(myFile.read());
}
// close the file:
myFile.close();

digitalWrite(DCS_pin, HIGH);
}


i also have the sdfat library, idk if that would be a little easier to use or if i have to learn the adi protocol for the sd card. please help.

fat16lib

Part of the problem is the version of SdFat that SD is based on only set SPI data mode when begin() is called and SdFat uses SPI mode zero.

I am about to release a new version of SdFat that initializes the SPI bus before every access to the SPI bus http://code.google.com/p/beta-lib/downloads/list.

You would to also have to setup the SPI bus before each mp3 access.

You can read data faster using SdFat directly.  I can read and play wave files at over 90 KB/sec with my WaveRP library.  This library records and plays wave audio files.

Leonidas

that's awsome... i've used your sdfat library but i haven'y had time to figure all of it out. and so could i use your sdfat library with the spi library on the arduino ide with no problem?

fat16lib

#3
Apr 11, 2011, 04:15 pm Last Edit: Apr 11, 2011, 04:20 pm by fat16lib Reason: 1
To repeat part of the last post, you must use the new beta version of SdFat if you change the SPI data mode with the SPI library.  Each call to SdFat will reinitialize the SPI bus with its mode and speed.  You must reset the SPI speed and mode each time your program accesses the SPI bus.

Once again, The Arduino SD library is just a thin wrapper for the SdFat library so SPI behavior of the two libraries is the same.

Leonidas

okay, i get it. that seems simple enough... also i would like to ask if you know how i could change the output pins of the spi library for the arduino, because i'm using an atmega644p from the arduino alternate core software. the pins are:

ss            pin 4
mosi        pin 5
miso        pin 6
sck          pin 7

could you please help me try to figure this out? i know the sd libray works, but not the spi library.

Go Up