cancel
Showing results for 
Search instead for 
Did you mean: 

Which non-blocking SPI function is more efficient: HAL_SPI_Transmit_IT or HAL_SPI_Transmit_DMA? And why?

yeuop
Associate
 
2 REPLIES 2
Bob S
Principal

It depends... on which CPU, which clock speed, what else is going on (other interrupts/DMA), and on what you mean by "more efficient". There are trade-offs in each method.

Toggle a pin before starting and again after it is done, put that pin and the SPI MOSI signal on a scope and see what happens. Or use a timer to time how long it takes. See which works best in your application. If the timing doesn't make sense, post your results here.

Nikita91
Lead II

As said @Bob S​ it depends.

As an example imagine a low power system with a clock of 16 MHz, and you want to use a SPI at 8 MHz.

If you use interrupts, you will observe silence between bytes on the SPI line if you plug a scope. Because the transmission is fast compared to system clock, interrupt handling and byte transmission have a timing of the same magnitude, so the MCU load is high, and the transmission time not optimal.

If you use DMA the data bytes can be contiguous on the data line, therefore the data buffer is transmitted in a shorter time. Also during the transmission the CPU can do something else. In this sense we can say that the use of DMA is more efficient.

But... If you only have a few bytes to transmit, the time it takes to set up the DMA can be more than the gain when transmitting, especially when the SPI has a FIFO.

You also have to consider how the code is written. The HAL is very generic so absolutely not optimized.

So there is no easy answer. You really have to analyze case by case.