2026年4月27日 星期一

在 Arduino IDE 查看 ESP32 的 RAM 和 Flash 資訊

環境:

Arduino IDE 2.3.8、ESP32 NodeMCU-32S 開發板


程式碼:

//#include "LittleFS.h"
void setup() {
  Serial.begin(115200);
  delay(2000);

  Serial.println("--- RAM 資訊 ---");

  // 內置 SRAM
  Serial.printf("SRAM 總大小: %d bytes\n", ESP.getHeapSize());
  Serial.printf("可用 SRAM: %d bytes\n", ESP.getFreeHeap());

  // 外部 PSRAM (如果硬體支持且已開啟)
  if (psramInit()) {
    Serial.printf("PSRAM 總大小: %d bytes\n", ESP.getPsramSize());
    Serial.printf("可用 PSRAM: %d bytes\n", ESP.getFreePsram());
  } else {
    Serial.println("未偵測到 PSRAM");
  }


  Serial.println("\n--- Flash 資訊 ---");

  // 硬體上的總容量
  uint32_t flashSize = ESP.getFlashChipSize();
  Serial.printf("Flash 實際硬體大小: %u bytes (%d MB)\n", flashSize, flashSize / (1024 * 1024));

  // 當前程式 (Sketch) 佔用與可用空間
  uint32_t sketchSize = ESP.getSketchSize();
  uint32_t freeSketchSpace = ESP.getFreeSketchSpace();

  Serial.printf("程式已佔用: %u bytes\n", sketchSize);
  Serial.printf("程式區剩餘可用: %u bytes\n", freeSketchSpace);

  // 如果有用 LittleFS 或 SPIFFS (檔案系統)
  // 需要先 #include "LittleFS.h"
  /*
  if (LittleFS.begin()) {
    Serial.printf("檔案系統總量: %llu bytes\n", LittleFS.totalBytes());
    Serial.printf("檔案系統已用: %llu bytes\n", LittleFS.usedBytes());
  }
  */

  Serial.println("\n--- 目前 Flash 分割表清單 ---");
  Serial.println("Type | SubType | Offset | Size       | Label");
  Serial.println("----------------------------------------------");

  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);
    Serial.printf("%02x   | %02x      | 0x%06x | %10d | %s\n",
                  p->type, p->subtype, p->address, p->size, p->label);
    it = esp_partition_next(it);
  }

  esp_partition_iterator_release(it);
  Serial.println("----------------------------------------------");
}

void loop() {
}


輸出結果:

--- RAM 資訊 ---
SRAM 總大小: 377912 bytes
可用 SRAM: 334240 bytes
未偵測到 PSRAM

--- Flash 資訊 ---
Flash 實際硬體大小: 4194304 bytes (4 MB)
程式已佔用: 282400 bytes
程式區剩餘可用: 1310720 bytes

--- 目前 Flash 分割表清單 ---
Type | SubType | Offset | Size       | Label
----------------------------------------------
01   | 02      | 0x009000 |      20480 | nvs
01   | 00      | 0x00e000 |       8192 | otadata
00   | 10      | 0x010000 |    1310720 | app0
00   | 11      | 0x150000 |    1310720 | app1
01   | 82      | 0x290000 |    1441792 | spiffs
01   | 03      | 0x3f0000 |      65536 | coredump
----------------------------------------------
我這塊板子沒有 PSRAM,有 4MB Flash。
Flash 分割區資訊裡的
  • Type 字段





參考:

沒有留言:

張貼留言