Skip to content

Proposal: replace Arduino-ESP32 startup on stock cores so unused Serial0 is not linked (#4892) — needs maintainer decision #4911

Closed

Description

@zackees

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

  1. Header part (sketch objects): a header included from FastLED.h emits weak definitions of app_main, loopTaskHandle and loopTaskWDTEnabled.
    • Sketch objects come before the core archive on the link line, so the core's main.o is never extracted.
    • Weak definitions are deliberate: if anything else forces main.o in, its strong definitions win silently and the build behaves exactly as stock.
  2. Library part (libFastLED.a): a replica of loopTask / app_main (USB CDC/MSC/DFU on boot, F_CPU/F_XTAL, the setup wait time, the chip debug report, the WDT reset and yieldIfNecessary). It differs from stock in a few places:
    • It calls uartSetPins(0, SOC_RX0, SOC_TX0, -1, -1), which is what Serial0.setPins resolves to, only if serialEventRun (already weak, and defined only in HardwareSerial.o) is linked. In that case it calls serialEventRun exactly as stock does.
    • When HardwareSerial is absent, it still dispatches HWCDCSerialEvent / USBSerialEvent through weak references, using a virtual call so the vtable lives in the already-linked CDC object.
    • The getArduinoLoopTaskStackSize / getArduinoSetupWaitTime_ms / shouldPrintChipDebugReport overrides are read through weak references, with stock defaults as the fallback.
  3. Gating: Arduino-ESP32 3.3.2–3.3.12 only, CONFIG_AUTOSTART_ARDUINO, and no NO_GLOBAL_SERIAL / NO_GLOBAL_INSTANCES. Opt-out with -DFASTLED_ESP32_STOCK_ARDUINO_MAIN=1. Any other core version gets the unchanged stock path.
  4. 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.cpp becomes FastLED's bug. Every core bump needs a re-diff (a CI or lint check could compare a hash of main.cpp).
  • The saving is zero for most sketches. Any sketch that touches Serial / Serial0 links HardwareSerial anyway. 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 into w. The weak references must therefore live in an isolated translation unit, which needs a new _build entry. That requires a maintainer directive.
  • Weak app_main collides with any sketch that defines its own app_main under 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 stock main.o back 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 define shouldPrintChipDebugReport with extern "C" linkage, while the core declares it as C++. If so, it never overrides anything. This needs a separate check.

Alternatives

Validation plan if approved

  • Matched bash bloat on stock 3.3.11 for S3, P4 and classic, with and without app Serial. Confirm HardwareSerial / Serial0 are absent or present as expected, and that main.o is 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 / HWCDCSerialEvent dispatch, and the default UART0 pins after Serial.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.

Activity

  1. zackees commented on Oct 23, 2026

    @zackees
    MemberAuthor

    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() calls Serial0.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 in main.cpp (getArduinoLoopTaskStackSize, shouldPrintChipDebugReport, getArduinoSetupWaitTime_ms) run around that line but cannot skip it. Stock exposes no override. Verified in the map: main.o is extracted for loopTaskWDTEnabled (referenced by esp32-hal-misc.o), and then pulls HardwareSerial.o through Serial0.

    Problems found (and what was done)

    1. 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.

    2. 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 of src/platforms is 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 other src/platforms code names those symbols. The sketch-side header defines only app_main, loopTaskHandle and loopTaskWDTEnabled, which sketches never reference.

    3. Link order. The weak definitions must sit in the sketch's own objects; libFastLED.a is scanned after the core, so it is too late. The sketch-side header is therefore included from FastLED.h. If anything still extracts main.o, its strong symbols win and startup is exactly stock. This is a safe fallback, but the saving is then silently lost.

    4. 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; only sdkconfig.h, esp_arduino_version.h and FreeRTOS.
      • src/platforms/arduino/esp32_lazy_serial0_main.cpp.hpp: the Arduino adapter holding the copy of loopTask / app_main.
    5. Outside the Arduino core it is never used. The gate requires all of:

      • FL_IS_ESP32
      • ARDUINO_ARCH_ESP32
      • esp_arduino_version.h present
      • CONFIG_AUTOSTART_ARDUINO
      • no NO_GLOBAL_SERIAL / NO_GLOBAL_INSTANCES
      • core version 3.3.2–3.3.12

      Plain 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.

    6. USB CDC events without HardwareSerial. Stock dispatches HWCDCSerialEvent / USBSerialEvent from serialEventRun(), which lives in HardwareSerial.o. Once that object is gone, the copy dispatches them itself, through weak instances and a virtual call. Verified on hardware (below).

    7. Every core bump needs a re-diff. The copy is only valid where main.cpp is 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.

    8. 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.hpp defined extern "C" shouldPrintChipDebugReport. Arduino.h declares it with C++ linkage, so it never overrode anything. Fixed in the same PR.

    9. 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=1

    config no Serial: flash / RAM with Serial: flash / RAM HardwareSerial linked (ours)
    esp32dev (UART0) −3,828 / −224 +12 / 0 no / yes (it is Serial)
    S3 HWCDC on boot −2,780 / −232 −2,808 / −232 no / no
    S3 native USB CDC −3,432 / −504 −3,464 / −496 no / no
    C6 HWCDC on boot −3,788 / −244 −3,688 / −240 no / no

    In every "ours" image main.o is absent from the map. In every stock image it is present.

    Hardware (FastLED image vs stock image, same sketch)

    • S3, USB-JTAG (/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 shows SET_TIME_BEFORE_STARTING_SKETCH_MS, SET_LOOP_TASK_STACK_SIZE(12K), the loop task handle, and enableLoopWDT/disableLoopWDT all behaving the same.
    • S3, Serial = HWCDC, HWCDCSerialEvent defined, host sends data. Both images report events=12 bytes=72; ours links no HardwareSerial.
    • Classic ESP32 (CP2104, /dev/ttyUSB0), no Serial. Both images: setup_ms=1532 task=loopTask handle_ok=1 stack_hw=11148 wdt_on=0 wdt_off=261 PASS.
    • Classic ESP32, Serial = Serial0, serialEvent() defined. Ours: UART0 pins are already RX=3/TX=1 before Serial.begin() (the startup setPins equivalent ran), and the result was events=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.
    • The C6 board is not attached today, so C6 is compile-only.
  2. added
    sanitized
    on Nov 6, 2026
  3. zackees commented on Nov 22, 2026

    @zackees
    MemberAuthor

    Closing as done.

    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.cpp diffed again. #5365 (HTTPS on 3.3.12) is tracked separately; FastLED was ruled out there by an A/B test.

  4. moved this from Triage to Done in FastLED Trackeron Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    sanitized

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions