ESPHome 2025.5.2
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; }
59#if ESP_IDF_VERSION_MAJOR >= 5
60uint32_t arch_get_cpu_cycle_count() { return esp_cpu_get_cycle_count(); }
61#else
62uint32_t arch_get_cpu_cycle_count() { return cpu_hal_get_cycle_count(); }
63#endif
65 uint32_t freq = 0;
66#ifdef USE_ESP_IDF
67#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
68 esp_clk_tree_src_get_freq_hz(SOC_MOD_CLK_CPU, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &freq);
69#else
70 rtc_cpu_freq_config_t config;
71 rtc_clk_cpu_freq_get_config(&config);
72 freq = config.freq_mhz * 1000000U;
73#endif
74#elif defined(USE_ARDUINO)
75 freq = ESP.getCpuFreqMHz() * 1000000;
76#endif
77 return freq;
78}
79
80#ifdef USE_ESP_IDF
81TaskHandle_t loop_task_handle = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
82
83void loop_task(void *pv_params) {
84 setup();
85 while (true) {
86 loop();
87 }
88}
89
90extern "C" void app_main() {
92 xTaskCreate(loop_task, "loopTask", 8192, nullptr, 1, &loop_task_handle);
93}
94#endif // USE_ESP_IDF
95
96#ifdef USE_ARDUINO
97extern "C" void init() { esp32::setup_preferences(); }
98#endif // USE_ARDUINO
99
100} // namespace esphome
101
102#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:60
void arch_init()
Definition core.cpp:40
void app_main()
Definition core.cpp:90
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:64
TaskHandle_t loop_task_handle
Definition core.cpp:81
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:773
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:83
void init()
Definition core.cpp:97
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:58