ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
core.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2
4#include "crash_handler.h"
6#include "esphome/core/hal.h"
8#include "preferences.h"
9#include <esp_clk_tree.h>
10#include <esp_cpu.h>
11#include <esp_idf_version.h>
12#include <esp_ota_ops.h>
13#include <esp_task_wdt.h>
14#include <esp_timer.h>
15#include <freertos/FreeRTOS.h>
16#include <freertos/task.h>
17
18void setup(); // NOLINT(readability-redundant-declaration)
19
20// Weak stub for initArduino - overridden when the Arduino component is present
21extern "C" __attribute__((weak)) void initArduino() {}
22
23namespace esphome {
24
25void HOT yield() { vPortYield(); }
26uint32_t IRAM_ATTR HOT millis() { return micros_to_millis(static_cast<uint64_t>(esp_timer_get_time())); }
27uint64_t HOT millis_64() { return micros_to_millis<uint64_t>(static_cast<uint64_t>(esp_timer_get_time())); }
28void HOT delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); }
29uint32_t IRAM_ATTR HOT micros() { return (uint32_t) esp_timer_get_time(); }
30void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); }
32 esp_restart();
33 // restart() doesn't always end execution
34 while (true) { // NOLINT(clang-diagnostic-unreachable-code)
35 yield();
36 }
37}
38
39void arch_init() {
40#ifdef USE_ESP32_CRASH_HANDLER
41 // Read crash data from previous boot before anything else
43#endif
44
45 // Enable the task watchdog only on the loop task (from which we're currently running)
46 esp_task_wdt_add(nullptr);
47
48 // Handle OTA rollback: mark partition valid immediately unless USE_OTA_ROLLBACK is enabled,
49 // in which case safe_mode will mark it valid after confirming successful boot.
50#ifndef USE_OTA_ROLLBACK
51 esp_ota_mark_app_valid_cancel_rollback();
52#endif
53}
54void HOT arch_feed_wdt() { esp_task_wdt_reset(); }
55
56uint32_t arch_get_cpu_cycle_count() { return esp_cpu_get_cycle_count(); }
58 uint32_t freq = 0;
59 esp_clk_tree_src_get_freq_hz(SOC_MOD_CLK_CPU, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &freq);
60 return freq;
61}
62
63TaskHandle_t loop_task_handle = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
64static StaticTask_t loop_task_tcb; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
65static StackType_t
66 loop_task_stack[ESPHOME_LOOP_TASK_STACK_SIZE]; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
67
68void loop_task(void *pv_params) {
69 setup();
70 while (true) {
71 App.loop();
72 }
73}
74
75extern "C" void app_main() {
76 initArduino();
78#if CONFIG_FREERTOS_UNICORE
79 loop_task_handle = xTaskCreateStatic(loop_task, "loopTask", ESPHOME_LOOP_TASK_STACK_SIZE, nullptr, 1, loop_task_stack,
80 &loop_task_tcb);
81#else
82 loop_task_handle = xTaskCreateStaticPinnedToCore(loop_task, "loopTask", ESPHOME_LOOP_TASK_STACK_SIZE, nullptr, 1,
83 loop_task_stack, &loop_task_tcb, 1);
84#endif
85}
86
87} // namespace esphome
88
89#endif // USE_ESP32
void ESPHOME_ALWAYS_INLINE loop()
Make a loop iteration. Call this in your loop() function.
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:32
void setup()
void setup_preferences()
void crash_handler_read_and_clear()
Read and validate crash data from NOINIT memory.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t arch_get_cpu_cycle_count()
Definition core.cpp:56
void arch_init()
Definition core.cpp:39
void app_main()
Definition core.cpp:75
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
void HOT yield()
Definition core.cpp:25
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:57
TaskHandle_t loop_task_handle
Definition core.cpp:63
uint64_t HOT millis_64()
Definition core.cpp:27
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:29
void IRAM_ATTR HOT delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
Definition helpers.cpp:908
void HOT arch_feed_wdt()
Definition core.cpp:54
constexpr ESPHOME_ALWAYS_INLINE ReturnT micros_to_millis(uint64_t us)
Convert a 64-bit microsecond count to milliseconds without calling __udivdi3 (software 64-bit divide,...
Definition helpers.h:848
void HOT delay(uint32_t ms)
Definition core.cpp:28
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
void arch_restart()
Definition core.cpp:31
Application App
Global storage of Application pointer - only one Application can exist.
void loop_task(void *pv_params)
Definition core.cpp:68
static void uint32_t