
In the previous two parts, we used the STM32 LL drivers to transmit and receive data over SPI in polling mode, which kept the CPU waiting on every byte. In this part, we hand the job to DMA: SPI1 sends a whole buffer in the background, and the CPU stays free until the transfer-complete interrupt tells us the data is out.
In this guide, we shall cover the following:
- Introduction.
- STM32CubeMX setup.
- Firmware Development.
- Results.
1. Introduction:
In the previous two parts of this series, we used the STM32 Low-Layer (LL) drivers to transmit and receive data over SPI in polling mode. Polling is the most direct way to talk to a peripheral. We write a byte to the data register, wait for the TXE flag, write the next byte, and at the end wait for the BSY flag to clear. It’s easy to follow and easy to debug, which is why we started there. Its weakness is that while it waits, the CPU does nothing useful. It sits in a loop checking a flag again and again until the hardware is ready.
Here’s how much that costs. Say SPI runs at 1 MHz. Each byte takes 8 µs on the wire. For an STM32F446RE running at 180 MHz, that’s about 1,440 clock cycles spent waiting for every single byte. Sending a few configuration bytes to a sensor is fine. Now picture refreshing a display, writing a page to external flash, or streaming audio samples to a DAC. Thousands of bytes are involved, and the CPU spends most of its time as a very expensive byte mover. Meanwhile, other tasks are delayed, timing becomes hard to predict, and the core can’t go to sleep to save power.
Interrupts are a partial improvement. Instead of polling, the SPI raises an interrupt when it’s ready for the next byte. That frees the CPU between bytes, but every byte still costs a full interrupt: entering the handler, saving context, writing the register, and returning. At higher SPI speeds, the handler fires so often that the overhead can approach what polling cost us. Interrupt latency can also leave gaps between bytes on the bus.
DMA (Direct Memory Access) solves the problem at its root. The DMA controller is a separate piece of hardware inside the microcontroller, and its only job is moving data. We set it up once: here is the buffer in memory, here is the SPI data register, move this many bytes. Then we start the transfer. Every time the SPI is ready for new data, it signals the DMA, and the DMA writes the next byte straight into the data register. The CPU doesn’t touch any of it. When the whole buffer has been handed off, the DMA raises a single interrupt to tell us the transfer is complete.
This brings several practical benefits:
- The CPU is free. While the transfer runs, the core can process data, serve other peripherals, or enter a low-power mode.
- Continuous transfers. The DMA keeps the SPI fed with no software delay, so bytes go out back-to-back without gaps.
- Predictable timing. Transfer time depends on the SPI clock and the buffer length, not on how busy the CPU happens to be.
- One interrupt per transfer instead of per byte. No matter how big the buffer is, software only steps in at the start and the end.
DMA isn’t always the right choice. For a transfer of just two or three bytes, setting up the DMA can take longer than simply polling. Its real strength is in larger blocks of data and in applications where the CPU has other work to do.
In this part, we’ll put this into practice on the STM32F446RE Nucleo-64. We’ll keep the same SPI1 setup as before and hand the transmit side to the DMA, using the LL drivers to configure each piece ourselves.
2. STM32CubeMX setup:
We shall continue from this guide here.
Open the .ioc project’s file in STM32CubeMX.
In SPI1 or the configured SPI, from DMA tab, enable the SPIx_TX as follows:
- Mode to Normal.
- Data Width to Byte.
Furthermore, remember the DMA channel and Stream, in this case, DMA2_Stream3.

Next, from NVIC tab, enable the SPI global interrupt as follows:

Next, from System Core, NVIC, Code generation, disable generate to interrupt handler for SPI and DMA as follows:

Finally, click on Generate code.
Thats all for the STM32CubeMX setup.
3. Firmware Development:
Before we develop the firmware, we shall see how the SPI
The flow as follows:

The flowchart shows the transmit process split across three parts: the function that starts the transfer, and the two interrupt handlers that finish it.
SPI_TransmitBytes_DMA() prepares and starts the transfer. If the size is zero, it calls the completion callback right away and returns. Otherwise, it disables DMA2 Stream 3 and waits until the stream is fully stopped, because a stream can only be reconfigured while it’s disabled. Next, it clears any leftover flags, sets the source (our buffer), the destination (the SPI data register) and the number of bytes, and enables the transfer-complete and transfer-error interrupts. Finally, it enables SPI TX DMA requests, enables the SPI, and enables the stream. From here on, the function returns and the CPU is free.
The DMA hardware then does the actual work. Each time the SPI is ready for new data, it requests a byte, and the DMA copies the next byte from the buffer into the data register, with no CPU involvement.
DMA2_Stream3_IRQHandler() runs once the DMA has written the last byte. At that point, the DMA’s job is done, but the SPI is still sending that byte. So the handler clears the TC flag and enables the SPI TXE interrupt, handing the final step over to the SPI. If a transfer error occurred, the TE branch clears the flag so it can be handled.
SPI1_IRQHandler() runs when the SPI data register becomes empty. It disables the TXE interrupt and the TX DMA request, then calls spi_tx_completed(). This is a weak callback that the application can override, for example to raise chip select or start the next transfer.
Next, open the project in STM32CubeIDE.
Open the spi.h header file.
In user code begin prototype, declare the following function:
void SPI_TransmitBytes_DMA(SPI_TypeDef *SPIx, const uint8_t *pData, uint16_t Size);
The function shall transmit block of data using the DMA and takes the following parameters:
- Instant the SPI which is SPI1 in this case/
- Buffer to hold the data to be sent.
- Size of the data to be sent.
Thats all for the header file.
Next, in spi.c source file, in user code begin 1, declare the following function:
__weak void spi_tx_completed(void) { }This function shall be called when SPI in DMA mode finishes the transmission of the data.
Next, the transmit function in DMA mode:
void SPI_TransmitBytes_DMA(SPI_TypeDef *SPIx, const uint8_t *pData, uint16_t Size)
Within the function:
First, check the size if it is zero or, if it is zero, return from the function:
if (Size == 0U)
{
spi_tx_completed();
return;
}Next, stop the DMA Stream and wait until it is disabled as follows:
/* Stop and wait for the stream to actually go idle (EN bit clear). */
LL_DMA_DisableStream(DMA2, LL_DMA_STREAM_3);
while (LL_DMA_IsEnabledStream(DMA2, LL_DMA_STREAM_3)) { }Note that we are using the same DMA channel and stream in STM32CubeMX. If your is different, change accordingly.
Next, clear all DMA interrupt flags as follows:
/* Clear all Stream 3 flags (F4 LL names are per-stream). */
LL_DMA_ClearFlag_TC3 (DMA2);
LL_DMA_ClearFlag_HT3 (DMA2);
LL_DMA_ClearFlag_TE3 (DMA2);
LL_DMA_ClearFlag_DME3(DMA2);
LL_DMA_ClearFlag_FE3 (DMA2);Next, configure the DMA memory address, peripheral address and the size of the data as follows:
/* Program source / destination / length (all generic, take Stream arg). */
LL_DMA_SetMemoryAddress(DMA2, LL_DMA_STREAM_3, (uint32_t)pData);
LL_DMA_SetPeriphAddress(DMA2, LL_DMA_STREAM_3, (uint32_t)&SPIx->DR);
LL_DMA_SetDataLength (DMA2, LL_DMA_STREAM_3, Size);Enable the transfer completed interrupt and error interrupt for the DMA as follows:
/* DMA TC will arm SPI TXEIE inside the DMA ISR. */
LL_DMA_EnableIT_TC(DMA2, LL_DMA_STREAM_3);
LL_DMA_EnableIT_TE(DMA2, LL_DMA_STREAM_3);Next, ensure the transmit buffer empty interrupt for SPI is disabled as follows:
LL_SPI_DisableIT_TXE(SPIx);
Next,
- Enable SPI DMA request.
- Enable the SPI.
- Start the DMA.
LL_SPI_EnableDMAReq_TX(SPIx);
LL_SPI_Enable(SPIx);
LL_DMA_EnableStream(DMA2, LL_DMA_STREAM_3);Thats all for the transmission function.
Next, when DMA finishes transferring the data to SPI, transfer complete interrupt will be triggered and we need to handle it as follows:
- If the source is Transfer TC, disable the DMA, clear the pending interrupt flag and enable transmit buffer empty interrupt for SPI.
- If the source is the error, this will be up to the user to be handled.
void DMA2_Stream3_IRQHandler(void)
{
if (LL_DMA_IsActiveFlag_TC3(DMA2))
{
LL_DMA_ClearFlag_TC3(DMA2);
LL_DMA_DisableIT_TC(DMA2, LL_DMA_STREAM_3);
/* Last byte is in SPI DR but still shifting out.
Arm TXEIE so we get one more TXE when the shift reg empties. */
LL_SPI_EnableIT_TXE(SPI1);
}
if (LL_DMA_IsActiveFlag_TE3(DMA2))
{
LL_DMA_ClearFlag_TE3(DMA2);
LL_DMA_DisableIT_TE(DMA2, LL_DMA_STREAM_3);
/* optional: error handling */
}
}Once the SPI transmit buffer is empty interrupt is generated, we shall handle it as follows:
void SPI1_IRQHandler(void)
{
if (LL_SPI_IsActiveFlag_TXE(SPI1))
{
LL_SPI_DisableIT_TXE(SPI1);
LL_SPI_DisableDMAReq_TX(SPI1);
spi_tx_completed();
}
}Hence, the functions as follows:
__weak void spi_tx_completed(void) { }
void SPI_TransmitBytes_DMA(SPI_TypeDef *SPIx, const uint8_t *pData, uint16_t Size)
{
if (Size == 0U)
{
spi_tx_completed();
return;
}
/* Stop and wait for the stream to actually go idle (EN bit clear). */
LL_DMA_DisableStream(DMA2, LL_DMA_STREAM_3);
while (LL_DMA_IsEnabledStream(DMA2, LL_DMA_STREAM_3)) { }
/* Clear all Stream 3 flags (F4 LL names are per-stream). */
LL_DMA_ClearFlag_TC3 (DMA2);
LL_DMA_ClearFlag_HT3 (DMA2);
LL_DMA_ClearFlag_TE3 (DMA2);
LL_DMA_ClearFlag_DME3(DMA2);
LL_DMA_ClearFlag_FE3 (DMA2);
/* Program source / destination / length (all generic, take Stream arg). */
LL_DMA_SetMemoryAddress(DMA2, LL_DMA_STREAM_3, (uint32_t)pData);
LL_DMA_SetPeriphAddress(DMA2, LL_DMA_STREAM_3, (uint32_t)&SPIx->DR);
LL_DMA_SetDataLength (DMA2, LL_DMA_STREAM_3, Size);
/* DMA TC will arm SPI TXEIE inside the DMA ISR. */
LL_DMA_EnableIT_TC(DMA2, LL_DMA_STREAM_3);
LL_DMA_EnableIT_TE(DMA2, LL_DMA_STREAM_3);
/* Guard against a stale TXEIE firing before DMA starts. */
LL_SPI_DisableIT_TXE(SPIx);
/* Arm SPI + DMA. */
LL_SPI_EnableDMAReq_TX(SPIx);
LL_SPI_Enable(SPIx);
LL_DMA_EnableStream(DMA2, LL_DMA_STREAM_3);
}
void DMA2_Stream3_IRQHandler(void)
{
if (LL_DMA_IsActiveFlag_TC3(DMA2))
{
LL_DMA_ClearFlag_TC3(DMA2);
LL_DMA_DisableIT_TC(DMA2, LL_DMA_STREAM_3);
/* Last byte is in SPI DR but still shifting out.
Arm TXEIE so we get one more TXE when the shift reg empties. */
LL_SPI_EnableIT_TXE(SPI1);
}
if (LL_DMA_IsActiveFlag_TE3(DMA2))
{
LL_DMA_ClearFlag_TE3(DMA2);
LL_DMA_DisableIT_TE(DMA2, LL_DMA_STREAM_3);
/* optional: error handling */
}
}
void SPI1_IRQHandler(void)
{
if (LL_SPI_IsActiveFlag_TXE(SPI1))
{
LL_SPI_DisableIT_TXE(SPI1);
LL_SPI_DisableDMAReq_TX(SPI1);
spi_tx_completed();
}
}Next, open the main.c file.
In user code begin PV, declare the following:
#define DataSize 5 uint8_t dataBuffer[DataSize]; volatile uint8_t spi1_tx_cmplt=0;
Next, in user code begin 0, declare the following function:
void spi_tx_completed(void)
{
spi1_tx_cmplt=1;
}This will be called each time the SPI has completed the transfer.
Next, in user code begin 2 in main function, fill the buffer as follows:
for (int i=0;i< DataSize; i++)
{
dataBuffer[i]=i+1;
}Next, in user code begin 3 in while 1 loop:
- Set the CS pin to low.
- Send the data.
Wait until the data has been transmitted. - Clear the flag.
- Set teh CS pin to high
CS_LOW(); SPI_TransmitBytes_DMA(SPI1, dataBuffer, DataSize); while(spi1_tx_cmplt==0); spi1_tx_cmplt=0; CS_HIGH();
Thats all for the firmware.
Save, build and run the project on your board.

You may download the code from here.
4. Results:
By probing PA5, PA7 and PA0 and using either oscilloscope or logic analyzer,. you should get the following:



Discussion