Skip to content

fix(spi): pace SPIBusManager software SPI at the requested speed (#5124) - #5200

Merged
zackees merged 1 commit into
masterfrom
fix/soft-spi-pace-5124
Nov 9, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/soft-spi-pace-5124

Conversation

@zackees

@zackees zackees commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Closes #5124. Part of #5024.

SPIBusManager::softwareSPIWrite never received the device's requested_speed_hz and added no delay, so the software SPI clock ran as fast as the loop allowed.

Fix

  • Pass the speed in: the SOFT_SPI transmit path now hands requested_speed_hz to softwareSPIWrite. It converts the speed to a half period (softwareSpiHalfPeriodNs).
  • Pace both writer loops: the fast-register loop (writeRuntimeSoftwareSpi) and the per-pin Pin fallback both wait half a period after setting data, before the rising edge. The clock then stays high for another half period. The first wait also gives data a real setup time before the edge.
  • Unchanged default: a speed of 0 means no pacing, as before.

Bench tooling

  • softSpiClockLoopback AutoResearch RPC: drives fl::SPIOutput<2, 0, 250000>, ESP32's SPIBusManager adapter, from a small custom controller and measures the clock on the RX jumper. It passes only when the RX capture starts, completes and records at least 8 high and 8 low clock edges, and the clock is no faster than requested.
    • It goes through SPIOutput because on ESP32 addLeds<APA102> uses the Channels SPI_UNIFIED hardware driver. That driver never reaches this writer, even with FASTLED_FORCE_SOFTWARE_SPI; a first attempt through addLeds measured hardware SPI at exactly 1000 kHz.
  • bash autoresearch --define NAME[=VALUE]: a repeatable flag that adds compile defines to the staged build.

Evidence (ESP32-C6, physical jumper GPIO 0 → GPIO 1)

Built with bash autoresearch esp32c6 --define FASTLED_FORCE_SOFTWARE_SPI=1. Each run was reset first, then ran findConnectedPins and the RPC. busType 0 (software SPI) was reported on every run.

Requested 250 kHz Clock high Clock low Period Measured
RED (master, 2 runs) 260 / 235 ns 728 / 750 ns 988 / 985 ns 1012 / 1015 kHz
GREEN (this PR, 2 builds) 2970 / 3077 ns 3416 / 3496 ns 6386 / 6573 ns 156 / 152 kHz

Other checks:

  • C++ tests: bash test --cpp --clean passes (361/361 unit, 95/95 examples).
  • Lint: bash lint exits 0.
  • Non-ESP32 build: bash compile teensy41 --examples AutoResearch succeeds.

Limits

  • Slow, not exact: the clock never runs faster than requested, but at 250 kHz it runs about 38% slow. delayNanoseconds(2000) measures about 2.9-3.1 µs on this chip, and the loop's own time adds to it. No calibration is attempted.
  • Only ESP32-C6 measured: the change is in shared code. The Pin-fallback loop was not measured, because ESP32 takes the fast-register path.
  • Flaky first capture: the first capture straight after a flash sometimes records no edges, a bench quirk; a reset clears it. The RPC now reports such a run as a failure instead of passing it.
  • Pre-existing failures: 101 tests in ci/tests/test_autoresearch_phases.py already fail on master with a missing max_power_mw Args argument. Open PR fix(ci): name max_power_mw in the autoresearch Args test defaults #5164 fixes that.
  • BitBangChannelDriver SPI: first channel's rate wins, and data setup time is minimal #5127 not covered: that issue is about BitBangChannelDriver's rate grouping and setup time, which is a different writer.

Summary by CodeRabbit

  • New Features
    • Added a repeatable --define NAME[=VALUE] option for supplying build definitions.
    • Added an example SPI loopback check on supported ESP32 builds, reporting bus status, capture diagnostics, and measured clock timing. The check requires GPIO 0-to-1 wiring and a board reset before a subsequent run.
  • Improvements
    • Software SPI transfers now use the requested clock speed to pace data and clock transitions.

@zackees
zackees enabled auto-merge November 9, 2026 00:03
@github-actions github-actions Bot added the sanitized Title, body and conversation screened; review text requires separate screening. label Nov 9, 2026
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 9, 2026
@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Walkthrough

The AutoResearch CLI now accepts compile definitions for staged builds. Software-SPI writes now use the device’s requested speed for pacing. An ESP32 RPC sends a software-SPI probe and reports captured clock timing.

Changes: AutoResearch compile definitions

Layer / File(s) Summary
Define argument and build integration
ci/autoresearch/args.py, ci/autoresearch/phases.py
The parser accepts repeatable --define NAME[=VALUE] arguments and stores them in Args. The build phase adds the values to its define list.

Software SPI pacing and loopback

Layer / File(s) Summary
Requested-speed pacing
src/platforms/shared/spi_bitbang/runtime_software_spi.h, src/platforms/shared/spi_manager.h, src/platforms/shared/spi_manager.cpp.hpp
softwareSPIWrite receives the requested speed and computes a half-period. The runtime GPIO and Pin bit-bang paths use delays based on that period.
Software-SPI clock loopback RPC
examples/AutoResearch/AutoResearchRemoteDriverMethods.cpp, examples/AutoResearch/AutoResearchRemotePinMethods.cpp
The new RPC runs on ESP32, captures RX edges, and reports clock interval measurements and capture diagnostics. The help entry describes its setup and response fields.

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

Change: Bug fix · Severity of issue fixed: Low

Possibly related issues

  • Issue 5049: The change adds pacing to softwareSPIWrite, which is the function named in the issue’s timing requirements.
  • Issue 5024: The change adds requested-speed pacing to the software-SPI path covered by this umbrella issue.
  • Issue 5031: The new loopback RPC reports board-captured timing data, but the PR does not implement the issue’s full validation gate.
Merge Risk: Moderate · up to 8bd9e

Resolve definition forwarding and shared-clock pacing before merging: auto-detected builds can omit requested features, and shared software-SPI devices can exceed their requested speed. Smaller argument-compatibility and repository-requirement issues also remain.

- [ ] 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

@zackees
zackees added this pull request to the merge queue Nov 9, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Memory usage (this PR vs. its merge base)

Change in examples/Blink per target. The left number is this PR's image flash / attributed RAM in bytes; the colored delta is the change from the merge base (red ↑ is larger, green ↓ is smaller, Same is unchanged).

Target FLASH [bytes] RAM [bytes]
attiny85 2,460 (Same) 136 (Same)

@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 9, 2026
Merged via the queue into master with commit 0a4bade Nov 9, 2026
131 of 132 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 9, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 9, 2026
@zackees
zackees deleted the fix/soft-spi-pace-5124 branch November 17, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sanitized Title, body and conversation screened; review text requires separate screening.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

SPIBusManager software SPI ignores requested_speed_hz

1 participant