Full example of using spiffs with ESP32

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Full example of using spiffs with ESP32

Postby loboris » Wed May 17, 2017 4:05 pm

The example source code can be found on GitHub:
https://github.com/loboris/ESP32_spiffs_example

How to build

Configure your esp32 build environment as for other esp-idf examples
Clone the repository

Code: Select all

git clone https://github.com/loboris/ESP32_spiffs_example.git
Execute menuconfig and configure your Serial flash config and other settings. Included sdkconfig.defaults sets some defaults to be used.
Navigate to SPIffs Example Configuration and set SPIFFS options.
Select if you want to use wifi (recommended) to get the time from NTP server and set your WiFi SSID and password.

Code: Select all

make menuconfig
Make and flash the example.

Code: Select all

make all && make flash
Features
  • full implementation of spiffs with VFS
  • directories are supported
  • file timestamp is added to standard spiffs
  • example of list directory functions
  • example of file copy functions
When using file related functions which has filename argument, prefix /spiffs/ has to be added to the file name.

Prepare SPIFFS image

It is not required to prepare the image, as the spiffs will be automatically formated on first use, but it might be convenient.

SFPIFFS image can be prepared on host and flashed to ESP32.
New: Tested and works under Linux, Windows and Mac OS.

Copy the files to be included on spiffs into components/spiffs_image/image/ directory. Subdirectories can also be added.

Execute:

Code: Select all

make makefs
to create spiffs image in build directory without flashing to ESP32

Execute:

Code: Select all

make flashfs
to create spiffs image in *build* directory and flash it to ESP32

Execute:

Code: Select all

make copyfs
to flash prepared components/spiffs_image/spiffs_image.img to ESP32

Example functions
  • get the time from NTP server and set the system time (if WiFi is enabled)
  • register spiffs as VFS file system; if the fs is not formated (1st start) it will be formated and mounted
  • perform some file system tests
    • write text to file
    • read the file back
    • make directory
    • copy file
    • remove file
    • remove directory
    • list files in root directory and subdirectories


Example output

Code: Select all

I (1185) cpu_start: Pro cpu start user code
I (1240) cpu_start: Starting scheduler on PRO CPU.
I (1242) cpu_start: Starting scheduler on APP CPU.
I (1242) [SPIFFS example]: Time is not set yet. Connecting to WiFi and getting time over NTP.
I (1271) wifi: wifi firmware version: 6c86a1c
I (1271) wifi: config NVS flash: enabled
I (1271) wifi: config nano formating: disabled
I (1272) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (1281) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (1313) wifi: Init dynamic tx buffer num: 32
I (1313) wifi: Init dynamic rx buffer num: 32
I (1314) wifi: wifi driver task: 3ffc2324, prio:23, stack:4096
I (1316) wifi: Init static rx buffer num: 16
I (1320) wifi: Init dynamic rx buffer num: 32
I (1324) wifi: Init rx ampdu len mblock:7
I (1328) wifi: Init lldesc rx ampdu entry mblock:4
I (1332) wifi: wifi power manager task: 0x3ffc9cbc prio: 21 stack: 2560
I (1339) [SPIFFS example]: Setting WiFi configuration SSID LoBoInternet...
I (1346) wifi: wifi timer task: 3ffcad44, prio:22, stack:3584
I (1371) phy: phy_version: 350, Mar 22 2017, 15:02:06, 1, 0
I (1371) wifi: mode : sta (24:0a:c4:00:97:c0)
I (1492) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:1
I (2150) wifi: state: init -> auth (b0)
I (2152) wifi: state: auth -> assoc (0)
I (2155) wifi: state: assoc -> run (10)
I (2189) wifi: connected with LoBoInternet, channel 1
I (4252) event: ip: 192.168.0.16, mask: 255.255.255.0, gw: 192.168.0.1
I (4252) [SPIFFS example]: Initializing SNTP
I (4253) [SPIFFS example]: Waiting for system time to be set... (1/10)
I (6260) [SPIFFS example]: System time is set.
I (6260) wifi: state: run -> init (0)
I (6260) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:1
E (6262) wifi: esp_wifi_connect 816 wifi not start


I (7262) [SPIFFS example]: ==== STARTING SPIFFS TEST ====

I (7262) [SPIFFS]: Registering SPIFFS file system
I (7263) [SPIFFS]: Mounting SPIFFS files ystem
I (7268) [SPIFFS]: Start address: 0x180000; Size 1024 KB
I (7311) [SPIFFS]: Mounted


==== Write to file "/spiffs/test.txt" ====
     361 bytes written

==== Reading from file "/spiffs/test.txt" ====
     361 bytes read [
ESP32 spiffs write to file, line 1
ESP32 spiffs write to file, line 2
ESP32 spiffs write to file, line 3
ESP32 spiffs write to file, line 4
ESP32 spiffs write to file, line 5
ESP32 spiffs write to file, line 6
ESP32 spiffs write to file, line 7
ESP32 spiffs write to file, line 8
ESP32 spiffs write to file, line 9
ESP32 spiffs write to file, line 10

]

==== Reading from file "/spiffs/spiffs.info" ====
     405 bytes read [
INTRODUCTION

Spiffs is a file system intended for SPI NOR flash devices on embedded targets.
Spiffs is designed with following characteristics in mind:

  * Small (embedded) targets, sparse RAM without heap
  * Only big areas of data (blocks) can be erased
  * An erase will reset all bits in block to ones
  * Writing pulls one to zeroes
  * Zeroes can only be pulled to ones by erase
  * Wear leveling

]

LIST of DIR [/spiffs/]
T  Size      Date/Time         Name
-----------------------------------
d         -                    /spiffs
d         -  17/05/2017 13:00  images
f      8532  17/05/2017 13:00  testSpiffs.c
f       405  17/05/2017 13:00  spiffs.info
f       361  17/05/2017 15:05  test.txt
-----------------------------------
       9298 in 3 file(s)
-----------------------------------
SPIFFS: free 772 KB of 957 KB

==== Make new directory "/spiffs/newdir" ====
     Directory created

LIST of DIR [/spiffs/]
T  Size      Date/Time         Name
-----------------------------------
d         -                    /spiffs
d         -  17/05/2017 13:00  images
f      8532  17/05/2017 13:00  testSpiffs.c
f       405  17/05/2017 13:00  spiffs.info
f       361  17/05/2017 15:05  test.txt
d         -  17/05/2017 15:05  newdir
-----------------------------------
       9298 in 3 file(s)
-----------------------------------
SPIFFS: free 772 KB of 957 KB
     Copy file from root to new directory...

LIST of DIR [/spiffs/newdir]
T  Size      Date/Time         Name
-----------------------------------
f       361  17/05/2017 15:05  test.txt.copy
-----------------------------------
        361 in 1 file(s)
-----------------------------------
SPIFFS: free 771 KB of 957 KB
     Removing file from new directory...

LIST of DIR [/spiffs/newdir]
T  Size      Date/Time         Name
-----------------------------------
-----------------------------------
SPIFFS: free 772 KB of 957 KB
     Removing directory...

LIST of DIR [/spiffs/]
T  Size      Date/Time         Name
-----------------------------------
d         -                    /spiffs
d         -  17/05/2017 13:00  images
f      8532  17/05/2017 13:00  testSpiffs.c
f       405  17/05/2017 13:00  spiffs.info
f       361  17/05/2017 15:05  test.txt
-----------------------------------
       9298 in 3 file(s)
-----------------------------------
SPIFFS: free 772 KB of 957 KB

==== List content of the directory "images" ====

LIST of DIR [/spiffs/images]
T  Size      Date/Time         Name
-----------------------------------
f     39310  17/05/2017 13:00  test1.jpg
f     50538  17/05/2017 13:00  test2.jpg
f     38460  17/05/2017 13:00  test3.jpg
f     47438  17/05/2017 13:00  test4.jpg
-----------------------------------
     175746 in 4 file(s)
-----------------------------------
SPIFFS: free 772 KB of 957 KB

Last edited by loboris on Mon Jun 12, 2017 9:30 am, edited 4 times in total.

jgfrmesp
Posts: 6
Joined: Mon May 15, 2017 11:06 pm

Re: Full example of using spiffs with ESP3232

Postby jgfrmesp » Thu May 18, 2017 3:08 pm

This works perfectly.

Thanks for sharing!

-- Jaap

xxRizinxx
Posts: 3
Joined: Tue Apr 25, 2017 8:37 pm

Re: Full example of using spiffs with ESP32

Postby xxRizinxx » Sun May 28, 2017 3:00 pm

Nice!!!
Works fine, but if I Upload the Image he could not find the Filesystem. After formating no files are available.
After reupload the Same issure.

Please help me

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: Full example of using spiffs with ESP32

Postby loboris » Mon May 29, 2017 11:32 pm

xxRizinxx wrote:Nice!!!
Works fine, but if I Upload the Image he could not find the Filesystem. After formating no files are available.
After reupload the Same issure.

Please help me
Some small updates were made to the example. Please pull the changes, run make menuconfig, make clean, make all, make flash to try again.
Already prepared image spiffs_image.img is added to components/spiffs_image directory.

You can use the new make command copyfs to flash it to ESP32:

Code: Select all

make copyfs
I've never had any problem flashing and using prepared spiffs image. Are you building on Linux?
Please, try to flash the included image and check if it works (it should if you haven't changed spiffs parameters).

xxRizinxx
Posts: 3
Joined: Tue Apr 25, 2017 8:37 pm

Re: Full example of using spiffs with ESP32

Postby xxRizinxx » Tue May 30, 2017 1:44 pm

Now it works!! Thank you very much.

I build the Image on Windows with mkspiffs.exe with the Same Parameter. But every time the same problem. So in future i will build the Image on Linux. Or exist a solution for Windows?

onehorse
Posts: 70
Joined: Mon Feb 15, 2016 1:35 am

Re: Full example of using spiffs with ESP32

Postby onehorse » Tue May 30, 2017 5:05 pm

Any idea when we might be able to use SPIFFS with the Arduino IDE?

cliftonk
Posts: 9
Joined: Sat Jun 10, 2017 8:43 pm

Re: Full example of using spiffs with ESP32

Postby cliftonk » Sat Jun 10, 2017 8:55 pm

Hello,

Thanks very much.
This is great.
Everything works. I can flash and test successfully.
However, one thing doesn't work.
I cannot build the "mkspiffs" utility under Windows environment.
Any suggestions would be appreciated.
I get the following errors:

Under windows, I get the following errors when I try to build the "mkspiffs" utility.

MINGW32 ~/esp/samples/esp32_spiffs_example
$ make makefs
Building mkspiffs ...
xtensa-esp32-elf-gcc.exe: error: unrecognized command line option '-mno-ms-bitfields'
make[1]: *** [Makefile:58: mkspiffs.exe] Error 1
make: *** [/home/Clifton/esp/samples/esp32_spiffs_example/components/mkspiffs/Makefile.projbuild:10: mkspiffs] Error 2

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: Full example of using spiffs with ESP32

Postby loboris » Sun Jun 11, 2017 10:48 am

I haven't try it under Windows.
mkspiffs has to be built with gcc for Windows, not with xtensa-esp32-elf-gcc.exe (it must build mkspiffs.exe).
You can try to build it directy, goto components/mkspiffs/src/ directory and execute make without PATH set to xtensa-esp32-elf/bin/.
You can also try to modify the Makefile (ifeq ($(OS),Windows_NT) section) and set CC & CXX variables to point Windows compilers (in /msys32/usr/bin/), like it is set in Linux section.
I'll try to test it, but I have no free time at the moment.
Edit: I've made some changes and now it works. Pull the changes from repository.
Last edited by loboris on Sun Jun 11, 2017 1:18 pm, edited 1 time in total.

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: Full example of using spiffs with ESP32

Postby loboris » Sun Jun 11, 2017 1:13 pm

UPDATE

I've made some changes to enable building mkspifs.exe under Windows.
Tested under Windows Server 2012R2 (should work under any Windows version) and works without any problem.

Also tested and works under Mac OS.

cliftonk
Posts: 9
Joined: Sat Jun 10, 2017 8:43 pm

Re: Full example of using spiffs with ESP32

Postby cliftonk » Sat Jun 17, 2017 7:18 pm

Tried it and it works great under Windows 7.

Thanks...

Who is online

Users browsing this forum: Google [Bot] and 24 guests