Skip to content

refactor(esp/io): compile-time-select UART vs USB-Serial-JTAG (−3.2 KB on ESP32-S3 Blink, #2773 item 1.4) - #2786

Merged
zackees merged 1 commit into
masterfrom
feat/2773-espio-split
Jun 5, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/2773-espio-split

Conversation

@zackees

@zackees zackees commented Jun 5, 2026 •

Copy link
Copy Markdown
Member

Summary

EspIO used to hold both UartEsp32 AND UsbSerialJtagEsp32 as
direct non-template members and runtime-probed which one was active.
The linker had to keep both drivers (plus the ESP-IDF JTAG driver each
pulls in) alive because both were reachable from every
read/write/print call site — ~3 KB of dead weight when only one
backend is in use
.

This PR replaces the runtime probe with a compile-time selection so
the unused driver's code is dead and gets dropped by --gc-sections.

Implements item 1.4 of #2773.

How the choice is made (no new user macro for common case)

The selection mirrors Arduino-ESP32's existing ARDUINO_USB_CDC_ON_BOOT
convention:

Build config Backend
ARDUINO_USB_CDC_ON_BOOT=1 (Arduino routes Serial through USB-JTAG) UsbSerialJtagEsp32
ARDUINO_USB_CDC_ON_BOOT=0 (default — UART0 console) UartEsp32
Non-Arduino ESP-IDF build UartEsp32
Chip without USB-Serial-JTAG (ESP32, S2, etc.) UartEsp32 (forced)

Explicit overrides for the rare case where a user wants a different
backend than their board config implies:

#define FASTLED_ESP_FORCE_UART_SERIAL        // forces UART
#define FASTLED_ESP_FORCE_USB_SERIAL_JTAG    // forces JTAG

Both are honored only when JTAG hardware is available; on chips
without JTAG the choice is fixed to UART.

Mechanism

EspIO now has a single ActiveDriver mDriver member (typedef to the
chosen driver), every method just calls mDriver.write/read/flush
without a runtime branch. The other driver's class isn't even named in
this build's EspIO instantiation, so its code is unreachable and
--gc-sections drops it.

This is the same shape Channel::showPixels uses for its
brightness/gamma/RGBW codepath selection (compile-time selection via
if constexpr — see #2773 item 2.1 for the planned variant).

Measured (ESP32-S3, examples/Blink/Blink.ino, PIO + IDF 5.4)

Build firmware.bin Δ
master (`8c87cbfddb`, both drivers) 657,392 B —
this PR (UART-only default) 654,192 B −3,200 B

Verification:

xtensa-esp32s3-elf-nm firmware.elf | grep "fl::UsbSerialJtag"
# empty after this PR — JTAG wrapper code is gone

xtensa-esp32s3-elf-nm firmware.elf | grep usb_serial_jtag | wc -l
# 2,765 B → 1,472 B (the residual is Arduino-ESP32 boot code that
# touches the JTAG peripheral regardless, not FastLED)

API impact

No breaking changes for normal users. fl::print/fl::println etc.
unchanged — still go through EspIO, which now uses one backend
instead of two.

Two minor surface changes for power users that previously called the
accessors directly:

  • EspIO::isUsingUsbSerialJtag() is now constexpr (compile-time
    answer instead of runtime). Existing callers don't need to change.
  • EspIO::getUart() is available only in UART builds;
    EspIO::getUsbSerialJtag() only in JTAG builds. Previously both
    were always available but asserted at runtime — same effective
    semantics, enforced earlier (link time vs FL_ASSERT).

Test plan

  • bash lint --cpp — clean.
  • bash compile esp32s3 --examples Blink --platformio — builds clean.
  • firmware.bin: 657,392 → 654,192 (−3,200 B).
  • nm firmware.elf | grep fl::UsbSerialJtag — empty.
  • nm firmware.elf | grep usb_serial_jtag total: 2,765 → 1,472 B
    (−1,293 B; residual is Arduino-ESP32 boot code).
  • CI green.

Issue: #2773

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • ESP32 serial backend selection now determined at compile-time instead of runtime probing.
    • Configuration macros available to force specific backend (UART or USB-Serial JTAG) selection.
    • Enhanced diagnostics reporting for serial backend initialization.

…er drops the unused driver (#2773 item 1.4)

`EspIO` used to hold both `UartEsp32` AND `UsbSerialJtagEsp32` as direct
non-template members and runtime-probed which one was active. The
linker had to keep both drivers (plus the ESP-IDF JTAG driver each
pulls in) alive because both were reachable from every read/write/print
call site — ~3 KB of dead weight when only one backend is in use.

Replace the runtime probe with a compile-time selection. The choice
mirrors Arduino-ESP32's existing `ARDUINO_USB_CDC_ON_BOOT` convention
so the common case is automatic (no new user macro):

  * ARDUINO_USB_CDC_ON_BOOT=1 (Arduino routes Serial through USB
    Serial-JTAG)                       → EspIO uses UsbSerialJtagEsp32.
  * otherwise (Arduino-default UART0 console, non-Arduino ESP-IDF
    builds, chips without JTAG)        → EspIO uses UartEsp32.

Explicit overrides for the rare case where the user wants a different
backend than what their board config implies:

  * `#define FASTLED_ESP_FORCE_UART_SERIAL`        → forces UART.
  * `#define FASTLED_ESP_FORCE_USB_SERIAL_JTAG`    → forces JTAG.

Both are honored only when JTAG hardware is available; on chips without
JTAG the choice is fixed to UART.

The new `EspIO` has a single `ActiveDriver mDriver` member (typedef to
the chosen driver), every method just calls `mDriver.write/read/flush`
without a runtime branch, and `--gc-sections` drops the unused
driver's code.

## Measured (ESP32-S3, examples/Blink/Blink.ino, PIO + IDF 5.4)

| Build                         | firmware.bin | Δ |
|-------------------------------|-------------:|---:|
| master (`8c87cbfddb`, both)   | 657,392 B    | —          |
| this PR (UART-only by default)| **654,192 B**| **−3,200 B** |

Verification:

    xtensa-esp32s3-elf-nm firmware.elf | grep "fl::UsbSerialJtag"
    # empty after this PR — the JTAG wrapper code is gone

ESP-IDF JTAG symbols (`usb_serial_jtag_*`) shrink from 2,765 B → 1,472 B
(−1,293 B). The residual is pulled by Arduino-ESP32 boot code (chip
init touches the JTAG peripheral regardless), not by FastLED.

## API: no breaking changes for normal users

- `fl::print`/`fl::println` etc. unchanged — they still go through
  `EspIO`, which now uses one backend instead of two.
- `EspIO::isUsingUsbSerialJtag()` is now `constexpr` (compile-time
  answer instead of runtime). Existing callers that check this don't
  need to change.
- `EspIO::getUart()` is available only in UART builds;
  `EspIO::getUsbSerialJtag()` only in JTAG builds. Previously both
  were always available but asserted at runtime — same effective
  semantics, just enforced earlier (link time vs `FL_ASSERT`).

## Verification

- [x] `bash lint --cpp` — clean.
- [x] `bash compile esp32s3 --examples Blink --platformio` — clean build.
- [x] `firmware.bin`: 657,392 → 654,192 (−3,200 B).
- [x] `nm firmware.elf | grep fl::UsbSerialJtag` — empty.
- [x] `nm firmware.elf | grep usb_serial_jtag` total: 2,765 → 1,472 B
      (−1,293 B; remainder is Arduino boot code, not FastLED).

Issue: #2773

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR converts ESP32's serial backend selection from runtime probing to compile-time selection. A new FL_ESP_IO_USE_USB_SERIAL_JTAG macro determines the backend at compile time using chip capability, optional override flags, and ARDUINO_USB_CDC_ON_BOOT configuration. The EspIO class is refactored to use a single driver member instead of dual drivers with runtime dispatch, simplifying all I/O methods and exposing only the active backend's getter method.

Changes

ESP32 Serial Backend Refactoring

Layer / File(s) Summary
Compile-time backend selection mechanism
src/platforms/esp/32/detail/io_esp_jtag_or_uart.hpp
FL_ESP_IO_USE_USB_SERIAL_JTAG is computed at compile time from ESP32 chip variant, FASTLED_ESP_FORCE_UART_SERIAL / FASTLED_ESP_FORCE_USB_SERIAL_JTAG override flags, and ARDUINO_USB_CDC_ON_BOOT when JTAG is available.
EspIO class refactoring to single driver
src/platforms/esp/32/detail/io_esp_jtag_or_uart.hpp
EspIO replaces dual runtime members (mUsbSerialJtag, mUart, mUseUsbSerialJtag) with a compile-time ActiveDriver type alias and single mDriver member initialized in the constructor. All I/O methods (print, println, available, peek, read, writeBytes, flush, isReady, isBufferedMode) route through mDriver. The new static constexpr isUsingUsbSerialJtag() exposes backend selection at compile time, and getUsbSerialJtag() / getUart() are conditionally declared based on the active backend, replacing previous runtime assertions.
Diagnostic reporting update
src/platforms/esp/32/detail/io_esp_jtag_or_uart.hpp
reportInitDiagnosticsIfNeeded() reports the initialization outcome of the single mDriver (USB-JTAG or UART) and includes a reference to the FASTLED_ESP_FORCE_USB_SERIAL_JTAG override flag in the UART diagnostic message.

Possibly related PRs

  • FastLED/FastLED#2620: Updates ESP32_P4 board to set ARDUINO_USB_CDC_ON_BOOT=1, aligning with the compile-time backend selection mechanism introduced in this PR.
  • FastLED/FastLED#2751: Also modifies io_esp_jtag_or_uart.hpp to refactor reportInitDiagnosticsIfNeeded() and route I/O through the active backend driver.

Poem

🐰 A serial path picked at compile time, so fleet,
No more runtime guessing—just one driver neat.
UART or JTAG, the chip decides which way,
Diagnostics report the choice made that day. 🎯

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: compile-time selection of UART vs USB-Serial-JTAG instead of runtime probing, with a concrete benefit (−3.2 KB on ESP32-S3 Blink).
Docstring Coverage ✅ Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/2773-espio-split

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant