11#include <esp_chip_info.h>
12#include <esp_ota_ops.h>
13#include <esp_bootloader_desc.h>
15#if __has_include(<esp_flash_chips/spi_flash_chip_generic.h>)
16#include <esp_flash_chips/spi_flash_chip_generic.h>
17#include <esp_flash_chips/spi_flash_chip_mxic.h>
19#include <spi_flash_chip_generic.h>
20#include <spi_flash_chip_mxic.h>
23#define ESPHOME_FLASH_VENDOR_DRIVERS \
24 (CONFIG_SPI_FLASH_SUPPORT_ISSI_CHIP + CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP + CONFIG_SPI_FLASH_SUPPORT_GD_CHIP + \
25 CONFIG_SPI_FLASH_SUPPORT_WINBOND_CHIP + CONFIG_SPI_FLASH_SUPPORT_BOYA_CHIP + CONFIG_SPI_FLASH_SUPPORT_TH_CHIP + \
26 CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP)
43static constexpr uint32_t SCHEDULER_FREELIST_TRIM_DELAY_MS = 10000;
50template<
typename Iterator,
float (Component::*GetPriority)() const>
51static void insertion_sort_by_priority(Iterator first, Iterator last) {
52 for (
auto it = first + 1; it != last; ++it) {
54 float key_priority = (
key->*GetPriority)();
58 while (j >= first && ((*j)->*GetPriority)() < key_priority) {
73 ESP_LOGI(TAG,
"Running through setup()");
74 ESP_LOGV(TAG,
"Sorting components by setup priority");
130 ESP_LOGI(TAG,
"setup() finished successfully!");
133 this->
scheduler.set_timeout(
this, SCHEDULER_FREELIST_TRIM_DELAY_MS, [
this]() { this->
scheduler.trim_freelist(); });
135#ifdef USE_SETUP_PRIORITY_OVERRIDE
140#if defined(USE_ESP32) || defined(USE_LIBRETINY)
160void Application::process_dump_config_() {
163 ESP_LOGI(TAG,
"ESPHome version " ESPHOME_VERSION
" compiled on %s", ESPHOME_BUILD_TIME_STR);
164#ifdef ESPHOME_PROJECT_NAME
165 ESP_LOGI(TAG,
"Project " ESPHOME_PROJECT_NAME
" version " ESPHOME_PROJECT_VERSION);
168 esp_chip_info_t chip_info;
169 esp_chip_info(&chip_info);
170 ESP_LOGI(TAG,
"ESP32 Chip: %s rev%d.%d, %d core(s)", ESPHOME_VARIANT, chip_info.revision / 100,
171 chip_info.revision % 100, chip_info.cores);
172 [[maybe_unused]]
static const char *
const ESP32_ADVANCED_PATH =
"under esp32 > framework > advanced";
173#if ESPHOME_FLASH_VENDOR_DRIVERS > 0
176 const spi_flash_chip_t *flash_driver = esp_flash_default_chip->chip_drv;
177#if ESPHOME_FLASH_VENDOR_DRIVERS > 1
178 constexpr bool idle_driver =
true;
180 const bool idle_driver = flash_driver == &esp_flash_chip_generic;
183 const char *value = flash_driver->name;
184#ifdef CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
185 if (flash_driver == &esp_flash_chip_mxic_opi)
188 ESP_LOGW(TAG,
"Set flash_chip: %s %s to save IRAM", value, ESP32_ADVANCED_PATH);
192#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_MIN_CHIP_REVISION_SET)
195 if (chip_info.revision >= 300) {
197 ESP_LOGW(TAG,
"Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to save ~10KB IRAM",
198 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
200 ESP_LOGW(TAG,
"Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to reduce binary size",
201 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
216 esp_bootloader_desc_t boot_desc;
217 if (esp_ota_get_bootloader_description(
nullptr, &boot_desc) != ESP_OK) {
218#ifdef USE_ESP32_VARIANT_ESP32
219 ESP_LOGW(TAG,
"Bootloader too old for OTA rollback and SRAM1 as IRAM (+40KB). "
220 "Flash via USB once to update the bootloader");
222 ESP_LOGW(TAG,
"Bootloader too old for OTA rollback. Flash via USB once to update the bootloader");
225#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_SRAM1_AS_IRAM)
227 ESP_LOGW(TAG,
"Bootloader supports SRAM1 as IRAM (+40KB). Set sram1_as_iram: true %s", ESP32_ADVANCED_PATH);
286 if ((
component->get_component_state() & flag) != 0)
293 ESP_LOGI(TAG,
"Forcing a reboot");
300 ESP_LOGI(TAG,
"Rebooting safely");
323 uint32_t start_time = MillisInternal::get();
334 for (
size_t i = 0; i < num_components; ++i) {
335 pending_components[i] = this->
components_[num_components - 1 - i];
339 size_t pending_count = num_components;
378 while (pending_count > 0 && (now - start_time) < timeout_ms) {
383 size_t still_pending = 0;
384 for (
size_t i = 0; i < pending_count; ++i) {
385 if (!pending_components[i]->teardown()) {
387 if (still_pending != i) {
388 pending_components[still_pending] = pending_components[i];
394 pending_count = still_pending;
397 if (pending_count > 0) {
402 now = MillisInternal::get();
405 if (pending_count > 0) {
408 for (
size_t i = 0; i < pending_count; ++i) {
409 ESP_LOGW(TAG,
"%s did not complete teardown within %" PRIu32
" ms",
410 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
417 if (obj->has_overridden_loop() &&
432 this->looping_components_active_end_--;
433 if (i != this->looping_components_active_end_) {
492 bool has_pending =
false;
517 ESP_LOGVV(TAG,
"%s loop enabled from ISR", LOG_STR_ARG(
component->get_component_log_str()));
533#ifndef __GXX_ABI_VERSION
534#error "Application placement new requires Itanium C++ ABI (GCC/Clang)"
536static_assert(std::is_default_constructible<Application>::value,
"Application must be default-constructible");
541#define ESPHOME_STRINGIFY_IMPL_(x) #x
542#define ESPHOME_STRINGIFY_(x) ESPHOME_STRINGIFY_IMPL_(x)
545 ESPHOME_STRINGIFY_(__USER_LABEL_PREFIX__)
"_ZN7esphome3AppE");
546#undef ESPHOME_STRINGIFY_
547#undef ESPHOME_STRINGIFY_IMPL_
550 ESPHOME_strncpy_P(
buffer.data(), ESPHOME_BUILD_TIME_STR,
buffer.size());
555 ESPHOME_strncpy_P(
buffer.data(), ESPHOME_COMMENT_STR, ESPHOME_COMMENT_SIZE);
556 buffer[ESPHOME_COMMENT_SIZE - 1] =
'\0';
void enable_pending_loops_()
uint32_t ESPHOME_ALWAYS_INLINE scheduler_tick_(uint32_t now)
void setup()
Reserve space for components to avoid memory fragmentation.
void ESPHOME_ALWAYS_INLINE feed_wdt_with_time(uint32_t time)
Feed the task watchdog, hot entry.
uint16_t looping_components_active_end_
void service_status_led_slow_(uint32_t time)
Slow path for the status_led dispatch rate limit.
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
StaticVector< Component *, ESPHOME_COMPONENT_COUNT > components_
bool any_component_has_status_flag_(uint8_t flag) const
Walk all registered components looking for any whose component_state_ has the given flag set.
void get_build_time_string(std::span< char, BUILD_TIME_STR_SIZE > buffer)
Copy the build time string into the provided buffer Buffer must be BUILD_TIME_STR_SIZE bytes (compile...
void enable_component_loop_(Component *component)
uint32_t loop_component_start_time_
void feed_wdt()
Feed the task watchdog.
void disable_component_loop_(Component *component)
void activate_looping_component_(uint16_t index)
uint32_t last_status_led_service_
void teardown_components(uint32_t timeout_ms)
Teardown all components with a timeout.
FixedVector< Component * > looping_components_
void add_looping_components_by_state_(bool match_loop_done)
volatile bool has_pending_enable_loop_requests_
void get_comment_string(std::span< char, ESPHOME_COMMENT_SIZE_MAX > buffer)
Copy the comment string into the provided buffer.
void feed_wdt_slow_(uint32_t time)
Slow path for feed_wdt(): actually calls arch_feed_wdt() and updates last_wdt_feed_.
void schedule_dump_config()
void run_safe_shutdown_hooks()
uint16_t current_loop_index_
time_t get_build_time()
Get the build time as a Unix timestamp.
uint32_t get_config_version_hash()
Get the config hash extended with ESPHome version.
void calculate_looping_components_()
void register_component_impl_(Component *comp, bool has_loop)
void run_powerdown_hooks()
float get_actual_setup_priority() const
uint8_t get_component_state() const
virtual bool can_proceed()
uint8_t component_state_
State of this component - each bit has a purpose: Bits 0-2: Component state (0x00=CONSTRUCTION,...
Minimal static vector - saves memory by avoiding std::vector overhead.
const Component * component
TaskHandle_t esphome_main_task_handle
Main loop task handle and wake helpers — shared between wake.h (C++) and lwip_fast_select....
ESPHOME_LOG_TAG(TAG, CRASH_LOG_TAG)
void ESPHOME_ALWAYS_INLINE wakeable_delay(uint32_t ms)
Host wakeable_delay uses select() over the registered fds — defined in wake_host.cpp.
StatusLED * global_status_led
constexpr uint8_t COMPONENT_HAS_LOOP
constexpr uint8_t STATUS_LED_MASK
constexpr uint8_t COMPONENT_STATE_LOOP
constexpr uint8_t APP_STATE_SETUP_COMPLETE
constexpr uint8_t STATUS_LED_WARNING
constexpr uint8_t COMPONENT_STATE_MASK
void clear_setup_priority_overrides()
constexpr uint8_t COMPONENT_STATE_LOOP_DONE
void wake_setup()
One-time setup of the loopback wake socket. Called from Application::setup().