{"id":6807,"date":"2020-10-13T13:56:18","date_gmt":"2020-10-13T20:56:18","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=6807"},"modified":"2025-09-09T19:27:54","modified_gmt":"2025-09-10T02:27:54","slug":"using-spiffs-partitions-with-esp32-projects","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/esp32\/spiffs\/","title":{"rendered":"Using SPIFFS Partitions with ESP32 Projects"},"content":{"rendered":"<p>This tutorial shows how to manage SPIFFS FLASH partitions for ESP32 projects built with the ESP-IDF framework. SPIFFS partitions can contain arbitrary files (i.e. resources) used by the application. You can use them to store web pages, images, configuration files, or any other information.<\/p>\n<p>In this tutorial we will create a basic &#8220;blinking LED&#8221; project and change it to read the blinking script from a text file in an SPIFFS partition. We will then show how to modify the program behavior by changing the SPIFFS partition contents without modifying the program itself.<\/p>\n<p>Before you begin, install VisualGDB 5.5 or later.<\/p>\n<ol>\n<li>Start Visual Studio and locate the VisualGDB ESP-IDF Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-newprj-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6808\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/01-newprj-1.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Enter the name and location of the project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-prjname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6809\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/02-prjname.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Proceed wit the default project type (new application using CMake): <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-cmake-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6810\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/03-cmake-3.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>On the Toolchain selection page pick your ESP32 toolchain and the ESP-IDF checkout. If you are using the Custom edition of VisualGDB, you can configure it to automatically open a terminal at a certain COM port when debugging the program. If not, you can view the COM port output using <a href=\"https:\/\/sysprogs.com\/SmarTTY\/\">SmarTTY<\/a>: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6811\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/04-target.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><strong>Update:<\/strong> For better compatibility with the latest ESP32 tools, we recommend selecting the <a href=\"https:\/\/visualgdb.com\/documentation\/espidf\/consolidated\/\">consolidated toolchain<\/a> instead.<\/li>\n<li>Pick a sample you would like to clone for the project. Although ESP-IDF includes the <strong>spiffsgen<\/strong> sample demonstrating SPIFFS, we will instead pick the <strong>blink<\/strong> sample and show how to add SPIFFS-specific functionality manually:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-blink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6812\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/05-blink.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Connect your development board to the USB port and make sure VisualGDB detects the debugging settings. Click the &#8220;Test&#8221; button to verify them: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-debug-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6813\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/06-debug-2.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a> Once the settings have been verified, click &#8220;Finish&#8221; to generate the project.<\/li>\n<li>VisualGDB will create a basic &#8220;blinking LED&#8221; project containing the default partition table (with no SPIFFS partitions). Locate the partition table in Solution Explorer, right-click on it and select &#8220;Add New SPIFFS Partition&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-addpart.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6814\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/07-addpart.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Proceed with switching the project to a custom partition table layout:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-table.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6815\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/08-table.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>VisualGDB will copy the default layout file (<strong>partitions.csv<\/strong>) to the project directory and will configure the project to use it. You can change this setting later via VisualGDB Project Properties:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/table.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6824\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/table.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Proceed with the default name and size of the partition: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-params.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6816\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/09-params.png\" alt=\"\" width=\"786\" height=\"221\" \/><\/a>Note that the partition contents will be stored in the <strong>&lt;Project Directory&gt;\\spiffs_image<\/strong> folder. Each time you build the project, the SPIFFS image will be rebuilt based on that directory contents, and will be programmed into the FLASH memory together with your project.<\/li>\n<li>VisualGDB will automatically edit the <strong>partitions.csv<\/strong> file for you, adding the new partition. Now you can begin adding files to it. Right-click on the partition and select &#8220;<strong>Add-&gt;New Item<\/strong>&#8220;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-add.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6817\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/10-add.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Enter &#8220;program.txt&#8221; as the file name:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/program.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6825\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/program.png\" alt=\"\" width=\"366\" height=\"142\" \/><\/a>You can also add files to SPIFFS partitions via &#8220;<strong>Add-&gt;Existing Item<\/strong>&#8221; or by simply copying them into the <strong>&lt;Project Directory&gt;\\spiffs_image<\/strong> folder.<\/li>\n<li>Enter the following text in program.txt:\n<pre class=\"\">on\r\nwait 100\r\noff\r\nwait 500<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-program.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6818\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/11-program.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Try building the project. You may get an error stating that the created partition doesn&#8217;t fit into the FLASH memory:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-overflow.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6819\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/12-overflow.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>If this happens, reduce the partition size via Solution Explorer. This will have the same effect as manually editing <strong>partitions.csv<\/strong>: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-size.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6820\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/13-size.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>Include the <strong>&lt;esp_spiffs.h&gt;<\/strong> and <strong>&lt;string.h&gt;<\/strong> files from <strong>blink.c<\/strong>. Then, update <strong>app_main()<\/strong> to mount the partition by calling <strong>esp_vfs_spiffs_register()<\/strong>:\n<pre class=\"\">    esp_vfs_spiffs_conf_t conf = {\r\n        .base_path = \"\/spiffs\",\r\n        .partition_label = NULL,\r\n        .max_files = 5,\r\n        .format_if_mount_failed = false};\r\n\r\n    esp_err_t ret = esp_vfs_spiffs_register(&amp;conf);\r\n\r\n    if (ret != ESP_OK)\r\n        asm(\"break 1, 1\");<\/pre>\n<p>This will mount the partition contents (i.e. <strong>program.txt<\/strong>) under the virtual <strong>\/spiffs<\/strong> path that can be used with <strong>fopen()<\/strong>. Replace the blinking loop in <strong>main()<\/strong> with the following code:<\/p>\n<pre class=\"\">    for (;;)\r\n    {\r\n        FILE *f = fopen(\"\/spiffs\/program.txt\", \"r\");\r\n\r\n        if (f == NULL)\r\n            asm(\"break 1, 1\");\r\n\r\n        for (char buffer[256], *line; (line = fgets(buffer, sizeof(buffer), f));)\r\n        {\r\n            for (int i = 0; i &lt; sizeof(buffer); i++)\r\n                if (buffer[i] == '\\r' || buffer[i] == '\\n')\r\n                {\r\n                    buffer[i] = 0;\r\n                    break;\r\n                }\r\n\r\n            if (!strcmp(line, \"on\"))\r\n                gpio_set_level(BLINK_GPIO, 0);\r\n            else if (!strcmp(line, \"off\"))\r\n                gpio_set_level(BLINK_GPIO, 1);\r\n            else if (!strncmp(line, \"wait \", 5))\r\n                vTaskDelay(atoi(line + 5) \/ portTICK_PERIOD_MS);\r\n        }\r\n\r\n        fclose(f);\r\n    }<\/pre>\n<p>This code reads the \/<strong>spiffs\/program.txt<\/strong> file line-by-line and controls the LED based on the commands stored in it.<\/li>\n<li>Set a breakpoint inside the loop and begin debugging the project by pressing F5. VisualGDB will automatically program the FLASH memory and start a debugging session:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-program.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6821\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/14-program.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a><\/li>\n<li>The breakpoint will soon trigger, showing the first line of <strong>program.txt<\/strong> read into <strong>buffer<\/strong>: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6822\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/15-bkpt.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a>Remove the breakpoint and resume debugging. The LED will now blink in shorter impulses with longer pauses.<\/li>\n<li>The SPIFFS partition can be rebuilt and programmed separately from the program itself. Try changing &#8220;wait 100&#8221; to &#8220;wait 1000&#8221; and then build the &#8220;<strong>storage-flash<\/strong>&#8221; target:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-flash.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6823\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2020\/10\/16-flash.png\" alt=\"\" width=\"1193\" height=\"768\" \/><\/a>The ESP-IDF framework will automatically update and program the image, making the LED stay on for longer.<\/li>\n<\/ol>\n<p>You can find the project shown in this tutorial in our <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/tree\/master\/visualgdb\/esp32\/SPIFFSDemo\">GitHub repository<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to manage SPIFFS FLASH partitions for ESP32 projects built with the ESP-IDF framework. SPIFFS partitions can<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[142],"tags":[138,225],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6807"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=6807"}],"version-history":[{"count":2,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6807\/revisions"}],"predecessor-version":[{"id":9035,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/6807\/revisions\/9035"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=6807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=6807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=6807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}