Implement a simple wear leveling strategy, flash store cleanup #3007
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements a simple flash wear leveling on various hardware.
Flash wear leveling
User settings are stored in the MCU onboard flash memory (except for the MKS TFT35 V1.0 which uses an AT24C16 EEPROM).
The STM datasheet guarantees 10K writes. The onboard SPI flash guarantees 100K writes, and the AT24C16 guarantees 1 million writes. So the SPI flash seems to be a good candidate to hold user settings, but the onboard program memory was selected.
For example, the BTT TFT35 V3.0 uses a 16K flash sector to hold less than 256 bytes of data. The whole sector is erased every time a user parameter is changed. This not only takes time, but also wears out the flash memory. The implemented wear leveling algorithm improves the wear leveling by a factor of 64, allowing 640K writes. Also responsiveness is improved as interrupt are disabled during flash erase cycles.
For other hardware like the BTT TFT35 V3.0.1 the benefit is less, but still an improvement of a factor of 8 is achieved, the improvement is less because the used flash sector is only 2KB, this could be increased if required.
Part of the improvement is achieve by not writing more data than needed. The settings are less than 256 bytes, but still always 384 bytes were written to flash. The unused data values are usually 0, which is the worst case scenario for flash memory. It would be better to initialize the data record to the status of erased flash (0xFF) so the cells that do not need to change are not changed.
FlashStore cleanup
The flash store did some odd things which were improved.
Requirements
Any TFT, except MKS TFT35 V1.0
Benefits
Flash wear leveling
More responsive saving of user settings
Some flash store code cleanup and assert
More reliable flash storage handling