Go Down

Topic: When to call sync() with SdFat.h? (Read 878 times) previous topic - next topic


I thought I was following the rules by counting my bytes and syncing with:

if(BytesWrittentoSD > (512 - # of bytes in my buffer) {
BytesWrittentoSD=0; }

before moving on to write my next record to my data loggers SD card with


but I recently found this post:


which seems to imply that tracking my bytes, and sync() ing before I hit 512 is no longer necessary with SdFat.h because it's internal caching does that for us now.

Is that true? Can anyone confirm that for me?
All the stuff I find on line seems to imply that any application which writes to a file using print(), println() or write() must call sync() manually. But I am wondering if that is just out of date.


You don't have to call sync as long as you eventually close the file. But if you fail to close the file for some reason (power failure, program crash) you will lose all of the data. So I think it is prudent to call sync on some schedule as insurance. That way in the event of a failure of some sort you will only lose data since the last sync.


I close the SD log file at the end of every eeprom data flush, but since my eeprom can buffer up to five days worth of data (32k), sometimes that writing event is a fairly big one. I will think about how to only sync a few times during the process, rather than every 512 characters as I had been doing. This change alone will likely save me quite a bit of power.


Go Up