esp-idf/components/wear_levelling
Jakob Hasse 548022fbe6 refactor(linux): excluded all non-Linux components from build
* All components which won't build (yet) on Linux are excluded.
  This enables switching to Linux in an application without
  explicitly setting COMPONENTS to main in the main
  CMakeLists.txt.
* ESP Timer provides headers for Linux now
* automatically disabling LWIP in Kconfig if it is not available

doc(linux): brought section
  "Component Linux/Mock Support Overview" up to date
2023-10-16 17:06:54 +08:00
..
doc Revamp variable names and update code documentation for wear levelling component 2023-06-21 10:34:24 +02:00
host_test fix(ci): change build-test-rules files folder 2023-09-20 19:17:06 +08:00
include Revamp variable names and update code documentation for wear levelling component 2023-06-21 10:34:24 +02:00
private_include feat(partition_table): Add read-only partition flag and functionality 2023-10-11 00:01:05 +02:00
test_apps fix(ci): change build-test-rules files folder 2023-09-20 19:17:06 +08:00
.gitignore nvs_flash, wear_levelling: ignore host test files 2017-04-17 11:01:18 +08:00
CMakeLists.txt Storage: Partition APIs moved to the new component 'esp_partition' 2022-11-02 21:54:45 +01:00
crc32.cpp host_test: wl migrated to Cmake and linux emulation of esp_partition 2023-04-11 16:16:53 +02:00
crc32.h spiffs, wear_levelling: update copyright headers 2022-02-22 00:09:24 +03:00
Kconfig Correct Kconfigs according to the coding style 2019-01-29 13:37:01 +01:00
out.txt refactor(linux): excluded all non-Linux components from build 2023-10-16 17:06:54 +08:00
Partition.cpp refactor(wear_levelling): WL_Flash flash_drv pointer changed to partition pointer 2023-10-10 14:35:45 +02:00
README_CN.rst docs: update wear levelling cn trans 2022-02-21 19:10:33 +08:00
README.rst docs: update wear levelling cn trans 2022-02-21 19:10:33 +08:00
SPI_Flash.cpp Revamp variable names and update code documentation for wear levelling component 2023-06-21 10:34:24 +02:00
wear_levelling.cpp feat(partition_table): Add read-only partition flag and functionality 2023-10-11 00:01:05 +02:00
WL_Ext_Perf.cpp refactor(wear_levelling): WL_Flash flash_drv pointer changed to partition pointer 2023-10-10 14:35:45 +02:00
WL_Ext_Safe.cpp refactor(wear_levelling): WL_Flash flash_drv pointer changed to partition pointer 2023-10-10 14:35:45 +02:00
WL_Flash.cpp feat(partition_table): Add read-only partition flag and functionality 2023-10-11 00:01:05 +02:00

Wear Levelling API
==================

:link_to_translation:`zh_CN:[中文]`

Overview
--------

Most of flash memory and especially SPI flash that is used in {IDF_TARGET_NAME} has a sector-based organization and also has a limited number of erase/modification cycles per memory sector. The wear levelling component helps to distribute wear and tear among sectors more evenly without requiring any attention from the user.

The wear levelling component provides API functions related to reading, writing, erasing, and memory mapping of data in external SPI flash through the partition component. The component also has higher-level API functions which work with the FAT filesystem defined in :doc:`FAT filesystem </api-reference/storage/fatfs>`.

The wear levelling component, together with the FAT FS component, uses FAT FS sectors of 4096 bytes, which is a standard size for flash memory. With this size, the component shows the best performance but needs additional memory in RAM.

To save internal memory, the component has two additional modes which both use sectors of 512 bytes:

- **Performance mode.** Erase sector operation data is stored in RAM, the sector is erased, and then data is copied back to flash memory. However, if a device is powered off for any reason, all 4096 bytes of data is lost.
- **Safety mode.** The data is first saved to flash memory, and after the sector is erased, the data is saved back. If a device is powered off, the data can be recovered as soon as the device boots up.

The default settings are as follows:

- Sector size is 512 bytes
- Performance mode

You can change the settings through the configuration menu.

The wear levelling component does not cache data in RAM. The write and erase functions modify flash directly, and flash contents are consistent when the function returns.


Wear Levelling access API functions
-----------------------------------

This is the set of API functions for working with data in flash:

- ``wl_mount`` - initializes the wear levelling module and mounts the specified partition
- ``wl_unmount`` - unmounts the partition and deinitializes the wear levelling module
- ``wl_erase_range`` - erases a range of addresses in flash
- ``wl_write`` - writes data to a partition
- ``wl_read`` - reads data from a partition
- ``wl_size`` - returns the size of available memory in bytes
- ``wl_sector_size`` - returns the size of one sector

As a rule, try to avoid using raw wear levelling functions and use filesystem-specific functions instead.


Memory Size
-----------

The memory size is calculated in the wear levelling module based on partition parameters. The module uses some sectors of flash for internal data.