ESPHome  2024.10.2
debug_esp8266.cpp
Go to the documentation of this file.
1 #include "debug_component.h"
2 #ifdef USE_ESP8266
3 #include "esphome/core/log.h"
4 #include <Esp.h>
5 
6 namespace esphome {
7 namespace debug {
8 
9 static const char *const TAG = "debug";
10 
12 #if !defined(CLANG_TIDY)
13  return ESP.getResetReason().c_str();
14 #else
15  return "";
16 #endif
17 }
18 
20  return ESP.getFreeHeap(); // NOLINT(readability-static-accessed-through-instance)
21 }
22 
23 void DebugComponent::get_device_info_(std::string &device_info) {
24  const char *flash_mode;
25  switch (ESP.getFlashChipMode()) { // NOLINT(readability-static-accessed-through-instance)
26  case FM_QIO:
27  flash_mode = "QIO";
28  break;
29  case FM_QOUT:
30  flash_mode = "QOUT";
31  break;
32  case FM_DIO:
33  flash_mode = "DIO";
34  break;
35  case FM_DOUT:
36  flash_mode = "DOUT";
37  break;
38  default:
39  flash_mode = "UNKNOWN";
40  }
41  ESP_LOGD(TAG, "Flash Chip: Size=%ukB Speed=%uMHz Mode=%s",
42  ESP.getFlashChipSize() / 1024, // NOLINT
43  ESP.getFlashChipSpeed() / 1000000, flash_mode); // NOLINT
44  device_info += "|Flash: " + to_string(ESP.getFlashChipSize() / 1024) + // NOLINT
45  "kB Speed:" + to_string(ESP.getFlashChipSpeed() / 1000000) + "MHz Mode:"; // NOLINT
46  device_info += flash_mode;
47 
48 #if !defined(CLANG_TIDY)
49  auto reset_reason = get_reset_reason_();
50  ESP_LOGD(TAG, "Chip ID: 0x%08X", ESP.getChipId());
51  ESP_LOGD(TAG, "SDK Version: %s", ESP.getSdkVersion());
52  ESP_LOGD(TAG, "Core Version: %s", ESP.getCoreVersion().c_str());
53  ESP_LOGD(TAG, "Boot Version=%u Mode=%u", ESP.getBootVersion(), ESP.getBootMode());
54  ESP_LOGD(TAG, "CPU Frequency: %u", ESP.getCpuFreqMHz());
55  ESP_LOGD(TAG, "Flash Chip ID=0x%08X", ESP.getFlashChipId());
56  ESP_LOGD(TAG, "Reset Reason: %s", reset_reason.c_str());
57  ESP_LOGD(TAG, "Reset Info: %s", ESP.getResetInfo().c_str());
58 
59  device_info += "|Chip: 0x" + format_hex(ESP.getChipId());
60  device_info += "|SDK: ";
61  device_info += ESP.getSdkVersion();
62  device_info += "|Core: ";
63  device_info += ESP.getCoreVersion().c_str();
64  device_info += "|Boot: ";
65  device_info += to_string(ESP.getBootVersion());
66  device_info += "|Mode: " + to_string(ESP.getBootMode());
67  device_info += "|CPU: " + to_string(ESP.getCpuFreqMHz());
68  device_info += "|Flash: 0x" + format_hex(ESP.getFlashChipId());
69  device_info += "|Reset: ";
70  device_info += reset_reason;
71  device_info += "|";
72  device_info += ESP.getResetInfo().c_str();
73 #endif
74 }
75 
77 #ifdef USE_SENSOR
78  if (this->block_sensor_ != nullptr) {
79  // NOLINTNEXTLINE(readability-static-accessed-through-instance)
80  this->block_sensor_->publish_state(ESP.getMaxFreeBlockSize());
81  }
82 #if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 5, 2)
83  if (this->fragmentation_sensor_ != nullptr) {
84  // NOLINTNEXTLINE(readability-static-accessed-through-instance)
85  this->fragmentation_sensor_->publish_state(ESP.getHeapFragmentation());
86  }
87 #endif
88 
89 #endif
90 }
91 
92 } // namespace debug
93 } // namespace esphome
94 #endif
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
Definition: helpers.cpp:347
void get_device_info_(std::string &device_info)
sensor::Sensor * fragmentation_sensor_
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
std::string to_string(int value)
Definition: helpers.cpp:80
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7