Go Down

Topic: An eaiser to use SD library (Read 2240 times) previous topic - next topic

fat16lib

Apr 11, 2011, 07:40 pm Last Edit: Apr 11, 2011, 07:53 pm by fat16lib Reason: 1
The native Arduino SD library was add to 022 as a wrapper for SdFat to make access to SD cards simpler.

I have updated SdFat to make it easier to use, added almost all features of the native SD library plus many new features.  

Error handling has been improved and SPI access to the SD card has been rewritten.  Try running the examples below with no SD card to see a sample message.

Typical sketches in the new SdFat have fewer line of code than the native SD library.  Sketches generally take a few bytes less flash than the native library even with the new features and error handling.

The new library can be downloaded here http://code.google.com/p/beta-lib/downloads/list.

I have ported two example from the native SD library as an illustration.  I would appreciate any comments on the new SdFat library so I can improve it.

Here is the DumPFile example:
Code: [Select]

/*
SD card file dump - modified for the ne SdFat library by Bill Greiman

This example shows how to read a file from the SD card using the
new SdFat library and send it over the serial port.

The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created  22 December 2010
modified Apr 11 2011 by Bill Greiman

This example code is in the public domain.
*/

#include <SdFat.h>
SdFat sd;  // Almost the same object as SD in the native SD library
SdFile dataFile;  // file to read from

// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 4;

void setup() {
 Serial.begin(9600);
 
 //  initialize SdFat or print a detailed error message and halt
 //  Use half speed like native library.  
 //  change to SPI_FULL_SPEED for more performance.
 if (!sd.init(SPI_HALF_SPEED, chipSelect))sd.initErrorHalt();
 
 // open data file
 if (!dataFile.open("datalog.txt", O_READ)) {
   sd.errorHalt("error opening datalog.txt");
 }
 int data;
 //  copy file to Serial
 while ((data = dataFile.read()) >= 0) Serial.write(data);
}

void loop() {}

Here is the ReadWrite.pde example:
Code: [Select]

/*
 SD card read/write - modified by Bill Greiman for SdFat

This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
modified by Bill Greiman 11 Apr 2011
This example code is in the public domain.

*/
#include <SdFat.h>
SdFat sd;
SdFile myFile;

// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 4;

void setup() {
 Serial.begin(9600);
 
 // Initialize SdFat or print a detailed error message and halt
 // Use half speed like native library.  
 // change to SPI_FULL_SPEED for more performance.
 if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();
 
 // open the file for write at end like the Native SD library
 if (!myFile.open("test.txt", O_WRITE | O_CREAT | O_APPEND)) {
   sd.errorHalt("error opening test.txt for write");
 }
 // if the file opened okay, write to it:
 Serial.print("Writing to test.txt...");
 myFile.println("testing 1, 2, 3.");
 
 // close the file:
 myFile.close();
 Serial.println("done.");

 // re-open the file for reading:
 if (!myFile.open("test.txt", O_READ)) {
   sd.errorHalt("error opening test.txt for read");
 }
 Serial.println("test.txt:");
   
 // read from the file until there's nothing else in it:
 int data;
 while ((data = myFile.read()) >= 0) Serial.write(data);
 // close the file:
 myFile.close();
}

void loop() {
// nothing happens after setup
}

Leonidas

I'm trying to use your library with the arduino spi library. sorry to bother you again but can you please help me out with the code?

Code: [Select]

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


sorry to bother you so much but i've been at this for like 2 weeks now and im super frustrated >.<

fat16lib

Leonidas,

This post is about a new library that has not been tested on the Sanguino.

The Sanguino support is so poor that I have given up testing SdFat on the 644.  I did not write the Arduino SPI library.

Sorry I can't be of more help.  I stopped using my Sanguino because of problems like this.

Leonidas

no this is on the arduino uno. i think i might do the same thing u did.

fat16lib

Making your mp3 player work is not the topic of this post.  It would be better for you to start a new topic.

Go Up