From e6c1a4670cf04cd3290609abeb733ea3c38aff4f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 2 Mar 2026 13:48:26 -1000 Subject: [PATCH] [bk72xx] Fix ~100ms loop stalls by raising main task priority The BK72xx SDK creates the main Arduino task at FreeRTOS priority 3, which is lower than all WiFi (4-5), LwIP (4), and TCP/IP (7) tasks. This causes the main loop to be preempted for ~100ms whenever WiFi background processing runs (beacon handling, DHCP, WPA supplicant). By contrast, RTL87xx creates its main task at osPriorityRealtime (the highest priority), so it never experiences this issue. Raise the BK72xx main task priority to 6 during arch_init(): above WiFi/LwIP tasks (4-5) so they don't preempt the main loop, but below the TCP/IP thread (7) so packet processing keeps priority. This is safe because ESPHome yields voluntarily via yield_with_select_() and the Arduino mainTask yield() after each loop() iteration. Tested on CB3S (BK7231N) - loop time drops from ~110ms to ~14ms. --- esphome/components/libretiny/core.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/esphome/components/libretiny/core.cpp b/esphome/components/libretiny/core.cpp index 4dda7c3856..34a3fda3ad 100644 --- a/esphome/components/libretiny/core.cpp +++ b/esphome/components/libretiny/core.cpp @@ -6,6 +6,9 @@ #include "esphome/core/helpers.h" #include "preferences.h" +#include +#include + void setup(); void loop(); @@ -20,6 +23,21 @@ void IRAM_ATTR HOT delayMicroseconds(uint32_t us) { ::delayMicroseconds(us); } void arch_init() { libretiny::setup_preferences(); lt_wdt_enable(10000L); +#ifdef USE_BK72XX + // BK72xx SDK creates the main Arduino task at priority 3, which is lower than + // all WiFi (4-5), LwIP (4), and TCP/IP (7) tasks. This causes ~100ms loop + // stalls whenever WiFi background processing runs, because the main task + // cannot resume until every higher-priority task finishes. + // + // By contrast, RTL87xx creates the main task at osPriorityRealtime (highest). + // + // Raise to priority 6: above WiFi/LwIP tasks (4-5) so they don't preempt the + // main loop, but below the TCP/IP thread (7) so packet processing keeps priority. + // This is safe because ESPHome yields voluntarily via yield_with_select_() and + // the Arduino mainTask yield() after each loop() iteration. + static constexpr UBaseType_t MAIN_TASK_PRIORITY = 6; + vTaskPrioritySet(nullptr, MAIN_TASK_PRIORITY); +#endif #if LT_GPIO_RECOVER lt_gpio_recover(); #endif