Store and read some static files using SPIFFS

Posts: 3
Joined: Mon Feb 20, 2017 1:19 pm

Store and read some static files using SPIFFS

Postby dlawnstjd » Mon Feb 20, 2017 2:26 pm

I am a beginner in embedded programming. I’m programming with the ESP32DevKitC( I want to flash some static files using a SPIFFS filesystem, and try to read those files in run-time. To do that, I made a SPIFFS filesystem image that contains some static files using mkspiffs(, flashed that filesystem image, and mount SPIFFS to VFS via esp_vfs_register() and SPIFFS_mount() functions.
I succeeded in opening a new file, write some text to the file, and read the text from the file again. However, when I try to open the static files that uploaded by mkspiffs using SPIFFS_open() function, I got a error code(-10002, NOT FOUND).

I've tried several different things, but it's not working. Here are some snippet code I wrote.

- make spiffs image and flash it.

Code: Select all

	mkspiffs -c filesystem -b 65536 -p 256 -s 524288 $(BUILD_DIR_BASE)/spiffs.img
	$(ESPTOOLPY_WRITE_FLASH) 0x180000 $(BUILD_DIR_BASE)/spiffs.img
- mount vfs

Code: Select all

void vfs_spiffs_register() {
    esp_vfs_t vfs = {
        .fd_offset = 0,
        .flags = ESP_VFS_FLAG_DEFAULT,
        .write = &vfs_spiffs_write,
        .open = &vfs_spiffs_open,
        .fstat = &vfs_spiffs_fstat,
        .close = &vfs_spiffs_close,
        .read = &vfs_spiffs_read,
        .lseek = &vfs_spiffs_lseek,
        .stat = &vfs_spiffs_stat,
        .link = NULL,
        .unlink = &vfs_spiffs_unlink,
        .rename = &vfs_spiffs_rename,
    		.mkdir = &vfs_spiffs_mkdir,
    		.opendir = &vfs_spiffs_opendir,
    		.readdir = &vfs_spiffs_readdir,
    		.closedir = &vfs_spiffs_closedir

    ESP_ERROR_CHECK(esp_vfs_register("/spiffs", &vfs, NULL));

    // Mount spiffs file system
    spiffs_config cfg;
    int res = 0;

    cfg.phys_size = 512*1024;
    cfg.phys_addr = 0x180000;
    cfg.phys_erase_block = 65536;
    cfg.log_block_size = 65536;
    cfg.log_page_size = 256;
    cfg.hal_read_f = (spiffs_read)low_spiffs_read;
    cfg.hal_write_f = (spiffs_write)low_spiffs_write;
    cfg.hal_erase_f = (spiffs_erase)low_spiffs_erase;

    res = SPIFFS_mount(&fs,

    if (res < 0) {
      ESP_LOGD(TAG, "spiffs mount error");
    ESP_LOGD(TAG, "spiffs mounted successfully");
- success code for creating a new file, writing, reading

Code: Select all

static void test_spiffs() {
    char buf[12];

    spiffs_file fd = SPIFFS_open(&fs, "my_file", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
    if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0) printf("errno %i\n", SPIFFS_errno(&fs));
    SPIFFS_close(&fs, fd);

    fd = SPIFFS_open(&fs, "my_file", SPIFFS_RDWR, 0);
    if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0) printf("errno %i\n", SPIFFS_errno(&fs));
    SPIFFS_close(&fs, fd);

    printf("--> %s <--\n", buf);
- fail code for reading a static file in spiffs filesystem image that flashed at build time.

Code: Select all

spiffs_file src = SPIFFS_open(&fs, "/init.txt", SPIFFS_RDONLY, 0); // SPIFFS_open returns -10002

Any help will be greatly appreciated.

Who is online

Users browsing this forum: No registered users and 27 guests