ESPHome 2026.11.0-dev
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
24void wake_scheduler_threadsafe() { k_sem_give(&esphome_wake_sem); }
25
27 wake_request_set();
29}
30
31namespace internal {
32void wakeable_delay(uint32_t ms) {
33 if (ms == 0) [[unlikely]] {
34 yield();
35 return;
36 }
37 k_sem_take(&esphome_wake_sem, ms == UINT32_MAX ? K_FOREVER : K_MSEC(ms));
38}
39} // namespace internal
40
41} // namespace esphome
42
43#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.
void wake_scheduler_threadsafe()
K_SEM_DEFINE(esphome_wake_sem, 0, 1)
static void uint32_t
Platform-specific main loop wake primitives.