Filesystem and write durability

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
pjennings
Posts: 2
Joined: Sat Jan 14, 2017 3:53 pm

Filesystem and write durability

Post by pjennings » Sat Jan 14, 2017 4:03 pm

I would like to write a few bits of data to the filesystem fairly frequently. Is there a recommended way to do this while maximizing the flash write durability?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Filesystem and write durability

Post by deshipu » Sat Jan 14, 2017 10:35 pm

Every write writes at least 512 bytes to the flash, no matter how many of those are actually modified -- even if you only change a single bit. The best way to avoid wear is to buffer your writes, and only actually write to flash once in a while.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Filesystem and write durability

Post by dhylands » Sun Jan 15, 2017 4:18 am

The internal flash is even worst. The filesystem may write 512 byte blocks, but you actually need to erase an entire sector (16K - 64K on th F series, 2K on the L series).

If you wrote to flash directly, (i.e. not using a filesystem), then you can write individual bytes at a time. Well you erase a block to all 0xff's and then you can flip bits to zero, one byte at a time. Once a bit has been flipped to a zero you need to erase the entire sector to get it back to a a 1.

chrismas9
Posts: 152
Joined: Wed Jun 25, 2014 10:07 am

Re: Filesystem and write durability

Post by chrismas9 » Wed Jan 18, 2017 11:56 am

If you are writing regularly I would suggest using an SD card as they implement wear levelling which spreads the writing over the whole device instead of re-writing the same sectors. I have seen reports that some SD cards do wear levelling in the background and can be corrupted when you turn the power off without doing an orderly shutdown.

For that reason I suggest using Sandisk SD cards as they appear to just use a replacement sector from the erase pool whenever you do a write. The process is described here:

https://web.archive.org/web/20150326122 ... elv1.0.pdf

pjennings
Posts: 2
Joined: Sat Jan 14, 2017 3:53 pm

Re: Filesystem and write durability

Post by pjennings » Sat Jan 21, 2017 9:08 pm

[quote="dhylands"]If you wrote to flash directly, (i.e. not using a filesystem), then you can write individual bytes at a time. Well you erase a block to all 0xff's and then you can flip bits to zero, one byte at a time. Once a bit has been flipped to a zero you need to erase the entire sector to get it back to a a 1.[/quote]

It would work for me to flip bits to zero progressively. Is there a way to do that within Micropython? I am using the ESP8266 port but I would be interested in a general answer.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Filesystem and write durability

Post by dhylands » Sun Jan 22, 2017 5:21 am

I'm not familiar with the esp8266. I know it has an external flash chip and I'm not sure what the underlying technology for it is.

I don't think that there are any modules for directly manipulating the flash.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Filesystem and write durability

Post by Roberthh » Sun Jan 22, 2017 12:16 pm

A quick look in the docs reveals the appropriate methods of the esp module:
http://docs.micropython.org/en/latest/e ... hlight=esp

Post Reply