# SPI：DMA FIFO溢出，通道2数据丢失

- **ID:** `embedded/spi-dma-fifo-overrun`
- **领域:** embedded
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

SPI DMA传输速率超过FIFO排空速度，导致接收FIFO中数据丢失。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| STM32Cube_FW_H7_V1.11.0 | active | — | — |
| FreeRTOSv202212.01 | active | — | — |
| ARM GCC 12.2.1 | active | — | — |

## 解决方案

1. ```
   Reduce SPI baud rate prescaler (e.g., from 2 to 4) in MX_SPI1_Init(): hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; then regenerate code.
   ```
2. ```
   Enable SPI RX FIFO threshold interrupt and use DMA with circular mode: set hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_08DATA; and configure DMA in circular mode.
   ```
3. ```
   Use DMA flow controller with burst transfers: configure DMA2_Stream0 with DMA_SxCR_PBURST and DMA_SxCR_MBURST enabled.
   ```

## 无效尝试

- **Increasing SPI clock speed to match DMA rate** — Higher clock speed worsens the FIFO overflow issue as data arrives faster. (90% 失败率)
- **Disabling DMA and using polling mode** — Polling mode reduces throughput and may cause CPU starvation in real-time systems. (70% 失败率)
- **Adding software delay loops in the DMA ISR** — Delays in ISR increase latency and can cause other interrupts to be missed. (80% 失败率)
