SD Card hang

02 Sep 2010

Hi All,

I'm working on a data logger capturing serial at writing it to SD card - which works fine for a period (many hours) but then hangs when trying to open a file on the SD device for writing. I have a watchdog that resets the mbed when this happens but each reset can't get past card initialisation - failing with the "No disk, or could not put SD card in to SPI idle state" message. The watchdog then fires & resets the mbed & this cycle repeats forever. The only thing that resolves this, albeit until the next crash, is a powercycle of the mbed - suggesting perhaps that the SD has found its way into the inactive state.

I'm using the mbed on a CoolComponents breakout board (actually I have 2 mbeds & 2 boards, both behave the same) capturing on UARTs 1 & 2 serial data at 4800baud 8:even:1 - logging to a variety of SD cards, including a Transcend 1GB microSD. All combinations fail eventually. I'm using the latest (as of today) mbed libs & SD card files.

I've looked around the forums & read a few posts on issues with the SD card - none appear to have been thoroughly investigated or resolved (at least in the threads I've read).

Attached is the main loop - it polls the serial ports (in case of any serial irq problems that may exist), formats the data into a timestamped string & writes it to the card. DBG_INSTR() is a macro that sets the state of the LEDs which flicker like mad until a hang when they show the 'OPENING_FILE' LED pattern.

Any input is welcome & invited.




Serial    uart[2] = { Serial(NC, p14), Serial(NC, p27) };
uint32_t  bytesRX[2];  
time_t    t = 0;

SDFileSystem sd(p5, p6, p7, p8, FILE_FS_NAME); // mosi, miso, sclk, cs
FILE         *pf = NULL;
int          fileSize = 0;
int          fileIdx = 0;
char         fileName[FILE_NAME_LEN];

DigitalOut   ledArr[4] = { LED1, LED2, LED3, LED4 };
DigitalIn    pushBtn(p16);
DigitalIn    sdDetect(p9);

    // The main loop
        for (int i = 0; i < 2; i++)
            if (uart[i].readable())
                idx = 0;
                (void)sprintf(buf, DATA_HEADER,
                              i, time(NULL), 
                // While there's still data in the serial FIFO
                while (uart[i].readable())
                    uint16_t offset = DATA_LINE_OFFSET(++idx);
                    // Ensure no overflow
                    if (offset > (DATA_LINE_LEN - (2 * DATA_FORMAT_EXT_LEN)))
                    // Read it & put in on the formatted line
                // Write data to the file
                if ((pf = fopen(fileName, "a")) == NULL)
                    error("Cannot open %s\n", fileName);
                fileSize += fprintf(pf, buf);
                if (fileSize > FILE_SIZE)


09 Sep 2010


Have you made any progress on using cards other than the ones specified in the SD card cookbook page? I also have some serial data that I would like to log to an SD but unfortunately, I don't have any of the (really old and hard to find) specified cards available to me at present so I have not been able to even get as far as you have.

One thing I did notice when I updated to the latest FATFileSystem (v 26 from v 24) was that after changing nothing in my main code I now get nothing (no flashy LEDs, not even a simple printf at the top of main(); ) at all on the hyperterminal window. Before the update I was at least being told that the card I was using was an "unknown SD version". This probably hasn't got too much to do with the problem you are having but maybe someone who knows how to solve your problem might know the cause of this one too.