Reading files from SD card error at 0x1000

Hi, I am trying to use the SdFat library to read a binary file from the SD card. Everything works fine up until byte 0x1000 where it fails to read.

I tried searching for this problem, but found no one else reporting it.

My code is extremely simple and looks like this:

#include <SdFat.h>

#define SD_CS_PIN 10
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)

#define error(s) sd.errorHalt(&Serial, F(s))

SdFs sd;
FsFile file;

uint8_t frame[3];

void setup() {
    int n;
    Serial.begin(115200);

    while (!Serial)
        SysCall::yield();

    while (!Serial.available())
        SysCall::yield();

    if (!sd.begin(SD_CONFIG))
        sd.initErrorHalt(&Serial);

    if (sd.exists("clp04cln.wav"))
        Serial.println("clp04cln.wav exists!");

    if (!file.open("clp04cln.wav"))
        error("clp04cln.wav is not readable.");

    for ( ; file.position() < 6000 ; ) {
        n = file.read(frame, sizeof(frame));
        if (n <= 0) {
            Serial.print(F("Current position is: "));
            Serial.print((uint32_t)file.position(), DEC);
            Serial.print(F(" / 0x"));
            Serial.println((uint32_t)file.position(), HEX);
            error("file.read failed");
        }
    }
}

void loop() {
}

It reads at most up to the 4095th byte in the file and then prints an error to Serial:

clp04cln.wav exists!
Current position is: 4096 / 0x1000
error: file.read failed
SdError: 0X1D,0XFF

I am trying to find the reason for this, but I really have no idea why this should be… the file size is about 34kb.

Not sure where you output the error code in your sketch as I can’t see code to generate the output below.

in any case, did you look up these error codes? There is a SDErrorCodes example in SDFat that lists all the error. Mine says that 0x1D is as listed below, but your version’s could be different.

0X1D,SD_CARD_ERROR_READ_TIMEOUT - Read data timeout

How big is the SD card?

It is a SanDisk 8GB SDHC card.

I did figure out that the error codes are written somewhere, and that this error code means “read error”. So I tried several things, first changing the read timeout from 300 to 3000, that didn’t help.

Then I tried re-opening and/or re-reading the file in the loop, it doesn’t work either and even after a file.close() it can’t do file.open() on that same file anymore.

I tried adding a delay(1000) and re-trying to either read from the same file, or closing the file and opening a new one. Again, with no success.

Today I bought a new SD card (Samsung 32GB) and it works fine. So my guess is that the SanDisk 8GB card is faulty somehow and fails reading more than 4k bytes. It does work fine with the computer, so still no idea what might be causing this.

Have you read this sticky for this forum? Not sure if this could be your problem.
Don’t Format SD cards with OS utilities! - Using Arduino / Storage - Arduino Forum

Yes, I did format the SD card using SDFormatter example from the SdFat library. So as far as I know it should be good.

Samsung cards are great but sometimes you get a counterfeit card. These sometimes work on PCs and phones which use the 4-bit SDIO bus but fail with the SPI interface.

Thanks for the links! The problem was actually with the SanDisk 8G SD, the new Samsung works.

First link is true for SanDisk, counterfeit as often as Samsung.

According to reports from Sandisk, one third of all memory cards on the market are counterfeit.

This topic was automatically closed after 120 days. New replies are no longer allowed.