README.md
1| Supported Targets | ESP32 |
2| ----------------- | ----- |
3
4# FAT FS on External Flash example
5
6(See the README.md file in the upper level 'examples' directory for more information about examples.)
7
8This example is similar to the [wear levelling](../wear_levelling/README.md) example, except that it uses an external SPI Flash chip. This can be useful if you need to add more storage to a module with only 4 MB flash size.
9
10The flow of the example is as follows:
11
121. Initialize the SPI bus and configure the pins. In this example, VSPI peripheral is used. The pins chosen in this example correspond to IOMUX pins for the VSPI peripheral. If the pin assignment is changed, SPI driver will instead connect the peripheral to the pins using the GPIO Matrix.
13
142. Initialize the SPI flash chip. This involves creating a run-time object which describes the flash chip (`esp_flash_t`), probing the flash chip, and configuring it for the selected read mode. By default this example uses DIO mode, which only requires 4 pins (MOSI, MISO, SCLK, CS) but we strongly recommand to connect (or pull-up) the WP and HD pins. For modes such as QIO and QOUT, additional pins (WP/DQ2, HD/DQ3) must be connected.
15
163. Register the entire area of the Flash chip as a *partition* (`esp_partition_t`). This allows other components (FATFS, SPIFFS, NVS, etc) to use the storage provided by the external flash chip.
17
184. Do some read and write operations using C standard library functions: create a file, write to it, open it for reading, print the contents to the console.
19
20## How to use example
21
22### Hardware required
23
24This example needs an SPI NOR Flash chip connected to the ESP32. The SPI Flash chip must have 3.3V logic levels. The example has been tested with Winbond W25Q32 SPI Flash chip.
25
26Use the following pin assignments:
27
28ESP32 pin | SPI bus signal | SPI Flash pin
29--------------|----------------|----------------
30GPIO23 | MOSI | DI
31GPIO19 | MISO | DO
32GPIO18 | SCLK | CLK
33GPIO5 | CS | CMD
34GPIO22 | WP | WP
35GPIO21 | HD | HOLD
36GND | | GND
37VCC | | VCC
38
39### Build and flash
40
41Build the project and flash it to the board, then run monitor tool to view serial output:
42
43```
44idf.py -p PORT flash monitor
45```
46
47(Replace PORT with serial port name.)
48
49(To exit the serial monitor, type ``Ctrl-]``.)
50
51See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
52
53## Example output
54
55Here is a typical example console output.
56
57```
58I (328) example: Initializing external SPI Flash
59I (338) example: Pin assignments:
60I (338) example: MOSI: 23 MISO: 19 SCLK: 18 CS: 5
61I (348) spi_flash: detected chip: generic
62I (348) spi_flash: flash io: dio
63I (348) example: Initialized external Flash, size=4096 KB, ID=0xef4016
64I (358) example: Adding external Flash as a partition, label="storage", size=4096 KB
65I (368) example: Mounting FAT filesystem
66I (378) example: FAT FS: 4024 kB total, 4020 kB free
67I (378) example: Opening file
68I (958) example: File written
69I (958) example: Reading file
70I (958) example: Read from file: 'Written using ESP-IDF v4.0-dev-1301-g0a1160468'
71```
72