Repository navigation
fix(spi): clamp bit-bang clock-high delay instead of wrapping (#974) - #3831
Merged
Merged
Conversation
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]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe 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. ChangesSPI timing correction
Estimated code review effort: 2 (Simple) | ~10 minutes ✨ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ondelaycycles<2147482745>. The hugeCYCLEScame from an unsigned underflow inCLOCK_HI_DELAY.What is already fixed
Two independent changes since 2020 handle the reported case:
SPI_SPEED > 10guard routes small values away from the subtraction, so the report's ownSPI_SPEED == 1no longer underflows.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_SPEEDclears 10 but(SPI_SPEED-6)/2is still belowMIN_DELAY:SPI_SPEEDis an unsigned template parameter, so that subtraction wraps. On a Teensy 3.6 at 180 MHz (MIN_DELAY == 4), a plainDATA_RATE_MHZ(4)givesSPI_SPEED == 12:Evaluated across the range with the real types:
Note this no longer surfaces as the depth error from the original report. Because
cycle_tisi64on non-AVR, the wrappedu32converts to a large positive value, so the<=0base-case specialisations never catch it — and with binary splitting the compiler now dutifully tries to emit ~4.29 billion inlinedNOPs. The failure mode got quieter, not safer.The fix
Clamp to zero.
MIN_DELAYhas already been emitted at that point and is longer than the half period being requested, so no additional delay is owed. Clock-high becomesmax(MIN_DELAY, half_period), which is what the code intended.Also hoists the twice-repeated
(SPI_SPEED-6)/2intoFL_SPI_HALF_PERIODso the high and low delays cannot drift apart.Behavior is unchanged for every
SPI_SPEEDoutside the broken window (table above).Verification
bash test --cpp— 279/279 unit, 83/83 examplesbash compile teensy31 --examples Blink— successbash lint— clean🤖 Generated with Claude Code
Summary by CodeRabbit