Repository navigation
Description
Proposal for #4892: drop the startup Serial0 root on stock Arduino-ESP32 (no forked framework), and keep working once espressif/arduino-esp32#13010 lands. Nothing has been implemented yet; this issue is for review before any code is written.
Root cause (stock core)
cores/esp32/main.cpp loopTask() calls Serial0.setPins(...) unconditionally. The linker pulls main.o because esp32-hal-misc.o references loopTaskWDTEnabled and loopTaskHandle. main.o then references Serial0, which drags in HardwareSerial.o, Stream::readString and related code (about 22 KB of flash and 1.1 KB of RAM on P4 native UART, measured in #4898).
main.cpp is byte-identical in tags 3.3.2 through 3.3.12 and on current master. It differs in 3.3.1 and earlier.
Proposed mechanism
- Header part (sketch objects): a header included from
FastLED.hemits weak definitions ofapp_main,loopTaskHandleandloopTaskWDTEnabled.- Sketch objects come before the core archive on the link line, so the core's
main.ois never extracted. - Weak definitions are deliberate: if anything else forces
main.oin, its strong definitions win silently and the build behaves exactly as stock.
- Sketch objects come before the core archive on the link line, so the core's
- Library part (
libFastLED.a): a replica ofloopTask/app_main(USB CDC/MSC/DFU on boot, F_CPU/F_XTAL, the setup wait time, the chip debug report, the WDT reset andyieldIfNecessary). It differs from stock in a few places:- It calls
uartSetPins(0, SOC_RX0, SOC_TX0, -1, -1), which is whatSerial0.setPinsresolves to, only ifserialEventRun(already weak, and defined only inHardwareSerial.o) is linked. In that case it callsserialEventRunexactly as stock does. - When
HardwareSerialis absent, it still dispatchesHWCDCSerialEvent/USBSerialEventthrough weak references, using a virtual call so the vtable lives in the already-linked CDC object. - The
getArduinoLoopTaskStackSize/getArduinoSetupWaitTime_ms/shouldPrintChipDebugReportoverrides are read through weak references, with stock defaults as the fallback.
- It calls
- Gating: Arduino-ESP32 3.3.2–3.3.12 only,
CONFIG_AUTOSTART_ARDUINO, and noNO_GLOBAL_SERIAL/NO_GLOBAL_INSTANCES. Opt-out with-DFASTLED_ESP32_STOCK_ARDUINO_MAIN=1. Any other core version gets the unchanged stock path. - With the upstream fix: the replica behaves identically on a patched core, so it is safe there too. Once a released core includes #13010, the gate can stop at that version.
Downsides / risks (why this needs a maintainer call)
- FastLED takes over the Arduino entry point for every ESP32 user on 3.3.2–3.3.12. Any behavioural drift between the replica and stock
main.cppbecomes FastLED's bug. Every core bump needs a re-diff (a CI or lint check could compare a hash ofmain.cpp). - The saving is zero for most sketches. Any sketch that touches
Serial/Serial0linksHardwareSerialanyway. The benefit applies only to Serial-free images (native-console / AutoResearch-style builds). - It depends on link-order details: sketch objects before the core archive (true for fbuild and arduino-cli), and FastLED compiled into the sketch rather than as a pre-linked component.
- On ESP-IDF + Arduino-as-component, it is disabled by the gate.
- With any other unusual order, the weak definitions just lose and the build behaves as stock. That is still correct, but nothing tells the user the saving didn't happen.
- Weak redeclarations in one unity TU weaken strong references in that same object. I verified this with xtensa gcc 13.2: an
__asm__alias declared weak turns a strong reference in the same TU intow. The weak references must therefore live in an isolated translation unit, which needs a new_buildentry. That requires a maintainer directive. - Weak
app_maincollides with any sketch that defines its ownapp_mainunder autostart. Such a sketch is already broken on stock, but the error changes. - Edge case: a sketch that calls
getArduinoLoopTaskStackSize()itself without defining it forces stockmain.oback in. That is correct, but the saving is lost. - Precedent concern:
src/extras/suppress_arduino_chip_debug_report.cpp.hpp(Meta: ESP32-S3 Blink bloat reduction (file:line concrete plan) — supersedes #2885 #2886) is opt-in for exactly this "overriding vendor startup is scope creep" reason. That file also appears to defineshouldPrintChipDebugReportwithextern "C"linkage, while the core declares it as C++. If so, it never overrides anything. This needs a separate check.
Alternatives
- A. Ship this default-on, version-gated, with the opt-out.
- B. Ship it opt-in (
-DFASTLED_ESP32_LAZY_SERIAL0=1), matching the Meta: ESP32-S3 Blink bloat reduction (file:line concrete plan) — supersedes #2885 #2886 precedent. Lower risk, but few users will benefit. - C. Keep perf(esp32p4): remove unnecessary Serial0 retention from Arduino framework startup #4892 waiting on upstream #13010. Document the limitation and the measured cost.
Validation plan if approved
- Matched
bash bloaton stock 3.3.11 for S3, P4 and classic, with and without app Serial. ConfirmHardwareSerial/Serial0are absent or present as expected, and thatmain.ois not extracted (map file). - AutoResearch hardware runs on S3 (HWCDC), C6, and classic ESP32 UART:
- startup and setup wait time, the stack-size override, the WDT enable/disable path
serialEvent/HWCDCSerialEventdispatch, and the default UART0 pins afterSerial.begin()
- The same runs on the patched-core pin, to prove the "works with the upstream fix" claim.
Refs #4892, #4888, espressif/arduino-esp32#13010, #4898.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Oct 23, 2026
Update: explored, prototyped, tested. Problems found are listed first.
Can the stock core be told to skip this? No.
In
cores/esp32/main.cpp(3.3.2–3.3.12, and current master),loopTask()callsSerial0.setPins(...)under#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL). That macro only works when the core itself is compiled with it; a library cannot set it. The weak hooks inmain.cpp(getArduinoLoopTaskStackSize,shouldPrintChipDebugReport,getArduinoSetupWaitTime_ms) run around that line but cannot skip it. Stock exposes no override. Verified in the map:main.ois extracted forloopTaskWDTEnabled(referenced byesp32-hal-misc.o), and then pullsHardwareSerial.othroughSerial0.Problems found (and what was done)
The saving is much smaller than perf(esp32): elide unused framework Serial0 startup #4898 claimed. On stock 3.3.11, a no-Serial sketch saves about 2.8–3.8 KB flash and 224–504 B RAM, not 22 KB. The 22 KB figure came from a different (P4 / AutoResearch) image. Full table below.
Weak redeclarations weaken a strong reference in the same object file. Verified with xtensa gcc 13.2: a weak declaration turns a user's strong call in the same object into
w. All ofsrc/platformsis a single unity object, so the library half redeclares nothing that other code in that object uses. A new test,ci/tests/test_esp32_lazy_serial0.py, fails if any othersrc/platformscode names those symbols. The sketch-side header defines onlyapp_main,loopTaskHandleandloopTaskWDTEnabled, which sketches never reference.Link order. The weak definitions must sit in the sketch's own objects;
libFastLED.ais scanned after the core, so it is too late. The sketch-side header is therefore included fromFastLED.h. If anything still extractsmain.o, its strong symbols win and startup is exactly stock. This is a safe fallback, but the saving is then silently lost.Lint architecture. Native ESP code must not include
Arduino.h. The split is:src/platforms/esp/32/esp32_lazy_serial0_{gate,main}.h: native-safe; onlysdkconfig.h,esp_arduino_version.hand FreeRTOS.src/platforms/arduino/esp32_lazy_serial0_main.cpp.hpp: the Arduino adapter holding the copy ofloopTask/app_main.Outside the Arduino core it is never used. The gate requires all of:
FL_IS_ESP32ARDUINO_ARCH_ESP32esp_arduino_version.hpresentCONFIG_AUTOSTART_ARDUINONO_GLOBAL_SERIAL/NO_GLOBAL_INSTANCESPlain ESP-IDF, host builds, Arduino 2.x, 3.0–3.3.1 and Arduino 4 all compile to nothing. Opt out with
-DFL_ESP32_STOCK_ARDUINO_MAIN=1. Arduino-as-an-IDF-component (with autostart) passes the gate, but whether the override wins there depends on IDF's link order; either outcome behaves as stock.USB CDC events without HardwareSerial. Stock dispatches
HWCDCSerialEvent/USBSerialEventfromserialEventRun(), which lives inHardwareSerial.o. Once that object is gone, the copy dispatches them itself, through weak instances and a virtual call. Verified on hardware (below).Every core bump needs a re-diff. The copy is only valid where
main.cppis byte-identical. I diffed every tag: 3.2.0–3.3.1 differ; 3.3.2–3.3.12 and master are identical. Newer cores fall back to stock until the copy is re-checked.Pre-existing bug found: the Meta: ESP32-S3 Blink bloat reduction (file:line concrete plan) — supersedes #2885 #2886 opt-in
suppress_arduino_chip_debug_report.cpp.hppdefinedextern "C" shouldPrintChipDebugReport. Arduino.h declares it with C++ linkage, so it never overrode anything. Fixed in the same PR.The upstream fix is still compatible. On a core carrying fix(serial): avoid retaining unused HardwareSerial from startup espressif/arduino-esp32#13010 the override still applies, and the copy sets the pins only when HardwareSerial is linked, exactly as the upstream hook does.
Build matrix: stock 3.3.11 (pioarduino 55.03.311), FastLED vs
-DFL_ESP32_STOCK_ARDUINO_MAIN=1In every "ours" image
main.ois absent from the map. In every stock image it is present.Hardware (FastLED image vs stock image, same sketch)
/dev/ttyACM0), no Serial, printf only. Both images print the same line:setup_ms=1511 task=loopTask handle_ok=1 stack_hw=10936 wdt_on=0 wdt_off=261 PASS. This showsSET_TIME_BEFORE_STARTING_SKETCH_MS,SET_LOOP_TASK_STACK_SIZE(12K), the loop task handle, andenableLoopWDT/disableLoopWDTall behaving the same.Serial= HWCDC,HWCDCSerialEventdefined, host sends data. Both images reportevents=12 bytes=72; ours links no HardwareSerial./dev/ttyUSB0), no Serial. Both images:setup_ms=1532 task=loopTask handle_ok=1 stack_hw=11148 wdt_on=0 wdt_off=261 PASS.Serial= Serial0,serialEvent()defined. Ours: UART0 pins are already RX=3/TX=1 beforeSerial.begin()(the startupsetPinsequivalent ran), and the result wasevents=12 bytes=72. The stock rerun of this one case was blocked by an fbuild deploy EBUSY on that port (deploy: daemon holds /dev/ttyUSB0 (CP2104) and then its own flash/esptool open fails with EBUSY fbuild#1705), so it has not been compared side by side.zackees commentedon Nov 22, 2026
Closing as done.
FL_ESP32_STOCK_ARDUINO_MAINworks, after fix(esp32): keep weak app_main out of FastLED library objects so the stock-startup opt-out works (#5365) #5385 kept the weakapp_mainout of FastLED's library objects.ci/tests/test_esp32_lazy_serial0.pyand test(esp32): assert weak app_main sits inside the library guard (#5365) #5392.Accepted limitation: the override applies only to the core versions it was diffed against, 3.3.2 through 3.3.12. Each core bump needs
main.cppdiffed again. #5365 (HTTPS on 3.3.12) is tracked separately; FastLED was ruled out there by an A/B test.