Repository navigation
Description
Context
Native child of #5024; discovery baseline 94dab9c6b. This is a distinct CPU GPIO writer hidden inside a peripheral class, not covered by generic BitbangPin policies in #5033/#5034.
Round 1 traced SAMDHardwareSPIOutput::writeBit<BIT> in src/platforms/arm/sam/fastspi_arm_sam.h:279-297: disable SERCOM, configure data/clock as GPIO outputs, branch and call fl::digitalWrite for data, write clock high/low, restore SCK/MOSI pinPeripheral functions, enable SERCOM. No generic software-SPI writer or BitbangPin policy is called.
Round 2 verified production reachability: src/platforms/arm/sam/spi_output_template.h:25-39 selects SAMDHardwareSPIOutput for native PIN_SPI_MOSI/PIN_SPI_SCK. SM16716Controller in src/fl/chipsets/sm16716.h:26-40,65-68 invokes writeBit<0>(0) twice in its 50-zero header, and writePixelsToSPI<FLAG_START_BIT> in src/platforms/shared/spi_pixel_writer.h:42-46 invokes writeBit<0>(1) for every pixel. Public SAMD addLeds uses the legacy clocked controller (FastLED.h:115-125,1129). Consequently the hardware-selected SM16716 path repeatedly enters this CPU GPIO writer between SERCOM byte transfers.
Important negative: SAMDHardwareSPIOutput::writePixels' own FLAG_START_BIT branch at fastspi_arm_sam.h:304-316 emits a word via the peripheral and does not call its GPIO writeBit method. The production SM16716 caller uses the free pixel writer instead. Ordinary APA102 byte payload stays peripheral-driven and is not a software-SPI optimization target here.
Proposal
Optimize the actual SERCOM-to-GPIO single-bit handoff using direct FastPin/PORT access only after proving the mux and transaction contract. D21 uses PORT_IOBUS OUTSET/OUTCLR; D51 uses PORT OUTSET/OUTCLR (d21/fastpin_arm_d21.h:45-61, d51/fastpin_arm_d51.h:45-61). Those atomic destinations allow a data value selection without per-bit fl::digitalWrite call overhead or whole-port snapshots. Preserve complete SERCOM drain/disable, GPIO mux/direction, SCK idle-low, MOSI data setup, pinPeripheral restoration and enable ordering.
Do not replace pinMode with FastPin::setOutput and assume it changes hardware mux: inspect actual BSP and fl::pinMode semantics first. fastspi_arm_samd.hpp:21-59 owns shared PERIPH_SPI configuration/init/select, so alternating controllers/rates must still work. Native pin selection is based on actual variant constants, not assumed Arduino pin numbers.
Acceptance criteria
- RED first: a focused trace/codegen gate calls the actual SM16716Controller/free writer or the selected real SAMDHardwareSPIOutput method with instrumented vendor/core operations. Expose current dynamic data/clock dispatch and repeated GPIO handoff work; correctness assertions alone do not prove an optimization. Include both data polarities through the public writeBit API because SM16716's production bit values are constant 0/1 and their branches may already fold away.
- GREEN: prove fewer real production calls/instructions/register operations for the selected case and unchanged bit order/edge count. Restore the old method while keeping the new gate intact and show the optimization gate fails. If constant-bit codegen already removes the branch, report that case honestly: branch elimination is not a measured improvement, and remaining call/handoff reduction must be independently demonstrated.
- Record event ordering: previous SERCOM byte completes, peripheral disables, both pins become GPIO, correct data precedes exactly one rising/falling clock edge, clock ends low, native SCK/MOSI mux restores, SERCOM enables, and subsequent byte transfer succeeds. Verify both native MOSI and SCK mapping; account for core synchronization and pending TX completion even though transferDataSPI currently blocks on receive-complete.
- Verify the full SM16716 stream: one start bit plus 24 payload bits per pixel, then exactly 50 zero header clocks, including both individually emitted header bits, multiple pixels, consecutive frames, empty data, modifiers and RGB order. No accidental extra clock edges during mux changes.
- Test back-to-back controllers with different rates and selectable callbacks; preserve configureClock/select ordering, shared PERIPH_SPI ownership, no premature deselect, and MISO state. Non-native pin pairs must continue using generic output covered by perf(samd21): enable verified software-SPI GPIO streaming and atomic writes #5033/perf(samd51): optimize non-SERCOM software-SPI port writes #5034.
- Real target compile/codegen matrix: Arduino Zero and Feather M0 (SAMD21), ItsyBitsy M4 and Metro/Feather M4 (SAMD51), actual native variant pins and supported cores, native SM16716 and direct dynamic-bit API, normal/forced-software-pin configuration where supported. Hardware APA102 byte path is a regression control, not a claimed optimization.
- Preserve conservative existing timing while reducing redundant work. Any new GPIO-latency credit, skipped synchronization/padding or claimed physical throughput/phase accuracy requires target measurements at the LED input. Explicitly report unmeasured electrical behavior; host traces and compilation do not validate mux glitch/setup/hold timing.
Decisions
- One child covers the shared SAMDHardwareSPIOutput implementation with a D21/D51 matrix; its writer is distinct from generic software-SPI policies.
- P2; focus on repeated GPIO bit handoff required by real SM16716 traffic, without inventing a universal bulk-SPI speedup.
- Reuse test: gate software SPI changes on exhaustive production-path and mutation evidence #5031 production trace/mutation infrastructure and perf: inline shared software SPI tiny-delay policy at production callsites #5032 shared delay work where relevant. This method has no explicit delay calls, so perf: inline shared software SPI tiny-delay policy at production callsites #5032 alone does not optimize it.
- No source edits, builds, tests or hardware measurements were performed during discovery.
Next steps
- Confirm actual BSP pinMode/digitalWrite/pinPeripheral implementation and SERCOM synchronization requirements.
- Establish the real-path RED trace/codegen gate and baseline both constant and dynamic bit cases.
- Implement the smallest register/mux-safe change, prove GREEN and old-method mutation failure.
- Compile the board matrix; measure any changed physical timing assumptions and retain unresolved hardware gaps explicitly.
Open questions
Which part of the mux/direction/enable sequence can be reduced safely on each supported core requires vendor-core inspection and actual target evidence. No speedup is measured. Metadata duplicate searches SAMD writeBit and SAMD SM16716 found no strong duplicate; generic children #5033/#5034 do not reach this independent method.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Nov 7, 2026
#5131 fixes the wrong-pad handoff in writeBit and removes its per-bit calls, so this stays open. Still unmet: a committed RED/GREEN trace and old-method mutation, SM16716 stream verification, back-to-back controllers, cycle counts, and any measurement at the LED input. The wider mapping bug is #5130.
fix(samd): drive the real SPI pads in writeBit through the pin table (F…
zackees commentedon Nov 22, 2026
Closing with an accepted limitation.
Fix: #5131 (a5cca25), refined by #5201 (4209350, keeps SCK low across the handoff). SAMD SERCOM
writeBit(src/platforms/arm/sam/fastspi_arm_sam.h:308onward) now resolves the real data and clock pads throughg_APinDescription, instead ofpin/32, which named the wrong pad (e.g. Zero SCK 24 is PB11, not PA24). It hands them to GPIO viaPINCFGwith SCK held low, and drives them with directOUTSET/OUTCLRstores padded to 35 ns. The per-bitpinMode/digitalWritecalls are gone.Proven by:
ci/tests/test_sam_spi_writebit_handoff.pyand SAMD board builds.Accepted limitations:
The #5031 binding is replaced by a rescoped host-side gate, tracked in #5031.