ESPHome 2025.9.0-dev
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_task_wdt.h>
10#include <esp_timer.h>
11#include <soc/rtc.h>
12
13#include <hal/cpu_hal.h>
14
15#ifdef USE_ARDUINO
16#include <Esp.h>
17#else
18#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
19#include <esp_clk_tree.h>
20#endif
21void setup();
22void loop();
23#endif
24
25namespace esphome {
26
27void IRAM_ATTR HOT yield() { vPortYield(); }
28uint32_t IRAM_ATTR HOT millis() { return (uint32_t) (esp_timer_get_time() / 1000ULL); }
29void IRAM_ATTR HOT delay(uint32_t ms) { vTaskDelay(ms / portTICK_PERIOD_MS); }
30uint32_t IRAM_ATTR HOT micros() { return (uint32_t) esp_timer_get_time(); }
31void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); }
33 esp_restart();
34 // restart() doesn't always end execution
35 while (true) { // NOLINT(clang-diagnostic-unreachable-code)
36 yield();
37 }
38}
39
40void arch_init() {
41 // Enable the task watchdog only on the loop task (from which we're currently running)
42#if defined(USE_ESP_IDF)
43 esp_task_wdt_add(nullptr);
44 // Idle task watchdog is disabled on ESP-IDF
45#elif defined(USE_ARDUINO)
46 enableLoopWDT();
47 // Disable idle task watchdog on the core we're using (Arduino pins the task to a core)
48#if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0) && CONFIG_ARDUINO_RUNNING_CORE == 0
49 disableCore0WDT();
50#endif
51#if defined(CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1) && CONFIG_ARDUINO_RUNNING_CORE == 1
52 disableCore1WDT();
53#endif
54#endif
55}
56void IRAM_ATTR HOT arch_feed_wdt() { esp_task_wdt_reset(); }
57
58uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
59uint32_t arch_get_cpu_cycle_count() { return esp_cpu_get_cycle_count(); }
61 uint32_t freq = 0;
62#ifdef USE_ESP_IDF
63#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
64 esp_clk_tree_src_get_freq_hz(SOC_MOD_CLK_CPU, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &freq);
65#else
66 rtc_cpu_freq_config_t config;
67 rtc_clk_cpu_freq_get_config(&config);
68 freq = config.freq_mhz * 1000000U;
69#endif
70#elif defined(USE_ARDUINO)
71 freq = ESP.getCpuFreqMHz() * 1000000;
72#endif
73 return freq;
74}
75
76#ifdef USE_ESP_IDF
77TaskHandle_t loop_task_handle = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
78
79void loop_task(void *pv_params) {
80 setup();
81 while (true) {
82 loop();
83 }
84}
85
86extern "C" void app_main() {
88 xTaskCreate(loop_task, "loopTask", 8192, nullptr, 1, &loop_task_handle);
89}
90#endif // USE_ESP_IDF
91
92#ifdef USE_ARDUINO
93extern "C" void init() { esp32::setup_preferences(); }
94#endif // USE_ARDUINO
95
96} // namespace esphome
97
98#endif // USE_ESP32
void setup()
void loop()
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:59
void arch_init()
Definition core.cpp:40
void app_main()
Definition core.cpp:86
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:31
void IRAM_ATTR HOT yield()
Definition core.cpp:27
uint32_t arch_get_cpu_freq_hz()
Definition core.cpp:60
TaskHandle_t loop_task_handle
Definition core.cpp:77
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:30
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:611
void IRAM_ATTR HOT arch_feed_wdt()
Definition core.cpp:56
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
void arch_restart()
Definition core.cpp:32
void loop_task(void *pv_params)
Definition core.cpp:79
void init()
Definition core.cpp:93
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:58