Page 1 of 1

NVS Arduino Library Wear Leveling?

Posted: Sun Oct 25, 2020 3:31 pm
by clearlynotstefan
Hi All,
I'm quite new at all this but am loving it and learning a ton. I'm still using arduino IDE with my esp32 projects but some day I'll graduate. I made smart roller shades using motors with encoders, esp32, and two l98ns (there are 3 shades connected). Rather than using endstop switches, I'm using this NVS library that ports nvs functions to the arduino platform.
https://github.com/rpolitex/ArduinoNvs
My code writes the relevant encoder position when it arrives at its targeted location and writes the target location when it gets a command to move. What I can't figure out is whether this library is capitalizing on Wear Leveling, or if it would need to be modified in some way to improve longevity by wear leveling. My current feeling is that wear leveling is built into the api that the library is using by default and thus I'm covered without doing anything further. I'm doing about 24 writes of integers per day, so I should be set for a while, but it'd still be dumb to keep hitting the same space every time if I could avoid it as replacing the board is a big pain! Any thoughts?

Re: NVS Arduino Library Wear Leveling?

Posted: Mon Oct 26, 2020 12:39 am
by ESP_Sprite
clearlynotstefan wrote:
Sun Oct 25, 2020 3:31 pm
...
My current feeling is that wear leveling is built into the api that the library is using by default and thus I'm covered without doing anything further. I'm doing about 24 writes of integers per day, so I should be set for a while, but it'd still be dumb to keep hitting the same space every time if I could avoid it as replacing the board is a big pain! Any thoughts?

Your feeling is correct, the underlying NVS API uses wear levelling. Especially with only 24 writes a day, flash wear should not be an issue in this use case.

Re: NVS Arduino Library Wear Leveling?

Posted: Mon Oct 26, 2020 6:18 pm
by OutOfLine
As far as I remember it works about like this: nvs stores sequential key value pairs in blocks. New pairs are always added to the end. If the value associated with an existing key gets updated it will *not* be stored in the same old location, but a new pair is added to the end and the old one is marked as deleted. When a block is full a new one is used. If there are too many invalid (deleted) pairs in a given block the remaining valid key value pairs will be copied to the end of the block where new entries are currently written and the old block is marked as free. This avoids wear leveling.

Re: NVS Arduino Library Wear Leveling?

Posted: Tue Oct 27, 2020 4:24 am
by ESP_Sprite
OutOfLine wrote:
Mon Oct 26, 2020 6:18 pm
This avoids wear leveling.
Well, technically this strategy is akin to a log-structured file-system, which is a wear-leveling mechanism all by itself.

Re: NVS Arduino Library Wear Leveling?

Posted: Tue Oct 27, 2020 8:59 am
by OutOfLine
Yes, sorry my wording ("This avoids wear leveling") was wrong. I wanted to say that this avoids problems with wearing. Thank you for correcting that.