ESPHome 2026.11.0b6
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 LoopBlockingGuard::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_VARIANT_RTL8710B)
39 // The tick is 976.6 us here, so millis() reads the system timer (rtl8710b_micros.c)
40 return millis();
41#elif defined(USE_LIBRETINY) && (defined(USE_RTL87XX) || defined(USE_LN882X))
42 // 1 kHz: xTaskGetTickCount() is already ms.
43 static_assert(configTICK_RATE_HZ == 1000, "MillisInternal fast path requires 1 kHz FreeRTOS tick");
44 return xTaskGetTickCount();
45#elif defined(USE_BK72XX)
46 // 500 Hz: scale by portTICK_PERIOD_MS (== 2). Inlined to avoid the
47 // out-of-line call to esphome::millis() (IRAM_ATTR is a no-op on BK72xx —
48 // SDK masks FIQ + IRQ during flash writes, see hal.h).
49 static_assert(configTICK_RATE_HZ == 500, "BK72xx MillisInternal assumes 500 Hz FreeRTOS tick");
50 return xTaskGetTickCount() * portTICK_PERIOD_MS;
51#else
52 return millis();
53#endif
54 }
55 friend class Application;
56 friend class LoopBlockingGuard;
58};
59
60} // namespace esphome
Leaves a stretch of the current loop pass out of the blocking warning.
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t