SD card read returns 0xFF

Hello Arduino Forum. This is my first Arduino project, but i have been using WINAVR for years. My goal is to create a data logging device with a web interface. I am using an Arduino Ethernet. The issue I am encountering occurs with no hardware attached.

The basic problem is that sometimes when the arduino reads from the SD card it obtains erroneous data. Here is the function where the undesired behaviour is occuring:

In Webserve.cpp:

int sendSdFile(EthernetClient _client, char* fname, byte ftype){
  // send rest of HTTP header
  HTTPheader(_client, ftype);
  // send web page
  File wFile;
  if(ftype == OCTET){
    wFile = SD.open(fname);        // open web page file
    #ifdef DEBUG
      Serial.println(fname);
    #endif
  }
  else{
    char fullpath[20];
    strcpy_P(fullpath, (const prog_char*)F("WEB/"));
    strcat(fullpath, fname);
    #ifdef DEBUG
      Serial.println((int)wFile);
    #endif
    wFile = SD.open(fullpath);        // open web page file
    #ifdef DEBUG
      Serial.println(fullpath);
      Serial.println((int)wFile);
        Serial.println(F("FR:"));
        Serial.println(freeMemory(), DEC);
    #endif
  }
  if (wFile) {
    unsigned int wdt_stamp = millis();
    while(wFile.available()) {
      byte b = wFile.read();
      _client.write(b); // send web page to client
    #ifdef DEBUG
      Serial.print(b);
    #endif
      halfminutewdtreset(wdt_stamp);
    }
    wFile.close();
    #ifdef DEBUG
      Serial.println(F("FC"));
    #endif

  }else _client.println(F("File Send Fail!"));
}

I have DEBUG #defined and I see the following on the serial port:

0
WEB/blank.js
1
FR:
1657
255255255255255255255255255255255255255255255255255255255255255255255255255(and more you get the point)
255FC

Note that this failure mode doesnt normally occur the first time i run the function. I have to reload the web page 1 or 2 times to get this failure to occur. I am totally baffled at these failures, please help if you can. :fearful:

I attached a complete set of files.

Thanks in advance.

Compatibility_Sensors.zip (11.9 KB)

WEB.zip (12.7 KB)

Serial Response.txt (1.81 KB)

I believe the Arduino Ethernet is 328 based and has 2 K SRAM.

It appears that this:

        Serial.println(F("FR:"));
        Serial.println(freeMemory(), DEC);

Prints this:

FR:
1657

This must be wrong since the SD library alone has a 512 byte block buffer.

I suspect there is a memory use problem.

This must be wrong since the SD library alone has a 512 byte block buffer.

The freeMemory() function returns junk if called after all memory has been used. So, yes, the output is meaningless.

Thanks for the response!

I am looking through my code and trying to reduce my SRAM usage. In winavr there was a way to look at the c code and the assembly code together. IIRC it was a .lst file. Does Arduino have something like this?

Please let me know if I am understanding the stack correctly. I realized that I have a few functions that that I am passing the ethernet client object to. If I change these functions to take a pointer to the ethernet client object, the stack usage should be reduced.

Please let me know if you have any other tips for writting functions to reduce stack usage. Note that I am already fully aware of the F() macro and PROGMEM.

-Daniel