From 30d694e62e9f501cba99732b6af396f0d94df50c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 21 Feb 2026 16:19:18 -0600 Subject: [PATCH] [libretiny] Enable MEMP_MEM_MALLOC=1 on BK72XX for dynamic MEMP pools Allocate all MEMP pools (TCP PCBs, UDP PCBs, netconns, TCP segments, pbufs, etc.) from the heap on demand instead of pre-allocated static arrays. Saves ~20KB RAM on BK7231N (87,312 B free vs 67,144 B before). Safe because the BK WiFi driver's receive path runs in task context (core_thread pops from g_wifi_core.io_queue), not ISR context. ESP32 and ESP8266 both ship with MEMP_MEM_MALLOC=1. Co-Authored-By: Claude Opus 4.6 --- esphome/components/libretiny/__init__.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/esphome/components/libretiny/__init__.py b/esphome/components/libretiny/__init__.py index 4e73ac9542..26be46dcd0 100644 --- a/esphome/components/libretiny/__init__.py +++ b/esphome/components/libretiny/__init__.py @@ -291,6 +291,8 @@ def _configure_lwip(config: dict) -> None: ──────────────────────────────────────────────────────────────────────────── TCP_SND_BUF 2×MSS 4×MSS 10×MSS 5×MSS 7×MSS 4×MSS TCP_WND 4×MSS 4×MSS 10×MSS 2×MSS 3×MSS 4×MSS + MEM_LIBC_MALLOC 1 1 0 0 1 1 BK + MEMP_MEM_MALLOC 1 1 0 0 0 1 BK MEM_SIZE N/A* N/A* 16/32KB 5KB N/A* N/A* BK PBUF_POOL_SIZE 10 16 3/10 20 20 10 BK MAX_SOCKETS_TCP 5 16 12 —** —** dynamic @@ -303,7 +305,8 @@ def _configure_lwip(config: dict) -> None: MEMP_NUM_NETBUF 0 2 16 2***** 8 4 MEMP_NUM_TCPIP_MSG_INPKT 4 8 16 8***** 12 8 - * ESP32 and LN882H use MEM_LIBC_MALLOC=1 (system heap, no dedicated pool). + * ESP8266/ESP32/LN882H use MEM_LIBC_MALLOC=1 (system heap, no dedicated pool). + ESP8266/ESP32 also use MEMP_MEM_MALLOC=1 (MEMP pools from heap, not static). ** RTL/LN SDKs don't define MAX_SOCKETS_TCP/UDP (LibreTiny-specific). *** BK LT overlay: MAX_SOCKETS_UDP+2+1 = 25. **** RTL/LN LT overlay overrides to flat 7. @@ -372,13 +375,21 @@ def _configure_lwip(config: dict) -> None: # - MEM_LIBC_MALLOC=1: Use system heap instead of dedicated lwIP heap. # BK SDK defaults to a 16/32KB dedicated pool (MEM_LIBC_MALLOC=0). # LN882H already uses MEM_LIBC_MALLOC=1. This eliminates the dedicated - # pool bottleneck that caused OTA slowness at MEM_SIZE=5KB. + # pool bottleneck that caused OTA slowness due to heap fragmentation. + # Safe because BK SDK wires mem_clib_malloc → os_malloc → pvPortMalloc + # (FreeRTOS thread-safe heap). + # - MEMP_MEM_MALLOC=1: Allocate MEMP pools from heap on demand instead + # of static arrays. Saves ~20KB RAM for unused pool entries. Safe because + # the BK WiFi driver's receive path runs in task context (core_thread), + # not ISR context — the ISR only posts to a queue. ESP32 and ESP8266 + # both ship with MEMP_MEM_MALLOC=1. # - PBUF_POOL_SIZE: BK SDK "reduced plan" sets this to only 3 — too few # for multiple concurrent connections (API + web_server + OTA). # BK default plan uses 10; match that. RTL(20) and LN(20) need no override. - # Cost: 10 × 1580 = 15.8KB static RAM (vs 3 × 1580 = 4.7KB before). + # With MEMP_MEM_MALLOC=1, this is a max count (allocated on demand). if CORE.is_bk72xx: lwip_opts.append("MEM_LIBC_MALLOC=1") + lwip_opts.append("MEMP_MEM_MALLOC=1") lwip_opts.append("PBUF_POOL_SIZE=10") cg.add_platformio_option("custom_options.lwip", lwip_opts)