SDfilesystem mount and dismount

06 Nov 2012

Hello,

I am designing a product that uses an SD card to log data. The user will need to be able to remove the SD card and re-insert it (or insert a different card) without physically resetting the product. I have written the following code to try this out:

// example writing to SD card, sford
// modified to show problem when reconnecting an sd card or a different card

#include "mbed.h"
#include "SDFileSystem.h"

// Change the pinout to the port you are using
SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd");
Serial pc(USBTX, USBRX);

int main() {
    int fileno = 0;
    
    // Create a directory
    printf("Making a directory\n");
    mkdir("/sd/mydir", 0777);
    
    while(1) {
        printf("Opening sd card\n");
        // Set file name
        char buffer[50];
        sprintf(buffer, "/sd/mydir/%i.txt", fileno);
        //****************************************************
        // This is necessary for card to work when reconnected
        // Initialise disk
        sd.disk_initialize();
        //****************************************************
        // Open a file for write
        FILE *fp = fopen(buffer, "w");
        if(fp == NULL) {
            // The file was not opened
            printf("Could not open file for write\n");
        }
        else
        {
            // The file was opened, so write a message to it
            fprintf(fp, "Hello sd card!");
            // then close it
            fclose(fp);
            printf("Write successful\n");
        }
        //****************************************************
        // Now remove the sd card, check that the first file
        // has written ok. If so, re-insert the card without
        // resetting the mbed, or insert another card. Then 
        // press any key on the terminal to start the write 
        // of a second file.
        //****************************************************
        while (!pc.readable()) {
            // Wait for a key press
        };
        // Clear character from buffer
        pc.getc();
        // Increment file number
        fileno++;
    }
}

With the disk_initialize() command commented out, if the sd card is removed and then re-inserted, the program never returns from the fopen call. This is probably because the sd card hasn't been re-initialised but I would have expected a NULL return instead of a crash. With the initialize_disk() command enabled, if the card is removed and re-inserted, the first attempt at fopen fails with the following messages, but all subsequent attempts work fine.

No disk or could not put SD card into idle state; Didn't get a response from the disk; Set 512-byte block timed out; Could not open file for write.

If however, I remove the card and insert a different card, the program hangs in fopen. Since fopen is hidden away in the closed FATFileSystem there is no way for me to debug this.

Does anyone have any suggestions on what is causing the hangs and how I can work around them? I am thinking along the lines of mount and dismount commands to ensure that both the SD card and the FAT file system are initialised properly when a new card is inserted.

Any suggestions gratefully received

Thanks Tim

06 Nov 2012

most SD card connectors have a card inserted switch (phisical)

so you might be able to use that, to prevent looking at card when not there,

and re-initilise on insertion.

also what about a switch/button so user needs to press button .. wate for MBED to OK .. then remove SD ??

Ceri

06 Nov 2012

Thanks Ceri,

I'll have a look at connecting up the SD switch and using an InterruptIn to re-initialise a new card. I can initialise the SD card by calling disk_initialize() but this won't re-initialise the FAT file system (hence the hang if I put in a different card). I'm thinking I might have to use "new" and "delete" to create and destroy an SDfilesystem when the card is inserted/removed. That way, the FAT file system will be initialised too.

Thanks Tim