Pages: [1]   Go Down
Author Topic: Sd and SPI library?  (Read 1249 times)
0 Members and 1 Guest are viewing this topic.
Houston
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Electronics is just part of life.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1484
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Houston
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Electronics is just part of life.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1484
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 11, 2011, 09:20:12 am by fat16lib » Logged

Houston
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Electronics is just part of life.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1484
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

This is not tested!!
Code:
#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;
#else
const static uint8_t SS   = 10;
const static uint8_t MOSI = 11;
const static uint8_t MISO = 12;
const static uint8_t SCK  = 13;
#endif
Logged

Houston
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Electronics is just part of life.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1484
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Houston
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Electronics is just part of life.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! =/
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1484
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Houston
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Electronics is just part of life.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://www.avr-developers.com/corefiles/index.html

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

Pages: [1]   Go Up
Jump to: