Introduction
If you are new to embedded microcontroller programming using the ESP8266 or ESP32 board then you often might hear about SPIFFS and LittleFS filesystem. What it is and how does it really work and how can we take advantage of its features in our Internet of Things (IOT) projects? Let us try to explore what these are in this ESP8266 LittleFS Tutorial series of mine. Our focus is on LittleFS but the content here is applicable as well to SPIFFS.
The following are the parts of this ESP8266 LittleFS Tutorial series:
- Part 1 – ESP8266 LittleFS Tutorial Series – Overview
- Part 2 – ESP8266 LittleFS Tutorial Series – Using LittleFS to serve web content in ESP8266 Webserver
- Part 3 – ESP8266 LittleFS Tutorial Series – Using LittleFS to save configurations in ESP8266
We will start with an overview of what LittleFS Filesystem is in this post so that succeeding posts will just focus on the use of LittleFS. Let’s get started!
What types of memory are in our Microcontroller?
Microcontrollers (MCU) generally contain two types of memory which can be subdivided as either a Random Access Memory (RAM) or a Read Only Memory (ROM).
The primary distinction between the two is their volatility. RAM is volatile and data is lost when power is cut off. ROM on the other hand retains its data even if the power supply is cut off.
Our sketches and MCU firmware data are stored in ROM including the Arduino compiled sketches program data that we upload. When our Arduino program is run then all runtime data, variables, and function pointers are stored in the RAM for faster access.
What is the ESP8266 Flash Memory?
One major characteristic of the ESP8266/ESP32 MCU boards is the size (512 KiB to 4 MiB typically) of their Flash Memory. Flash memory is a type of ROM and they vary for each ESP8266/ESP32 model.
If you have programmed an Arduino board like the Uno before then you might be familiar with the EEPROM (Electrically Erasable Programmable Read-only Memory). We used this in situations where we need to store some value that we need even if our Arduino restarts. Their difference is how they are managed as EEPROM is at the byte level while Flash Memory is at the block level. So it is easier to use Flash memory for saving large contents of data.
Looking at the size of this Flash memory will give you an idea of how much data we can store in it.
What data are stored in the ESP8266 Flash?
The image above is the Flash layout when using the Arduino core framework in ESP8266. As you can see, the Flash layout is subdivided into several categories. It has spaces allotted for your sketches, OTA updates, and Filesystem. There are others as well but we won’t be discussing them much.
Even if you upload your sketches multiple times then it won’t overwrite the contents of the Filesystem. So what really is a Filesystem?
The size of each allocation differs depending on the size of the Flash Chip so the value is not fix.
What is Filesystem in Microcontrollers?
I would like to think about Filesystem in MCU as to how we interact with your folders and files in your Windows Operating System. In it, we can store files that our Microcontroller needs for example:
- The last Weather data that we retrieve from the OpenWeatherMap
- The web content files of our Webserver such as the HTML/Javascript/CSS files
- The configurations of our project like the last count of our timer, the position of our servo, or the angle of the stepper motors.
All these things have something in common. They are there stored inside the Microcontroller Filesystem so that even if our MCU restarts or reboots then we can just re-read them again.
Types of Filesystems
There are two types of Filesystems used in the ESP8266/ESP32 board to allow you to manipulate its content. The SPIFFS is the original while the LittleFS is the newer of the two. Please see the table below for comparison.
Features | SPIFFS | LittleFS |
---|---|---|
Directory Support | No | Yes |
Performance/Speed | Slow | Fast |
File Overhead | 256 bytes | 4K |
File Name Size | 32 Chars | 32 Chars |
For ESP8266, developers are advised to use LittleFS as support for SPIFFS might be removed soon.
For ESP32, the majority are still using the SPIFFS but LittleFS support is coming through. We will explore this in a future post.
LittleFS and SPIFFS API Call Comparison
If you have been using SPIFFS and want to transition to LittleFS then it won’t be that hard as both Filesystems support a common API. You will just need to change your header file into:
#include "SPIFFS.h" // comment this line
#include <LittleFS.h>
There are some subtle API differences between the two which you can take a look at in the description of the Filesystem API like the openDir()
method because SPIFFS doesn’t really support directories.
I often see the following question(s) in forums wherein “How do I write to the Flash Memory of my ESP8266/ESP32?”. Maybe now that you know what Filesystems are then you should say that you should use either the SPIFSS or the LittleFS API. Cool!
Wrap Up
We have taken a brief overview of what Flash Memory, Filesystem, and LittleFS are in this post. In the next two posts of this ESP8266 LittleFS Tutorial, we will be diving more into how to take advantage of this feature.
Stay Tuned! Happy Exploring!
Leave a Reply