</>EmbeddedInterview

What is polling versus interrupts versus DMA?

EasyEmbedded C

Short answer: With polling, the CPU repeatedly checks a status flag and handles the peripheral when it is ready: simple and predictable, but it wastes CPU time and power. With interrupts, the peripheral signals the CPU when something happens, so the CPU can do other work or sleep: efficient for sporadic events, but each event costs an ISR entry and exit. With DMA, a DMA controller moves data between the peripheral and memory without the CPU, interrupting only when a block is done: best for high-throughput streams.

Polling Interrupts DMA
CPU load high (busy waiting) per event/byte per block
Latency lowest if the CPU is already polling interrupt latency + ISR hardware speed after set-up
Power poor (cannot sleep) good best
Complexity lowest moderate (shared data, priorities) highest (descriptors, buffers, cache)
Good for very short waits, simple boot code, very fast devices buttons, low-rate UART, timers, events ADC streaming, audio (I2S), SPI displays, UART at high baud, memory copies

Example: receiving UART data at 1 Mbaud

  • Polling: the CPU can do nothing else.
  • Interrupt per byte: 100,000 interrupts per second; at ~50 cycles each, a large fraction of a slow MCU's time.
  • DMA into a circular buffer with "half complete", "complete" and UART idle line interrupts: a few interrupts per message.

Combining them is normal: DMA for the data, an interrupt at the end of each block, and polling for very short waits (e.g. waiting a few cycles for a flag after starting a conversion).

DMA traps: cache coherency on Cortex-M7 and Cortex-A (next question), buffers that must stay valid until the transfer finishes (never a local variable of a function that returns), bus matrix restrictions (some DMA controllers cannot access certain RAM banks or TCM), and handling errors and overruns.