Skip to main content
GS1
Senior III
August 19, 2019
Question

FAT_FS f_write: Writes wrong data to file when writing odd number of bytes

  • August 19, 2019
  • 2 replies
  • 5968 views

Hi all,

In my project with STM32H743 (created with CubeMX version 5.0.1) I have a strange behaviour of f_write when writing data to the file and the amount is not a number which can be devided by 32.

In my test I wrote 256, 512 or 4096 Bytes at a time. Everything perfect. Data in File as expected.

BUT: When writing e.g. 590 Bytes (which is not dividable by 32) -> Errors appear.

I create a test file with following text pattern (590 Bytes)

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789##

This pattern is written to a file 100 times appending to an open file.

Now the result shows wrong blocks after multiple writes. Here is the wrong part:

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789##

012345678989:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

01234567:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789##

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklm

In one block 2 characters are moved around to the wrong place!!

It seams that this issue only appears when writing a number of Bytes which is not dividable by 32 at a time, 256 is ok, 512 is ok, 4096 is ok, but: 590 is not ok! (This is just an assumption on the current state of testing)

Can anyone help?

ff.c Revision:

#if _FATFS != 68300   /* Revision ID */

This topic has been closed for replies.

2 replies

Tesla DeLorean
Guru
August 19, 2019

Caching is done on 32-byte boundaries, you need to make sure any coherency code handles this correctly.

DMA will also have alignment requirements.

Make sure your write routines clean the cache, and perhaps handle unaligned cases.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
GS1
GS1Author
Senior III
August 19, 2019

Cache is actually already handled in the routines. The wrong data even appears when DCache is disabled totally.

The mistake appears after a repeated and regular amount of block writes of 590 Bytes in this example:

13 ok blocks

-- wrong block

5 ok blocks

-- wrong block

19 ok blocks

-- wrong block

5 ok blocks

-- wrong block

13 ok blocks

-- wrong block

5 ok blocks

-- wrong block

19 ok blocks

and so on.

There is strange repeated pattern behind it.

Tesla DeLorean
Guru
August 19, 2019

You might try a newer version of FatFS that isn't 2+ years old, in case such an error has been fixed.

You could try building without optimization, and see if there is a tool-chain related issue. A software issue (logic/compliation) should be platform agnostic.

I try to avoid using f_write in this fashion because it is super inefficient and requires multiple reads/writes to address sector spanning issue.

Test case seems reasonably scoped, might try when I find some time.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
RSouz.1
Visitor II
January 19, 2021

Hi everyone.

I had the same issue. I updated the version of FatFs library to the version R0.14a and the issue keeped happening. The unique workaround that worked for me is this solution to only request to write an amount of bytes dividable by 32.

Piranha
Principal III
January 20, 2021

That's an indication that the cache management is flawed.