Skip to content

fix(avr): test selected bit in native SPI and USART GPIO writeBit methods #5052

Closed

Description

@zackees

Context

Follow-up two-round source audit of 94dab9c for #5024 corrects an earlier overbroad exclusion of native AVR SPI/UART classes: byte transmission is peripheral-driven, but five independently implemented writeBit methods drive CPU GPIO directly.

In src/platforms/avr/fastspi_avr.h, AVRUSART1SPIOutput::writeBit at 92-102 (UBRR1 guard), AVRUSART0SPIOutput::writeBit at 248-258 (UBRR0 guard), and modern AVRHardwareSPIOutput::writeBit at 427-437 (SPI0_CTRLA guard) test b && (1 << BIT). For BIT 0..7 the shifted mask is nonzero, so every nonzero byte sets data high regardless of the selected bit. Concrete counterexamples: writeBit<0>(2) and writeBit<7>(1) must drive data low but drive high.

The classic SPSR implementation at 615-629 and SPSR0 implementation at 803-817 already use correct bitwise &, temporarily disable SPE, pulse the clock, restore SPE and clear wait state. These are correctness baselines, not optimization positives.

Reachability: all five expose public static writeBit<BIT>(u8); native writePixels invokes writeBit<0>(1) for FLAG_START_BIT at 168-169,319-320,498-499,688-689,876-877. Those constant-one start-bit calls happen to behave correctly and cannot detect the bug. SM16716 also calls public writeBit<0>(0) (src/fl/chipsets/sm16716.h:32,36), likewise unaffected. Modern native SPIOutput is selected by src/platforms/avr/spi_output_template.h:22-24 when AVR_HARDWARE_SPI is defined. UART classes have public direct-instantiation API; no current internal UART class instantiation was found. Thus observed current in-tree constant callers are unaffected, but the public byte/bit contract is wrong.

Proposal

Replace logical truth testing with selected-bit testing in the three defective production methods. Preserve pin order, peripheral enable/wait behavior and all native byte paths. Keep this separate from generic _AVRPIN streaming #5025, which never reaches these methods.

Acceptance criteria

  • RED first: execute the actual production methods with fake GPIO/register observation, exhaustively all 256 byte values and template BIT 0..7 for each of five independently defined methods. The first three must fail before the fix; the last two establish correct baseline behavior.
  • GREEN after fix: sampled data equals (byte & (1U << BIT)) != 0, with exactly the existing clock high/low sequence and unrelated GPIO preservation. Restoring && independently in each repaired method must fail the corresponding regression. Merely testing duplicated predicates or only start bits is insufficient.
  • Cover register guards/instantiation via uno/mega for USART0, leonardo or mega for USART1, available ATmega4809/Nano Every or ATtiny1604/1616 for SPI0_CTRLA, uno for SPSR and an available SPSR0 target (e.g. ATmega328PB). If no board inventory target exists for SPSR0, document that and instantiate the actual guarded production implementation in the register fixture.
  • Preserve FLAG_START_BIT and SM16716 zero/start-bit behavior and register restoration for classic SPI methods. Compile representative actual boards via repository wrappers; no new workflow files.

Decisions

P2 public-API correctness bug, no claim that current constant-one/zero chipset calls are broken. Group three instances of the same faulty predicate in one correctness issue while requiring independent mutation gates. All five GPIO methods are explicitly inventoried; peripheral byte paths remain excluded from generic speedups. Do not claim throughput gains: AVR SBI/CBI may already be optimal and no physical measurement was performed.

Reproduction

rg -n 'writeBit|FLAG_START_BIT|b &&|b &' src/platforms/avr/fastspi_avr.h
rg -n 'FLAG_START_BIT|writeBit<' src/fl/chipsets src/fastspi_types.h
All-state metadata duplicate searches AVR SPI writeBit logical and AVR writeBit found no matching correctness report. No builds/tests/source edits were performed during discovery; orchestrator owns RED/GREEN.

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