Sphere
Log in Join
☆ 5

stm32 hal spi dma transfer only sends first byte

STM32F407VG, CubeIDE 1.14, HAL drivers. Trying to send a 16-byte buffer to a display over SPI1 with HAL_SPI_Transmit_DMA. The first byte goes out, then the DMA transfer complete callback fires but the rest is garbage / never arrives. Polling mode (HAL_SPI_Transmit) works fine with the same buffer and settings. I'm using a global uint8_t buffer. My theory is the buffer goes out of scope - but it's global, so no. What am I missing with DMA? Do I need to wait for the callback before modifying the buffer?

⚑ Report

4 replies

☆ 0
G ghost ·2026-11-07#1

Classic DMA issue: the buffer must stay valid for the entire transfer, and a global survives, but check the DMA mode. In CubeMX, 'Normal' mode stops after one transfer; 'Circular' repeats. For a single transmit, Normal is correct. More likely: you're starting the DMA transfer then immediately modifying the buffer. What's the DMA mode setting, and are you waiting on HAL_SPI_TxCpltCallback before touching the buffer? Also check the return value of HAL_SPI_Transmit_DMA.

⚑ Report
☆ 0
N null76 ·2026-11-07#2

DMA mode is Normal. I do modify the buffer in a loop right after starting the transfer - I'm sending data in chunks and filling the buffer each iteration. So I'm overwriting it while DMA reads it. That would explain garbage. But why does the callback fire immediately? I think I'm calling HAL_SPI_Transmit_DMA again while the previous transfer is still running, which returns HAL_BUSY and maybe resets something.

⚑ Report
☆ 0
N null ·2026-11-08#3

Yes - you can't start a new DMA transfer until the previous one completes. Check the return value; it returns HAL_BUSY if SPI/DMA is still occupied. Use the TxCpltCallback to signal completion (flag or semaphore) and only then fill the next chunk and start the next transfer. Double-buffering helps: fill buffer B while A transmits, then swap on callback. That's the standard pattern for continuous SPI streams.

⚑ Report
☆ 0
C cache ·2026-11-08#4

Double-buffering with a flag set in HAL_SPI_TxCpltCallback fixed it. Also learned to check HAL_SPI_Transmit_DMA's return value - I was ignoring HAL_BUSY. One more gotcha: make sure CS is held low for the entire multi-chunk transfer; I was toggling it per chunk and the display interpreted each chunk as a new command. DMA itself was fine, my buffer management was the problem.

⚑ Report
Log in to reply.