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