ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
core.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2
4#include "esphome/core/hal.h"
6#include "preferences.h"
7#include <freertos/FreeRTOS.h>
8#include <freertos/task.h>
9#include <esp_idf_version.h>
10#include <esp_ota_ops.h>
11#include <esp_task_wdt.h>
12#include <esp_timer.h>
13#include <soc/rtc.h>
14
15#include <hal/cpu_hal.h>
16
17#ifdef USE_ARDUINO
18#include <Esp.h>
19#else
20#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
21#include <esp_clk_tree.h>
22#endif
23void setup();
24void loop();
25#endif
26
27namespace esphome {
28
29void IRAM_ATTR HOT yield() { vPortYield(); }
30uint32_t IRAM_ATTR HOT millis() { return (uint32_t) (esp_timer_get_time() / 1000ULL); }
31void IRAM_ATTR HOT delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); }
32uint32_t IRAM_ATTR HOT micros() { return (uint32_t) esp_timer_get_time(); }
33void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); }
35 esp_restart();
36 // restart() doesn't always end execution
37 while (true) { // NOLINT(clang-diagnostic-unreachable-code)
38 yield();
39 }
40}
41
42void arch_init() {
43 // Enable the task watchdog only on the loop task (from which we're currently running)
44#if defined(USE_ESP_IDF)
45 esp_task_wdt_add(nullptr);
46 // Idle task watchdog is disabled on ESP-IDF
47#elif defined(USE_ARDUINO)
48 enableLoopWDT();
49 // Disable idle task watchdog on the core we're using (Arduino pins the task to a core)
50#if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0) && CONFIG_ARDUINO_RUNNING_CORE == 0
51 disableCore0WDT();
52#endif
53#if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1) && CONFIG_ARDUINO_RUNNING_CORE == 1
54 disableCore1WDT();
55#endif
56#endif
57
58 // If the bootloader was compiled with CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE the current
59 // partition will get rolled back unless it is marked as valid.
60 esp_ota_img_states_t state;
61 const esp_partition_t *running = esp_ota_get_running_partition();
62 if (esp_ota_get_state_partition(running, &state) == ESP_OK) {
63 if (state == ESP_OTA_IMG_PENDING_VERIFY) {
64 esp_ota_mark_app_valid_cancel_rollback();
65 }
66 }
67}
68void IRAM_ATTR HOT arch_feed_wdt() { esp_task_wdt_reset(); }
69
70uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
71uint32_t arch_get_cpu_cycle_count() { return esp_cpu_get_cycle_count(); }
73 uint32_t freq = 0;
74#ifdef USE_ESP_IDF
75#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
76 esp_clk_tree_src_get_freq_hz(SOC_MOD_CLK_CPU, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &freq);
77#else
78 rtc_cpu_freq_config_t config;
79 rtc_clk_cpu_freq_get_config(&config);
80 freq = config.freq_mhz * 1000000U;
81#endif
82#elif defined(USE_ARDUINO)
83 freq = ESP.getCpuFreqMHz() * 1000000;
84#endif
85 return freq;
86}
87
88#ifdef USE_ESP_IDF
89TaskHandle_t loop_task_handle = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
90
91void loop_task(void *pv_params) {
92 setup();
93 while (true) {
94 loop();
95 }
96}
97
98extern "C" void app_main() {
100#if CONFIG_FREERTOS_UNICORE
101 xTaskCreate(loop_task, "loopTask", ESPHOME_LOOP_TASK_STACK_SIZE, nullptr, 1, &loop_task_handle);
102#else
103 xTaskCreatePinnedToCore(loop_task, "loopTask", ESPHOME_LOOP_TASK_STACK_SIZE, nullptr, 1, &loop_task_handle, 1);
104#endif
105}
106#endif // USE_ESP_IDF
107
108#ifdef USE_ARDUINO
109extern "C" void init() { esp32::setup_preferences(); }
110#endif // USE_ARDUINO
111
112} // namespace esphome
113
114#endif // USE_ESP32
void setup()
void loop()
bool state
Definition fan.h:0
void setup_preferences()
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:71
void arch_init()
Definition core.cpp:42
void app_main()
Definition core.cpp:98
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:33
void IRAM_ATTR HOT yield()
Definition core.cpp:29
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:72
TaskHandle_t loop_task_handle
Definition core.cpp:89
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:32
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:671
void IRAM_ATTR HOT arch_feed_wdt()
Definition core.cpp:68
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:31
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:30
void arch_restart()
Definition core.cpp:34
void loop_task(void *pv_params)
Definition core.cpp:91
void init()
Definition core.cpp:109
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:70