diff --git a/libraries/fs/fat/ChaN/ff.cpp b/libraries/fs/fat/ChaN/ff.cpp index b63b2632b48..fe786a747de 100644 --- a/libraries/fs/fat/ChaN/ff.cpp +++ b/libraries/fs/fat/ChaN/ff.cpp @@ -2526,7 +2526,7 @@ FRESULT f_write ( UINT wcnt, cc; const BYTE *wbuff = (const BYTE *)buff; BYTE csect; - + bool need_sync = false; *bw = 0; /* Clear write byte counter */ @@ -2559,6 +2559,13 @@ FRESULT f_write ( if (clst == 1) ABORT(fp->fs, FR_INT_ERR); if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR); fp->clust = clst; /* Update current cluster */ + +#ifdef FLUSH_ON_NEW_CLUSTER + // We do not need to flush for the first cluster + if (fp->fptr != 0) { + need_sync = true; + } +#endif } #if _FS_TINY if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0)) /* Write-back sector cache */ @@ -2591,6 +2598,9 @@ FRESULT f_write ( } #endif wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */ +#ifdef FLUSH_ON_NEW_SECTOR + need_sync = true; +#endif continue; } #if _FS_TINY @@ -2623,6 +2633,10 @@ FRESULT f_write ( if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */ fp->flag |= FA__WRITTEN; /* Set file change flag */ + if (need_sync) { + f_sync (fp); + } + LEAVE_FF(fp->fs, FR_OK); } diff --git a/libraries/fs/fat/ChaN/ffconf.h b/libraries/fs/fat/ChaN/ffconf.h index cc94eaac643..959f984689d 100644 --- a/libraries/fs/fat/ChaN/ffconf.h +++ b/libraries/fs/fat/ChaN/ffconf.h @@ -187,5 +187,12 @@ /* To enable file lock control feature, set _FS_LOCK to 1 or greater. The value defines how many files can be opened simultaneously. */ +#define FLUSH_ON_NEW_CLUSTER 0 /* Sync the file on every new cluster */ +#define FLUSH_ON_NEW_SECTOR 1 /* Sync the file on every new sector */ +/* Only one of these two defines needs to be set to 1. If both are set to 0 + the file is only sync when closed. + Clusters are group of sectors (eg: 8 sectors). Flushing on new cluster means + it would be less often than flushing on new sector. Sectors are generally + 512 Bytes long. */ #endif /* _FFCONFIG */