Go Down

Topic: Sd and SPI library? (Read 3527 times) previous topic - next topic


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.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
  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");

//CPOL = 0, CPHA = 1
//see en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_Numbers
//and decoder chip datasheet
//max SDI clock freq = CLKI/7 and (datasheet) CLKI = 36.864, hence max clock = 5MHz
//SPI clock arduino = 16MHz. 16/ 4 = 4MHz -- ok!


void loop()
digitalWrite(XRST_pin, LOW);

digitalWrite(CS_pin, HIGH);
digitalWrite(DCS_pin, HIGH);
digitalWrite(XRST_pin, HIGH);

// CHIP Write
// sets sci_mode register, SM_SDINEW, SM_TESTS.  pg 25, 26
byte aux;
digitalWrite(CS_pin, LOW);
SPI.transfer(0x02);  //write command
SPI.transfer(0x00); //SDI_MODE register
//extract and send higher byte of data
aux = 0x0820 >> 8;
//extract and send lower byte of data
aux = 0x0820 & 0b11111111;
//wait for the chip to finish executing command
while (!DREQ_pin){};
digitalWrite(CS_pin, HIGH);

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

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.


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.


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?


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.


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.


At about line 52 of arduino22\hardware\arduino\cores\arduino\pins_arduino.h add the 644 pin defs.

This is not tested!!
Code: [Select]
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
const static uint8_t SS   = 53;
const static uint8_t MOSI = 51;
const static uint8_t MISO = 50;
const static uint8_t SCK  = 52;
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
const static uint8_t SS   = 4;
const static uint8_t MOSI = 5;
const static uint8_t MISO = 6;
const static uint8_t SCK  = 7;
const static uint8_t SS   = 10;
const static uint8_t MOSI = 11;
const static uint8_t MISO = 12;
const static uint8_t SCK  = 13;


it doesn't work!... now that i notice... with the different arduino cores none of the pins work above thirteen on the atmega644p and miso mosi and sck still dont output a thing


You need all the mods for a Sanguino.  I don't know if there is an up to date version.  Too bad the 644 is not supported.


what do you mean all the mods for a sanguino? i have the sanguino stuff on my arduino ide aswell but the libraries don't work with it! =/


Looks like the Sanguino stuff is out of date.  Last I saw was for 018.


yeah it is, they haven't updated... so what i started doing was using the alternate cores:


the libraries work with that but apparently the pins are a little off.

Go Up