ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
hal.cpp
Go to the documentation of this file.
1#ifdef USE_ESP8266
2
3#include "esphome/core/hal.h"
5
6#include <Arduino.h>
7#include <core_esp8266_features.h>
8#include <coredecls.h>
9
10extern "C" {
11#include <user_interface.h>
12}
13
14// Empty esp8266 namespace block to satisfy ci-custom's lint_namespace check.
15// HAL functions live in namespace esphome (root) — they are not part of the
16// esp8266 component's API.
17namespace esphome::esp8266 {} // namespace esphome::esp8266
18
19namespace esphome {
20
21// yield(), micros(), millis_64(), delayMicroseconds(), arch_feed_wdt(),
22// progmem_read_*() are inlined in components/esp8266/hal.h.
23//
24// Fast accumulator replacement for Arduino's millis() (~3.3 μs via 4× 64-bit
25// multiplies on the LX106). Tracks a running ms counter from 32-bit
26// system_get_time() deltas using pure 32-bit ops. Installed as __wrap_millis
27// (via -Wl,--wrap=millis) so Arduino libs and IRAM_ATTR ISR handlers (e.g.
28// Wiegand, ZyAura) also get the fast version. xt_rsil(15) guards the static
29// state against ISR re-entry; the critical section is bounded (≤10 while-loop
30// iterations, ~100 ns on the common path, or a constant-time /1000 ~2.5 μs on
31// the rare path — well under WiFi's ~10 μs ISR latency budget). NMIs (level
32// >15) are not masked, but the ESP8266 SDK's NMI handlers don't call millis().
33//
34// system_get_time() wraps every ~71.6 min; unsigned (now_us - last_us) handles
35// one wrap. The main loop calls millis() at 60+ Hz, so delta stays tiny — a
36// >71 min block would trip the watchdog long before it could matter here.
37static constexpr uint32_t MILLIS_RARE_PATH_THRESHOLD_US = 10000;
38static constexpr uint32_t US_PER_MS = 1000;
39
40uint32_t IRAM_ATTR HOT millis() {
41 // Struct packs the three statics so the compiler loads one base address
42 // instead of three separate literal pool entries (saves ~8 bytes IRAM).
43 static struct {
44 uint32_t cache;
45 uint32_t remainder;
46 uint32_t last_us;
47 } state = {0, 0, 0};
48 uint32_t ps = xt_rsil(15);
49 uint32_t now_us = system_get_time();
50 uint32_t delta = now_us - state.last_us;
51 state.last_us = now_us;
52 state.remainder += delta;
53 if (state.remainder >= MILLIS_RARE_PATH_THRESHOLD_US) {
54 // Rare path: large gap (WiFi scan, boot, long block). Constant-time
55 // conversion keeps the critical section bounded.
56 uint32_t ms = state.remainder / US_PER_MS;
57 state.cache += ms;
58 // Reuse ms instead of `remainder %= US_PER_MS` — `%` would compile to a
59 // second __umodsi3 call on the LX106 (no hardware divide).
60 state.remainder -= ms * US_PER_MS;
61 } else {
62 // Common path: small gap. At most ~10 iterations since remainder was
63 // < threshold (10 ms) on entry and delta adds at most one more threshold
64 // before exiting this branch.
65 while (state.remainder >= US_PER_MS) {
66 state.cache++;
67 state.remainder -= US_PER_MS;
68 }
69 }
70 uint32_t result = state.cache;
71 xt_wsr_ps(ps);
72 return result;
73}
74
75// Arduino's os_timer-based esp_delay() suspends the cont task, so the SDK and WiFi run freely while
76// we wait. It returns on the first esp_schedule() from anywhere (wake_loop(), lwIP callbacks), so
77// suspend again for the rest of the time, measured with micros() so the wait never ends early.
78void HOT delay(uint32_t ms) {
79 if (ms == 0) {
80 optimistic_yield(1000);
81 return;
82 }
83 const uint32_t start = micros();
84 const uint32_t total_us = ms * 1000;
85 uint32_t elapsed;
86 // >> 10 avoids a software divide; it undercounts by 2.4%, so a long wait takes one more short suspend
87 while ((elapsed = micros() - start) < total_us)
88 esp_delay(((total_us - elapsed) >> 10) + 1);
89}
90
91void arch_restart() {
92 system_restart();
93 // restart() doesn't always end execution
94 while (true) { // NOLINT(clang-diagnostic-unreachable-code)
95 yield();
96 }
97}
98
99} // namespace esphome
100
101// Linker wrap: redirect all ::millis() calls (Arduino libs, ISRs) to our accumulator.
102// Requires -Wl,--wrap=millis in build flags (added by __init__.py).
103// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
104extern "C" uint32_t IRAM_ATTR __wrap_millis() { return esphome::millis(); }
105// Note: Arduino's init() registers a 60-second overflow timer for micros64().
106// We leave it running — wrapping init() as a no-op would break micros64()'s
107// overflow tracking, and the timer's cost is negligible (~3 μs per 60 s).
108
109#endif // USE_ESP8266
void yield(void)
uint32_t IRAM_ATTR __wrap_millis()
Definition hal.cpp:104
bool state
Definition fan.h:2
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
void HOT delay(uint32_t ms)
Definition hal.cpp:78
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
void arch_restart()
Definition hal.cpp:36
static void uint32_t
esp_err_t result