Fix SD card busy-wait loops that can lock up flight controller #11321
+30
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
User description
Summary
sdcardSpi_deselect()to spin forever onbusIsBusy(), freezing the entire FC since blackbox runs inline with the PID loop viaprocessBlackbox()infc_core.cChanges
sdcard_spi.c—sdcardSpi_deselect()(called ~15 times after every SPI transaction):while (busIsBusy) { __NOP(); }with 100K-iteration timeout (~4µs at 168MHz)failureCount, disable card at threshold (8), always release SPI CS linesdcardSpi_init()(line 864)failureCountresets to 0 on any successful read/writesdmmc_sdio_f4xx.c— DMA disable loop:while (pDMA->CR & DMA_SxCR_EN)(~0.4µs)sdmmc_sdio_f4xx.c— FIFO read loops (3 locations:SD_HighSpeed,SD_GetCardStatus,SD_FindSCR):SD_DATATIMEOUTsoftware iteration counter as defense-in-depth behind hardwareSDIO_STA_DTIMEOUTTest plan
PR Type
Bug fix
Description
Add timeout guards to unbounded busy-wait loops in SD card drivers
sdcard_spi.c: 100K-iteration timeout insdcardSpi_deselect()with failure trackingsdmmc_sdio_f4xx.c: 10K-iteration timeout for DMA disable loopsdmmc_sdio_f4xx.c: Software timeout backstops in three FIFO read loopsPrevents flight controller lockup from problematic SD cards during blackbox operations
Diagram Walkthrough
File Walkthrough
sdcard_spi.c
Add timeout to SPI deselect busy-wait loopsrc/main/drivers/sdcard/sdcard_spi.c
while (busIsBusy)loop with 100K-iteration timeout(~4µs at 168MHz)
failureCounton timeout and disable card when threshold (8)is reached
busDeselectDevice()after timeoutsdcardSpi_init()sdmmc_sdio_f4xx.c
Add timeouts to DMA and FIFO read loopssrc/main/drivers/sdcard/sdmmc_sdio_f4xx.c
SD_StartBlockTransfert()(~0.4µs)SD_DATATIMEOUTsoftware iteration counter to three FIFO readloops:
SD_HighSpeed(),SD_GetCardStatus(), andSD_FindSCR()SDIO_STA_DTIMEOUTimpact