Skip to content

fix(avr): link Arduino Serial only when a sketch uses it (Fixes #4725) - #4730

Merged
zackees merged 2 commits into
masterfrom
fix/4725-avr-serial
Oct 7, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/4725-avr-serial

Conversation

@zackees

@zackees zackees commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Fixes #4725 (part of #4728).

Root cause

platforms/avr/io_avr.cpp.hpp includes platforms/arduino/io_arduino.hpp, so FastLED's print/available/read/... shims live in the platforms+ unity object that every sketch links. Their strong references to Serial pulled HardwareSerial0.cpp.o (uno) or TinySoftwareSerial.cpp.o (ATtiny) out of the core archive before LTO ran. That brought in the Serial object, its ring buffers, and the USART ISRs that keep them alive. On ATTinyCore, GCC also devirtualized Serial.available() etc. into direct calls to TinySoftwareSerial::*, which live in the same archive member.

Fix

  • AVR only: #pragma weak Serial in io_arduino.hpp. Each shim checks &Serial != 0 and calls through an opaque Stream& (an empty asm hides the dynamic type, so the calls stay virtual).
  • fl::serial_begin() (AVR+Arduino) and fl::SerialPort::begin() are now header-inline. Their strong Serial reference lands in the caller's object, so a sketch that calls fl::serial_begin() still links Serial and prints. A sketch that calls Serial.begin() does too.
  • Behaviour does not change for any sketch that opens a port. A sketch that never opens one had no working UART anyway.
  • Uno max_size_apa102 goes from 9300 to 8500 (claimed savings), and the frozen list and SIZE_THRESHOLD_HISTORY.md are updated to match.

Before / after (flash / RAM bytes)

board / example 3.10.3 master this PR
uno Blink 3784 / 131 5158 / 629 5158 / 629 (Blink prints a banner by design)
uno ColorPalette 5294 / 333 6612 / 643 5782 / 468
uno Apa102 3178 / 187 4510 / 501 3682 / 326
attiny85 Blink 3338 / 131 3132 / 233 2638 / 163
attiny85 ColorPalette 4826 / 333 4822 / 435 4328 / 365
atmega8a Blink 4402 / 356 3596 / 181
atmega8a ColorPalette 5968 / 558 5160 / 383
nano_every ColorPalette 7795 / 530 6141 / 375
leonardo Blink / ColorPalette 7086 / 594, 8628 / 608 unchanged (USB CDC is always linked)

Uno Blink RAM delta, 3.10.3 to master (symbol diff, bloat report)

  • str1.1 +187: Blink's banner literals in .data (example, by design)
  • Serial +157 and vtable for HardwareSerial +18: Blink calls Serial.begin (example, by design; library-side zero-cost now)
  • static addLeds<NEOPIXEL> controller 24 → 89 (+65) and guard ±0
  • controller vtables: CLEDController 20 → 26, Clockless 22 → 28, NEOPIXEL 22 → 28 (+18 net)
  • gControllersData 16 → 32 (+16)
  • timer_millis +4, sWaitSpinBudgetUs +4, FastLED +2, small renames ±0

Evidence that printing still works

  • Uno ColorPalette with fl::serial_begin(115200); fl::println("probe-4725"); links HardwareSerial0, and the string is in the ELF (6786 / 657).
  • attiny85 with Serial.begin(9600); fl::println(...) links TinySoftwareSerial, Print::write, and the string.
  • bash test --cpp: 317/317 unit and 95/95 examples pass. bash lint is clean.

Summary by CodeRabbit

  • Improvements
    • AVR builds can use serial functionality with less firmware space.
    • Serial operations now handle an unavailable serial interface safely, returning appropriate empty or failure results instead of accessing it.
    • Starting serial communication continues to use the configured baud rate across supported platforms.

FastLED's Arduino I/O shims sit in the always-linked platforms+ unity
object. Their strong references to Serial (and, on ATTinyCore, to
TinySoftwareSerial methods defined next to it) pulled the core's Serial
object, ring buffers and USART ISRs into every AVR sketch.

On AVR the shims now reference Serial weakly and call it through an
opaque Stream&, so no devirtualized direct references remain.
fl::serial_begin() and fl::SerialPort::begin() are header-inline on AVR,
so their strong reference lands in the caller's object: sketches that
begin a port (Arduino Serial or fl::serial_begin) still print.

Tighten uno max_size_apa102 9300 -> 8500 (Apa102 4510 -> 3682 B).

Co-Authored-By: Claude <[email protected]>
@zackees zackees added the ci-full Run the complete release-equivalent CI matrix on this PR SHA label Oct 7, 2026
@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Repository: FastLED/FastLED/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 693c7ca8-268f-4b44-ae24-6cc88cd11481
📥 Commits

Reviewing files that changed from the base of the PR and between 094a52e and bd50973.

📒 Files selected for processing (4)
  • ci/lint/check_size_thresholds.py
  • docs/SIZE_THRESHOLD_HISTORY.md
  • src/fl/stl/cstdio.h
  • src/fl/system/serial.h
 ____________________________________________________________
< If you don't finish then you're just busy, not productive. >
 ------------------------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: FastLED/FastLED/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 194626f9-b267-41a8-885d-2906d623e575
📥 Commits

Reviewing files that changed from the base of the PR and between ec8bd69 and 094a52e.

📒 Files selected for processing (8)
  • .github/workflows/check_uno_size.yml
  • ci/lint/check_size_thresholds.py
  • docs/SIZE_THRESHOLD_HISTORY.md
  • src/fl/stl/cstdio.cpp.hpp
  • src/fl/stl/cstdio.h
  • src/fl/system/serial.cpp.hpp
  • src/fl/system/serial.h
  • src/platforms/arduino/io_arduino.hpp
💤 Files with no reviewable changes (1)
  • src/fl/system/serial.cpp.hpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.


📝 Walkthrough

Walkthrough

AVR Arduino serial setup and access now use conditional inline definitions and a weakly linked stream. The Uno Apa102 size threshold changes from 9300 to 8500, and the threshold checker and size history reflect that change.

Changes

AVR serial setup and stream access

Layer / File(s) Summary
AVR serial setup and stream access
src/fl/stl/cstdio.h, src/fl/stl/cstdio.cpp.hpp, src/fl/system/serial.h, src/fl/system/serial.cpp.hpp, src/platforms/arduino/io_arduino.hpp
AVR Arduino builds define serial_begin inline and omit its out-of-line definition. SerialPort::begin is inline and forwards to fl::serial_begin. AVR serial operations use a weakly linked stream and return without accessing it when Serial is not linked.

Uno Apa102 size threshold

Layer / File(s) Summary
Uno Apa102 size threshold and history
.github/workflows/check_uno_size.yml, ci/lint/check_size_thresholds.py, docs/SIZE_THRESHOLD_HISTORY.md
The Uno Apa102 maximum-size threshold changes from 9300 to 8500. The checker comments and size history record the reduction and the measured change from 4510 to 3682 bytes.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 094a5

No actionable issue remains from this review. The change is ready to merge after normal checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 18 functions across 5 files. (2 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main change: Arduino Serial links on AVR only when a sketch uses it.
Linked Issues check ✅ Passed #4725 asks AVR sketches that do not use Serial to avoid linking HardwareSerial or TinySoftwareSerial, while preserving FastLED serial output when the port is opened. io_arduino.hpp uses a weak AVR `…
Out of Scope Changes check ✅ Passed The Uno Apa102 size limit, frozen threshold, and size-history updates document and enforce the measured size reduction from #4725. The remaining changes implement the AVR Serial-linking fix or support…
Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 18 functions across 5 files. (2 skipped: 2 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@zackees zackees removed the ci-full Run the complete release-equivalent CI matrix on this PR SHA label Oct 7, 2026
@zackees
zackees merged commit 6ceddf5 into master Oct 7, 2026
87 of 92 checks passed
@zackees
zackees deleted the fix/4725-avr-serial branch November 5, 2026 05:52
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.

AVR: Arduino Serial (HardwareSerial / TinySoftwareSerial) always linked, ~175 B RAM + ~1 KB flash on Uno

1 participant