ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
wake_zephyr.cpp
Go to the documentation of this file.
2
3#ifdef USE_ZEPHYR
4
5#include "esphome/core/hal.h"
6#include "esphome/core/wake.h"
7
8#include <zephyr/kernel.h>
9
10namespace esphome {
11
12// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
13K_SEM_DEFINE(esphome_wake_sem, 0, 1);
14
15// === Wake-requested flag storage ===
16// Zephyr has preemptive threads and ISRs, so wake_loop_threadsafe() is genuinely
17// called cross-context. volatile uint8_t is sufficient because: (1) Cortex-M
18// 8-bit aligned store/load is a single non-tearing instruction, and (2) every
19// producer pairs the store with k_sem_give() (release barrier) and the consumer
20// pairs the load with k_sem_take() (acquire barrier).
21// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
22volatile uint8_t g_wake_requested = 0;
23
25 wake_request_set();
26 k_sem_give(&esphome_wake_sem);
27}
28
29namespace internal {
30void wakeable_delay(uint32_t ms) {
31 if (ms == 0) [[unlikely]] {
32 yield();
33 return;
34 }
35 k_sem_take(&esphome_wake_sem, ms == UINT32_MAX ? K_FOREVER : K_MSEC(ms));
36}
37} // namespace internal
38
39} // namespace esphome
40
41#endif // USE_ZEPHYR
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
void wake_loop_threadsafe()
Non-ISR: always inline.
K_SEM_DEFINE(esphome_wake_sem, 0, 1)
static void uint32_t
Platform-specific main loop wake primitives.