# SPI：DMA 传输大小不匹配，通道 1 上预期 256 字节但实际传输 128 字节

- **ID:** `embedded/spi-dma-transfer-size-mismatch`
- **领域:** embedded
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

DMA 配置使用了错误的数据大小或突发设置，或 SPI FIFO 阈值与 DMA 传输长度不对齐。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| STM32CubeMX 6.10.0 | active | — | — |
| HAL SPI Driver 2.3.1 | active | — | — |
| ARM GCC 13.2.1 | active | — | — |

## 解决方案

1. ```
   Align DMA data size with SPI data size: ensure DMA_SxCR->PSIZE and MSIZE match SPI data width (e.g., 8-bit for SPI 8-bit frames). Set DMA_NNDT register to exact number of transfers: DMA1_Stream3->NDTR = 256;
   ```
2. ```
   Disable SPI FIFO by setting SPI_CR2->FRXTH = 0 and use 8-bit data frames to match DMA byte transfers; adjust DMA burst settings to single transfers (MBURST = 0, PBURST = 0).
   ```

## 无效尝试

- **Increase SPI baud rate to speed up transfer** — Baud rate does not affect DMA transfer size; mismatch is due to configuration, not speed. (95% 失败率)
- **Set DMA to circular mode to repeat transfer** — Circular mode repeats the same incorrect size; it does not fix the underlying mismatch and may cause buffer overflow. (85% 失败率)
