[SOLVED] Can't mount external flash after formatting

filo_gr
Posts: 109
Joined: Wed Jul 28, 2021 12:25 pm
Location: Italy

[SOLVED] Can't mount external flash after formatting

Postby filo_gr » Mon Jul 04, 2022 2:09 pm

I'm using an external flash memory connected to my ESP32-S2.
I mounted a FATFS partition on it and I can read/write files correctly.
However I can't understand how to mount again the partition after I invalidate it.

The way I invalidate the flash partition is by erasing a block of memory (8 sectors). If I manually restart the ESP32-S2, all works well and it mounts again the partition.
If I try to mount the partition after the erasure, the ESP32 crashes giving:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (StoreProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x400947a6  PS      : 0x00060630  A0      : 0x80095d20  A1      : 0x3ffeb0b0
0x400947a6: f_mount at C:/Espressif/frameworks/esp-idf-v4.4.1/components/fatfs/src/ff.c:3527

A2      : 0x3ffcc760  A3      : 0x00000001  A4      : 0x00000001  A5      : 0x00000000
A6      : 0x00000004  A7      : 0x3ffcc77c  A8      : 0x80094755  A9      : 0x3ffeb090
A10     : 0x00000001  A11     : 0x80088608  A12     : 0x3ffcc770  A13     : 0x3ffd31c0
A14     : 0xc98dc025  A15     : 0x3ffd452c  SAR     : 0x0000001a  EXCCAUSE: 0x0000001d
EXCVADDR: 0x80088608  LBEG    : 0x3ffcc770  LEND    : 0x3ffd31c0  LCOUNT  : 0x400243e1
0x400243e1: _xt_user_exc at C:/Espressif/frameworks/esp-idf-v4.4.1/components/freertos/port/xtensa/xtensa_vectors.S:633



Backtrace:0x400947a3:0x3ffeb0b00x40095d1d:0x3ffeb0f0 0x40089f96:0x3ffeb120 0x4008860b:0x3ffeb160
0x400947a3: f_mount at C:/Espressif/frameworks/esp-idf-v4.4.1/components/fatfs/src/ff.c:3537

0x40095d1d: esp_vfs_fat_spiflash_mount at C:/Espressif/frameworks/esp-idf-v4.4.1/components/fatfs/vfs/vfs_fat_spiflash.c:76

0x40089f96: spi_ext_flash_mount_fatfs at C:\Users\User\Documents\Lavoro\build/../components/spi_manager_component/spi_manage.c:191      

0x4008860b: task_testing at C:\Users\User\Documents\Lavoro\build/../main/core.c:1690

ELF file SHA256: 3306af21b5f4bc15

Rebooting...
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x3 (RTC_SW_SYS_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4002339d
0x4002339d: esp_restart_noos_dig at C:/Espressif/frameworks/esp-idf-v4.4.1/components/esp_system/esp_system.c:44 (discriminator 1)
To mount the partition I simply use the following funtion where I use, as an input, the string of the SPI flash.
  1. esp_err_t spi_ext_flash_mount_fatfs (const char * partition_label)
  2. {
  3.     ESP_LOGI(g_p_tag, "Mounting FAT filesystem");
  4.     FATFS * p_filesys;
  5.     uint32_t free_clusters = 0;
  6.     uint32_t total_sectors = 0;
  7.     uint32_t free_sectors = 0;
  8.     wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
  9.     esp_err_t ret = ESP_FAIL;
  10.  
  11.     const esp_vfs_fat_mount_config_t mount_config = {
  12.         .max_files = FAT_NMAX_FILES,
  13.         .format_if_mount_failed = true,
  14.         .allocation_unit_size = CONFIG_WL_SECTOR_SIZE,
  15.     };
  16.     ret = esp_vfs_fat_spiflash_mount(g_p_base_path, partition_label,
  17.                                      &mount_config, &s_wl_handle);
  18.     if (ret != ESP_OK)
  19.     {
  20.         ESP_LOGE(g_p_tag, "Failed to mount FATFS (%s)", esp_err_to_name(ret));
  21.         ret = ESP_FAIL;
  22.     }
  23.     else
  24.     {
  25.         if (FR_OK == f_getfree("0:", &free_clusters, &p_filesys))
  26.         {
  27.             total_sectors = (p_filesys->n_fatent - 2) * p_filesys->csize;
  28.             free_sectors = free_clusters * p_filesys->csize;
  29.             ESP_LOGI(g_p_tag, "FAT FS: %d KB total, %d KB free",
  30.                      (total_sectors * p_filesys->ssize) / 1024,
  31.                      (free_sectors * p_filesys->ssize) / 1024);
  32.         }
  33.         else
  34.         {
  35.             ESP_LOGW(g_p_tag, "Errore nell'ottenimento dei cluster.");
  36.         }
  37.         ret = ESP_OK;
  38.     }
  39.  
  40.     return ret;
  41. }
How can I use the partition again without rebooting?
Last edited by filo_gr on Tue Jul 05, 2022 7:48 am, edited 2 times in total.
Filippo

User avatar
gtjoseph
Posts: 81
Joined: Fri Oct 15, 2021 10:55 pm

Re: Can't mount external flash after formatting

Postby gtjoseph » Mon Jul 04, 2022 6:23 pm

Did you unmount it with esp_vfs_fat_spiflash_unmount before you invalidated the partition?
You're not going to be able to mount it a second time to the same mountpoint without doing that first.

filo_gr
Posts: 109
Joined: Wed Jul 28, 2021 12:25 pm
Location: Italy

Re: Can't mount external flash after formatting

Postby filo_gr » Tue Jul 05, 2022 7:47 am

gtjoseph wrote:
Mon Jul 04, 2022 6:23 pm
Did you unmount it with esp_vfs_fat_spiflash_unmount before you invalidated the partition?
You're not going to be able to mount it a second time to the same mountpoint without doing that first.
Thank you, you're right! I didn't release the resources and that's why I wasn't able to mount it again :)
Filippo

Who is online

Users browsing this forum: No registered users and 116 guests