Skip to content

perf(avr): opt hardware pins into generic software-SPI streaming #5025

Closed

Description

@zackees

Context

Audit of baseline 94dab9c for parent #5024, after two source investigation rounds. The generic software SPI stream can combine the preceding clock falling edge and next data update into two full-port writes per bit, but BitbangPin defaults kTwoWriteStream=false (src/platforms/shared/spi_bitbang/bitbang_pin.h:35). AVR never opts in.

src/platforms/avr/atmega/common/avr_pin.h:13-44 implements _AVRPIN with an 8-bit port_t, a real output-latch port(), and fastset that writes a whole latch. Its same-port data/clock semantics satisfy the exclusive full-port streaming contract. The implementation is shared by ATmega328P/2560/32U4, legacy ATmega variants, ATtiny and megaAVR pin maps; retain the width rather than using the 32-bit set/clear helper.

Production reachability: src/fastspi.h:107 exposes SoftwareSPIOutput; additionally src/platforms/avr/spi_output_template.h:28-30 selects the generic writer when AVR_HARDWARE_SPI is absent (e.g. ATtiny4313). Generic bulk APIs call writeByteStreaming (generic_software_spi.h:354-389, 398-439, 459-525); today it falls back to the old per-bit writer at 250-255. AVR peripheral SPI/UART implementations in fastspi_avr.h are excluded. APA102 calls public writeByte (src/fl/chipsets/apa102.h:43,57-69), so bulk-only savings must not be advertised as APA102 improvements.

Proposal

Specialize the existing pin policy for actual _AVRPIN-backed hardware pins to enable exclusive same-port streaming. Preserve forced software pins, separate-port behavior and FAST_SPI_INTERRUPTS_WRITE_PINS. Audit public-byte applicability explicitly; do not claim a hardware throughput gain without measurement.

Acceptance criteria

  • Before implementation, a focused RED trace/count regression using production policy and an 8-bit port model shows the missing opt-in; after implementation, GREEN confirms 16 streaming stores per byte plus one final clock-low store per nonempty transaction, MSB-first data at every rising edge, preserved unrelated bits, and clock low at completion. Mutating the policy to false must fail the optimization assertion.
  • Cover writeBytes, writeBytesValueRaw, pixels, empty and multi-byte input, all-zero/all-one/alternating data, separate ports and interrupt mode. Public writeByte/APA102 behavior must have an explicit tested disposition.
  • Compile actual pin-policy instantiations for uno, mega, leonardo, attiny85/attiny4313 and available megaAVR board configuration; include forced software pins and interrupt-mode coverage. Use repository board inventory and validation wrappers.
  • Maintain existing timing/minimum-pulse behavior and native hardware routing. Local traces prove write count and semantics only; any speed claims require on-device measurements.

Decisions

  • P2 optimization; this is code-confirmed missing applicability, not a measured device-speed claim.
  • Group AVR variants because they share _AVRPIN; do not enable arbitrary software-pin fallbacks.
  • No source edits or builds were performed during discovery; orchestrator owns RED/GREEN and board validation.

Open questions

Confirm whether public-byte streaming is worth separate work; it cannot simply leave the clock high across arbitrary calls. Verify megaAVR output-latch mapping in actual compile coverage.

Reproduction

rg -n 'kTwoWriteStream|writeByteStreaming|finishByteStream' src/platforms/shared/spi_bitbang
rg -n 'port_t|fastset|port\(' src/platforms/avr/atmega/common/avr_pin.h
Duplicate search: all-state issue metadata queries software SPI AVR and two write SPI; no matching optimization issue found.

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