1

I want to switch from SPIFFS to FAT on my Esp32 projects due to encryption. In my example project I have this.

esp_vfs_spiffs_conf_t conf = {
  .base_path = "/spiffs",
  .partition_label = NULL,
  .max_files = 5,   // This decides the maximum number of files that can be created on the storage
  .format_if_mount_failed = true
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);

...

ESP_LOGI(TAG, "Opening file 1");
FILE *f = fopen("/spiffs/hello.json", "w");
if (f == NULL) {
    ESP_LOGE(TAG, "Failed to open file 1 for writing test");
}
ESP_LOGI(TAG, "Opening file 2");
FILE *f2 = fopen("/spiffs/hello.txt", "w");
if (f2 == NULL) {
    ESP_LOGE(TAG, "Failed to open file 2 for writing test");
    return ESP_FAIL;
}

It works fine and creates both files as expected, but this:

esp_vfs_fat_mount_config_t conf = {
     .format_if_mount_failed = true,
     .max_files = 5,
     .allocation_unit_size = CONFIG_WL_SECTOR_SIZE
   };
esp_err_t ret = esp_vfs_fat_spiflash_mount("/fatfs", "storage", &conf, &s_wl_handle);    

ESP_LOGI(TAG, "Opening file 1");
FILE *f = fopen("/fatfs/hello.json", "w");
if (f == NULL) {
    ESP_LOGE(TAG, "Failed to open file 1 for writing test");
}
ESP_LOGI(TAG, "Opening file 2");
FILE *f2 = fopen("/fatfs/hello.txt", "w");
if (f2 == NULL) {
    ESP_LOGE(TAG, "Failed to open file 2 for writing test");
    return ESP_FAIL;
}

fails for the json file.

I (3672) example: Opening file 1
E (3672) example: Failed to open file 1 for writing test
I (3682) example: Opening file 2
...

Google so far gave me nothing on FatFS having any issue with file extensions. Can someone help me understand this?

2
  • Can you change the debug level to Debug and post the logs? Commented Apr 1, 2021 at 9:14
  • Also check that the esp_vfs_fat_spiflash_mount is returning ESP_OK, maybe the fs is not mounted... Commented Apr 1, 2021 at 9:39

1 Answer 1

3

I hope the issue is already resolved, but I will answer anyway to support other developers with a similar struggle.

I believe this may happen because you didn't enable long file names for FATFS. With no LFN support, you only can have three characters in the extension (and 8 in the filename). To fix this, please invoke menuconfig using idf.py menuconfig and then head to Component config -> FAT Filesystem support -> Long filename support -> Long filename buffer {in heap|on stack}.

Then rebuild the project and check if it works now.

Sign up to request clarification or add additional context in comments.

2 Comments

I have not worked on it in a while. But it was not resolved, I think. As soon as possible I will re-check, but I think LFN was enabled. Will update here when there is news. Thanks.
I think there's a bug in the FatFS library. We are using R0.11a and I cannot for the life of me figure out why LFN is not working as fully intended. With it enabled, I am able to write a file in an 8.4 format (blahblah.json). However, if I attempt to read from the directory, it treats it cuts off the last letter of the extension (so the filename is blahblah.jso) . Stepping through the code, it enters all LFN blocks, but fails to extract the entire filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.