Skip to content

fix(spi): clamp bit-bang clock-high delay instead of wrapping (#974) - #3831

Merged
zackees merged 1 commit into
masterfrom
fix/issue-974-bitbang-delay-underflow
Aug 2, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/issue-974-bitbang-delay-underflow

Conversation

@zackees

@zackees zackees commented Aug 2, 2026 •

Copy link
Copy Markdown
Member

Addresses the surviving tail of #974.

What #974 reported

Software SPI (explicit DATA+CLOCK pins) on Teensy failed to compile with template instantiation depth exceeds maximum, recursing on delaycycles<2147482745>. The huge CYCLES came from an unsigned underflow in CLOCK_HI_DELAY.

What is already fixed

Two independent changes since 2020 handle the reported case:

  1. The SPI_SPEED > 10 guard routes small values away from the subtraction, so the report's own SPI_SPEED == 1 no longer underflows.
  2. delaycycles<> now binary-splits (N -> N/2 + N-N/2) instead of recursing linearly by 1, so depth is logarithmic and the depth diagnostic can no longer occur.

What is not fixed — and is reachable

The guard only covers the low end. A window survives where SPI_SPEED clears 10 but (SPI_SPEED-6)/2 is still below MIN_DELAY:

#define MIN_DELAY ((NS(35)>3) ? (NS(35) - 3) : 1)
delaycycles<((SPI_SPEED > 10) ? (((SPI_SPEED-6) / 2) - MIN_DELAY) : (SPI_SPEED))>();

SPI_SPEED is an unsigned template parameter, so that subtraction wraps. On a Teensy 3.6 at 180 MHz (MIN_DELAY == 4), a plain DATA_RATE_MHZ(4) gives SPI_SPEED == 12:

(12 - 6) / 2 - 4  ->  -1  ->  4294967295

Evaluated across the range with the real types:

SPI_SPEED before after
1 1 1
8 8 8
11 4294967294 0
12 4294967295 0
13 4294967295 0
16 1 1
24 5 5
48 17 17

Note this no longer surfaces as the depth error from the original report. Because cycle_t is i64 on non-AVR, the wrapped u32 converts to a large positive value, so the <=0 base-case specialisations never catch it — and with binary splitting the compiler now dutifully tries to emit ~4.29 billion inlined NOPs. The failure mode got quieter, not safer.

The fix

Clamp to zero. MIN_DELAY has already been emitted at that point and is longer than the half period being requested, so no additional delay is owed. Clock-high becomes max(MIN_DELAY, half_period), which is what the code intended.

Also hoists the twice-repeated (SPI_SPEED-6)/2 into FL_SPI_HALF_PERIOD so the high and low delays cannot drift apart.

Behavior is unchanged for every SPI_SPEED outside the broken window (table above).

Verification

  • bash test --cpp — 279/279 unit, 83/83 examples
  • bash compile teensy31 --examples Blink — success
  • bash lint — clean

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved SPI timing reliability for very short clock periods.
    • Prevented timing calculations from underflowing, helping maintain stable communication across supported hardware.

CLOCK_HI_DELAY computed its extra delay as

    ((SPI_SPEED - 6) / 2) - MIN_DELAY

with SPI_SPEED an unsigned template parameter. When the requested half
period is shorter than the 35ns minimum clock pulse, that subtraction
wraps to ~4.29e9 and is then handed to delaycycles<> as its CYCLES
template argument.

This is the tail of #974. The report's own case -- SPI_SPEED == 1 -- was
already fixed by the `SPI_SPEED > 10` guard, which routes small values
away from the subtraction entirely. But the guard only covers the low
end. A window survives at the boundary, where SPI_SPEED clears 10 yet
(SPI_SPEED-6)/2 is still below MIN_DELAY.

That window is reachable from ordinary user code. On a Teensy 3.6 at
180 MHz, MIN_DELAY is 4 and DATA_RATE_MHZ(4) yields SPI_SPEED == 12:

    (12 - 6) / 2 - 4  ->  -1  ->  4294967295

Measured across the range on that config (MIN_DELAY = 4):

    SPI_SPEED   11 -> 4294967294
    SPI_SPEED   12 -> 4294967295
    SPI_SPEED   13 -> 4294967295

Since cycle_t is i64 on non-AVR, the wrapped u32 converts to a large
positive value rather than a negative one, so the <=0 base cases never
catch it.

Clamp to zero. MIN_DELAY has already been emitted by that point and is
longer than the half period being requested, so no further delay is
owed; the resulting clock-high time is max(MIN_DELAY, half_period),
which is what the code intended all along.

Behavior is unchanged for every SPI_SPEED outside the broken window --
verified by evaluating both the old and new expressions across the range
(1, 8, 11, 12, 13, 16, 24, 48) with the real types and F_CPU_MHZ=180.

Also hoists the repeated (SPI_SPEED-6)/2 into FL_SPI_HALF_PERIOD so the
high and low delays cannot drift apart.

Verified: bash test --cpp (279/279 unit, 83/83 examples),
bash compile teensy31 --examples Blink, bash lint.

Co-Authored-By: Claude <[email protected]>
@zackees
zackees merged commit ac36749 into master Aug 2, 2026
10 of 12 checks passed
@zackees
zackees deleted the fix/issue-974-bitbang-delay-underflow branch August 2, 2026 00:49
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Aug 2, 2026
@coderabbitai

coderabbitai Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 367f4150-552d-4b86-b88c-3465a1b371a8

📥 Commits

Reviewing files that changed from the base of the PR and between 69ea5b9 and 37ce623.

📒 Files selected for processing (1)
  • src/platforms/shared/spi_bitbang/generic_software_spi.h

📝 Walkthrough

Walkthrough

The SPI bit-bang timing macros now calculate a shared half-period and clamp high-phase extra delay to zero when the minimum high-pulse delay exceeds the requested period.

Changes

SPI timing correction

Layer / File(s) Summary
Clamp SPI phase delays
src/platforms/shared/spi_bitbang/generic_software_spi.h
Adds FL_SPI_HALF_PERIOD and clamps FL_SPI_HI_EXTRA_DELAY to prevent unsigned underflow. Low-phase timing continues to use the half-period calculation.

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

✨ 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 fix/issue-974-bitbang-delay-underflow

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.

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