diff --git a/esphome/components/libretiny/core.cpp b/esphome/components/libretiny/core.cpp index 1d7cb6ff60..1b74e3addb 100644 --- a/esphome/components/libretiny/core.cpp +++ b/esphome/components/libretiny/core.cpp @@ -16,23 +16,23 @@ void loop(); namespace esphome { void HOT yield() { ::yield(); } -// Skip the Arduino core's ::millis() wrapper so the public esphome::millis() is -// inlinable at its call sites and matches MillisInternal::get()'s fast path. -// IRAM_ATTR is kept because components (e.g. rotary_encoder) call millis() from -// ISR handlers; xTaskGetTickCountFromISR() is used in ISR context to satisfy the -// FreeRTOS API contract. +// Inline the tick read so esphome::millis() matches MillisInternal::get()'s fast +// path instead of going through the Arduino core's out-of-line ::millis() wrapper. // -// RTL87xx and LN882x run FreeRTOS at 1 kHz, so xTaskGetTickCount() is already in -// milliseconds. BK72xx runs at 500 Hz — multiply by portTICK_PERIOD_MS (== 2) to -// convert ticks to milliseconds, matching the Arduino core's wiring.c. +// RTL87xx / LN882x (1 kHz): xTaskGetTickCount() is already ms. IRAM_ATTR + ISR +// dispatch are needed because ISR handlers (e.g. rotary_encoder) call millis(). +// +// BK72xx (500 Hz): ticks * portTICK_PERIOD_MS (== 2). IRAM_ATTR and ISR dispatch +// are both unnecessary — the SDK masks FIQ + IRQ during flash writes (see hal.h), +// so no ISR runs while flash is stalled. #if defined(USE_RTL87XX) || defined(USE_LN882X) uint32_t IRAM_ATTR HOT millis() { static_assert(configTICK_RATE_HZ == 1000, "millis() fast path requires 1 kHz FreeRTOS tick"); return in_isr_context() ? xTaskGetTickCountFromISR() : xTaskGetTickCount(); } #elif defined(USE_BK72XX) -uint32_t IRAM_ATTR HOT millis() { - // BK72xx's Arduino millis() does not dispatch on ISR context; match that. +uint32_t HOT millis() { + static_assert(configTICK_RATE_HZ == 500, "BK72xx millis() fast path assumes 500 Hz FreeRTOS tick"); return xTaskGetTickCount() * portTICK_PERIOD_MS; } #else diff --git a/esphome/core/millis_internal.h b/esphome/core/millis_internal.h index ded49f9cd2..bc1d55a1c4 100644 --- a/esphome/core/millis_internal.h +++ b/esphome/core/millis_internal.h @@ -36,14 +36,13 @@ class MillisInternal { #if defined(USE_ESP32) && CONFIG_FREERTOS_HZ == 1000 return xTaskGetTickCount(); #elif defined(USE_LIBRETINY) && (defined(USE_RTL87XX) || defined(USE_LN882X)) - // RTL87xx and LN882x run FreeRTOS at 1 kHz, so xTaskGetTickCount() is - // already in milliseconds. + // 1 kHz: xTaskGetTickCount() is already ms. static_assert(configTICK_RATE_HZ == 1000, "MillisInternal fast path requires 1 kHz FreeRTOS tick"); return xTaskGetTickCount(); #elif defined(USE_BK72XX) - // BK72xx runs FreeRTOS at 500 Hz; scale ticks by portTICK_PERIOD_MS (== 2). - // Inlined here because esphome::millis() on BK72xx has no-op IRAM_ATTR but - // is still out-of-line, so calling it would cost a real function call. + // 500 Hz: scale by portTICK_PERIOD_MS (== 2). Inlined to avoid the + // out-of-line call to esphome::millis() (IRAM_ATTR is a no-op on BK72xx — + // SDK masks FIQ + IRQ during flash writes, see hal.h). static_assert(configTICK_RATE_HZ == 500, "BK72xx MillisInternal assumes 500 Hz FreeRTOS tick"); return xTaskGetTickCount() * portTICK_PERIOD_MS; #else