5 years, 5 months ago.

Using stdio Buffering on SD File Streams?

In an effort to improve SD card performance (not stressing the card with too many small writes, avoiding blocking from the writes causing missed interrupts) without writing my own buffer, I tried to use stdio's buffer-enable on the stream:

    // Prepare file stream for saving data
    FILE *fp = fopen("/sd/data.csv", "w");
    
    char buffer[BUFSIZ]; // create an appropriately sized buffer
    if(0 == setvbuf(fp, buffer, _IOFBF, BUFSIZ)) {
        pc.printf("Buffering \"/sd/\" with %d bytes.\r\n", BUFSIZ);
    } else {
        pc.printf("Failed to buffer SD card with %d bytes.\r\n", BUFSIZ);
    }

However, now the file on the SD card is just empty, even though setvbuf returns 0. I write fewer than 80 characters and flush before I begin the rapid writes to the stream, but even that data doesn't appear in the file. Does mbed's stdio properly implement setvbuf? Documentation on setvbuf.

Question relating to:

SDFileSystem FAT, file, SD, System

What target are you using ?

Edit: I see K64F only.

posted by Martin Kojtal 30 May 2014

setvbuf is toolchain dependant as mbed does not yet have own stdio library, thus the implementation of ARMCC stdio library is used in the online IDE for K64F (some targets are using microlib).

posted by Martin Kojtal 30 May 2014

Well, all of a sudden, the file starts getting written again and the buffering appears to be working. The data is being written out with missed samples at regular intervals as the buffer is flushed.

posted by Thomas Murphy 30 May 2014
Comment on this question
Be the first to answer this question.

You need to log in to post a question