Skip to content

fix(avr): allow Servo to own Timer1 vector - #4884

Merged
zackees merged 1 commit into
masterfrom
fix/avr-servo-timer1-vector
Oct 22, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/avr-servo-timer1-vector

Conversation

@zackees

@zackees zackees commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Fixes #4836

On classic ATmega targets, FastLED now exposes its Timer1 Compare A ISR through a weak vector alias. Servo's strong vector can link and take ownership. The FastLED timer API detects an overridden vector and returns the existing timer-in-use error before changing Timer1 registers.

Verification:

  • bash compile uno --examples Blink passed.
  • Temporarily added a strong TIMER1_COMPA_vect to Blink; the Uno build linked successfully, then the sketch was restored.
  • Built Blink with the official Arduino Servo library and Servo::attach(9) to force Servo's Timer1 ISR into the final Uno image; linking succeeded. The temporary sketch change and test library were removed.
  • bash lint --cpp passed.

The initial AVR8JS run was blocked by the legacy Docker fallback. After fixing native fbuild shutdown in FastLED/fbuild#1697, a real-Servo Uno runtime probe passed: FastLED returned -16, left the output handle invalid, and preserved TCCR1A, TCCR1B, TIMSK1, OCR1A, OCR1B, and ICR1. Runtime evidence.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Timer1 compatibility with other libraries that use the same hardware timer.
    • The system now detects when Timer1 is already managed elsewhere, preserves the existing configuration, and reports a warning instead of reconfiguring it.
    • Timer setup failures are now reported with a specific error result.

@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

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: 9d7ffe54-32d3-443e-862b-eb49fb6d11f7
📥 Commits

Reviewing files that changed from the base of the PR and between ced53ec and 07eb140.

📒 Files selected for processing: 1
  • src/platforms/avr/atmega/isr_avr_atmega.hpp

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

📝 Walkthrough

The ATmega Timer1 ISR now uses a named handler with a weak vector alias. Before attaching a timer handler, FastLED checks whether another library provides the Timer1 compare-match vector. If it does, FastLED warns and returns -16.

Changes: ATmega Timer1 vector ownership

Layer / File(s) Summary
Timer1 handler and ownership check
src/platforms/avr/atmega/isr_avr_atmega.hpp
FastLED uses a named ISR with a weak TIMER1_COMPA_vect alias. If another library provides a strong vector implementation, attach_timer_handler warns and returns -16 before reconfiguring the timer.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 07eb1

No confirmed issue blocks merging. The Servo attachment behavior remains unverified in a target build.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: allowing Arduino Servo to own the ATmega Timer1 vector.
Linked Issues check Passed Issue #4836 requires FastLED and the standard Servo library to link on ATmega328P without a duplicate Timer1 Compare A vector. The change defines FastLED's handler as fastled_timer1_compa_isr and ex…
Out of Scope Changes check Passed The pull request changes only src/platforms/avr/atmega/isr_avr_atmega.hpp. The weak vector alias and ownership check directly implement issue #4836. The warning and existing timer-in-use error suppo…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 1 files.
Full details: Linked Issues check

Explanation

Issue #4836 requires FastLED and the standard Servo library to link on ATmega328P without a duplicate Timer1 Compare A vector. The change defines FastLED's handler as fastled_timer1_compa_isr and exposes TIMER1_COMPA_vect through a weak alias. A strong Servo vector can therefore take precedence. attach_timer_handler checks the resolved vector before it changes Timer1 registers and returns -16 when another owner is present. The PR reports a successful Uno build with the official Servo library and Servo::attach(9), plus a successful strong-vector link test.

Full details: Out of Scope Changes check

Explanation

The pull request changes only src/platforms/avr/atmega/isr_avr_atmega.hpp. The weak vector alias and ownership check directly implement issue #4836. The warning and existing timer-in-use error support the same compatibility behavior. No unrelated product, platform, or administrative changes are shown.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · 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 merged commit d9472f3 into master Oct 22, 2026
121 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Oct 22, 2026
@zackees

zackees commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

Follow-up runtime validation is now complete using the native fbuild fix in FastLED/fbuild#1697 (2.5.43).

Linked the official Arduino Servo library at arduino-libraries/Servo@440b319ec11ea2664df8ce201c271c935de28a2d, called Servo::attach(9), then called fl::isr::attach_timer_handler at 1 kHz. With interrupts disabled during the comparison, the probe checked that TCCR1A, TCCR1B, TIMSK1, OCR1A, OCR1B, and ICR1 were unchanged and the output handle remained invalid.

The Uno firmware linked and ran in native AVR8JS, reporting:

timer result=-16
SERVO_TIMER_PRESERVED
avr8js ATmega328P test-emu passed: passed: halt-on-success pattern matched: SERVO_TIMER_PRESERVED

This verifies the Timer1 ownership guard and register preservation for this configuration. It does not establish concurrent FastLED timer-ISR operation with Servo: Servo owns the vector and FastLED's timer attachment is rejected as intended. The temporary fixture/library were kept outside the tracked source and removed from the staged project after validation.

@zackees

zackees commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

Full CI for #4895 found a regression in this merged weak alias: AVR GCC 7.3 LTO crashes while linking AnalogOutput. Moving the alias to assembly links but lets the CRT weak default win, so that approach is also invalid for standalone timers.

Correction pushed in 8e6df0e (#4895): strong Timer1 compare-B ISR at the same CTC TOP, plus a pre-write compare-A ownership check. Local AVR8JS probes now verify both standalone callbacks (101 ticks over 100 ms at 1 kHz) and real Servo rejection (-16, invalid handle, all six Timer1 snapshots preserved). AnalogOutput links with LTO enabled. The correction is awaiting full CI in #4895.

@zackees
zackees deleted the fix/avr-servo-timer1-vector 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 Uno: multiple definition of __vector_11 when including Servo.h and FastLED.h (3.10.7)

1 participant