Problem with SdFat and bitmap files

I modified a function from the UTFT lib to use SdFat, and a sample sketch is attached. It is supposed to load a map tile. It works, but at some random point will quit loading and return 65535. Perhaps I'm not using SdFat correctly. Is there a different method rather than reading one sector at a time that would work better? Thanks for any help.

testloadmap.ino (2.57 KB)

I changed the sketch to use SdFile, not SdBaseFile, and to print the result of myFile.read. It is always 0, until the sketch crashes, then is 65535. I believe the problem is how I'm reading the file data, not the display code as that is lifted from UTFT and works with that. The function needs to reliably read binary data, 512 bytes at a time, and I don't understand why this works for some random length of time, then stops. I hope that someone knowledgable in SdFat will look at this code. Thanks very much if it is you.

testloadmap.ino (2.76 KB)

Start by changing the if statement:

  res=myFile.open(mapname, O_READ);
  if (res = 1) {

to this:

  res=myFile.open(mapname, O_READ);
  if (res == 1) {

Also change the type of result from word to int

  word temp, result;

I don't see temp use but result should be:

  int result;

Type word is unsigned so if the read() returns -1, this statement will print 65535.

Serial.println(result);

Thank you for helping. I fixed the (stupid) errors you pointed out, and simplified the sketch to remove all references to the display (attached). It should now open the file, read repeatedly into the buffer, then close the file, moving on to the next.

When I run the attached sketch, it will open and print the names of the files up to some random file, then will print file error and return 0. So there is apparently some problem with

res=myFile.open(mapname, O_READ);

I tried setting the SPI speed to half and quarter, and used the SdFat formatting tools to reformat the card, with no joy. Am I using the open command correctly? Could it be the card? (2GB, 50X Kingston brand) I commented out the section to stop the sketch if a file did not open, and learned that once open fails, it will not work again.

Thanks again for help.

testReadSd.ino (1.47 KB)

Add the following to check the nature of the open error.

  else {
    //file did not open

    // Add these three lines
    Sd.errorPrint(mapname);
    Serial.print("exists: ");
    Serial.println(Sd.exists(mapname)); 

    return res;
  }

It should print something like this if you are opening "TEST.BIN".

error: TEST.BIN
SD errorCode: 0X19,0X0
exists: 0

The SD errorCode line will only appear if there is a hardware problem with the SD.

Post the result.

This is the result: (Good until 1424)

/raw/map1424.raw
error: /raw/map1424.raw
SD errorCode: 0XF,0XFD
exists: 0
/raw/map1425.raw
error: /raw/map1425.raw
SD errorCode: 0X4,0XFF
exists: 0
/raw/map1426.raw
error: /raw/map1426.raw
SD errorCode: 0X4,0XFF
exists: 0

It continues through the files with the 0X4,0XFF code.

These are basic hardware read errors. The card may lockup after the first error since the transfer does not complete correctly.

The SPI bus/SD module could be marginal. What is your SPI/SD module setup.

Another possibility is a faulty SD card. This is rare. Could you try another SD card?

It is unlikely to be the card format unless the card has a corrupt file system. You might check the file system for errors on a PC.

I swapped cards for one by Transend, and I'm happy to say it's working; maps have been loading for more than an hour without a crash. Given my level of coding ability, it probably would not have occurred to me that it might be a hardware problem. Thank you again for your assistance.