From b2a3161cf3a531c7ead90d4a9b62e9c97f8d7148 Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Wed, 25 Dec 2024 23:52:51 +0100 Subject: [PATCH] [debug] Add partition table logging to `dump_config` This enhances the `DebugComponent` by adding functionality to log detailed partition table information during the `dump_config()` process. The partition table details are displayed in a clean, aligned format, providing developers with clear insights into the device's flash memory layout. #### **Features:** 1. Logs the following attributes for each partition: - Name - Type - Subtype - Start Address - Size 2. Uses the `esp_partition` API for ESP32 devices running either Arduino or ESP-IDF. 3. Ensures alignment of columns for improved readability. #### **Example Log Output:** ``` [23:37:05][C][debug:033]: Partition table: [23:37:05][C][debug:034]: Name Type Subtype Address Size [23:37:05][C][debug:038]: nvs 1 2 0x00009000 0x00005000 [23:37:05][C][debug:038]: otadata 1 0 0x0000E000 0x00002000 [23:37:05][C][debug:038]: app0 0 16 0x00010000 0x003C0000 [23:37:05][C][debug:038]: app1 0 17 0x003D0000 0x003C0000 [23:37:05][C][debug:038]: eeprom 1 153 0x00790000 0x00001000 [23:37:05][C][debug:038]: spiffs 1 130 0x00791000 0x0000F000 ``` #### **Why This Change is Useful:** 1. **Improved Debugging:** Developers can verify partition configurations directly from logs, without requiring physical access or external tools. 2. **Diagnostic Aid:** Identifies potential issues related to insufficient partition sizes or incorrect layouts. 3. **Consistency:** Provides a clear and formatted output to improve log readability. #### **Implementation Details:** - **Platform-Specific:** The feature is implemented for ESP32 devices using the ESP-IDF framework. - **Formatted Output:** Ensures aligned columns using fixed-width formatting for better clarity. --- esphome/components/debug/debug_component.cpp | 4 ++++ esphome/components/debug/debug_component.h | 14 ++++++++++++++ esphome/components/debug/debug_esp32.cpp | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/esphome/components/debug/debug_component.cpp b/esphome/components/debug/debug_component.cpp index cbd4249d92..c2dc647080 100644 --- a/esphome/components/debug/debug_component.cpp +++ b/esphome/components/debug/debug_component.cpp @@ -50,6 +50,10 @@ void DebugComponent::dump_config() { this->reset_reason_->publish_state(get_reset_reason_()); } #endif // USE_TEXT_SENSOR + +#ifdef USE_ESP32 + this->log_partition_info_(); // Log partition information for ESP32 +#endif // USE_ESP32 } void DebugComponent::loop() { diff --git a/esphome/components/debug/debug_component.h b/esphome/components/debug/debug_component.h index 2b54406603..608addb4a3 100644 --- a/esphome/components/debug/debug_component.h +++ b/esphome/components/debug/debug_component.h @@ -55,6 +55,20 @@ class DebugComponent : public PollingComponent { #endif // USE_ESP32 #endif // USE_SENSOR +#ifdef USE_ESP32 + /** + * @brief Logs information about the device's partition table. + * + * This function iterates through the ESP32's partition table and logs details + * about each partition, including its name, type, subtype, starting address, + * and size. The information is useful for diagnosing issues related to flash + * memory or verifying the partition configuration dynamically at runtime. + * + * Only available when compiled for ESP32 platforms. + */ + void log_partition_info_(); +#endif // USE_ESP32 + #ifdef USE_TEXT_SENSOR text_sensor::TextSensor *device_info_{nullptr}; text_sensor::TextSensor *reset_reason_{nullptr}; diff --git a/esphome/components/debug/debug_esp32.cpp b/esphome/components/debug/debug_esp32.cpp index 5f7b9cdbb0..194150b926 100644 --- a/esphome/components/debug/debug_esp32.cpp +++ b/esphome/components/debug/debug_esp32.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #if defined(USE_ESP32_VARIANT_ESP32) #include @@ -28,6 +29,23 @@ namespace debug { static const char *const TAG = "debug"; +void DebugComponent::log_partition_info_() { + ESP_LOGCONFIG(TAG, "Partition table:"); + ESP_LOGCONFIG(TAG, " %-12s %-4s %-8s %-10s %-10s", "Name", "Type", "Subtype", "Address", "Size"); + esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL); + while (it != NULL) { + const esp_partition_t *partition = esp_partition_get(it); + ESP_LOGCONFIG(TAG, " %-12s %-4d %-8d 0x%08X 0x%08X", + partition->label, + partition->type, + partition->subtype, + partition->address, + partition->size); + it = esp_partition_next(it); + } + esp_partition_iterator_release(it); +} + std::string DebugComponent::get_reset_reason_() { std::string reset_reason; switch (esp_reset_reason()) {