So I was thinking about Flash memory today, and I realized that the FTL / Flash Filesystems are overly-complicated for a lot of microcontroller applications. Still, microcontroller projects need some way to ensure that the NAND Flash wears out evenly (some uCs, like the AVR DD, only has 10,000 erase/write cycles on its Flash).


The traditional answer is of course, FTL (Flash Translation Layers), a component of modern Flash-based filesystems. As a modern filesystem writes data, the FTL remembers how many times each Flash-page has been written to. The FTL / Page allocation logic ensures that the least-used page gets malloc()'d next, and therefore balances the erase cycles across all the flash to maximize endurance.

In particular, when the application code wishes to “erase” a Flash page, it instead is “malloc()” from the pool of flash (usually: the one with the fewest erase cycles), while the old page is conceptually free()'d back into the pool. (a process known as “TRIM” on modern Flash controllers)

This way, as the application-code writes files (or rewrites files), the FTL walks across the whole flash and load-balances the erase cycles.

Things can get far more complex (ex: static wear leveling), but this is enough of a discussion that I think beginners can follow along now.


Anyway, the FTL itself needs a wear-balanced data-structure. I can imagine a few off the top of my head (writing to a journal-like log of FTL pairing changes)… where the Microcontroller would read the whole log upon startup and “recreate” the most recent data (ensuring integrity even in a random power loss). After all, the FTL itself needs to be persistently stored, and the best place for that is… well… the Flash system you’ve got. So FTL persistent storage data-structures necessarily solve this problem.

But I was wondering: is there a book, an encyclopedia, or other such reference which discusses wear-leveled data-structures such as the FTL?

I have to imagine I’m not the first programmer to think about using these 64kB or 32kB Flash Microcontrollers in a wear-leveled manner. Someone out there must have implemented something and wrote about it!

EDIT: For my research, I’m looking into JFFS2, YAFFS, and other Flash-based embedded filesystems to see if their source code has any references to anything, or other such concepts. But if anyone has pointers for me on this question I’d be very interested!