Skip to content

fix(teensy4): avoid MHz truncation in software-SPI phase delays #5027

Closed

Description

@zackees

Context

Two-round audit of baseline 94dab9c for #5024 found a concrete timing correctness/validation gap in generic software SPI on Teensy4. src/fastspi.h:74-78 represents Teensy4 SPI rates in Hz; the non-LPSPI fallback (src/platforms/arm/teensy/teensy4_common/spi_output_template.h:17-20,31-54) passes this rate to GenericSoftwareSPIOutput.

The Teensy4-specific timing branch at src/platforms/shared/spi_bitbang/generic_software_spi.h:169-172 defines DELAY_NS = 1000 / (SPI_SPEED/1000000) then calls delayNanoseconds. For DATA_RATE_KHZ(500) the inner integer quotient is zero, so instantiated write paths divide by zero rather than obtaining a finite delay. Rates not divisible by 1 MHz also lose precision (e.g. 1.5 MHz truncates to 1 MHz). This is not a Hz-to-cycle fallback bug: Teensy4 already has a dedicated Hz timing branch. Both public writeByte (204-211) and bulk snapshot/stream paths use the macros.

Production example to instantiate in existing AutoResearch or test fixtures: APA102 controller with data pin 0, clock pin 1, DATA_RATE_KHZ(500) on Teensy40/41. These GPIO6 pins are not a hardware LPSPI pair, so selector fallback is unavoidable (spi_output_selector.h:15-22); APA102 invokes public writeByte (src/fl/chipsets/apa102.h:43,57-69). Native LPSPI paths are excluded.

Proposal

Compute the requested nanosecond phase duration directly from the Hz rate using overflow-safe arithmetic and explicit zero/unsupported-rate handling. Preserve/document the intended per-phase delay and GPIO overhead; do not silently reinterpret Teensy4 rate units. Cover sub-MHz and fractional-MHz rates before adding further GPIO timing metadata.

Acceptance criteria

  • RED first: compile or timing-helper regression instantiates the real Teensy4 software SPI path at 500 kHz and demonstrates the zero denominator/current incorrect calculation. GREEN after implementation must produce finite positive appropriate phase delays without divide-by-zero warnings or runtime behavior. Restoring MHz truncation must fail the regression.
  • Cover 100 kHz, 500 kHz, 1 MHz, 1.5 MHz, 4 MHz and zero-rate policy, including public-byte and bulk APIs, default and interrupt mode; assert arithmetic and waveform ordering, rather than merely successful construction.
  • Compile teensy40 and teensy41 through public APA102/nonhardware pin selection (0/1); cover 0/2 separate ports and an actual hardware pair 11/13 to prove routing remains hardware there.
  • Any frequency accuracy or throughput claim requires physical capture/measurement; host tests and target compiles establish arithmetic/selection safety only.

Decisions

P2 correctness issue found while auditing applicability of ESP8266 speedups. Keep separate from GPIO policy adoption because rate arithmetic is independently wrong without any optimization. No source edits or tests were run during discovery; parent orchestrator owns RED/GREEN.

Reproduction

rg -n 'DELAY_NS|FL_IS_TEENSY_4X' src/platforms/shared/spi_bitbang/generic_software_spi.h
rg -n 'FL_DATA_RATE_KHZ|FASTLED_TEENSY4' src/fastspi.h
Metadata-only all-state duplicate searches: software SPI Teensy, Teensy SPI frequency fallback, Teensy SPI 500. No matching sub-MHz arithmetic issue found; #974 is a different cycle-divider underflow problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    sanitized

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions