Settings:
Sector size = cluster size = 512 byte
Fat12
Process:
pFile = f_open("name.bin", "w+"); // Create file. In my case cluster 3 (sector 42)
f_write(data, 1, 512, pFile); // Allocate cluster 4 (sector 43) at the end
f_close(pFile);
pFile = f_open("name.bin", "r+");
f_seek(pFile, 512, F_SEEK_SET);
f_write(data, 1, 512, pFile); // cause a wrong fat entry at the next f_open(); f_write();
What happened:
f_seek() set gl_file.relpos to 512 but didn’t change gl_file.pos
fn_write() increment gl_file.pos.sector, copies the data to the local buffer (gl_sector) and calls _f_emptywritebuffer()
_f_emptywritebuffer() writes the data to cluster 5 (that’s the correct cluster). But gl_file.pos.cluster was not incremented so _f_getclustervalue() returns cluster 4 for the next cluster. Because the next cluster is 4 there will be no allocation for the next cluster.
At the next "f_open(); f_seek(); f_write();" _f_emptywritebuffer() writes to cluster 5 (that’s also the correct cluster) and allocates cluster 11. The FAT entries are now cluster 3 -> cluster 4 -> cluster 11 (-> = points to). But Cluster 5 is be missing in the FAT chain.
I fixed the issue by editing the function _f_fseek().
Code:
static FatErrorCodeType _f_fseek ( long offset )
{
unsigned long cluster;
unsigned long tmp;
FatErrorCodeType ret = F_NO_ERROR;
long remain;
} / _f_fseek /
Thank you for the information - this is being looked at internally.
Regards.
Fixed in V1.0.1, which is already available in SVN, and will be included in FreeRTOS V8.0.1.