ESP32-xx to use 16MB Flash with LittleFS.

In my understanding:
To use 16MB Flash with LittleFS, have to select Partition Scheme of 16M Flash (2MB APP/12.5MB FATFS) or 16M Flash (3MB APP/9.9MB FATFS). Tested on Waveshare ESP32-C5-WIFI6-KIT-N16R8.



ESP32-xx_LittleLS_16M.ino
/*
Exercise on ESP32-xx to use 16MB Flash with LittleFS.

In my understanding:
To use 16MB Flash with LittleFS
Have to select Partition Scheme of 16M Flash (2MB APP/12.5MB FATFS) or 16M Flash (3MB APP/9.9MB FATFS).

But, if call simple LittleFS.begin(true), will fail.
To use it, have to set partitionLabel to "ffat".

In my test on Waveshare ESP32-C5-WIFI6-KIT-N16R8:
With "16M Flash (2MB APP/12.5MB FATFS)":
LittleFS.totalBytes: 12451840 bytes
LittleFS.usedBytes:  8192 bytes

With "16M Flash (3MB APP/9.9MB FATFS)":
LittleFS.totalBytes: 10354688 bytes
LittleFS.usedBytes:  8192 bytes


*/
#include <LittleFS.h>
#include "esp_partition.h"

void setup() {
  delay(2000);
  Serial.begin(115200);
  delay(1000);

  Serial.println("\n\n~ Start ~");

  Serial.println("\n--- Current Partition Table ---");
  // Get partition iterator (finds all types of partitions)
  esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);

  while (it != NULL) {
    const esp_partition_t *p = esp_partition_get(it);
    
    // Print out partition details
    Serial.printf("Label: %-10s | Type: 0x%02x | SubType: 0x%02x | Size: %8u bytes (%.2f MB)\n", 
                  p->label, 
                  p->type, 
                  p->subtype, 
                  p->size, 
                  p->size / (1024.0 * 1024.0));

    it = esp_partition_next(it); // move to next
  }
  
  // Release iterator
  esp_partition_iterator_release(it);
  Serial.println("-------------------------------\n");

  Serial.println("--- Flash ---");
  Serial.printf("Flash chip size: %d bytes\n", ESP.getFlashChipSize());
  Serial.printf("Flash chip size: %d MB\n", ESP.getFlashChipSize() / (1024*1024));
  Serial.printf("Flash frequency: %d Hz\n", ESP.getFlashChipSpeed());
  Serial.println();

  Serial.println("--- LittleFS ---");

/*
  // Simple using of LittleFS.begin(true).
  if (!LittleFS.begin(true)){
    Serial.println("LittleFS mount failed!");
    return;
  }
*/

/*
Force mount to ffat:
bool begin(bool formatOnFail=false, 
           const char * basePath="/littlefs", 
           uint8_t maxOpenFiles=10, 
           const char * partitionLabel="spiffs");

*/
  if (!LittleFS.begin(true, "/littlefs", 10, "ffat")){
    Serial.println("LittleFS mount failed!");
    return;
  }

  Serial.println("LittleFS mount Success!");
  Serial.printf("LittleFS.totalBytes: %u bytes\n", LittleFS.totalBytes());
  Serial.printf("LittleFS.usedBytes:  %u bytes\n", LittleFS.usedBytes());

}

void loop()
{
  delay(1000); 
}





Auto mount LittleFS to spiffs/ffat

This code will automatically determine the current environment. No matter you choose Default, 16M FATFS or a custom partition, it can be mounted correctly.

ESP32_xx_LittleFS_auto_mount.ino

/*
Exercise on ESP32-xx
to auto-mount /LittleFS to spiffs or ffat.

*/
#include <LittleFS.h>
#include "esp_partition.h"

void setup() {
  delay(2000);
  Serial.begin(115200);
  delay(1000);

  Serial.println("\n\n~ Start ~");
  Serial.printf("Chip model: %s\n", ESP.getChipModel());
  Serial.printf("Chip revision: %d\n", ESP.getChipRevision());
  Serial.printf("ESP-IDF Version: %s\n", esp_get_idf_version());
  Serial.printf("Arduino ESP32 Version: %s\n", ESP_ARDUINO_VERSION_STR);

  Serial.println("\n--- Flash Size ---");
  Serial.printf("Flash chip size: %d bytes\n", ESP.getFlashChipSize());
  Serial.printf("Flash chip size: %d MB\n", ESP.getFlashChipSize() / (1024*1024));
  Serial.printf("Flash frequency: %d Hz\n", ESP.getFlashChipSpeed());

  Serial.println("\n--- Current Partition Table ---");
  // Get partition iterator (finds all types of partitions)
  esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);

  while (it != NULL) {
    const esp_partition_t *p = esp_partition_get(it);
    
    // Print out partition details
    Serial.printf("Label: %-10s | Type: 0x%02x | SubType: 0x%02x | Size: %8u bytes (%.2f MB)\n", 
                  p->label, 
                  p->type, 
                  p->subtype, 
                  p->size, 
                  p->size / (1024.0 * 1024.0));

    it = esp_partition_next(it); // move to next
  }
  
  // Release iterator
  esp_partition_iterator_release(it);
  Serial.println("-------------------------------\n");

  Serial.println("--- Auto Mount LittleFS ---");
  const char* finalLabel = NULL;

  // 1. First look for partitions marked as SPIFFS
  if (esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL) != NULL) {
    finalLabel = "spiffs";
    Serial.println("Found partition label: [spiffs]");
  } 
  // 2. If not, look for a partition marked FATFS
  else if (esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL) != NULL) {
    finalLabel = "ffat";
    Serial.println("Found partition label: [ffat]");
  }

  // 3. Execute mount
  if (finalLabel != NULL) {
    if (LittleFS.begin(true, "/littlefs", 10, finalLabel)) {
      Serial.printf("Success! LittleFS mounted on [%s]\n", finalLabel);
      Serial.printf("Total size: %u bytes\n", LittleFS.totalBytes());
    } else {
      Serial.println("Mount failed even though partition exists.");
    }
  } else {
    Serial.println("Error: No suitable Flash partition found in Partition Scheme!");
  }

}

void loop()
{
  delay(1000); 
}




Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

Drive 320x240 ILI9341 SPI TFT using ESP32-S3 (NodeMCU ESP-S3-12K-Kit) using TFT_eSPI library, in Arduino Framework.