# FreeRTOS + FAT ff\_fwrite() issue

**URL:** <https://forums.freertos.org/t/freertos-fat-ff-fwrite-issue/19889>\
**Category:** Libraries\
**Created:** [April 26, 2024, 1:23pm UTC](https://forums.freertos.org/t/freertos-fat-ff-fwrite-issue/19889 "2024-04-26T13:23:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bbuyukyilmaz](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/bbuyukyilmaz/32/4147_2.png) [@Bbuyukyilmaz](https://forums.freertos.org/u/Bbuyukyilmaz)\
**Post date:** [April 26, 2024, 1:23pm UTC](https://forums.freertos.org/t/freertos-fat-ff-fwrite-issue/19889/1 "2024-04-26T13:23:24Z")

</div>

Hello everyone,  
I’m working on custom zynq board that includes 64GB emmc. I’ve encountered an issue where if I write more than 272KB of data at once, an unexpected extra byte appears at offset 0x10000 hence data beyond this offset are shifted by one byte. Below is my test code for clarity:

```auto
#define WRITE_BUFFER_SIZE 500 * 1024u
#define READ_BUFFER_SIZE 500 * 1024u

uint8_t WriteBuffer[WRITE_BUFFER_SIZE];
uint8_t ReadBuffer[READ_BUFFER_SIZE];
uint8_t Count = 0;

for(int i = 0; i < WRITE_BUFFER_SIZE; i++){
    WriteBuffer[i] = i++;
}

pCurrentFile = ff_fopen(CurrentFilePath, "w+");
size_t bytes_written = ff_fwrite((const void*)WriteBuffer, sizeof(uint8_t), 1024 * 271, pCurrentFile);
fs_ret = ff_fclose(pCurrentFile);

pCurrentFile = ff_fopen(CurrentFilePath, "r+");
size_T bytes_read = ff_fread(ReadBuffer, sizeof(uint8_t), 1024 * 271, pCurrentFile);
fs_ret = ff_fclose(pCurrentFile);

```

After running the code, I downloaded the file to my PC then I check the data in the file by using Hex Editor Neo. For the 271KB file everything is expected no shifting or extra byte.  
271KB file:  
[271KB.zip](https://forums.freertos.org/uploads/short-url/nEmiinYisayvpBNQIfFJTqAgLaw.zip) (1.6 KB)

Hex Editor Screen Shot:

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/a/ab416ee281db2a9c5e5bb28488458bfb68932995.png)

When I increase the write and read size to 272KB, the issue appears.

The code for writing 272KB :

```auto
#define WRITE_BUFFER_SIZE 500 * 1024u
#define READ_BUFFER_SIZE 500 * 1024u

uint8_t WriteBuffer[WRITE_BUFFER_SIZE];
uint8_t ReadBuffer[READ_BUFFER_SIZE];
uint8_t Count = 0;

for(int i = 0; i < WRITE_BUFFER_SIZE; i++){
    WriteBuffer[i] = i++;
}

pCurrentFile = ff_fopen(CurrentFilePath, "w+");
size_t bytes_written = ff_fwrite((const void*)WriteBuffer, sizeof(uint8_t), 1024 * 272, pCurrentFile);
fs_ret = ff_fclose(pCurrentFile);

pCurrentFile = ff_fopen(CurrentFilePath, "r+");
size_T bytes_read = ff_fread(ReadBuffer, sizeof(uint8_t), 1024 * 272, pCurrentFile);
fs_ret = ff_fclose(pCurrentFile);

```

Again I download the file to PC and check with Hex Editor. I saw extra 0xFF byte at the address 0x10000. After that address all data addresses are shifted one.  
272KB file:  
[272KB.zip](https://forums.freertos.org/uploads/short-url/hhgSdMdDY2SoLg8tQSqDXpAAY2r.zip) (1.6 KB)

Hex Editor Screen Shot:

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/8/878c9166d2e800f0021c21160bf157c8f513b704.png)

To ensure the integrity of the data, I also dump the RAM after reading the file and compare the dumped file with the one downloaded using FileZilla. Both files are identical.

What could be causing this extra 0xFF byte? Does the `ff_fwrite()` function have a limitation on the size of data it can write in one go?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![carlk3](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/carlk3/32/266_2.png) [@carlk3](https://forums.freertos.org/u/carlk3)\
**Post date:** [April 26, 2024, 4:44pm UTC](https://forums.freertos.org/t/freertos-fat-ff-fwrite-issue/19889/2 "2024-04-26T16:44:11Z")

</div>

I commonly write files of up to 4095 MiB with FreeRTOS + FAT `ff_fwrite()` and read them back, verifying the data (see [big\_file\_test.c](https://github.com/carlk3/FreeRTOS-FAT-CLI-for-RPi-Pico/blob/master/examples/command_line/tests/big_file_test.c)), without problems.

I recommend testing your Media Driver by porting ChaN’s [Compatibility Checker for Storage Device Control Module](http://elm-chan.org/fsw/ff/res/app4.c) (one of my ports: [app4-IO\_module\_function\_checker.c](https://github.com/carlk3/FreeRTOS-FAT-CLI-for-RPi-Pico/blob/master/examples/command_line/tests/app4-IO_module_function_checker.c)). It bypasses the file system entirely and drives the block device driver (Media Driver) interface directly. This makes driver testing and debug much simpler. However, you will have to reformat your medium after running it.

---

<div class="post-metadata">

**Author:** ![Bbuyukyilmaz](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/bbuyukyilmaz/32/4147_2.png) [@Bbuyukyilmaz](https://forums.freertos.org/u/Bbuyukyilmaz)\
**Post date:** [April 29, 2024, 6:41am UTC](https://forums.freertos.org/t/freertos-fat-ff-fwrite-issue/19889/3 "2024-04-29T06:41:57Z")

</div>

Hi Carl, thanks for you reply.  
Before the porting ChaN’s module I have checked your code and I noticed that your test code call the write API like that `size_t bw = ff_fwrite(buff, 1, BUFFSZ, file_p);`. Then your code keep calling it until it reaches the value of the size parameter. If I follow your way, yes it works for me.

But I don’t know is it the correct way (if it is not suitable any reason please inform me I have no idea about that), I want to write big chunk of data to the file by calling just one write API like `size_t bytes_written = ff_fwrite((const void*)WriteBuffer, sizeof(uint8_t), 512000, pCurrentFile);`. If I use ff\_fwrite() like that, the issue that I mentioned above occures.

Could you test your system after the increasing the xItems parameter around the 512000 if it is make sense?

---

<div class="post-metadata">

**Author:** ![carlk3](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/carlk3/32/266_2.png) [@carlk3](https://forums.freertos.org/u/carlk3)\
**Post date:** [April 29, 2024, 5:51pm UTC](https://forums.freertos.org/t/freertos-fat-ff-fwrite-issue/19889/4 "2024-04-29T17:51:11Z")

</div>

> [@Bbuyukyilmaz](#):
>
> Could you test your system after the increasing the xItems parameter around the 512000 if it is make sense?

I don’t have any MCUs that have enough SRAM for a write buffer that large. For example, the Raspberry Pi Pico only has 264kB of SRAM.

---

<div class="post-metadata">

**Author:** ![Bbuyukyilmaz](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/bbuyukyilmaz/32/4147_2.png) [@Bbuyukyilmaz](https://forums.freertos.org/u/Bbuyukyilmaz)\
**Post date:** [April 30, 2024, 6:04am UTC](https://forums.freertos.org/t/freertos-fat-ff-fwrite-issue/19889/5 "2024-04-30T06:04:10Z")

</div>

I got it. Thanks for helping. I would update the post if I found something.
