1Wear Levelling API
2==================
3
4:link_to_translation:`zh_CN:[中文]`
5
6Overview
7--------
8Most 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.
9
10The 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>`.
11
12The 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.
13
14To save internal memory, the component has two additional modes which both use sectors of 512 bytes:
15
16- **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.
17- **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.
18
19The default settings are as follows:
20- Sector size is 512 bytes
21- Performance mode
22
23You can change the settings through the configuration menu.
24
25
26The 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.
27
28
29Wear Levelling access API functions
30-----------------------------------
31
32This is the set of API functions for working with data in flash:
33
34- ``wl_mount`` - initializes the wear levelling module and mounts the specified partition
35- ``wl_unmount`` - unmounts the partition and deinitializes the wear levelling module
36- ``wl_erase_range`` - erases a range of addresses in flash
37- ``wl_write`` - writes data to a partition
38- ``wl_read`` - reads data from a partition
39- ``wl_size`` - returns the size of available memory in bytes
40- ``wl_sector_size`` - returns the size of one sector
41
42As a rule, try to avoid using raw wear levelling functions and use filesystem-specific functions instead.
43
44
45Memory Size
46-----------
47
48The memory size is calculated in the wear levelling module based on partition parameters. The module uses some sectors of flash for internal data.
49
50