1# Wear levelling example 2 3(See the README.md file in the upper level 'examples' directory for more information about examples.) 4 5This example demonstrates how to use wear levelling library and FATFS library to store files in a partition inside SPI flash. Example does the following steps: 6 71. Use an "all-in-one" `esp_vfs_fat_spiflash_mount` function to: 8 - find a partition in SPI flash, 9 - initialize wear levelling library using this partition 10 - mount FAT filesystem using FATFS library (and format the filesystem, if the filesystem can not be mounted), 11 - register FAT filesystem in VFS, enabling C standard library and POSIX functions to be used. 122. Create a file using `fopen` and write to it using `fprintf`. 133. Open file for reading, read back the line, and print it to the terminal. 14 15Wear levelling partition size is set in partitions_example.csv file. See [Partition Tables](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/partition-tables.html) documentation for more information. 16 17## How to use example 18 19### Hardware required 20 21This example does not require any special hardware, and can be run on any common development board. 22 23### Build and flash 24 25Build the project and flash it to the board, then run monitor tool to view serial output: 26 27``` 28idf.py -p PORT flash monitor 29``` 30 31(Replace PORT with serial port name.) 32 33(To exit the serial monitor, type ``Ctrl-]``.) 34 35See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. 36 37## Example output 38 39Here is a typical example console output. 40 41``` 42I (280) example: Mounting FAT filesystem 43W (440) vfs_fat_spiflash: f_mount failed (13) 44I (440) vfs_fat_spiflash: Formatting FATFS partition, allocation unit size=4096 45I (660) vfs_fat_spiflash: Mounting again 46I (660) example: Opening file 47I (910) example: File written 48I (910) example: Reading file 49I (920) example: Read from file: 'written using ESP-IDF v3.1-dev-171-gf9ad17eee-dirty' 50I (920) example: Unmounting FAT filesystem 51I (1000) example: Done 52``` 53 54To erase the contents of wear levelling partition, run `idf.py erase-flash` command. Then upload the example again as described above. 55