Esp32 - SPIFFS data storage

Post and discuss new components that you have created.
Post Reply
mnfisher
Valued Contributor
Posts: 1453
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Esp32 - SPIFFS data storage

Post by mnfisher »

Another advanced idea for esp32 users.

A useful feature of the esp32 is the ability to use some of the flash memory as a file system. This is accessed using standard 'read' and 'write' commands (amongst others) and data can also be written to he 'disk' space from a PC.

Multiple files can be used - and it ideal for loggers / html pages etc. Data is non-volatile, so is stored between restarts / power cycles etc. Note that directories are not supported.

I wrote a small program that accesses this - here it initialises the SPIFFS data area (and formats it if needed) - and displays the size and used space. Then in this tiny example it writes the text file twi.h to UART. (twi.h was written to the storage from windows)

Anyone any comments - ideas. As it stands it can easily be converted to a component. I think some directory operations (getting the list of files for example) are possible.

I've added several macros - to open, close and read/write strings or data as an array - a natural addition would be macros to read / write a byte of data. Also Seek.

To test.

The esp32 needs to have a SPIFFS partition allocated. In the program directory (in my case r:\spiffs) write the following as partitions.csv

Code: Select all

# Name,   Type, SubType, Offset,  Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs,      data, nvs,     0x9000,  0x6000,
phy_init, data, phy,     0xf000,  0x1000,
factory,  app,  factory, 0x10000, 1M,
storage,  data, spiffs,  ,        0xF0000,
Note - I used a 2MB esp32 = the storage are can be larger on a 4MB chip.

Then point the idf to the new partitions table using idf.py menuconfig (under partition table - set to custom and point Custom csv to your file)

Now when you compile and upload the program it will allocate the data as per partitions.csv

If you upload the program now and watch the UART output - it will format the SPIFFS area.

Creating and uploading the spiffs data area.

Copy all the files to a directory and then use mkspiffs (note I had to find a copy (and there is one in the Arduino IDE directories (but not in the espressif framworks))

Code: Select all

mkspiffs.exe -c . -b 4096 -p 256 -s 0xF0000 spiffs.bin
(See the espressif docs for more detail)

The data is uploaded using esptool

Code: Select all

esptool -p COM4 -b 512000 write_flash 0x110000 spiffs.bin
Again see the espressif docs at https://docs.espressif.com/projects/esp ... piffs.html

The simple program only outputs a text file (files support the usual C file modes "r", "w", "a" and "rb", "wb", "ab"') - this is hardcoded as twi.h - adjust to suit. File writing also works okay.. Please test and report on the other functions...

SPIFFS.fcfx
(29.04 KiB) Downloaded 449 times

Martin

Post Reply