SD card fopen problem

01 Apr 2012

I am using a LPC11U24 that is interfaced to an SD card. In order to write files to the SD card I am using the SDFileSystem code as well as the fat code from the two links below. I have a ticker that runs every two seconds, and it reads in a value from an A-to-D pin and then transmits it over a serial connection to a Bluetooth module. It also then writes the value to a file on the SD card.

http://mbed.org/users/simon/programs/SDFileSystem/5yj8f http://mbed.org/users/simon/programs/fat/m3oya6

It works fine for the first few times the ticker is called (about 4 times), it is able to write the values to the SD card file as well as transmit them over Bluetooth. However, when it gets to about the fifth time the ticker function is called, the mbed will freeze up whenever it tries to call fopen at this line: FILE *fp = fopen(buf, "a");

Whenever I remove all of my Bluetooth code the ticker function will not stop at the fifth iteration and it will keep writing to the SD card. If I remove all of the SD card code then the values will be transmitted over Bluetooth and will not stop either. Does anybody know what is causing this behavior?

Thanks!

01 Apr 2012

If you post your code, then peapole will usualy help :)

Ceri

01 Apr 2012

ceri clatworthy wrote:

If you post your code, then peapole will usualy help :)

Ceri

Here is the code for the ticker function.

//called every 2 seconds by Ticker
void getData() {
    float sample = ain.read();
    sample = sample * 3.3f;

    if (rn41.writeable())
        rn41.printf("%f\n", sample);

    FILE *fp = fopen(buf, "a"); //freezes here after about 4 or 5 calls to this function
    if (fp != NULL) {
        fprintf(fp, "%f\r\n", sample);
    }
    fclose(fp);

}
01 Apr 2012

This is a bit of a guess, but I am going to say that the fprintf isn't completing it's operation, or fopen is taking too long.

You might try something like this:

FILE *fp;

// Initialize The Opening of the File
void InitFile()
{
    fp = fopen( buf, "w" );
};

// Close the file if opened
void CloseFile()
{
    if ( fp ) fclose( fp );
};

//called every 2 seconds by Ticker
void getData() 
{
    float sample = ain.read() * 3.3f;

    if ( rn41.writeable()) rn41.printf( "%f\n", sample );

    if ( fp ) 
    {
        fprintf(fp, "%f\r\n", sample);
    };

}

The open and close may be the actual problem... this is just a guess though.

-Doug

21 Feb 2015