Prepare FAT image and flash it to ESP32

kearins
Posts: 21
Joined: Sun Jul 23, 2017 11:48 pm

Prepare FAT image and flash it to ESP32

Postby kearins » Thu Sep 21, 2017 7:30 pm

Here is a tool to prepare FAT image on host and flash it to esp32.
https://github.com/jkearins/ESP32_mkfatfs

The tool is inspired by mkspiffs https://github.com/loboris/ESP32_spiffs_example tool created by loboris https://esp32.com/viewtopic.php?f=18&t=1901

Example of using FAT with ESP32

Features
  • prepare of FAT image
  • flash FAT image to ESP32
  • example of FAT functions
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/jkearins/ESP32_mkfatfs.git
Execute menuconfig and configure your Serial flash config and other settings. Included sdkconfig.defaults sets some defaults to be used.

Code: Select all

make menuconfig
Make and flash the example:

Code: Select all

make all && make flash && make flashfatfs
Prepare FAT image

FAT image can be prepared on host and flashed to ESP32.
Tested and works under Windows. It is not tested under Linux and Mac OS yet but should work.

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

To create FAT image in build directory without flashing to ESP32 execute:

Code: Select all

make makefatfs
To create FAT image in build directory and flash it to ESP32 execute:

Code: Select all

make flashfatfs
To flash previously prepared in build directory FAT image to ESP32 execute:

Code: Select all

make copyfatfs
Example functions
  • registers FAT as VFS file system; if the fs is not mounted the system will halt
  • lists files in root directory
Example output:

Code: Select all

I (399) cpu_start: Pro cpu up.
I (399) cpu_start: Starting app cpu, entry point is 0x40080e98
I (0) cpu_start: App cpu up.
I (402) heap_init: Initializing. RAM available for dynamic allocation:
I (417) heap_init: At 3FFAE2A0 len 00001D60 (7 KiB): DRAM
I (433) heap_init: At 3FFB3E60 len 0002C1A0 (176 KiB): DRAM
I (449) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (466) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (483) heap_init: At 40087D04 len 000182FC (96 KiB): IRAM
I (499) cpu_start: Pro cpu start user code
I (582) cpu_start: Starting scheduler on PRO CPU.
I (192) cpu_start: Starting scheduler on APP CPU.
I (192) FATFS: Mounting FAT partition...
I (195) FATFS: Done.
I (196) FATFS: ============= Begin of root contents
I (199) FATFS: 	ESP32Explorer.html              	   8332 bytes
I (202) FATFS: 	ESP32Explorer.js                	  11434 bytes
I (208) FATFS: 	<images>
I (210) FATFS: 	<jquery>
I (212) FATFS: 	<jQuery-File-Upload-9.18.0>
I (216) FATFS: 	<jqueryui>
I (218) FATFS: 	<jstree>
I (220) FATFS: ============= End of root contents
To manage FATFS objects I used Kolban's cpp_utils from esp32-snippets project: https://github.com/nkolban/esp32-snippets.git.
Files for sample flash image have been gotten from another Kolban's project https://github.com/nkolban/ESP32_Explorer.git introduced there https://esp32.com/viewtopic.php?f=17&t=2977

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Prepare FAT image and flash it to ESP32

Postby kolban » Thu Sep 21, 2017 8:31 pm

OOooh ... I'm super excited to try this out. MANY thanks for building this ... this will be a huge addition to those of us using FATFS.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

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

Re: Prepare FAT image and flash it to ESP32

Postby loboris » Fri Sep 22, 2017 7:11 am

On Linux the compilation fails on make makefatfs:

Code: Select all

Makefile:26: warning: undefined variable 'OS'
Makefile:97: warning: undefined variable 'TARGET_LDFLAGS'
Building mkfatfs ...
main.cpp:9:0: warning: "VERSION" redefined
 #define VERSION "0.3.6"
 
<command-line>:0:0: note: this is the location of the previous definition
main.cpp: In function ‘int addFile(char*, const char*)’:
main.cpp:90:24: error: ‘__getreent’ was not declared in this scope
     struct _reent *r = __getreent();
                        ^~~~~~~~~~
main.cpp:90:24: note: suggested alternative: ‘_reent’
     struct _reent *r = __getreent();
                        ^~~~~~~~~~
                        _reent
main.cpp:119:55: error: invalid use of incomplete type ‘struct _reent’
             std::cerr << "esp_vfs_write() error " << r->_errno << std::endl;
                                                       ^~
In file included from main.cpp:38:0:
fatfs/fatfs.h:27:38: note: forward declaration of ‘struct _reent’
 ssize_t emulate_esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size);
                                      ^~~~~~
main.cpp:119:57: error: invalid use of incomplete type ‘struct _reent’
             std::cerr << "esp_vfs_write() error " << r->_errno << std::endl;
                                                         ^~~~~~
In file included from main.cpp:38:0:
fatfs/fatfs.h:27:38: note: forward declaration of ‘struct _reent’
 ssize_t emulate_esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size);
                                      ^~~~~~
Makefile:97: recipe for target 'mkfatfs' failed
make[1]: *** [mkfatfs] Error 1
/home/LoBo2_Razno/ESP32/Projekti/ESP32_mkfatfs/components/mkfatfs/Makefile.projbuild:10: recipe for target 'mkfatfs' failed
make: *** [mkfatfs] Error 2

kearins
Posts: 21
Joined: Sun Jul 23, 2017 11:48 pm

Re: Prepare FAT image and flash it to ESP32

Postby kearins » Fri Sep 22, 2017 2:12 pm

loboris wrote:On Linux the compilation fails on make makefatfs
Thank you for testing under linux. Now it should be ok. I've tested it on Ubuntu 16.04.3 x64.

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

Re: Prepare FAT image and flash it to ESP32

Postby loboris » Fri Sep 22, 2017 5:08 pm

It compiles now on Linux without errors. Thank you.

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

Re: Prepare FAT image and flash it to ESP32

Postby loboris » Sat Sep 23, 2017 8:59 am

Building on on MacOS (Sierra) fails with:

Code: Select all

Boriss-iMac:ESP32_mkfatfs borisl$ make makefatfs
Building mkfatfs ...
main.cpp:33:10: fatal error: 'wear_levelling.h' file not found
#include "wear_levelling.h"
         ^
1 error generated.
make[1]: *** [mkfatfs] Error 1
make: *** [mkfatfs] Error 2
After adding $(IDF_INCLUDES) to TARGET_CFLAGS and TARGET_CXXFLAGS of Darwin section in ESP32_mkfatfs/components/mkfatfs/src/Makefile it compiles without problem.

Code: Select all

	ifeq ($(UNAME_S),Darwin)
		TARGET_OS := OSX
		DIST_SUFFIX := osx
		CC=clang
		CXX=clang++
		TARGET_CFLAGS   = -std=gnu99 -Os -Wall -Itclap -Ifatfs -I. -D$(TARGET_OS) -DVERSION=\"$(VERSION)\" -D__NO_INLINE__ -mmacosx-version-min=10.7 -arch x86_64 $(IDF_INCLUDES)
		TARGET_CXXFLAGS = -std=gnu++11 -Os -Wall -Itclap -Ifatfs -I. -D$(TARGET_OS) -DVERSION=\"$(VERSION)\" -D__NO_INLINE__ -mmacosx-version-min=10.7 -arch x86_64 -stdlib=libc++ $(IDF_INCLUDES)
		TARGET_LDFLAGS  = -arch x86_64 -stdlib=libc++
	endif

Code: Select all

Boriss-iMac:ESP32_mkfatfs borisl$ make makefatfs
Building mkfatfs ...
Making fatfs image ...
python /Users/borisl/esp/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port COM5 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect
Debug output enabled
W fatfs: f_mount failed (13)
I fatfs: Formatting FATFS partition
I fatfs: Mounting again
Mounted successfully
adding to image: /ESP32Explorer.html
...
...
Unmounted successfully
Image file is written to "/Users/borisl/esp/ESP32_mkfatfs/build/fatfs_image.img"

kearins
Posts: 21
Joined: Sun Jul 23, 2017 11:48 pm

Re: Prepare FAT image and flash it to ESP32

Postby kearins » Sat Sep 23, 2017 9:30 am

loboris wrote:Building on on MacOS (Sierra) fails...
Got it, repository is updated, thank you.

murtuzaq
Posts: 1
Joined: Thu May 02, 2019 10:31 am

Re: Prepare FAT image and flash it to ESP32

Postby murtuzaq » Thu May 02, 2019 3:13 pm

Thanks, got it to work as well. The example project really helped alot. Had to of course create a 'C' wrapper for the C++ file FATFS_VFS, but that was no big deal ;) .

Just one question:
How do you change the fatfs image size using the tool makefatfs? By default it seems to only be able to make an img of size 1024KB. I have a fat storage partition of size 2M (0x200000) defined in my partition table. And the 1M image file seems to throw an error when i try to mount the fatfs drive using the function: esp_vfs_fat_spiflash_mount.

Everything of course is ok, when i shrink my partition size back down to 1M (0x100000).

I am guessing that its because the size of the image file has to be exactly the size specified for the storage area in the partition table. So again, how do you change the image size with the tool makefatfs?

Thanks.
MurtuzaQ

Who is online

Users browsing this forum: No registered users and 96 guests