Repository navigation
Resync the SPI link after a framing slip - #13
Conversation
SPI2 runs as a slave feeding circular DMA of a fixed 89 bytes, and nothing realigned the DMA with the host's frames. One transfer of the wrong length, or a glitch on SCK or chip select, offset every later frame: the firmware dropped every host write and libwallaby logged "DMA de-synchronized" on every transfer until the STM32 was reset. botui#60 reports this is common in the field, where reflashing (which resets the STM32) is the current fix. handle_dma() now sets spi_resync when a frame's start or end byte is wrong. A wrong version byte alone doesn't, since a reset can't fix a protocol mismatch. The main loop then calls spi2_dma_resync(), which waits for chip select to be high, stops both DMA streams, resets SPI2 through RCC (dropping any partial byte), and reinitializes and restarts the link with interrupts briefly disabled. To share that sequence with boot, SPI_DMA_Config() is split: the SPI2 and DMA stream setup moves to spi2_dma_init(), which leaves GPIO alone because GPIO_DeInit(GPIOB) would reset digital and analog pins on port B at runtime, and the enable sequence that init() ran inline moves to spi2_dma_start(). Boot runs the same steps in the same order, except that DMA interrupts and streams are now enabled after the NVIC setup instead of before it. Part of #11.
6b3c24f to
abffabc
Compare
Review of the first version found that recovery was slower than documented. The resync was only attempted once per 922 us main-loop pass and skipped whenever chip select was low, which is about 30% of the time under continuous traffic. A simulation with libwallaby's timing (89 bytes at 16 MHz, usleep(50) between transfers) lost 5.7 transfers per slip on average, 16 at p95 and up to 54. The main loop now calls spi2_dma_check_sync() every pass, which: - Treats the link as slipped if, between transfers, the RX stream's count isn't a full frame. A wrong-length transfer shows up right after it ends, instead of one misaligned frame later. The last byte can still be in flight just after chip select rises, so a mismatch is confirmed 2 us later. The start/end byte check in handle_dma() stays for slips the count can't see, like an extra SCK edge. - Treats an SPI2 overrun as a slip, since it stops reception. - Waits up to 60 us for chip select to go high instead of retrying a pass later. In the same simulation this gives 3.0 lost transfers on average, 7 at p95 and 20 at most, and none when the host pauses between calls. - Re-checks chip select with interrupts off before resetting, and again after restarting. If the host started a transfer during the reset, SPI2 caught only its tail, so spi_resync stays set. - Bounds the wait for the DMA streams to stop and retries on the next pass if it runs out. Also computes the framing result once in handle_dma(), drops the comments in spi2_dma_start() that restated each call, and corrects the recovery-time wording in AGENTS.md. Part of #11.
|
I tested this on a Wombat. The recovery works for most slips and a healthy link is unaffected, but on hardware I found that a reset can occasionally leave the link stuck in a state the firmware doesn't leave by itself. I'm working on tracking that down and will follow up here. SetupMaster now has #8 and #12, so I tested What works
Those numbers are better than the description's simulation (3.0 / 7 / 20). What doesn't1. A transfer one byte short of a frame is never recovered. After an 88- or 177-byte transfer the link stays desynchronized, 6 of 6 times; n = 88 is in your test list. The main loop is also starved: of 40 replies read 20 ms apart only 5–9 are distinct, against 40 on a healthy link, so sensors, back-EMF and PID are barely running. Master starves the same way after such a transfer, so that part isn't new. My current reading, not yet proven: the TX stream runs one byte ahead of RX, so after 89k + 88 bytes TX has completed a frame and RX hasn't. 2. About 0.3% of resets leave the link stuck. This is the one that's new with this PR. Sending one disturbance at a time under continuous traffic and checking the link after each (a write and a read-back), on the image with counters:
The plainly desynchronized case looks like a reset racing the start of the next host transfer and landing in the state from point 1. I don't have a mechanism for the silent case yet. Smaller notes
Not tested: motors and servos through libkipr on this build, and real glitches on SCK or chip select. |
A transfer one byte short of a frame (88, 177, ... bytes) hung the firmware. The TX stream runs ahead of RX because the next reply byte is preloaded, so after such a transfer TX had completed a frame and RX had not. handle_dma() only cleared the transfer-complete flags when both were set, so the TX stream's interrupt re-entered until the host's next transfer. The main loop, SysTick and the motor and servo timer interrupts all stopped while the host was idle, and the main loop only ran while chip select was low, so spi2_dma_check_sync() never saw chip select high and could not resync. The same lone TX flag is set for about two byte times in every healthy frame, so the handler also spun for about 1 us per frame at 16 MHz. handle_dma() now treats the RX stream's flag as the frame boundary and the TX stream's interrupt is no longer enabled. Frames are processed at the same moment as before, since the RX flag was always the later one. Tested on a Wombat with this change merged onto master: - 88-, 177- and 266-byte transfers recover with no transfer lost after a 1 s pause, and the main loop keeps its rate through the pause. Before, none recovered. - 18,000 unframed or wrong-length transfers left the link stuck 0 times. Before, about 0.3% of resyncs left it stuck with the main loop starved. - Healthy-link soak, slow clocks, boot while polling and the PR 12 test set are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Follow-up to my earlier comment: both failures have the same root and one small change fixes them. I pushed it to this branch as Cause
I measured point 2 with an image that holds the reset at a chosen step until chip select falls, waits a set delay, and records both DMA counts afterwards. About 5,700 injected races on this PR without the fix: the link stuck only when the enable landed in bytes 0–1, always in one of three count states, and the 46 stuck links from random traffic on the same image showed the same three. The change
Results with the changeTested as before: this branch merged onto master
The 78,000 includes back-to-back frames and gaps of 50 µs, 200 µs and random 0–1 ms, at 16 and 4 MHz. |
Resolve the main.c conflict with PR #13: call update_servo_power() after spi2_dma_check_sync(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Part of #11 (item 2). Stacked on #12. This PR targets
oscar/dma-write-bounds, so the diff shows only this change. I'll retarget it tomasteronce #12 merges.What and why
SPI2 is a slave feeding circular DMA of a fixed 89 bytes, and nothing realigns the DMA with the host's frames. One transfer of the wrong length, or a glitch on SCK or chip select, offsets every later frame. After that:
DMA de-synchronizedon every transfer.kipr/botui#60 reports this is "common enough that it is a hassle to explain to customers over the phone how to … reflash." Reflashing fixes it only because it resets the STM32.
Changes
Detection. The main loop calls
spi2_dma_check_sync()once per pass. It treats the link as slipped if any of these is true:handle_dma()saw a bad start ('J') or end ('S') byte and setspi_resync. This catches slips the count can't see, like an extra SCK edge that shifts bits without changing the byte count. A wrong version byte alone doesn't count as a slip, because a reset can't fix a protocol mismatch.Recovery.
spi_resyncstays set and the next pass resets again.Shared boot code.
SPI_DMA_Config()is split so boot and resync use the same code:spi2_dma_init()holds the SPI2 and DMA stream setup. It doesn't reconfigure pins, becauseGPIO_DeInit(GPIOB)at runtime would reset digital ports 4, 5, 13, 14, 15 and analog 0.spi2_dma_start()holds the enable sequence thatinit()ran inline.Boot runs the same steps, except that DMA interrupts and streams are now enabled after the NVIC setup rather than just before it.
Docs. AGENTS.md describes the resync.
Not handled: a slip in the TX direction alone, which neither the RX count nor the received bytes can show. Nothing suggests it happens; wrong-length transfers and SCK/chip-select glitches shift both directions together.
Expected recovery
I simulated the link with libwallaby's timing (89 bytes at 16 MHz,
usleep(50)between transfers), one check per 922 µs pass, and 20,000 random slip lengths. The table shows transfers lost after the slip transfer itself.Writes sent in lost transfers are dropped; libwallaby doesn't retry.
Verification
spi2_dma_check_sync()checked:IDRbit 12 and Stream3's count, and confirms a mismatch afterdelay_us(2).0x40).usCountat 60 µs.cpsid iandcpsie i, with a 100-try bound on stopping the streams.spi_resyncis set from the inverted chip-select bit after the restart.main.cis still LF.spidevsetup from Bounds-check host register writes in handle_dma #12:s.xfer2([0] * n), then run a program that readsanalog(0)in a loop for a few seconds. Try n = 1, 10, 50, 88, 90 and 200.DMA de-synchronizedcontinues untilwallaby_reset_coproc.msleep(10)), there should be none.wallaby_reset_coproca few times while botui is running. The link should come back each time.wallaby_flash.DMA de-synchronizedlines.