ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
hal.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ZEPHYR
4
5#include <cstdint>
6
7#include <zephyr/kernel.h>
8
9#define IRAM_ATTR
10#define PROGMEM
11
12namespace esphome::zephyr {}
13
14namespace esphome {
15
17__attribute__((always_inline)) inline bool in_isr_context() { return k_is_in_isr(); }
18
19inline k_tid_t &main_loop_thread() {
20 static k_tid_t thread = nullptr;
21 return thread;
22}
23
26__attribute__((always_inline)) inline bool is_main_loop_thread() {
27 return !in_isr_context() && k_current_get() == main_loop_thread();
28}
29
30__attribute__((always_inline)) inline void yield() { ::k_yield(); }
31__attribute__((always_inline)) inline void delay(uint32_t ms) { ::k_msleep(ms); }
32#if CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC == 1000000
33// The system timer counts microseconds (the GRTC on nRF54L) and wraps at 2^32 like micros().
34__attribute__((always_inline)) inline uint32_t micros() { return k_cycle_get_32(); }
35#elif defined(CONFIG_SOC_SERIES_NRF52X) || defined(CONFIG_SOC_SERIES_NRF52) // the name changed in sdk-nrf 3
36#define USE_ZEPHYR_DWT_MICROS
37uint32_t micros(); // CPU cycles kept close to the kernel clock, see hal.cpp
38#else
39__attribute__((always_inline)) inline uint32_t micros() { return k_ticks_to_us_floor32(k_uptime_ticks()); }
40#endif
41__attribute__((always_inline)) inline uint64_t millis_64() { return static_cast<uint64_t>(k_uptime_get()); }
42__attribute__((always_inline)) inline uint32_t millis() { return static_cast<uint32_t>(millis_64()); }
43
44// NOLINTNEXTLINE(readability-identifier-naming)
45__attribute__((always_inline)) inline void delayMicroseconds(uint32_t us) { ::k_busy_wait(us); }
46uint32_t arch_get_cpu_cycle_count(); // Stops while the CPU sleeps on nRF52, so only for busy-wait timing.
48
49void arch_feed_wdt();
50void arch_init();
51
52} // namespace esphome
53
54#endif // USE_ZEPHYR
struct @67::@68 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
void yield(void)
uint32_t arch_get_cpu_cycle_count()
Definition hal.cpp:71
void arch_init()
Definition hal.cpp:44
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
pthread_t & main_loop_thread()
Definition hal.h:20
uint32_t arch_get_cpu_freq_hz()
Definition hal.cpp:57
uint64_t millis_64()
Definition hal.cpp:29
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
void arch_feed_wdt()
Definition hal.cpp:133
void HOT delay(uint32_t ms)
Definition hal.cpp:78
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t