项目作者: loboris

项目描述 :
Full example of using SPIFFS with ESP32 VFS
高级语言: C
项目地址: git://github.com/loboris/ESP32_spiffs_example.git
创建时间: 2017-05-17T15:42:10Z
项目社区:https://github.com/loboris/ESP32_spiffs_example

开源协议:

下载


Example of using SPIFFS with modified esp_idf spiffs VFS driver

esp-idf has support for the spiffs file system, but the directory support is not implemented.

This example uses the modified spiffs VFS driver which enables the directory support.


The original esp-idf spiffs driver is modified in a way that directories are enabled.

To enable the new spiffs driver, spiffs directory from esp-idf components directory is copied to the project’s components directory.

Only the esp_spiffs.c file was modified to enable directory support.

The standard mkdir() and rmdir() functions are added to the spiffs VFS driver.

The modified version of mkspiffs is also provided which supports building spiffs file system image with directories.


Features

  • directory handling enabled
  • example of directory list 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.


How to build

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

Clone the repository

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.

make menuconfig

Make and flash the example.

make all && make flash


Prepare SPIFFS image


Build mkspiffs

Before the image can be prepared, the mkspiffs executable must be created.

The sdkconfig.h is used in build process!

Before building, copy the sdkconfig.h from build/include directory to components/mkspiffs/include.

Change the working directory to components/mkspiffs/include.

Execute:

  1. make clean
  2. make

The simple build_mkspiffs script is provided which executes all the necessary steps automatically, just execute

  1. ./build_mkspiffs

Important: whenever you change spiffs configuration using menuconfig you must rebuild mkspiffs


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.

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

Execute:

make makefs

to create spiffs image in build directory without flashing to ESP32

Execute:

make flashfs

to create spiffs image in build directory and flash it 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 from root to the new directory
    • remove file from new directory
    • remove the new directory
    • list files in root directory and subdirectories
    • perform timing test
    • perform multitask test (if enabled in menuconfig)


Example output:

  1. I (4221) [SPIFFS example]: ==== STARTING SPIFFS TEST ====
  2. =======================
  3. ==== Write to file ====
  4. =======================
  5. file: "/spiffs/test.txt"
  6. 351 bytes written
  7. ========================
  8. ==== Read from file ====
  9. ========================
  10. file: "/spiffs/test.txt"
  11. 351 bytes read [
  12. ESP32 spiffs write to file, line 1
  13. ESP32 spiffs write to file, line 2
  14. ESP32 spiffs write to file, line 3
  15. ESP32 spiffs write to file, line 4
  16. ESP32 spiffs write to file, line 5
  17. ESP32 spiffs write to file, line 6
  18. ESP32 spiffs write to file, line 7
  19. ESP32 spiffs write to file, line 8
  20. ESP32 spiffs write to file, line 9
  21. ESP32 spiffs write to file, line 10
  22. ]
  23. =================================================
  24. ==== Read from file included in sfiffs image ====
  25. =================================================
  26. file: "/spiffs/spiffs.info"
  27. 405 bytes read [
  28. INTRODUCTION
  29. Spiffs is a file system intended for SPI NOR flash devices on embedded targets.
  30. Spiffs is designed with following characteristics in mind:
  31. * Small (embedded) targets, sparse RAM without heap
  32. * Only big areas of data (blocks) can be erased
  33. * An erase will reset all bits in block to ones
  34. * Writing pulls one to zeroes
  35. * Zeroes can only be pulled to ones by erase
  36. * Wear leveling
  37. ]
  38. =============================
  39. ==== List root directory ====
  40. =============================
  41. List of Directory [/spiffs/]
  42. -----------------------------------
  43. T Size Date/Time Name
  44. -----------------------------------
  45. d - 07/06/2018 10:59 images
  46. f 405 07/06/2018 10:59 spiffs.info
  47. f 351 07/06/2018 10:59 test.txt
  48. -----------------------------------
  49. 756 in 2 file(s)
  50. -----------------------------------
  51. SPIFFS: free 758 KB of 934 KB
  52. -----------------------------------
  53. ============================
  54. ==== Make new directory ====
  55. ============================
  56. dir: "/spiffs/newdir"
  57. Directory created
  58. Copy file from root to new directory...
  59. List the new directory
  60. List of Directory [/spiffs/newdir]
  61. -----------------------------------
  62. T Size Date/Time Name
  63. -----------------------------------
  64. f 351 07/06/2018 10:59 test.txt.copy
  65. -----------------------------------
  66. 351 in 1 file(s)
  67. -----------------------------------
  68. SPIFFS: free 757 KB of 934 KB
  69. -----------------------------------
  70. List root directory, the "newdir" should be listed
  71. List of Directory [/spiffs/]
  72. -----------------------------------
  73. T Size Date/Time Name
  74. -----------------------------------
  75. d - 07/06/2018 10:59 images
  76. f 405 07/06/2018 10:59 spiffs.info
  77. f 351 07/06/2018 10:59 test.txt
  78. d - 07/06/2018 10:59 newdir
  79. -----------------------------------
  80. 756 in 2 file(s)
  81. -----------------------------------
  82. SPIFFS: free 757 KB of 934 KB
  83. -----------------------------------
  84. Try to remove non empty directory...
  85. Error removing directory (90) Directory not empty
  86. Removing file from new directory...
  87. Removing directory...
  88. List root directory, the "newdir" should be gone
  89. List of Directory [/spiffs/]
  90. -----------------------------------
  91. T Size Date/Time Name
  92. -----------------------------------
  93. d - 07/06/2018 10:59 images
  94. f 405 07/06/2018 10:59 spiffs.info
  95. f 351 07/06/2018 10:59 test.txt
  96. -----------------------------------
  97. 756 in 2 file(s)
  98. -----------------------------------
  99. SPIFFS: free 758 KB of 934 KB
  100. -----------------------------------
  101. ================================================
  102. ==== List content of the directory "images" ====
  103. ==== which is included on spiffs image ====
  104. ================================================
  105. List of Directory [/spiffs/images]
  106. -----------------------------------
  107. T Size Date/Time Name
  108. -----------------------------------
  109. d - 07/06/2018 10:59 test
  110. f 39310 07/06/2018 10:59 test1.jpg
  111. f 50538 07/06/2018 10:59 test2.jpg
  112. f 47438 07/06/2018 10:59 test4.jpg
  113. -----------------------------------
  114. 137286 in 3 file(s)
  115. -----------------------------------
  116. SPIFFS: free 758 KB of 934 KB
  117. -----------------------------------
  118. ==============================================================
  119. ==== Get the timings of spiffs operations
  120. ==== Operation:
  121. ==== Open file for writting, append 1 byte, close file
  122. ==== Open file for readinging, read 8 bytes, close file
  123. ==== 2 ms sleep between operations
  124. ==== 1000 operations will be executed
  125. ==============================================================
  126. I (9028) Test: Started.
  127. I (9498) Test: 100 reads/writes
  128. I (9918) Test: 200 reads/writes
  129. I (10346) Test: 300 reads/writes
  130. I (10779) Test: 400 reads/writes
  131. I (11209) Test: 500 reads/writes
  132. I (11645) Test: 600 reads/writes
  133. I (12079) Test: 700 reads/writes
  134. I (12511) Test: 800 reads/writes
  135. I (12945) Test: 900 reads/writes
  136. I (13376) Test: 1000 reads/writes
  137. W (13376) Test: Min write time: 2523 us
  138. W (13376) Test: Max write time: 2987 us
  139. W (13377) Test: Min read time: 327 us
  140. W (13382) Test: Max read time: 462 us
  141. W (13386) Test: Total run time: 4348 ms
  142. I (13391) Test: Finished
  143. List of Directory [/spiffs/]
  144. -----------------------------------
  145. T Size Date/Time Name
  146. -----------------------------------
  147. d - 07/06/2018 10:59 images
  148. f 405 07/06/2018 10:59 spiffs.info
  149. f 351 07/06/2018 10:59 test.txt
  150. f 1001 07/06/2018 10:59 testfil3.txt
  151. -----------------------------------
  152. 1757 in 3 file(s)
  153. -----------------------------------
  154. SPIFFS: free 756 KB of 934 KB
  155. -----------------------------------
  156. ====================================================
  157. STARTING MULTITASK TEST (3 tasks created)
  158. Operation:
  159. Open file for writting, append 1 byte, close file
  160. Open file for readinging, read 8 bytes, close file
  161. 2 ms sleep between operations
  162. Each task will perform 1000 operations
  163. Expected run time 40~100 seconds
  164. ====================================================
  165. I (14970) [TASK_1]: Started.
  166. I (15170) [TASK_2]: Started.
  167. I (15350) [TASK_1]: 100 reads/writes
  168. I (15370) [TASK_3]: Started.
  169. I (15840) [TASK_2]: 100 reads/writes
  170. I (16220) [TASK_1]: 200 reads/writes
  171. I (16340) [TASK_3]: 100 reads/writes
  172. I (16722) [TASK_2]: 200 reads/writes
  173. I (17149) [TASK_1]: 300 reads/writes
  174. I (17295) [TASK_3]: 200 reads/writes
  175. I (17644) [TASK_2]: 300 reads/writes
  176. I (18055) [TASK_1]: 400 reads/writes
  177. I (18295) [TASK_3]: 300 reads/writes
  178. I (18573) [TASK_2]: 400 reads/writes
  179. I (18999) [TASK_1]: 500 reads/writes
  180. I (19308) [TASK_3]: 400 reads/writes
  181. I (20023) [TASK_2]: 500 reads/writes
  182. I (21294) [TASK_1]: 600 reads/writes
  183. I (21900) [TASK_3]: 500 reads/writes
  184. I (22153) [TASK_2]: 600 reads/writes
  185. I (22577) [TASK_1]: 700 reads/writes
  186. I (22829) [TASK_3]: 600 reads/writes
  187. I (23086) [TASK_2]: 700 reads/writes
  188. I (23541) [TASK_1]: 800 reads/writes
  189. I (23731) [TASK_3]: 700 reads/writes
  190. I (24002) [TASK_2]: 800 reads/writes
  191. I (24417) [TASK_1]: 900 reads/writes
  192. I (24699) [TASK_3]: 800 reads/writes
  193. I (24932) [TASK_2]: 900 reads/writes
  194. I (25308) [TASK_1]: 1000 reads/writes
  195. I (25308) [TASK_1]: Finished
  196. I (25669) [TASK_3]: 900 reads/writes
  197. I (26083) [TASK_2]: 1000 reads/writes
  198. I (26083) [TASK_2]: Finished
  199. I (26875) [TASK_3]: 1000 reads/writes
  200. W (26875) [TASK_3]: Min write time: 2553 us
  201. W (26875) [TASK_3]: Max write time: 154609 us
  202. W (26878) [TASK_3]: Min read time: 337 us
  203. W (26883) [TASK_3]: Max read time: 144128 us
  204. W (26888) [TASK_3]: Total run time: 57729 ms
  205. I (26893) [TASK_3]: Finished
  206. List of Directory [/spiffs/]
  207. -----------------------------------
  208. T Size Date/Time Name
  209. -----------------------------------
  210. f 1001 07/06/2018 11:00 testfil2.txt
  211. f 405 07/06/2018 10:59 spiffs.info
  212. f 1001 07/06/2018 11:00 testfil3.txt
  213. d - 07/06/2018 10:59 images
  214. f 1001 07/06/2018 11:00 testfil1.txt
  215. f 351 07/06/2018 10:59 test.txt
  216. -----------------------------------
  217. 3759 in 5 file(s)
  218. -----------------------------------
  219. SPIFFS: free 754 KB of 934 KB
  220. -----------------------------------
  221. Total multitask test run time: 60 seconds
  222. I (28870) [SPIFFS example]: ==== SPIFFS TEST FINISHED ====