SDFat - closing file on power loss

I have built a medical data logger which uses SDFat to write a CSV file. The device is powered on with a hardware button but has a touch sensitive screen with a button to start and stop recording. The stop recording button closes the file nicely.

However, I also want to guard against the patient powering off with the hardware button. Some data loss is inevitable but we cannot afford to lose the entire record just because a patient fails to follow the prescribed process.

Up until now I have been using FATFS and it is very robust in this case. SDFat is much faster but the file is zero length if the power button is used. I tried a synch command at 1 Hz intervals but this also resulted in a zero length file.

Any advice would be appreciated.

How large is the buffer in SDFat?

I note that the binary to csv routine in the fast data logger example performs a synch based on a cluster counter but I am yet to work out exactly how that works.

I was able to get the sync to work using a second software timer to set the interval between sync calls. I have tested at both 15 and 60 second intervals.

The sync seems to cause a 2 msec jitter in timing. I use a software timer to set the adc sampling interval.

I am currently able to sample at 200 Hz which is the aim. Without sync I can log for the desired 20 minutes without any timing jitter but I lose the file if the patient kills the power via the hardware switch.

At present it appears I can get the 2 msec jitter once every 10-60 seconds by using sync to ensure that the bulk of the data is not lost if the hardware switch is used to power down.

For comparison FATFS caused a 2 msec jitter every 320 msec when sampling at 100 Hz and reliable sampling at 200 Hz was unachievable.

Any ideas how to overcome the 2msec jitter would be appreciated.