ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
wake_rp2040.cpp
Go to the documentation of this file.
2
3#ifdef USE_RP2040
4
5#include "esphome/core/hal.h"
6#include "esphome/core/wake.h"
7
8#include <hardware/sync.h>
9#include <pico/time.h>
10
11namespace esphome {
12
13// === Wake-requested flag + main-loop woke flag storage ===
14// RP2040 is always ESPHOME_THREAD_SINGLE.
15// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
16volatile uint8_t g_wake_requested = 0;
17volatile bool g_main_loop_woke = false;
18// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
19
20// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
21static volatile bool s_delay_expired = false;
22
23static int64_t alarm_callback_(alarm_id_t id, void *user_data) {
24 (void) id;
25 (void) user_data;
26 s_delay_expired = true;
27 __sev();
28 return 0;
29}
30
31namespace internal {
32void wakeable_delay(uint32_t ms) {
33 if (ms == 0) [[unlikely]] {
34 yield();
35 return;
36 }
37 if (g_main_loop_woke) {
38 g_main_loop_woke = false;
39 // Yield even on the already-woken fast path so callers in tight loops
40 // (e.g. lwIP raw TCP wait_for_data_) make forward progress when async
41 // wakes keep re-setting g_main_loop_woke between iterations.
42 yield();
43 return;
44 }
45 s_delay_expired = false;
46 alarm_id_t alarm = add_alarm_in_ms(ms, alarm_callback_, nullptr, true);
47 if (alarm <= 0) {
48 delay(ms);
49 return;
50 }
51 while (!g_main_loop_woke && !s_delay_expired) {
52 __wfe();
53 }
54 if (!s_delay_expired)
55 cancel_alarm(alarm);
56 g_main_loop_woke = false;
57}
58} // namespace internal
59
60} // namespace esphome
61
62#endif // USE_RP2040
void yield(void)
void ESPHOME_ALWAYS_INLINE wakeable_delay(uint32_t ms)
Host wakeable_delay uses select() over the registered fds — defined in wake_host.cpp.
std::atomic< uint8_t > g_wake_requested
Definition wake.h:49
volatile bool g_main_loop_woke
void HOT delay(uint32_t ms)
Definition hal.cpp:82
static void uint32_t
Platform-specific main loop wake primitives.