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.
<Makefile>
Code: Select all
flashfs:
mkspiffs -c filesystem -b 65536 -p 256 -s 524288 $(BUILD_DIR_BASE)/spiffs.img
$(ESPTOOLPY_WRITE_FLASH) 0x180000 $(BUILD_DIR_BASE)/spiffs.img
<my_spiffs.c>
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,
&cfg,
spiffs_work_buf,
spiffs_fds,
sizeof(spiffs_fds),
spiffs_cache_buf,
sizeof(spiffs_cache_buf),
NULL);
if (res < 0) {
ESP_LOGD(TAG, "spiffs mount error");
return;
}
ESP_LOGD(TAG, "spiffs mounted successfully");
}
<success.c>
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);
}
<client.c>
Code: Select all
spiffs_file src = SPIFFS_open(&fs, "/init.txt", SPIFFS_RDONLY, 0); // SPIFFS_open returns -10002
Any help will be greatly appreciated.