fwrite and buffer

06 Apr 2011

Hi,

I write on usb mass storage with fwrite, and I have questions :

1.What is the buffer size of fwrite function ?

2.How write without buffer ? I want a POSIX write like function.

3.In the class MSCFileSystem, disk_sync isn't implemented. How request the key about the state of is buffer ? FATFileSystem has it a buffer ?

Thank you very much, and sorry for my bad english.

Steph.

07 Apr 2011

Are you seeing issues with the data not getting saved out immediately when you write data to a file?

1) In section 2.4 of ARM document DUI 0475C - Using ARM® C and C++ Libraries and Floating-Point Support it mentions that a 512 byte buffer is associated with each file.

2) You should be able to use the setvbuf() API to remove the buffer if that is what you want.

3) I don't know the answer to this but you might want to try the setvbuf() trick first.

Sample usage of setvbuf()

#include "mbed.h"

LocalFileSystem local("local");

int main() 
{
    static const char SampleString[] = "Hello World\n";
    
    FILE* fp = fopen("/local/out.txt", "w");
    
    // If you issue this call right after the fopen then a buffer will never be allocated for the FILE
    setvbuf(fp, NULL, _IONBF, 0);
    
    fwrite(SampleString, strlen(SampleString), 1, fp);
    
    fclose(fp);
}
07 Apr 2011

Thank you for this response.

Sorry, but I can't do blank line. It's a bug of this forum ?

512 byte is coherent with my result. And setvbuff solve a part of my problem :).

-

MSCFileSystem has only two fonction available for FATFileSystem : disk_read and disk_write. The two function read and write one bloc(512 bytes) of the USB Key.

When I use this function directly, to write a large set of data, I can write 512 times 512 bytes(256kB), and after writing fail. The result of disk_write is alwais the same (successful).

May be the key has 256kB of buffer ?

How FAT Filesystem know when he can write ? MSCFileSystem has no function for this.

-

NB : opening the source code of mbed library is very important for user. It's still planed ?

08 Apr 2011

Do you have some code that you could share which shows the problem you are seeing?

08 Apr 2011

I resolve my problem. I did a stupid mistake in my code ;).

My code is (WARNING : this code ERASE the filesystem on key !) :

char buff[512];
#define NBMESURE 1000
unsigned int mesure[NBMESURE];

void main()
{
    // Timer is configured to count all 1us.
    // PR was calibrated with oscilloscope.
    LPC_TIM0->TCR |= 0x03;
    LPC_SC->PCONP |= 0x02; 
    LPC_SC->PCLKSEL0= (LPC_SC->PCLKSEL0 & ~(0x03 << 2)) | (TIMER_DIVIDE << 2);
    LPC_TIM0->PR = 95;
    LPC_TIM0->TCR &= ~0x02;

    massStorage.disk_initialize();
    
    // place des données dans le buffer
    for(unsigned int i=0;i<512;i++)
    {
        buff[i]=0xAB;
    }    

    wait_ms(5000); // Because disk_initialize is async.

    TimeMesure[0]=LPC_TIM0->TC;
    for(i=1;i<64*NBMESURE;i++) 
    {
        if(massStorage.disk_write(buff,i))
        {
            printf("ERROR in bloc %d\n",i);
            while(1);
        }
        if(i%64 == 0)
            TimeMesure[i/64] = LPC_TIM0->TC;
        
        //TimeMesure[i]=LPC_TIM0->TC;
    }
    
    // Flushing data in buffer.
    wait(10);

    printf("Result :\n");
    printf("Starting bloc;Times\n");
    for(unsigned int i=0;i<1000-1;i++)
    {
        printf("%u;%u\n",i*64,TimeMesure[i+1]-TimeMesure[i]);
    }
    while(1);
}

The key write the data at less than 100ko/s. It's very bad, and I don't understand the reason. Do you have idea ?