[esp32] Add sram1_as_iram option and bootloader version detection (#14874)

This commit is contained in:
J. Nick Koston
2026-03-23 13:39:29 -10:00
committed by GitHub
parent e6a73cab8f
commit 0fb31726f6
4 changed files with 62 additions and 9 deletions
+41 -9
View File
@@ -9,6 +9,8 @@
#endif
#ifdef USE_ESP32
#include <esp_chip_info.h>
#include <esp_ota_ops.h>
#include <esp_bootloader_desc.h>
#endif
#ifdef USE_LWIP_FAST_SELECT
#include "esphome/core/lwip_fast_select.h"
@@ -167,19 +169,49 @@ void Application::process_dump_config_() {
esp_chip_info(&chip_info);
ESP_LOGI(TAG, "ESP32 Chip: %s rev%d.%d, %d core(s)", ESPHOME_VARIANT, chip_info.revision / 100,
chip_info.revision % 100, chip_info.cores);
#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_MIN_CHIP_REVISION_SET)
// Suggest optimization for chips that don't need the PSRAM cache workaround
if (chip_info.revision >= 300) {
#ifdef USE_PSRAM
ESP_LOGW(TAG, "Set minimum_chip_revision: \"%d.%d\" to save ~10KB IRAM", chip_info.revision / 100,
chip_info.revision % 100);
#else
ESP_LOGW(TAG, "Set minimum_chip_revision: \"%d.%d\" to reduce binary size", chip_info.revision / 100,
chip_info.revision % 100);
#if defined(USE_ESP32_VARIANT_ESP32) && (!defined(USE_ESP32_MIN_CHIP_REVISION_SET) || !defined(USE_ESP32_SRAM1_AS_IRAM))
static const char *const ESP32_ADVANCED_PATH = "under esp32 > framework > advanced";
#endif
#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_MIN_CHIP_REVISION_SET)
{
// Suggest optimization for chips that don't need the PSRAM cache workaround
if (chip_info.revision >= 300) {
#ifdef USE_PSRAM
ESP_LOGW(TAG, "Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to save ~10KB IRAM",
chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
#else
ESP_LOGW(TAG, "Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to reduce binary size",
chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
#endif
}
}
#endif
{
// esp_bootloader_desc_t is available in ESP-IDF >= 5.2; if readable the bootloader is modern.
//
// Design decision: We intentionally do NOT mention sram1_as_iram when the bootloader is too old.
// Enabling sram1_as_iram with an old bootloader causes a hard brick (device fails to boot,
// requires USB reflash to recover). Users don't always read warnings carefully, so we only
// suggest the option once we've confirmed the bootloader can handle it. In practice this
// means a user with an old bootloader may need to flash twice: once via USB to update the
// bootloader (they'll see the suggestion on next boot), then OTA with sram1_as_iram: true.
// Two flashes is a better outcome than a bricked device.
esp_bootloader_desc_t boot_desc;
if (esp_ota_get_bootloader_description(nullptr, &boot_desc) != ESP_OK) {
#ifdef USE_ESP32_VARIANT_ESP32
ESP_LOGW(TAG, "Bootloader too old for OTA rollback and SRAM1 as IRAM (+40KB). "
"Flash via USB once to update the bootloader");
#else
ESP_LOGW(TAG, "Bootloader too old for OTA rollback. Flash via USB once to update the bootloader");
#endif
}
#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_SRAM1_AS_IRAM)
else {
ESP_LOGW(TAG, "Bootloader supports SRAM1 as IRAM (+40KB). Set sram1_as_iram: true %s", ESP32_ADVANCED_PATH);
}
#endif
}
#endif // USE_ESP32
}
this->components_[this->dump_config_at_]->call_dump_config_();