Skip to content

LPC845 SPI ISR streaming: first stream green (1358 toggles), repeat kick corrupts memory (wild-PC HardFault) #3585

Closed

Description

@zackees

State

ARMHardwareSPIOutputDMA::kickDmaStreamAsync() (ISR ping-pong refill, FASTLED_LPC_DMA_ISR=1) is silicon-proven for the first stream per boot:

dmaSpiStreamOverlap(1024, 0x5A) → "1,2577,1358"

1024 bytes (4 chained 256-byte chunks, 2 ISR refills) in 2577 µs with 1358 main-thread beacon toggles. The ISR trace breadcrumbs show the full chain: enter→armed→refilled ×2, enter→armed, enter→final.

A second kickDmaStreamAsync in the same boot HardFaults with a wild PC (0x20006798 — not even valid LPC845 address space; another run 0x0000817D with IPSR=3 = fault-in-fault). Memory-corruption signature, deterministic on the repeat-kick path.

Fixed along the way (in feat/delegate-flash-to-fbuild)

  • Stale INTA pending at registerDmaChannelIsr fired the callback before stream-state init → hub now W1C-clears INTA + NVIC pending before enabling.
  • Leaving INTENSET on after the final chunk let a residual completion HardFault the RPC reply path → the ISR's final branch now quiesces via INTENCLR (both SPI and UART drivers).

Repro

bash autoresearch lpc845 --dma-spi build (defines injected automatically), then over RPC:

dmaSpiStreamOverlap(1024, 90)   → PASS
dmaSpiStreamOverlap(1024, 165)  → HardFault, watchdog reboot

Set -DFASTLED_LPC_SPI_DMA_ISR_TRACE=1 for the ISR breadcrumbs.

Suspects for the follow-up session

  1. CTLSTAT.VALIDPENDING accumulation — every armHalf issues SETVALID; VALIDPENDING was observed stuck at 1 in earlier register snapshots. A second stream's XFERCFG write with VALIDPENDING latched may make the engine consume the in-memory descriptor's reserved word (0) via the reload path — worth clearing/ABORTing the channel between streams.
  2. Channel not fully disarmed between streams — consider COMMON[0].ABORT + ENABLECLR/ENABLESET cycle in kickDmaStreamAsync before re-arming.
  3. dmaSpiTransferOnce (sync, SETINTA) interleaved with streams shares the channel — same disarm hygiene applies.

Note: the UART twin is fully green including back-to-back streams (bash autoresearch lpc845 --dma-uart → PASS ×2), so the hub/vector/NVIC machinery is sound; the delta is the SPI driver's ping-pong arm path vs UART's simple chain.

Refs: #3453 (async drivers), #3580 (the six-bug sync bring-up), framework-arduino-lpc8xx#38 (weak IRQ vectors), fbuild#944 (pin bump).

Activity

  1. zackees commented on Jul 5, 2026

    @zackees
    MemberAuthor

    Root-caused and resolved. Not a driver logic bug — a stack/heap collision from RAM pressure, plus a pyserial transport bug masking the diagnosis.

    The "repeat-kick corrupts memory"

    The kickDmaStreamAsync ping-pong state machine is correct. Proof: with RAM headroom it runs 5 back-to-back streams green (667–878 toggles each, consistent timing), then a clean echo.

    The crash was a stack/heap collision. The map for the crashing build:

    • .bss ends 0x10002738; heap (.heap_stack) ends 0x10003338; stack grows down from _vStackTop = 0x10004000. Only ~3.3 KB shared heap+stack on the 16 KB part.
    • Crash PCs: 0x100032D4 (100 B below heap-end — stack already inside the heap) and 0x100033F0 (184 B above it). The "wild PCs" (0x20006798, 0x0000817D IPSR=3) are corrupted return addresses read off a stack that overran the heap.
    • The DMA completion ISR (encode + descriptor arm) preempts the RPC/JSON/sstream machinery at its deepest point and pushes SP past the heap top.

    Why UART's twin survived: ~800 B smaller .bss (no u16 encode buffer) + a shallower ISR (armNextChunk reads the caller's buffer in place, no encode loop). More headroom + less depth = the margin held.

    Fix: the SPI ISR-stream harness stream_src buffer defaults to 512 B (was 1024). 512 B is a genuine multi-chunk stream (2 chunks + 1 ISR refill at kHalfCap=256) and frees the ~512 B of stack clearance. For a real user with a lean sketch (no JSON-RPC stack) there's ample headroom and streaming works unmodified. Landed on feat/delegate-flash-to-fbuild.

    The masking transport bug (also fixed)

    The bench's "~1 reply per session" flakiness that made this hard to bisect was a separate pyserial transport bug — the LPC runners hand-rolled raw pyserial, which drops replies on Windows. Both runners now go through fbuild's Rust serial monitor via RpcClient (per @zackees's directive; documented in agents/docs/hardware-autoresearch.md → "Device serial"). Post-migration the SPI bench is 100% deterministic: 5/5 cases green including dmaSpiStreamOverlap ×3.

    Closing — both the stack/heap collision and the transport flakiness are resolved and silicon-verified.

  2. moved this from Triage to Done in FastLED Trackeron Jul 5, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions