ESPHome 2026.4.0
Loading...
Searching...
No Matches
wake.h
Go to the documentation of this file.
1#pragma once
2
6
8#include "esphome/core/hal.h"
9
10#if defined(USE_ESP32) || defined(USE_LIBRETINY)
12#endif
13#ifdef USE_ESP8266
14#include <coredecls.h>
15#elif defined(USE_RP2040)
16#include <hardware/sync.h>
17#include <pico/time.h>
18#endif
19
20namespace esphome {
21
22// === Wake flag for ESP8266/RP2040 ===
23#if defined(USE_ESP8266) || defined(USE_RP2040)
24// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
25extern volatile bool g_main_loop_woke;
26#endif
27
28// === ESP32 / LibreTiny (FreeRTOS) ===
29#if defined(USE_ESP32) || defined(USE_LIBRETINY)
30
31#ifdef USE_ESP32
33void wake_loop_isrsafe(BaseType_t *px_higher_priority_task_woken);
36#else
40inline void wake_loop_any_context() { esphome_main_task_notify(); }
41#endif
42
43inline void wake_loop_threadsafe() { esphome_main_task_notify(); }
44
45namespace internal {
46inline void wakeable_delay(uint32_t ms) {
47 if (ms == 0) {
48 yield();
49 return;
50 }
51 ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(ms));
52}
53} // namespace internal
54
55// === ESP8266 ===
56#elif defined(USE_ESP8266)
57
59inline void ESPHOME_ALWAYS_INLINE wake_loop_impl() {
60 g_main_loop_woke = true;
61 esp_schedule();
62}
63
66
68inline void wake_loop_threadsafe() { wake_loop_impl(); }
69
70namespace internal {
71inline void wakeable_delay(uint32_t ms) {
72 if (ms == 0) {
73 delay(0);
74 return;
75 }
76 if (g_main_loop_woke) {
77 g_main_loop_woke = false;
78 return;
79 }
80 esp_delay(ms, []() { return !g_main_loop_woke; });
81}
82} // namespace internal
83
84// === RP2040 ===
85#elif defined(USE_RP2040)
86
87inline void wake_loop_any_context() {
88 g_main_loop_woke = true;
89 __sev();
90}
91
93
95namespace internal {
97} // namespace internal
98
99// === Host / Zephyr / other ===
100#else
101
102#ifdef USE_HOST
105#else
109inline void wake_loop_threadsafe() {}
110#endif
111
113
114namespace internal {
115inline void wakeable_delay(uint32_t ms) {
116 if (ms == 0) {
117 yield();
118 return;
119 }
120 delay(ms);
121}
122} // namespace internal
123
124#endif
125
126} // namespace esphome
void wakeable_delay(uint32_t ms)
Definition wake.cpp:47
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void wake_loop_threadsafe()
Non-ISR: always inline.
Definition wake.cpp:74
void ESPHOME_ALWAYS_INLINE wake_loop_impl()
Inline implementation — IRAM callers inline this directly.
Definition wake.h:59
volatile bool g_main_loop_woke
Definition wake.cpp:26
void HOT yield()
Definition core.cpp:25
void HOT delay(uint32_t ms)
Definition core.cpp:28
void IRAM_ATTR wake_loop_any_context()
IRAM_ATTR entry point — defined in wake.cpp.
Definition wake.cpp:20
void IRAM_ATTR wake_loop_isrsafe(BaseType_t *px_higher_priority_task_woken)
IRAM_ATTR entry point — defined in wake.cpp.
Definition wake.cpp:17
static void uint32_t