ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
millis_internal.h
Go to the documentation of this file.
1#pragma once
2
3#include "esphome/core/hal.h"
5
6#if defined(USE_ESP32)
7#include <freertos/FreeRTOS.h>
8#include <freertos/task.h>
9#include <sdkconfig.h>
10#elif defined(USE_LIBRETINY)
11#include <FreeRTOS.h>
12#include <task.h>
13#endif
14
15namespace esphome {
16
17// Friend-gated accessor for a fast millis() variant intended only for
18// known task-context callers on the main loop hot path (Application::loop()
19// and WarnIfComponentBlockingGuard::finish()). It skips the ISR-context
20// dispatch that the public esphome::millis() pays on ESP32 and libretiny.
21//
22// MUST NOT be called from ISR context: on ESP32 and libretiny it calls the
23// non-FromISR FreeRTOS API directly, which is undefined behavior in ISR
24// context.
25//
26// Adding new callers requires adding a friend declaration here — that
27// is the review point. Do not relax the access (e.g. by making get()
28// public) without considering the ISR-safety contract.
29//
30// Other platforms currently delegate to the public millis(); the friend
31// gate still enforces the intent so platform-specific fast paths can be
32// added later without changing call sites.
34 private:
35 static ESPHOME_ALWAYS_INLINE uint32_t get() {
36#if defined(USE_ESP32) && CONFIG_FREERTOS_HZ == 1000
37 return xTaskGetTickCount();
38#elif defined(USE_LIBRETINY) && (defined(USE_RTL87XX) || defined(USE_LN882X))
39 // 1 kHz: xTaskGetTickCount() is already ms.
40 static_assert(configTICK_RATE_HZ == 1000, "MillisInternal fast path requires 1 kHz FreeRTOS tick");
41 return xTaskGetTickCount();
42#elif defined(USE_BK72XX)
43 // 500 Hz: scale by portTICK_PERIOD_MS (== 2). Inlined to avoid the
44 // out-of-line call to esphome::millis() (IRAM_ATTR is a no-op on BK72xx —
45 // SDK masks FIQ + IRQ during flash writes, see hal.h).
46 static_assert(configTICK_RATE_HZ == 500, "BK72xx MillisInternal assumes 500 Hz FreeRTOS tick");
47 return xTaskGetTickCount() * portTICK_PERIOD_MS;
48#else
49 return millis();
50#endif
51 }
52 friend class Application;
54};
55
56} // namespace esphome
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t