Problem with SdFile open on Teensy 3.1

Status
Not open for further replies.

craiglindley

Well-known member
Hello,

I'm writing an application which runs on the Teensy 3.1 which uses SdFat/SdFile to access GIF files on an attached SD card.

I use this code to initialize the SD card:

Code:
    // initialize the SD card at full speed
    if (! sd.begin(SD_CS, SPI_FULL_SPEED)) {
        sd.initErrorHalt();
    }

which always works perfectly.

I then use the following code to enumerate all GIF files on the attached SD card:

Code:
// Cache all animated GIF filenames
void cacheGIFFilenames() {

    numberOfFiles = 0;

    // Set the current working directory
    if (! sd.chdir("GIFS", true)) {
        sd.errorHalt("Could not change to gifs directory");
    }
    sd.vwd()->rewind();

    char fn[13];
    while (file.openNext(sd.vwd(), O_READ)) {
        file.getFilename(fn);
        // If filename not deleted
        if ((fn[0] != '_') && (numberOfFiles < MAX_FILENAMES)) {
            strcpy(filenames[numberOfFiles++], fn);
        }
        file.close();
    }
    // Set the current working directory
    if (! sd.chdir("/", true)) {
        sd.errorHalt("Could not change to root directory");
    }
}
and this works 100% of the time as well.

But when I try to open a file on the SD card with this code:

Code:
// Attempt to parse the gif file
int processGIFFile(char *pathname) {

    // Initialize variables
    frameCount = 0;

    Serial.print("Pathname: ");
    Serial.println(pathname);

    // Attempt to open the file for reading
    if (! file.open(pathname)) {
        Serial.println("Error opening GIF file");
        return ERROR_FILEOPEN;
    }
    Serial.println("File open successful");

about 90% of the time the open attempt hangs; about 10% of the open attempts succeed. When the failure occurs I see the pathname message with the correct filename on the serial monitor but I never see anything else afterwards. That is I never see the "Error opening GIF file" message nor the "File open successful" message. Something is hanging within the open call.

On my teensy 3.1 board I have installed the crystal for the RTC if that makes any difference. I should also mention I am driving a 32x32 matrix of RGB LEDs with the teensy and it is working perfectly up until this point.

My questions are:
1. Anybody else see this?
2. Is there a special version of the SdFat library for the teensy 3.1? If so where does one get it?

I've tried slowing SPI down to 1/2 speed but it doesn't help.

Thanks
 
OK more info

The code I am having trouble with is attempting to parse and decompress animated GIF files. I have a lot of Serial.print and Serial.println calls sprinkled around my code. I have wrapped many of these with conditional compilation expressions like the following so I can turn them off once the code is functioning:

Code:
#if DEBUG == 1
    Serial.print("ImageLeftPosition: ");
    Serial.println(tbiImageX);
    Serial.print("ImageTopPosition: ");
    Serial.println(tbiImageY);
    Serial.print("ImageWidth: ");
    Serial.println(tbiWidth);
    Serial.print("ImageHeight: ");
    Serial.println(tbiHeight);
    Serial.print("PackedBits: ");
    Serial.println(tbiPackedBits, HEX);
#endif

What I just found is that my open code always works if I set DEBUG to 0, effectively removing these print statements and others like them. So my new question is, could I be exceeding the size of some segment in memory where these strings are stored? Is there some limit on the number or length of the static strings?

When I compile my code it doesn't look like I'm anywhere close to exceeding flash or ram. Here is what I see:
Binary sketch size: 29,520 bytes (of a 262,144 byte maximum)
Estimated memory use: 15,992 bytes (of a 65,536 byte maximum)

Does this ring a bell for anyone?

Thanks
 
Too many strings in RAM? If so, there's a way to force them to be flash-only.
Otherwise, maybe the time spent doing Serial.print(), if omitted, causes a problem with "too fast".
 
Too many strings in RAM? If so, there's a way to force them to be flash-only.
On the ARM systems (i.e. Teensy 3.x), string literal constants should automatically go in flash memory. If you use the const keyword, global and static arrays will also be put into flash memory. Your estimated memory use is near 16,384 (power of 2). Maybe the stack used pushes it over the 16k boundary, and something doesn't like the transition.
 
On the ARM systems (i.e. Teensy 3.x), string literal constants should automatically go in flash memory. If you use the const keyword, global and static arrays will also be put into flash memory. Your estimated memory use is near 16,384 (power of 2). Maybe the stack used pushes it over the 16k boundary, and something doesn't like the transition.
Really? I had read that string constants have to be explicitly attributed as const to go in flash and not in initialized RAM data.

So are you sure that Serial.print("Hello World\n"); is flash-only, not in initialized RAM? I suppose the compiler could have a command line switch for Von Neumann architecture MCUs (like Teensy's ARM Cortex) to automatically put strings in flash as named constants.
 
Status
Not open for further replies.
Back
Top