Repository navigation
refactor(esp/io): compile-time-select UART vs USB-Serial-JTAG (−3.2 KB on ESP32-S3 Blink, #2773 item 1.4) - #2786
Conversation
…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]>
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughThis PR converts ESP32's serial backend selection from runtime probing to compile-time selection. A new ChangesESP32 Serial Backend Refactoring
Possibly related PRs
Poem
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Summary
EspIOused to hold bothUartEsp32ANDUsbSerialJtagEsp32asdirect 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_BOOTconvention:
ARDUINO_USB_CDC_ON_BOOT=1(Arduino routes Serial through USB-JTAG)UsbSerialJtagEsp32ARDUINO_USB_CDC_ON_BOOT=0(default — UART0 console)UartEsp32UartEsp32UartEsp32(forced)Explicit overrides for the rare case where a user wants a different
backend than their board config implies:
Both are honored only when JTAG hardware is available; on chips
without JTAG the choice is fixed to UART.
Mechanism
EspIOnow has a singleActiveDriver mDrivermember (typedef to thechosen driver), every method just calls
mDriver.write/read/flushwithout a runtime branch. The other driver's class isn't even named in
this build's
EspIOinstantiation, so its code is unreachable and--gc-sectionsdrops it.This is the same shape
Channel::showPixelsuses for itsbrightness/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)
firmware.binVerification:
API impact
No breaking changes for normal users.
fl::print/fl::printlnetc.unchanged — still go through
EspIO, which now uses one backendinstead of two.
Two minor surface changes for power users that previously called the
accessors directly:
EspIO::isUsingUsbSerialJtag()is nowconstexpr(compile-timeanswer 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 bothwere 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_jtagtotal: 2,765 → 1,472 B(−1,293 B; residual is Arduino-ESP32 boot code).
Issue: #2773
🤖 Generated with Claude Code
Summary by CodeRabbit