Possible to force write to disk with FatFileSystem?

20 Jul 2012

I am using the FatFileSystem and SDFileSystem packages to log data to files on an SD card.

I would like to keep a file open for a relatively long period (hours), but to also protect against data loss should the mbed lose power or reset before fclose is called.

I have tried using fflush after every write and I also tried using setvbuf(f, 0, _IONBF, 0) to force all writes to be immediate, but neither method seemed to do anything.

Is there no alternative to opening and closing the file periodically? Could frequent open/close calls corrupt the file or SD FS?

Thanks!

07 Dec 2012

I'm looking to do the same thing, does anyone have an answer?

09 Dec 2012

Use disk_sync()

11 Dec 2012

disk_sync() is not implemented in SDFileSystem or FATFileSystem.

11 Dec 2012

disk_sync is implemented in the subjacent FatFileSystem from ChaN. The "glue" is missing. Please try to call the function : f_sync((FIL*)fp);

11 Dec 2012

I don't think I understand the inner workings of SDFileSystem or FatFileSystem. Please help me.

From reading the source code it looks like a SDFileSystem is a FatFileSystem object. However, neither SDFileSystem or FatFileSystem implements disk_sync(). They both create it as a virtual function to be implemented. All that SDFileSystem implements is some of the SPI interface for the SD card, specifically: disk_initialize(), disk_read(), and disk_write(). Assumedly these are the functions that are called by the cstdio library functions. Where I'm lost is how FatFileSystem works and how I'm supposed to access or use it.

Also, what is a *FIL as opposed to an *FILE?

Thank you.

12 Dec 2012

You have to digg into the ChaN folder. The filesystem functions are in ff.cpp. The public functions start at line 2233. Here we have f_open, f_sync ... The problem is - in SDFileSystem.cpp is only a empty function - no wraper.

int SDFileSystem::disk_sync() { return 0; }

We have to fill this with code or we can call the underlaying function f_sync. The parameter is a pointer to the file handle. To get the compiler happy we have to cast the *FILE to *FIL used in ff.cpp. Compare to the close command defined in FATFileHandle.cpp line 32.

30 Jan 2015

How we can make such parameter when disk_sync does not know anything about our file?