0
votes

I am using STM32F3 microcontrollers and the HAL library. For many peripherals (e.g. ADC, SPI, I2C), the HAL library provides 3 ways to read/write data: polling mode, interrupt mode, and DMA mode. I know I don't want the polling mode because it's blocking. However, I am unsure how to choose between interrupt and DMA mode. Is there a general rule of thumb? I feel like DMA mode should always be better because it can write values into memory without CPU intervention?

1
It depends on what you are doing with the i/o: what numbers and types of access, frequency, conversion times, etc. Can you expand on what you want to do?wallyk
Since I mentioned several peripherals, let's pick one to narrow down the problem! I want to use i2c to send/receive data to/from an EEPROM and an IMU. Would I use i2c in interrupt or DMA mode?Ken Lin
Inertial Measurement Unit. The point is I'm reading sensor values over i2c.Ken Lin
Always you must use them according to your requirements. Some of the peripherals need a combination of them. For example, if you need to using RX of UART and you do not know the actual size to receive data via DMA, then you probably need to use of the IDLE interrupt of UART with DMA simultaneous.HamidReza
Although, due to the reason that you mentioned above, using the DMA is preferred always.HamidReza

1 Answers

2
votes

The advantage of DMA is that it does not require CPU intervention. DMA transfers can run while the CPU is busy doing other things, or while it is idle.

Some disadvantages of DMA are that:

  • Most microcontrollers have a limited number of DMA channels, so it may not be possible to use DMA for all peripherals.

  • The overhead of setting up and executing a DMA transfer may negate its benefits when many small transfers are required, e.g. when receiving individual characters over a USART.

  • Unusual interactions with devices (like bidirectional data transfers with some SPI devices) are often not supported with DMA.

  • DMA transfers place heavier (and less predictable) loads on the microcontroller's bus matrix, making them a frequent source of errata.

Generally speaking, I'd advise against using DMA for I2C. The protocol typically only runs at 100 - 200 kHz, so using interrupts will not place an especially heavy load on the microcontroller.