ESP32 - FILESYSTEMS with ESP-IDF // How to use Files and Folders

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there have you ever wanted to create and lose files inside of USB 32 a file system is probably what you're looking for let me show how to create one hi guys I know I know it's been a while since I don't post any videos here trust me this last year was quite busy for me but now I'm back on YouTube Bringing more videos and projects for you guys I hope you like this video and stay tuned hey good to see you again I'm yuriar and this is my fourth video of my espriter 2 Series and today we are going to work with file system inside of the asp32 using idea by the way I want to let you guys know that I have created that mainly list where I'm going to share tips and tricks related to Firmware programming announcements and even exclusive videos so if you don't want to miss it make sure to sign up I will leave the link here and in the description below if you ever wondered how to store files such as images audio log files for sensors or even safety state of your program after a power down your esp32 this video is for you are we sure how to set up a file system example your mic controller and explain how the memory and partition table work having the ability to read and write files is extremely useful for embedded systems it gives us great flexibility to work with data while allowing us to create read and exchange files between computers and mic controllers for example we can create simple database to keep track of events save historical data for sensors or display image and play audio files through the speaker your imagination is the limit so what exactly is memory if you look at the esp32 we can see that it has flash memory and run memory but what's the difference between those two basically flash is a non-volatile type of memory meaning that it can hold its contents even when there is no power and that's why its main application is usually storage image audio or anything that needs to be permanently saved are commonly stored in the flash an SD card for example is just a big removable flash memory it's also important to know that your program's code is normally stored in the flash as well and it's read by the processor while executing it also flash memory has a limited amount of writing Cycles allowed before it stops working so keep that in mind when creating your application to not ride too often to it now Ram is a short-term volatile memory meaning that it lose its content when there is no power and it's used for everything that's temporary the big advantage of RAM is that usually it's way faster to access compared to flash memory when your mic controller starts it reads your code from the flash memory and starts loading data such as variable values and other temporary things to the ram this gives the mic control the ability to have a high speed space to work with this data the esp32 usually comes with around 500 kilobytes of RAM and 4 megabytes of external flash other microcontrollers such as the stm32 for example comes with the flash internally which is an advantage when you need to run your code faster and work with the high performance applications because it's not limited by the SPI Buzz speed the stm32 is a great Mac controller and nowadays one of the most used professionally and I will start making videos about it soon if you are think of working as a professional firmware developer you'll definitely want to check this out but let's focus on the esp32 for today now before we get started with the example it's very important to know how the ESPN2 flash memory works here's a diagram that shows how the flash memory in the esp32 is generally divided the memory is mapped using numeric addresses that go from zero to whatever the total memory size is the definition of all these addresses and revisions is stored in this memory space called partition table the numbers are usually written using a hexadecimal base there are many different ways that we can divide the esp32's memory the idap comes with two default partition tables single Factor app no OTA and the factory app two OTA definitions let's focus on the first one today bootloader is a space dedicated for the bootloader code nvs is a spaceted head to store important values that can be encrypted such as Wi-Fi SSD and password find it is a space used by the esp32 to restore RF calibration data Factory is the memory space dedicated to our main firmware application and finally what we are going to do today is add another partition the end using this free space for our file system as a storage partition all of these partitions their size and structure can be customized according to our projects so let's start by creating a simple project by typing idaf dot Pi create project Dash p Dot and the name of the project so if you don't know how to set up your Visual Studio code and create a simple IDF project I have done some videos that you can check out here all right now the first thing that you have to do is Define our partition structure in our memory we can easily do this by using the menu config in the IDF terminal type start IDF dot Pi menuconfig or click on the button that we have created in a previous video first we have to set the correct flash size available in our mic controller by selecting serial flasher config and then flash size just select the right type for your module in my case I have 4 megabytes now let's jump back to the main menu and select partition table select the first option here there are a few options with predefined partition table templates you can try them but in this example we are going to create a custom partition table so let's select custom partition table cxv exit and save the configuration this tells IDF that you are going to provide a CSV file with a table structure for each use now let's create this file click with the right button and select new files name it partition.csv here is where we Define the partition's size and offset the default IDF flash memory structure defines three partitions nvs Pi init and factor which is our main application you can change this structure as you like but in this video we are going to use the default settings in offset to keep it simple now let's add our storage partitions according to the documentation this file needs to specify the following field name types of type of set size and flag name is our partition name let's use storage the type of our partition is going to be data the subtype is spiffs spiff stands for SPI flash file system which is a lightweight file system created for microcontrollers with an SPI flash chip offset is a starting memory address where the partition is going to be written in our case we will use one one zero zero zero zero size is the total size of our partition I will use one zero zero zero zero zero which is one megabyte and flag to specify if it's going to be encrypted or not let's skip this empty by the way if you leave this empty it will get the previous offset and the size and calculate the next offset for you but I prefer to add this manually just to be safe now let's include some files together with our fumer this is useful when you need to add a default configuration file image or anything else that your firmware would need let's try doing that by creating a folder to include let's call it partition and create an example text file to include in the film as well for example myfile.txt let's write something here like and Save now according to the documentation in order to create a spiffs partition we need to include this command inside the cmake list.txt file we need to open the cmake list.txt in your main folder and add a new command there let's write spiffs create partition image storage the name of the folder you want to send to the esp32 in our case dot dot slash partition and Flash in Project alright let's build it [Music] when the compilation is done you'll notice that the commands of flash our microcontroller now includes our storage partition as well that's great now when you load our film we have all the partitions included alright let's get our hands on the code and start using the file system let's open the documentation to find out what we need to do on the idef page click on API reference storage API and then spiffs file system like we did in my last video let's first find out what header file is needed in order to work with the IDF space interface here we can see that we have to start by adding the header espif.h [Music] soak these red squiggles hover the mouse over the header and click on the light bulb and select edit include path settings open the dot vs code folder and then the properties.json file and right here your IDF path followed by double star and Save the first thing that we have to do is register our space partition in this function ESP VFS speeds register we can see that it needs to be configured using a configuration structure which is described down here let's define this structure let's start by typing ESP VFS spacecraft T and the variable name config equals and then dot base path which is our partitions base path used to access our files later in this case slash storage partition label is used to indicate which partition we are going to use in case we have multiple ones since you have just one we can set this to no Max files which is the max number of simultaneously opened files let's use 5. and formative mouth failed which in case our partition face to mouth the esp32 will format and fix it for us in our case let's use true be careful with this option just to not lose any important information now let's use the register command and pass this config structure to it I added this variable here so we can check the result of that command and print using the ESP log functions the idea framework has a well-implemented logger which prints in a different color errors and information in order to use this log we have to include the ESP log.h header we can also add a tag to label our log message let's edit static cons Char star tag equals to file system you can use the ESP logger functions just by adding ESP log e for error message and ESP log I for general information let's try it here for example if the result of the command is not ESP OK we can print an arrow using ESP log e [Music] there is another function in the documentation that's worth adding in our code which is ESP info that function reads information on our partition and with that we can calculate how much memory we have used and how much memory we have left in our partition let's try it as well now if you are able to read the info we will print a total size and use space otherwise we will print an error let's build and take a look [Music] cool now that you have our partition running we can read and write any files here let's try reading that file that have uploaded from our computer in the beginning of the video which was my file.txt let's type file star file equals to F open and the file that you want to open in our case slash storage slash mifi.txt comma and R because you are just reading a file [Music] let's create a string and read the file using the fget function [Music] now we can print our string and run the code it should display what you have written in the beginning of the video [Music] cool our file was open red and the text was correctly written in the terminal the build of it is that we can use the same zip functions as we are coding for computers I'll make more videos about embedded C in the future so stay tuned and don't forget to sign up my mailing list and if you're not subscribed to the channel consider doing it so you won't miss any new videos see in the next one foreign [Music]
Info
Channel: Yuri R
Views: 5,341
Rating: undefined out of 5
Keywords: ESP32, Filesystem, Firmware, STM32, adafruit, arduino, c++, electronics, embedded, embedded c, embedded c++, embedded systems, esp idf, esp-idf, esp32, esp32 datasheet, esp32 mesh, esp32 mesh network, esp32 project, esp32 tutorial, esp32 visual studio code, espressif, espressif esp32, espressif esp32 tutorial, espressif idf, file system, firmware, freertos, getting started, how-to, iot, mesh network, programming, sparkfun, ttgo, tutorial, visual studio, visual studio code, vs code, windows
Id: V9-cgXag4Ko
Channel Id: undefined
Length: 12min 59sec (779 seconds)
Published: Tue Jul 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.