embedded runtime_error ai_generated true

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

SPI: DMA transfer size mismatch, expected 256 bytes but transferred 128 bytes on channel 1

ID: embedded/spi-dma-transfer-size-mismatch

其他格式: JSON · Markdown 中文 · English
92%修复率
87%置信度
1证据数
2025-05-03首次发现

版本兼容性

版本状态引入弃用备注
STM32CubeMX 6.10.0 active
HAL SPI Driver 2.3.1 active
ARM GCC 13.2.1 active

根因分析

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

English

DMA configuration uses incorrect data size or burst settings, or SPI FIFO threshold misaligned with DMA transfer length.

generic

官方文档

https://www.st.com/resource/en/application_note/an4031-dma-controller-on-stm32-mcus-stmicroelectronics.pdf

解决方案

  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).

无效尝试

常见但无效的做法:

  1. Increase SPI baud rate to speed up transfer 95% 失败

    Baud rate does not affect DMA transfer size; mismatch is due to configuration, not speed.

  2. Set DMA to circular mode to repeat transfer 85% 失败

    Circular mode repeats the same incorrect size; it does not fix the underlying mismatch and may cause buffer overflow.