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_() {
164 ESP_LOGI(TAG,
"ESPHome version " ESPHOME_VERSION
" compiled on %s", build_time_str);
165#ifdef ESPHOME_PROJECT_NAME
166 ESP_LOGI(TAG,
"Project " ESPHOME_PROJECT_NAME
" version " ESPHOME_PROJECT_VERSION);
169 esp_chip_info_t chip_info;
170 esp_chip_info(&chip_info);
171 ESP_LOGI(TAG,
"ESP32 Chip: %s rev%d.%d, %d core(s)", ESPHOME_VARIANT, chip_info.revision / 100,
172 chip_info.revision % 100, chip_info.cores);
173 [[maybe_unused]]
static const char *
const ESP32_ADVANCED_PATH =
"under esp32 > framework > advanced";
174#if ESPHOME_FLASH_VENDOR_DRIVERS > 0
177 const spi_flash_chip_t *flash_driver = esp_flash_default_chip->chip_drv;
178#if ESPHOME_FLASH_VENDOR_DRIVERS > 1
179 constexpr bool idle_driver =
true;
181 const bool idle_driver = flash_driver == &esp_flash_chip_generic;
184 const char *value = flash_driver->name;
185#ifdef CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
186 if (flash_driver == &esp_flash_chip_mxic_opi)
189 ESP_LOGW(TAG,
"Set flash_chip: %s %s to save IRAM", value, ESP32_ADVANCED_PATH);
193#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_MIN_CHIP_REVISION_SET)
196 if (chip_info.revision >= 300) {
198 ESP_LOGW(TAG,
"Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to save ~10KB IRAM",
199 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
201 ESP_LOGW(TAG,
"Chip rev >= 3.0 detected. Set minimum_chip_revision: \"%d.%d\" %s to reduce binary size",
202 chip_info.revision / 100, chip_info.revision % 100, ESP32_ADVANCED_PATH);
217 esp_bootloader_desc_t boot_desc;
218 if (esp_ota_get_bootloader_description(
nullptr, &boot_desc) != ESP_OK) {
219#ifdef USE_ESP32_VARIANT_ESP32
220 ESP_LOGW(TAG,
"Bootloader too old for OTA rollback and SRAM1 as IRAM (+40KB). "
221 "Flash via USB once to update the bootloader");
223 ESP_LOGW(TAG,
"Bootloader too old for OTA rollback. Flash via USB once to update the bootloader");
226#if defined(USE_ESP32_VARIANT_ESP32) && !defined(USE_ESP32_SRAM1_AS_IRAM)
228 ESP_LOGW(TAG,
"Bootloader supports SRAM1 as IRAM (+40KB). Set sram1_as_iram: true %s", ESP32_ADVANCED_PATH);
287 if ((
component->get_component_state() & flag) != 0)
294 ESP_LOGI(TAG,
"Forcing a reboot");
301 ESP_LOGI(TAG,
"Rebooting safely");
324 uint32_t start_time = MillisInternal::get();
335 for (
size_t i = 0; i < num_components; ++i) {
336 pending_components[i] = this->
components_[num_components - 1 - i];
340 size_t pending_count = num_components;
379 while (pending_count > 0 && (now - start_time) < timeout_ms) {
384 size_t still_pending = 0;
385 for (
size_t i = 0; i < pending_count; ++i) {
386 if (!pending_components[i]->teardown()) {
388 if (still_pending != i) {
389 pending_components[still_pending] = pending_components[i];
395 pending_count = still_pending;
398 if (pending_count > 0) {
403 now = MillisInternal::get();
406 if (pending_count > 0) {
409 for (
size_t i = 0; i < pending_count; ++i) {
410 ESP_LOGW(TAG,
"%s did not complete teardown within %" PRIu32
" ms",
411 LOG_STR_ARG(pending_components[i]->get_component_log_str()), timeout_ms);
418 if (obj->has_overridden_loop() &&
433 this->looping_components_active_end_--;
434 if (i != this->looping_components_active_end_) {
493 bool has_pending =
false;
518 ESP_LOGVV(TAG,
"%s loop enabled from ISR", LOG_STR_ARG(
component->get_component_log_str()));
534#ifndef __GXX_ABI_VERSION
535#error "Application placement new requires Itanium C++ ABI (GCC/Clang)"
537static_assert(std::is_default_constructible<Application>::value,
"Application must be default-constructible");
542#define ESPHOME_STRINGIFY_IMPL_(x) #x
543#define ESPHOME_STRINGIFY_(x) ESPHOME_STRINGIFY_IMPL_(x)
546 ESPHOME_STRINGIFY_(__USER_LABEL_PREFIX__)
"_ZN7esphome3AppE");
547#undef ESPHOME_STRINGIFY_
548#undef ESPHOME_STRINGIFY_IMPL_
551 ESPHOME_strncpy_P(
buffer.data(), ESPHOME_BUILD_TIME_STR,
buffer.size());
556 ESPHOME_strncpy_P(
buffer.data(), ESPHOME_COMMENT_STR, ESPHOME_COMMENT_SIZE);
557 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.
static constexpr size_t BUILD_TIME_STR_SIZE
Size of buffer required for build time string (including null terminator)
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....
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 uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
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()
ESPHOME_LOG_TAG(TAG, "app")
constexpr uint8_t COMPONENT_STATE_LOOP_DONE
void wake_setup()
One-time setup of the loopback wake socket. Called from Application::setup().