Go Down

Topic: Sdfatlib readData and readStart failing (Read 872 times) previous topic - next topic

Mr_arduino

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: [Select]

myfile.readStart(bgnBlock)
myfile.readData(bufferOne))

both return zero However
Code: [Select]

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: [Select]

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

fat16lib

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

You need to use sd.card() like this:
Code: [Select]

#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() {}

Mr_arduino

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy