cancel
Showing results for 
Search instead for 
Did you mean: 

Interrupt vs DMA - which one is more efficient for Timer PWM output and SPI communication?

Broman3100
Associate III

I'm doing project that includes outputting an increasing/decreasing duty signal for led while also working on SPI communication, from my understanding I need to set up both timer output and spi in non-blocking mode and I can do both with HAL-s interrupt functions, as well as DMA functions. Here's where I'm confused, if both IT and DMA work in nonblocking mode and get the job done same way, what should I look out for, which one is more efficient and recommended to use?

1 ACCEPTED SOLUTION

Accepted Solutions

There are many factors, the decision is not simple and not black-and-white.

Individual DMA transfers do not directly involve the processor and generally they are faster, so you definitively want to use them in places where speed/latency is important. It also allows (in some STM32 models) to put the processor into some of the power-saving modes ("sleep") while still transferring data using DMA and some of the peripherals.

Setting up DMA transfers and wrapping up after transfers are finished does involve some processor activity, so generally transfers of only a few bytes/frames is not worth the hassle.

DMA is a relatively limited resource - usually there are one or two DMA controllers with 6-8 channels/streams each - and within a DMA controller, channels/streams share one or two common bus interfaces to the rest of the chip, i.e. they compete to each other. In other words, if you need some DMAs to have low latency/high throughput, you think twice before using more DMAs in the same controller.

If there is significant processing per-byte involved and if the individual bytes arrive/depart relatively slowly (which is typically the case of UART), a well-written interrupt-based process may be generally better than DMA.

As I've said above, the third option is polling in a loop (fourth is polling using RTOS), which may be not the most efficient, but is usually simpler than interrupts or DMA, which means that it is the most likely to avoid some common bugs and pitfalls of both interrupts and DMA (mostly atomicity-related).

JW

View solution in original post

3 REPLIES 3

> I can do both with HAL-s interrupt functions, as well as DMA functions.

Or you can write your own code, including simple polling.

> if both IT and DMA work in nonblocking mode and get the job done same way,

Well, obviously not the same way. Interrupts interrupt regularly, and DMA don't (except usually at the end of transfer).

> which one is more efficient and recommended to use?

This depends on the application's details. In microcontrollers, there is never a "best way".

JW

To be precise, in what kind of application would I prefer to use one over the other?

There are many factors, the decision is not simple and not black-and-white.

Individual DMA transfers do not directly involve the processor and generally they are faster, so you definitively want to use them in places where speed/latency is important. It also allows (in some STM32 models) to put the processor into some of the power-saving modes ("sleep") while still transferring data using DMA and some of the peripherals.

Setting up DMA transfers and wrapping up after transfers are finished does involve some processor activity, so generally transfers of only a few bytes/frames is not worth the hassle.

DMA is a relatively limited resource - usually there are one or two DMA controllers with 6-8 channels/streams each - and within a DMA controller, channels/streams share one or two common bus interfaces to the rest of the chip, i.e. they compete to each other. In other words, if you need some DMAs to have low latency/high throughput, you think twice before using more DMAs in the same controller.

If there is significant processing per-byte involved and if the individual bytes arrive/depart relatively slowly (which is typically the case of UART), a well-written interrupt-based process may be generally better than DMA.

As I've said above, the third option is polling in a loop (fourth is polling using RTOS), which may be not the most efficient, but is usually simpler than interrupts or DMA, which means that it is the most likely to avoid some common bugs and pitfalls of both interrupts and DMA (mostly atomicity-related).

JW