Pages: [1]   Go Down
Author Topic: Sdfatlib readData and readStart failing  (Read 805 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 11
Posts: 349
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To improve preformance in reading a file I have decided that it would be better to use the Sd2card libary which is part of sdfatlib and for some reason doing
Code:
myfile.readStart(bgnBlock)
myfile.readData(bufferOne))
both return zero However
Code:
file.contiguousRange(&bgnBlock, &endBlock)
returns One after I defragmented my sdcard but before I defragmented it the function returned zero.
Here is my code:
Code:
#include <SdFat.h>
#include <Sd2Card.h>
#include <SdBaseFile.h>
#include <SdFatUtil.h>
const int chipSelect = 10;
#define buffer_size 512
uint8_t bufferOne[buffer_size];
uint8_t bufferTwo[buffer_size];
SdFat sd;
Sd2Card myfile;
SdFile file;
unsigned long audio_size_full;
void setup()
{
 Serial.begin(115200);
  pinMode(TIMER_PIN, OUTPUT );
  SAMPLE_RATE=44100;
 // pinMode(13, OUTPUT);                       //turn on led pin to see voltage
 // digitalWrite(13, HIGH);
   PgmPrint("FreeRam: ");
  Serial.println(FreeRam());
  if (!sd.init(SPI_FULL_SPEED, chipSelect)) sd.initErrorHalt();
  file.open("axelf.snd",O_READ);
    Serial.println(file.contiguousRange(&bgnBlock, &endBlock));
 audio_size_full=file.fileSize();
  //Serial.print("staring play back\nFilesize=");
  PgmPrint("starting play back\nFilersize=");
   Serial.println(audio_size_full);
 // file.rewind();
  //read the first buffer
  //Serial.println(myfile.read(bufferOne,buffer_size));
  //get block range
  //file.close();
  Serial.println(myfile.readStart(bgnBlock));
  Serial.println(myfile.readData(bufferOne));
    startPlayback();
 // Serial.println("started");
 PgmPrintln("started");
I do not understand why it is not working.
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1654
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is myfile is not the object that can access the SD card.

You need to use sd.card() like this:
Code:
#include <SdFat.h>
SdFat sd;
SdFile file;
const int chipSelect = 10;
uint32_t bgnBlock;
uint32_t endBlock;
uint8_t bufferOne[512];
void setup() {
  Serial.begin(9600);
  if (!sd.init(SPI_FULL_SPEED, chipSelect)) sd.initErrorHalt();
  file.open("axelf.snd",O_READ);
  Serial.println(file.contiguousRange(&bgnBlock, &endBlock));
  Sd2Card *card = sd.card();
  Serial.println(card->readStart(bgnBlock));
  Serial.println(card->readData(bufferOne));
  for (uint8_t i = 0; i < 20; i++) {
  //  Serial.println(bufferOne[i], HEX);
  }
}
void loop() {}
Logged

Offline Offline
Sr. Member
****
Karma: 11
Posts: 349
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you works perfectly! Also thank you for creating the awesome library.
Logged

Pages: [1]   Go Up
Jump to: