Showing results for 
Search instead for 
Did you mean: 

Integration of FATFS file system on raw NAND flash memory

Associate III
Posted on November 22, 2017 at 21:05


I'm required to integrate FatFs file system on my raw NAND flash memory. I'm interfacing the NAND through SPI peripheral of the STM32L486RG microcontroller. I have developed NAND a low level driver through which I can erase, write and read data in different locations of the memory.

Then, customized the diskio.c file (diskio_write , diskio_read , diskio_ioctl APIs ) which will be used by FatFs APIs.

I have succeeded to make a functional FAT16 file system through f_mkfs function and managing files (create, open, write, read and close) operations.

However, my actual throughput is still limited (especially when writing into files using f_write function). In fact, as the FatFs doesn't contain a wear levelling mechanism, I have developed my own mechanism which ensures a secure write operation, and consists on the following steps:

1- Erase and reserve a single block of the NAND Memory area;

2- Copy the required block data to the reserved block area;

3- Erase the required block area;

4- Write the new content into the required block area;

5- Copy back the old data to the required block area;

This mechanism is functional but not in line with required performances, since it�s time consuming (3mn to erase an area of 512Mo).

My question is:

1. Is it possible to handle the wear levelling mechanism to bypass this limitation in term of performance? If YES, does FatFs can support the integration of a Wear Levelling mechanism with no impact on functionalities.

2. Are there any known limitations on FatFs that may impact the performance of managing NAND memories with STM32?



#fatfs #nand-flash #stm32l486 #spi
Posted on November 22, 2017 at 22:20

It is a file system where the primary block size is 512 bytes, large aligned reads/writes from the application level will improve efficiency at the block level.

It is not responsible for deblocking at the NAND level. There is no expectation that FATFS is going to do anything to assist you as it is agnostic to the medium being utilized. FAT isn't really a good file system for a raw NAND implementation, and especially bad on systems with limited RAM resources.

Efficiencies could be gained if you implement caching, and lazy-write functionality, so you aren't repeatedly erasing and replacing small amounts of data within the larger erase blocks. You'd want to sort and combine writes to minimize the cycles on the memory.

SD Cards believe use a block translation so the write/copy operation is done of an available empty block, and the old block discarded to an erase list to be recovered later.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Gabriele Guerreiro
Posted on November 25, 2017 at 17:11

Hi Elliot, i´m starting with NAND FLASH and FATFs and all I can get is that i need to change the diskio.c. You´ve said that you did that, right? Can you show me your´s modifications or some example on how to use the nand?



Posted on November 25, 2017 at 17:59

This isn't inherently complicated you presumably understand how to read and write to the NAND device, you have to deblock the larger blocks within the NAND to the 512 byte sectors the file system is requesting. Writing is slightly more difficult because you are likely replacing a smaller block of data within in larger erase block. ie you have a 128K erase block and changing 512 bytes within that. Here you must read and hold the original content of 128K block while you erase and rewrite.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 27, 2017 at 13:54



Just to confirm with



FAT isn't really a good file system for a raw NAND implementation, and especially bad on systems with limited RAM resources

You can build your solution around the erase capability feature of FS to come over the Block and sector size difference on NAND, for that a block cache will be allocated in the RAM to be able to copy back old data on the cache, modify the data (sectors) and then erase theNAND block and copy the content of the cache on the NAND block.

Otherwise,if you have limited RAM resources, you can't come out with such implementation.

You have to use a NAND flash file system like YAFFS or JFFS2 or UBI/UBIFS or UFFS.

The FatFS file system is not usable on NAND because of lack of wear leveling and bad block handling.

Today the FatFS is rather fully adapted to work with SD cards or RAM disks.



Posted on November 28, 2017 at 09:54

Hi Clive,

Many thanks for your feedback ! 

I have one more question here. When you talked about implementing cache I thought about exploiting SRAM 2 memory of my STM32L486RG microcontroller. However, SRAM2 size is limited to 32 Kbytes and knowing that a single page in my NAND memory has a size of 2 Kbytes. In addition , a single block within this memory has a size of 128 Kbytes.

Knowing I have dedicated SRAM1 with 96 Kbytes size for application use, could my 32 Kbytes memory be sufficient in this case ? I'm afraid of further lack of memory and data loss.